blob: 8825174c4999f9766c701760ce37ae0e2994157d [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
Manuel Klimek7fca93b2012-10-23 10:40:50 +00001640TEST(Matcher,ThisExpr) {
1641 EXPECT_TRUE(
1642 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1643 EXPECT_TRUE(
1644 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1645}
1646
Manuel Klimek04616e42012-07-06 05:48:52 +00001647TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001648 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001649
1650 std::string ClassString = "class string { public: string(); ~string(); }; ";
1651
1652 EXPECT_TRUE(
1653 matches(ClassString +
1654 "string GetStringByValue();"
1655 "void FunctionTakesString(string s);"
1656 "void run() { FunctionTakesString(GetStringByValue()); }",
1657 TempExpression));
1658
1659 EXPECT_TRUE(
1660 notMatches(ClassString +
1661 "string* GetStringPointer(); "
1662 "void FunctionTakesStringPtr(string* s);"
1663 "void run() {"
1664 " string* s = GetStringPointer();"
1665 " FunctionTakesStringPtr(GetStringPointer());"
1666 " FunctionTakesStringPtr(s);"
1667 "}",
1668 TempExpression));
1669
1670 EXPECT_TRUE(
1671 notMatches("class no_dtor {};"
1672 "no_dtor GetObjByValue();"
1673 "void ConsumeObj(no_dtor param);"
1674 "void run() { ConsumeObj(GetObjByValue()); }",
1675 TempExpression));
1676}
1677
Sam Panzer68a35af2012-08-24 22:04:44 +00001678TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1679 std::string ClassString =
1680 "class string { public: string(); int length(); }; ";
1681
1682 EXPECT_TRUE(
1683 matches(ClassString +
1684 "string GetStringByValue();"
1685 "void FunctionTakesString(string s);"
1686 "void run() { FunctionTakesString(GetStringByValue()); }",
1687 materializeTemporaryExpr()));
1688
1689 EXPECT_TRUE(
1690 notMatches(ClassString +
1691 "string* GetStringPointer(); "
1692 "void FunctionTakesStringPtr(string* s);"
1693 "void run() {"
1694 " string* s = GetStringPointer();"
1695 " FunctionTakesStringPtr(GetStringPointer());"
1696 " FunctionTakesStringPtr(s);"
1697 "}",
1698 materializeTemporaryExpr()));
1699
1700 EXPECT_TRUE(
1701 notMatches(ClassString +
1702 "string GetStringByValue();"
1703 "void run() { int k = GetStringByValue().length(); }",
1704 materializeTemporaryExpr()));
1705
1706 EXPECT_TRUE(
1707 notMatches(ClassString +
1708 "string GetStringByValue();"
1709 "void run() { GetStringByValue(); }",
1710 materializeTemporaryExpr()));
1711}
1712
Manuel Klimek04616e42012-07-06 05:48:52 +00001713TEST(ConstructorDeclaration, SimpleCase) {
1714 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001715 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001716 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001717 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001718}
1719
1720TEST(ConstructorDeclaration, IsImplicit) {
1721 // This one doesn't match because the constructor is not added by the
1722 // compiler (it is not needed).
1723 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001724 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001725 // The compiler added the implicit default constructor.
1726 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001727 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001728 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001729 constructorDecl(unless(isImplicit()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001730}
1731
Daniel Jasper1dad1832012-07-10 20:20:19 +00001732TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1733 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001734 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001735}
1736
1737TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001738 EXPECT_TRUE(notMatches("class Foo {};",
1739 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001740}
1741
Manuel Klimek04616e42012-07-06 05:48:52 +00001742TEST(HasAnyConstructorInitializer, SimpleCase) {
1743 EXPECT_TRUE(notMatches(
1744 "class Foo { Foo() { } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001745 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001746 EXPECT_TRUE(matches(
1747 "class Foo {"
1748 " Foo() : foo_() { }"
1749 " int foo_;"
1750 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001751 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001752}
1753
1754TEST(HasAnyConstructorInitializer, ForField) {
1755 static const char Code[] =
1756 "class Baz { };"
1757 "class Foo {"
1758 " Foo() : foo_() { }"
1759 " Baz foo_;"
1760 " Baz bar_;"
1761 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001762 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1763 forField(hasType(recordDecl(hasName("Baz"))))))));
1764 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001765 forField(hasName("foo_"))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001766 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1767 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001768}
1769
1770TEST(HasAnyConstructorInitializer, WithInitializer) {
1771 static const char Code[] =
1772 "class Foo {"
1773 " Foo() : foo_(0) { }"
1774 " int foo_;"
1775 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001776 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001777 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001778 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001779 withInitializer(integerLiteral(equals(1)))))));
1780}
1781
1782TEST(HasAnyConstructorInitializer, IsWritten) {
1783 static const char Code[] =
1784 "struct Bar { Bar(){} };"
1785 "class Foo {"
1786 " Foo() : foo_() { }"
1787 " Bar foo_;"
1788 " Bar bar_;"
1789 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001790 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001791 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001792 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001793 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001794 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001795 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1796}
1797
1798TEST(Matcher, NewExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001799 StatementMatcher New = newExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001800
1801 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1802 EXPECT_TRUE(
1803 matches("class X { public: X(); }; void x() { new X(); }", New));
1804 EXPECT_TRUE(
1805 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1806 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1807}
1808
1809TEST(Matcher, NewExpressionArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001810 StatementMatcher New = constructExpr(
1811 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001812
1813 EXPECT_TRUE(
1814 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1815 New));
1816 EXPECT_TRUE(
1817 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1818 New));
1819 EXPECT_TRUE(
1820 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1821 New));
1822
Daniel Jasper848cbe12012-09-18 13:09:13 +00001823 StatementMatcher WrongIndex = constructExpr(
1824 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001825 EXPECT_TRUE(
1826 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1827 WrongIndex));
1828}
1829
1830TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001831 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001832
1833 EXPECT_TRUE(
1834 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1835 EXPECT_TRUE(
1836 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1837 New));
1838}
1839
Daniel Jasper1dad1832012-07-10 20:20:19 +00001840TEST(Matcher, DeleteExpression) {
1841 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001842 deleteExpr()));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001843}
1844
Manuel Klimek04616e42012-07-06 05:48:52 +00001845TEST(Matcher, DefaultArgument) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001846 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001847
1848 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1849 EXPECT_TRUE(
1850 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1851 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1852}
1853
1854TEST(Matcher, StringLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001855 StatementMatcher Literal = stringLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001856 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1857 // wide string
1858 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1859 // with escaped characters
1860 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1861 // no matching -- though the data type is the same, there is no string literal
1862 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1863}
1864
1865TEST(Matcher, CharacterLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001866 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001867 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1868 // wide character
1869 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1870 // wide character, Hex encoded, NOT MATCHED!
1871 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1872 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1873}
1874
1875TEST(Matcher, IntegerLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001876 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001877 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1878 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1879 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1880 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1881
1882 // Non-matching cases (character literals, float and double)
1883 EXPECT_TRUE(notMatches("int i = L'a';",
1884 HasIntLiteral)); // this is actually a character
1885 // literal cast to int
1886 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1887 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1888 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1889}
1890
Daniel Jasper91f1c8c2013-07-26 18:52:58 +00001891TEST(Matcher, FloatLiterals) {
1892 StatementMatcher HasFloatLiteral = floatLiteral();
1893 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1894 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1895 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1896 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1897 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1898
1899 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1900}
1901
Daniel Jasper5901e472012-10-01 13:40:41 +00001902TEST(Matcher, NullPtrLiteral) {
1903 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1904}
1905
Daniel Jasper87c3d362012-09-20 14:12:57 +00001906TEST(Matcher, AsmStatement) {
1907 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1908}
1909
Manuel Klimek04616e42012-07-06 05:48:52 +00001910TEST(Matcher, Conditions) {
1911 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1912
1913 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1914 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1915 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1916 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1917 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1918}
1919
1920TEST(MatchBinaryOperator, HasOperatorName) {
1921 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1922
1923 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1924 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1925}
1926
1927TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1928 StatementMatcher OperatorTrueFalse =
1929 binaryOperator(hasLHS(boolLiteral(equals(true))),
1930 hasRHS(boolLiteral(equals(false))));
1931
1932 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1933 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1934 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1935}
1936
1937TEST(MatchBinaryOperator, HasEitherOperand) {
1938 StatementMatcher HasOperand =
1939 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1940
1941 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1942 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1943 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1944}
1945
1946TEST(Matcher, BinaryOperatorTypes) {
1947 // Integration test that verifies the AST provides all binary operators in
1948 // a way we expect.
1949 // FIXME: Operator ','
1950 EXPECT_TRUE(
1951 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1952 EXPECT_TRUE(
1953 matches("bool b; bool c = (b = true);",
1954 binaryOperator(hasOperatorName("="))));
1955 EXPECT_TRUE(
1956 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1957 EXPECT_TRUE(
1958 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1959 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1960 EXPECT_TRUE(
1961 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1962 EXPECT_TRUE(
1963 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1964 EXPECT_TRUE(
1965 matches("int i = 1; int j = (i <<= 2);",
1966 binaryOperator(hasOperatorName("<<="))));
1967 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1968 EXPECT_TRUE(
1969 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1970 EXPECT_TRUE(
1971 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1972 EXPECT_TRUE(
1973 matches("int i = 1; int j = (i >>= 2);",
1974 binaryOperator(hasOperatorName(">>="))));
1975 EXPECT_TRUE(
1976 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1977 EXPECT_TRUE(
1978 matches("int i = 42; int j = (i ^= 42);",
1979 binaryOperator(hasOperatorName("^="))));
1980 EXPECT_TRUE(
1981 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1982 EXPECT_TRUE(
1983 matches("int i = 42; int j = (i %= 42);",
1984 binaryOperator(hasOperatorName("%="))));
1985 EXPECT_TRUE(
1986 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1987 EXPECT_TRUE(
1988 matches("bool b = true && false;",
1989 binaryOperator(hasOperatorName("&&"))));
1990 EXPECT_TRUE(
1991 matches("bool b = true; bool c = (b &= false);",
1992 binaryOperator(hasOperatorName("&="))));
1993 EXPECT_TRUE(
1994 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1995 EXPECT_TRUE(
1996 matches("bool b = true || false;",
1997 binaryOperator(hasOperatorName("||"))));
1998 EXPECT_TRUE(
1999 matches("bool b = true; bool c = (b |= false);",
2000 binaryOperator(hasOperatorName("|="))));
2001 EXPECT_TRUE(
2002 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2003 EXPECT_TRUE(
2004 matches("int i = 42; int j = (i *= 23);",
2005 binaryOperator(hasOperatorName("*="))));
2006 EXPECT_TRUE(
2007 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
2008 EXPECT_TRUE(
2009 matches("int i = 42; int j = (i /= 23);",
2010 binaryOperator(hasOperatorName("/="))));
2011 EXPECT_TRUE(
2012 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
2013 EXPECT_TRUE(
2014 matches("int i = 42; int j = (i += 23);",
2015 binaryOperator(hasOperatorName("+="))));
2016 EXPECT_TRUE(
2017 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
2018 EXPECT_TRUE(
2019 matches("int i = 42; int j = (i -= 23);",
2020 binaryOperator(hasOperatorName("-="))));
2021 EXPECT_TRUE(
2022 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2023 binaryOperator(hasOperatorName("->*"))));
2024 EXPECT_TRUE(
2025 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2026 binaryOperator(hasOperatorName(".*"))));
2027
2028 // Member expressions as operators are not supported in matches.
2029 EXPECT_TRUE(
2030 notMatches("struct A { void x(A *a) { a->x(this); } };",
2031 binaryOperator(hasOperatorName("->"))));
2032
2033 // Initializer assignments are not represented as operator equals.
2034 EXPECT_TRUE(
2035 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2036
2037 // Array indexing is not represented as operator.
2038 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2039
2040 // Overloaded operators do not match at all.
2041 EXPECT_TRUE(notMatches(
2042 "struct A { bool operator&&(const A &a) const { return false; } };"
2043 "void x() { A a, b; a && b; }",
2044 binaryOperator()));
2045}
2046
2047TEST(MatchUnaryOperator, HasOperatorName) {
2048 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2049
2050 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2051 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2052}
2053
2054TEST(MatchUnaryOperator, HasUnaryOperand) {
2055 StatementMatcher OperatorOnFalse =
2056 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2057
2058 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2059 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2060}
2061
2062TEST(Matcher, UnaryOperatorTypes) {
2063 // Integration test that verifies the AST provides all unary operators in
2064 // a way we expect.
2065 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2066 EXPECT_TRUE(
2067 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2068 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2069 EXPECT_TRUE(
2070 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2071 EXPECT_TRUE(
2072 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2073 EXPECT_TRUE(
2074 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2075 EXPECT_TRUE(
2076 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2077 EXPECT_TRUE(
2078 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2079 EXPECT_TRUE(
2080 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2081 EXPECT_TRUE(
2082 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2083
2084 // We don't match conversion operators.
2085 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2086
2087 // Function calls are not represented as operator.
2088 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2089
2090 // Overloaded operators do not match at all.
2091 // FIXME: We probably want to add that.
2092 EXPECT_TRUE(notMatches(
2093 "struct A { bool operator!() const { return false; } };"
2094 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2095}
2096
2097TEST(Matcher, ConditionalOperator) {
2098 StatementMatcher Conditional = conditionalOperator(
2099 hasCondition(boolLiteral(equals(true))),
2100 hasTrueExpression(boolLiteral(equals(false))));
2101
2102 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2103 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2104 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2105
2106 StatementMatcher ConditionalFalse = conditionalOperator(
2107 hasFalseExpression(boolLiteral(equals(false))));
2108
2109 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2110 EXPECT_TRUE(
2111 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2112}
2113
Daniel Jasper1dad1832012-07-10 20:20:19 +00002114TEST(ArraySubscriptMatchers, ArraySubscripts) {
2115 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2116 arraySubscriptExpr()));
2117 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2118 arraySubscriptExpr()));
2119}
2120
2121TEST(ArraySubscriptMatchers, ArrayIndex) {
2122 EXPECT_TRUE(matches(
2123 "int i[2]; void f() { i[1] = 1; }",
2124 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2125 EXPECT_TRUE(matches(
2126 "int i[2]; void f() { 1[i] = 1; }",
2127 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2128 EXPECT_TRUE(notMatches(
2129 "int i[2]; void f() { i[1] = 1; }",
2130 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2131}
2132
2133TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2134 EXPECT_TRUE(matches(
2135 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002136 arraySubscriptExpr(hasBase(implicitCastExpr(
2137 hasSourceExpression(declRefExpr()))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002138}
2139
Manuel Klimek04616e42012-07-06 05:48:52 +00002140TEST(Matcher, HasNameSupportsNamespaces) {
2141 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002142 recordDecl(hasName("a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002143 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002144 recordDecl(hasName("::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002145 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002146 recordDecl(hasName("b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002147 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002148 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002149 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002150 recordDecl(hasName("c::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002151 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002152 recordDecl(hasName("a::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002153 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002154 recordDecl(hasName("a::b::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002155 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002156 recordDecl(hasName("::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002157 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002158 recordDecl(hasName("::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002159 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002160 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002161 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002162 recordDecl(hasName("a+b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002163 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002164 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002165}
2166
2167TEST(Matcher, HasNameSupportsOuterClasses) {
2168 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002169 matches("class A { class B { class C; }; };",
2170 recordDecl(hasName("A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002171 EXPECT_TRUE(
2172 matches("class A { class 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(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002175 matches("class A { class B { class C; }; };",
2176 recordDecl(hasName("B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002177 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002178 matches("class A { class B { class C; }; };",
2179 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002180 EXPECT_TRUE(
2181 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002182 recordDecl(hasName("c::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002183 EXPECT_TRUE(
2184 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002185 recordDecl(hasName("A::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002186 EXPECT_TRUE(
2187 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002188 recordDecl(hasName("A::B::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002189 EXPECT_TRUE(
2190 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002191 recordDecl(hasName("::C"))));
2192 EXPECT_TRUE(
2193 notMatches("class A { class B { class C; }; };",
2194 recordDecl(hasName("::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002195 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002196 recordDecl(hasName("z::A::B::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::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002200}
2201
2202TEST(Matcher, IsDefinition) {
2203 DeclarationMatcher DefinitionOfClassA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002204 recordDecl(hasName("A"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002205 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2206 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2207
2208 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002209 varDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002210 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2211 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2212
2213 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002214 methodDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002215 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2216 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2217}
2218
2219TEST(Matcher, OfClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002220 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +00002221 ofClass(hasName("X")))));
2222
2223 EXPECT_TRUE(
2224 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2225 EXPECT_TRUE(
2226 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2227 Constructor));
2228 EXPECT_TRUE(
2229 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2230 Constructor));
2231}
2232
2233TEST(Matcher, VisitsTemplateInstantiations) {
2234 EXPECT_TRUE(matches(
2235 "class A { public: void x(); };"
2236 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002237 "void f() { B<A> b; b.y(); }",
2238 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002239
2240 EXPECT_TRUE(matches(
2241 "class A { public: void x(); };"
2242 "class C {"
2243 " public:"
2244 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2245 "};"
2246 "void f() {"
2247 " C::B<A> b; b.y();"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002248 "}",
2249 recordDecl(hasName("C"),
2250 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002251}
2252
Daniel Jasper1dad1832012-07-10 20:20:19 +00002253TEST(Matcher, HandlesNullQualTypes) {
2254 // FIXME: Add a Type matcher so we can replace uses of this
2255 // variable with Type(True())
2256 const TypeMatcher AnyType = anything();
2257
2258 // We don't really care whether this matcher succeeds; we're testing that
2259 // it completes without crashing.
2260 EXPECT_TRUE(matches(
2261 "struct A { };"
2262 "template <typename T>"
2263 "void f(T t) {"
2264 " T local_t(t /* this becomes a null QualType in the AST */);"
2265 "}"
2266 "void g() {"
2267 " f(0);"
2268 "}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002269 expr(hasType(TypeMatcher(
Daniel Jasper1dad1832012-07-10 20:20:19 +00002270 anyOf(
2271 TypeMatcher(hasDeclaration(anything())),
2272 pointsTo(AnyType),
2273 references(AnyType)
2274 // Other QualType matchers should go here.
2275 ))))));
2276}
2277
Manuel Klimek04616e42012-07-06 05:48:52 +00002278// For testing AST_MATCHER_P().
Daniel Jasper1dad1832012-07-10 20:20:19 +00002279AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002280 // Make sure all special variables are used: node, match_finder,
2281 // bound_nodes_builder, and the parameter named 'AMatcher'.
2282 return AMatcher.matches(Node, Finder, Builder);
2283}
2284
2285TEST(AstMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002286 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002287
2288 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002289 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002290
2291 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002292 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002293
2294 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002295 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002296}
2297
2298AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +00002299 polymorphicHas,
2300 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2301 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002302 return Finder->matchesChildOf(
Manuel Klimekeb958de2012-09-05 12:12:07 +00002303 Node, AMatcher, Builder,
Manuel Klimek04616e42012-07-06 05:48:52 +00002304 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2305 ASTMatchFinder::BK_First);
2306}
2307
2308TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002309 DeclarationMatcher HasClassB =
2310 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek04616e42012-07-06 05:48:52 +00002311
2312 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002313 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002314
2315 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002316 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002317
2318 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002319 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002320
2321 StatementMatcher StatementHasClassB =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002322 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002323
2324 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2325}
2326
2327TEST(For, FindsForLoops) {
2328 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2329 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper6f595392012-10-01 15:05:34 +00002330 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2331 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00002332 forStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002333}
2334
Daniel Jasper4e566c42012-07-12 08:50:38 +00002335TEST(For, ForLoopInternals) {
2336 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2337 forStmt(hasCondition(anything()))));
2338 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2339 forStmt(hasLoopInit(anything()))));
2340}
2341
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002342TEST(For, ForRangeLoopInternals) {
2343 EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2344 forRangeStmt(hasLoopVariable(anything()))));
2345}
2346
Daniel Jasper4e566c42012-07-12 08:50:38 +00002347TEST(For, NegativeForLoopInternals) {
2348 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002349 forStmt(hasCondition(expr()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002350 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2351 forStmt(hasLoopInit(anything()))));
2352}
2353
Manuel Klimek04616e42012-07-06 05:48:52 +00002354TEST(For, ReportsNoFalsePositives) {
2355 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2356 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2357}
2358
2359TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002360 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2361 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2362 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002363}
2364
2365TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2366 // It's not a compound statement just because there's "{}" in the source
2367 // text. This is an AST search, not grep.
2368 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002369 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002370 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002371 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002372}
2373
Daniel Jasper4e566c42012-07-12 08:50:38 +00002374TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002375 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002376 forStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002377 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002378 forStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002379 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002380 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002381 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002382 doStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002383}
2384
2385TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2386 // The simplest case: every compound statement is in a function
2387 // definition, and the function body itself must be a compound
2388 // statement.
2389 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002390 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002391}
2392
2393TEST(HasAnySubstatement, IsNotRecursive) {
2394 // It's really "has any immediate substatement".
2395 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002396 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002397}
2398
2399TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2400 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002401 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002402}
2403
2404TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2405 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002406 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002407}
2408
2409TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2410 EXPECT_TRUE(matches("void f() { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002411 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002412 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002413 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002414}
2415
2416TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2417 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002418 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002419 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002420 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002421 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002422 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002423}
2424
2425TEST(StatementCountIs, WorksWithMultipleStatements) {
2426 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002427 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002428}
2429
2430TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2431 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002432 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002433 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002434 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002435 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002436 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002437 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002438 compoundStmt(statementCountIs(4))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002439}
2440
2441TEST(Member, WorksInSimplestCase) {
2442 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002443 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002444}
2445
2446TEST(Member, DoesNotMatchTheBaseExpression) {
2447 // Don't pick out the wrong part of the member expression, this should
2448 // be checking the member (name) only.
2449 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002450 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002451}
2452
2453TEST(Member, MatchesInMemberFunctionCall) {
2454 EXPECT_TRUE(matches("void f() {"
2455 " struct { void first() {}; } s;"
2456 " s.first();"
2457 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002458 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002459}
2460
Daniel Jasperb0c7b612012-10-23 15:46:39 +00002461TEST(Member, MatchesMember) {
2462 EXPECT_TRUE(matches(
2463 "struct A { int i; }; void f() { A a; a.i = 2; }",
2464 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2465 EXPECT_TRUE(notMatches(
2466 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2467 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2468}
2469
Daniel Jasper639522c2013-02-25 12:02:08 +00002470TEST(Member, UnderstandsAccess) {
2471 EXPECT_TRUE(matches(
2472 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2473 EXPECT_TRUE(notMatches(
2474 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2475 EXPECT_TRUE(notMatches(
2476 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2477
2478 EXPECT_TRUE(notMatches(
2479 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2480 EXPECT_TRUE(notMatches(
2481 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2482 EXPECT_TRUE(matches(
2483 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2484
2485 EXPECT_TRUE(notMatches(
2486 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2487 EXPECT_TRUE(matches("class A { protected: int i; };",
2488 fieldDecl(isProtected(), hasName("i"))));
2489 EXPECT_TRUE(notMatches(
2490 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2491
2492 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2493 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2494 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2495 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2496}
2497
Dmitri Gribenko06963042012-08-18 00:29:27 +00002498TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper5901e472012-10-01 13:40:41 +00002499 // Fails in C++11 mode
2500 EXPECT_TRUE(matchesConditionally(
2501 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2502 "class X { void *operator new(std::size_t); };",
2503 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002504
2505 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002506 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002507
Daniel Jasper5901e472012-10-01 13:40:41 +00002508 // Fails in C++11 mode
2509 EXPECT_TRUE(matchesConditionally(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002510 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2511 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper5901e472012-10-01 13:40:41 +00002512 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002513}
2514
Manuel Klimek04616e42012-07-06 05:48:52 +00002515TEST(HasObjectExpression, DoesNotMatchMember) {
2516 EXPECT_TRUE(notMatches(
2517 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002518 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002519}
2520
2521TEST(HasObjectExpression, MatchesBaseOfVariable) {
2522 EXPECT_TRUE(matches(
2523 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002524 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002525 EXPECT_TRUE(matches(
2526 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002527 memberExpr(hasObjectExpression(
2528 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002529}
2530
2531TEST(HasObjectExpression,
2532 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2533 EXPECT_TRUE(matches(
2534 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002535 memberExpr(hasObjectExpression(
2536 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002537 EXPECT_TRUE(matches(
2538 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002539 memberExpr(hasObjectExpression(
2540 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002541}
2542
2543TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002544 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2545 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2546 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2547 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002548}
2549
2550TEST(Field, MatchesField) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002551 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002552}
2553
2554TEST(IsConstQualified, MatchesConstInt) {
2555 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002556 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002557}
2558
2559TEST(IsConstQualified, MatchesConstPointer) {
2560 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002561 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002562}
2563
2564TEST(IsConstQualified, MatchesThroughTypedef) {
2565 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002566 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002567 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002568 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002569}
2570
2571TEST(IsConstQualified, DoesNotMatchInappropriately) {
2572 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002573 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002574 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002575 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002576}
2577
Sam Panzer80c13772012-08-16 16:58:10 +00002578TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002579 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2580 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2581 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2582 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002583}
2584TEST(CastExpression, MatchesImplicitCasts) {
2585 // This test creates an implicit cast from int to char.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002586 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002587 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002588 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002589}
2590
2591TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002592 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2593 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2594 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2595 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002596}
2597
Manuel Klimek04616e42012-07-06 05:48:52 +00002598TEST(ReinterpretCast, MatchesSimpleCase) {
2599 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002600 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002601}
2602
2603TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002604 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002605 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002606 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002607 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002608 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002609 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2610 "B b;"
2611 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002612 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002613}
2614
2615TEST(FunctionalCast, MatchesSimpleCase) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002616 std::string foo_class = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002617 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002618 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002619}
2620
2621TEST(FunctionalCast, DoesNotMatchOtherCasts) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002622 std::string FooClass = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002623 EXPECT_TRUE(
2624 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002625 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002626 EXPECT_TRUE(
2627 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002628 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002629}
2630
2631TEST(DynamicCast, MatchesSimpleCase) {
2632 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2633 "B b;"
2634 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002635 dynamicCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002636}
2637
2638TEST(StaticCast, MatchesSimpleCase) {
2639 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002640 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002641}
2642
2643TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002644 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002645 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002646 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002647 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002648 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002649 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2650 "B b;"
2651 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002652 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002653}
2654
Daniel Jasper417f7762012-09-18 13:36:17 +00002655TEST(CStyleCast, MatchesSimpleCase) {
2656 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2657}
2658
2659TEST(CStyleCast, DoesNotMatchOtherCasts) {
2660 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2661 "char q, *r = const_cast<char*>(&q);"
2662 "void* s = reinterpret_cast<char*>(&s);"
2663 "struct B { virtual ~B() {} }; struct D : B {};"
2664 "B b;"
2665 "D* t = dynamic_cast<D*>(&b);",
2666 cStyleCastExpr()));
2667}
2668
Manuel Klimek04616e42012-07-06 05:48:52 +00002669TEST(HasDestinationType, MatchesSimpleCase) {
2670 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002671 staticCastExpr(hasDestinationType(
2672 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002673}
2674
Sam Panzer80c13772012-08-16 16:58:10 +00002675TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2676 // This test creates an implicit const cast.
2677 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002678 implicitCastExpr(
2679 hasImplicitDestinationType(isInteger()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002680 // This test creates an implicit array-to-pointer cast.
2681 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002682 implicitCastExpr(hasImplicitDestinationType(
2683 pointsTo(TypeMatcher(anything()))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002684}
2685
2686TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2687 // This test creates an implicit cast from int to char.
2688 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002689 implicitCastExpr(hasImplicitDestinationType(
2690 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002691 // This test creates an implicit array-to-pointer cast.
2692 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002693 implicitCastExpr(hasImplicitDestinationType(
2694 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002695}
2696
2697TEST(ImplicitCast, MatchesSimpleCase) {
2698 // This test creates an implicit const cast.
2699 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002700 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002701 // This test creates an implicit cast from int to char.
2702 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002703 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002704 // This test creates an implicit array-to-pointer cast.
2705 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002706 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002707}
2708
2709TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002710 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer80c13772012-08-16 16:58:10 +00002711 // are present, and that it ignores explicit and paren casts.
2712
2713 // These two test cases have no casts.
2714 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002715 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002716 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002717 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002718
2719 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002720 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002721 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002722 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002723
2724 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002725 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002726}
2727
2728TEST(IgnoringImpCasts, MatchesImpCasts) {
2729 // This test checks that ignoringImpCasts matches when implicit casts are
2730 // present and its inner matcher alone does not match.
2731 // Note that this test creates an implicit const cast.
2732 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002733 varDecl(hasInitializer(ignoringImpCasts(
2734 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002735 // This test creates an implict cast from int to char.
2736 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002737 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002738 integerLiteral(equals(0)))))));
2739}
2740
2741TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2742 // These tests verify that ignoringImpCasts does not match if the inner
2743 // matcher does not match.
2744 // Note that the first test creates an implicit const cast.
2745 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002746 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002747 unless(anything()))))));
2748 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002749 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002750 unless(anything()))))));
2751
2752 // These tests verify that ignoringImplictCasts does not look through explicit
2753 // casts or parentheses.
2754 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002755 varDecl(hasInitializer(ignoringImpCasts(
2756 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002757 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002758 varDecl(hasInitializer(ignoringImpCasts(
2759 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002760 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002761 varDecl(hasInitializer(ignoringImpCasts(
2762 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002763 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002764 varDecl(hasInitializer(ignoringImpCasts(
2765 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002766}
2767
2768TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2769 // This test verifies that expressions that do not have implicit casts
2770 // still match the inner matcher.
2771 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002772 varDecl(hasInitializer(ignoringImpCasts(
2773 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002774}
2775
2776TEST(IgnoringParenCasts, MatchesParenCasts) {
2777 // This test checks that ignoringParenCasts matches when parentheses and/or
2778 // casts are present and its inner matcher alone does not match.
2779 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002780 varDecl(hasInitializer(ignoringParenCasts(
2781 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002782 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002783 varDecl(hasInitializer(ignoringParenCasts(
2784 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002785
2786 // This test creates an implict cast from int to char in addition to the
2787 // parentheses.
2788 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002789 varDecl(hasInitializer(ignoringParenCasts(
2790 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002791
2792 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002793 varDecl(hasInitializer(ignoringParenCasts(
2794 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002795 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002796 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002797 integerLiteral(equals(0)))))));
2798}
2799
2800TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2801 // This test verifies that expressions that do not have any casts still match.
2802 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002803 varDecl(hasInitializer(ignoringParenCasts(
2804 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002805}
2806
2807TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2808 // These tests verify that ignoringImpCasts does not match if the inner
2809 // matcher does not match.
2810 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002811 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002812 unless(anything()))))));
2813
2814 // This test creates an implicit cast from int to char in addition to the
2815 // parentheses.
2816 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002817 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002818 unless(anything()))))));
2819
2820 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002821 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002822 unless(anything()))))));
2823}
2824
2825TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2826 // This test checks that ignoringParenAndImpCasts matches when
2827 // parentheses and/or implicit casts are present and its inner matcher alone
2828 // does not match.
2829 // Note that this test creates an implicit const cast.
2830 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002831 varDecl(hasInitializer(ignoringParenImpCasts(
2832 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002833 // This test creates an implicit cast from int to char.
2834 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002835 varDecl(hasInitializer(ignoringParenImpCasts(
2836 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002837}
2838
2839TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2840 // This test verifies that expressions that do not have parentheses or
2841 // implicit casts still match.
2842 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002843 varDecl(hasInitializer(ignoringParenImpCasts(
2844 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002845 EXPECT_TRUE(matches("int 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, DoesNotMatchIncorrectly) {
2851 // These tests verify that ignoringParenImpCasts does not match if
2852 // the inner matcher does not match.
2853 // This test creates an implicit cast.
2854 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002855 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002856 unless(anything()))))));
2857 // These tests verify that ignoringParenAndImplictCasts does not look
2858 // through explicit casts.
2859 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002860 varDecl(hasInitializer(ignoringParenImpCasts(
2861 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002862 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002863 varDecl(hasInitializer(ignoringParenImpCasts(
2864 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002865 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002866 varDecl(hasInitializer(ignoringParenImpCasts(
2867 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002868}
2869
Manuel Klimeke9235692012-07-25 10:02:02 +00002870TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002871 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2872 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002873 implicitCastExpr(
2874 hasSourceExpression(constructExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002875}
2876
Manuel Klimeke9235692012-07-25 10:02:02 +00002877TEST(HasSourceExpression, MatchesExplicitCasts) {
2878 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002879 explicitCastExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002880 hasSourceExpression(hasDescendant(
Daniel Jasper848cbe12012-09-18 13:09:13 +00002881 expr(integerLiteral()))))));
Manuel Klimeke9235692012-07-25 10:02:02 +00002882}
2883
Manuel Klimek04616e42012-07-06 05:48:52 +00002884TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002885 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002886}
2887
2888TEST(Statement, MatchesCompoundStatments) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002889 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002890}
2891
2892TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002893 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002894}
2895
2896TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002897 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002898}
2899
Daniel Jasper1dad1832012-07-10 20:20:19 +00002900TEST(InitListExpression, MatchesInitListExpression) {
2901 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2902 initListExpr(hasType(asString("int [2]")))));
2903 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002904 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002905}
2906
2907TEST(UsingDeclaration, MatchesUsingDeclarations) {
2908 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2909 usingDecl()));
2910}
2911
2912TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2913 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2914 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2915}
2916
2917TEST(UsingDeclaration, MatchesSpecificTarget) {
2918 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2919 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002920 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002921 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2922 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002923 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002924}
2925
2926TEST(UsingDeclaration, ThroughUsingDeclaration) {
2927 EXPECT_TRUE(matches(
2928 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002929 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002930 EXPECT_TRUE(notMatches(
2931 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002932 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002933}
2934
Sam Panzerd624bfb2012-08-16 17:20:59 +00002935TEST(SingleDecl, IsSingleDecl) {
2936 StatementMatcher SingleDeclStmt =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002937 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002938 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2939 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2940 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2941 SingleDeclStmt));
2942}
2943
2944TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002945 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002946
2947 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002948 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002949 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002950 declStmt(containsDeclaration(0, MatchesInit),
2951 containsDeclaration(1, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002952 unsigned WrongIndex = 42;
2953 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002954 declStmt(containsDeclaration(WrongIndex,
Sam Panzerd624bfb2012-08-16 17:20:59 +00002955 MatchesInit))));
2956}
2957
2958TEST(DeclCount, DeclCountIsCorrect) {
2959 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002960 declStmt(declCountIs(2))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002961 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002962 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002963 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002964 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002965}
2966
Manuel Klimek04616e42012-07-06 05:48:52 +00002967TEST(While, MatchesWhileLoops) {
2968 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2969 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2970 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2971}
2972
2973TEST(Do, MatchesDoLoops) {
2974 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2975 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2976}
2977
2978TEST(Do, DoesNotMatchWhileLoops) {
2979 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2980}
2981
2982TEST(SwitchCase, MatchesCase) {
2983 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2984 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2985 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2986 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2987}
2988
Daniel Jasper87c3d362012-09-20 14:12:57 +00002989TEST(SwitchCase, MatchesSwitch) {
2990 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2991 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2992 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2993 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2994}
2995
Peter Collingbourne3154a102013-05-10 11:52:02 +00002996TEST(SwitchCase, MatchesEachCase) {
2997 EXPECT_TRUE(notMatches("void x() { switch(42); }",
2998 switchStmt(forEachSwitchCase(caseStmt()))));
2999 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
3000 switchStmt(forEachSwitchCase(caseStmt()))));
3001 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
3002 switchStmt(forEachSwitchCase(caseStmt()))));
3003 EXPECT_TRUE(notMatches(
3004 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3005 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3006 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3007 switchStmt(forEachSwitchCase(
3008 caseStmt(hasCaseConstant(integerLiteral()))))));
3009 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3010 switchStmt(forEachSwitchCase(
3011 caseStmt(hasCaseConstant(integerLiteral()))))));
3012 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3013 switchStmt(forEachSwitchCase(
3014 caseStmt(hasCaseConstant(integerLiteral()))))));
3015 EXPECT_TRUE(matchAndVerifyResultTrue(
3016 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3017 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3018 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3019}
3020
Manuel Klimekba46fc02013-07-19 11:50:54 +00003021TEST(ForEachConstructorInitializer, MatchesInitializers) {
3022 EXPECT_TRUE(matches(
3023 "struct X { X() : i(42), j(42) {} int i, j; };",
3024 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3025}
3026
Daniel Jasper87c3d362012-09-20 14:12:57 +00003027TEST(ExceptionHandling, SimpleCases) {
3028 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3029 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3030 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3031 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3032 throwExpr()));
3033 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3034 throwExpr()));
3035}
3036
Manuel Klimek04616e42012-07-06 05:48:52 +00003037TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3038 EXPECT_TRUE(notMatches(
3039 "void x() { if(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003040 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003041 EXPECT_TRUE(notMatches(
3042 "void x() { int x; if((x = 42)) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003043 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003044}
3045
3046TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3047 EXPECT_TRUE(matches(
3048 "void x() { if(int* a = 0) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003049 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003050}
3051
3052TEST(ForEach, BindsOneNode) {
3053 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003054 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003055 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003056}
3057
3058TEST(ForEach, BindsMultipleNodes) {
3059 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003060 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003061 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003062}
3063
3064TEST(ForEach, BindsRecursiveCombinations) {
3065 EXPECT_TRUE(matchAndVerifyResultTrue(
3066 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003067 recordDecl(hasName("C"),
3068 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003069 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003070}
3071
3072TEST(ForEachDescendant, BindsOneNode) {
3073 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003074 recordDecl(hasName("C"),
3075 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003076 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003077}
3078
Daniel Jasper94a56852012-11-16 18:39:22 +00003079TEST(ForEachDescendant, NestedForEachDescendant) {
3080 DeclarationMatcher m = recordDecl(
3081 isDefinition(), decl().bind("x"), hasName("C"));
3082 EXPECT_TRUE(matchAndVerifyResultTrue(
3083 "class A { class B { class C {}; }; };",
3084 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3085 new VerifyIdIsBoundTo<Decl>("x", "C")));
3086
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003087 // Check that a partial match of 'm' that binds 'x' in the
3088 // first part of anyOf(m, anything()) will not overwrite the
3089 // binding created by the earlier binding in the hasDescendant.
3090 EXPECT_TRUE(matchAndVerifyResultTrue(
3091 "class A { class B { class C {}; }; };",
3092 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3093 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper94a56852012-11-16 18:39:22 +00003094}
3095
Manuel Klimek04616e42012-07-06 05:48:52 +00003096TEST(ForEachDescendant, BindsMultipleNodes) {
3097 EXPECT_TRUE(matchAndVerifyResultTrue(
3098 "class C { class D { int x; int y; }; "
3099 " class E { class F { int y; int z; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003100 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003101 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003102}
3103
3104TEST(ForEachDescendant, BindsRecursiveCombinations) {
3105 EXPECT_TRUE(matchAndVerifyResultTrue(
3106 "class C { class D { "
3107 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003108 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3109 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003110 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003111}
3112
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003113TEST(ForEachDescendant, BindsCombinations) {
3114 EXPECT_TRUE(matchAndVerifyResultTrue(
3115 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3116 "(true) {} }",
3117 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3118 forEachDescendant(whileStmt().bind("while"))),
3119 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3120}
3121
3122TEST(Has, DoesNotDeleteBindings) {
3123 EXPECT_TRUE(matchAndVerifyResultTrue(
3124 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3125 new VerifyIdIsBoundTo<Decl>("x", 1)));
3126}
3127
3128TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3129 // Those matchers cover all the cases where an inner matcher is called
3130 // and there is not a 1:1 relationship between the match of the outer
3131 // matcher and the match of the inner matcher.
3132 // The pattern to look for is:
3133 // ... return InnerMatcher.matches(...); ...
3134 // In which case no special handling is needed.
3135 //
3136 // On the other hand, if there are multiple alternative matches
3137 // (for example forEach*) or matches might be discarded (for example has*)
3138 // the implementation must make sure that the discarded matches do not
3139 // affect the bindings.
3140 // When new such matchers are added, add a test here that:
3141 // - matches a simple node, and binds it as the first thing in the matcher:
3142 // recordDecl(decl().bind("x"), hasName("X")))
3143 // - uses the matcher under test afterwards in a way that not the first
3144 // alternative is matched; for anyOf, that means the first branch
3145 // would need to return false; for hasAncestor, it means that not
3146 // the direct parent matches the inner matcher.
3147
3148 EXPECT_TRUE(matchAndVerifyResultTrue(
3149 "class X { int y; };",
3150 recordDecl(
3151 recordDecl().bind("x"), hasName("::X"),
3152 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3153 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3154 EXPECT_TRUE(matchAndVerifyResultTrue(
3155 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3156 anyOf(unless(anything()), anything())),
3157 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3158 EXPECT_TRUE(matchAndVerifyResultTrue(
3159 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3160 classTemplateSpecializationDecl(
3161 decl().bind("x"),
3162 hasAnyTemplateArgument(refersToType(asString("int")))),
3163 new VerifyIdIsBoundTo<Decl>("x", 1)));
3164 EXPECT_TRUE(matchAndVerifyResultTrue(
3165 "class X { void f(); void g(); };",
3166 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3167 new VerifyIdIsBoundTo<Decl>("x", 1)));
3168 EXPECT_TRUE(matchAndVerifyResultTrue(
3169 "class X { X() : a(1), b(2) {} double a; int b; };",
3170 recordDecl(decl().bind("x"),
3171 has(constructorDecl(
3172 hasAnyConstructorInitializer(forField(hasName("b")))))),
3173 new VerifyIdIsBoundTo<Decl>("x", 1)));
3174 EXPECT_TRUE(matchAndVerifyResultTrue(
3175 "void x(int, int) { x(0, 42); }",
3176 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3177 new VerifyIdIsBoundTo<Expr>("x", 1)));
3178 EXPECT_TRUE(matchAndVerifyResultTrue(
3179 "void x(int, int y) {}",
3180 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3181 new VerifyIdIsBoundTo<Decl>("x", 1)));
3182 EXPECT_TRUE(matchAndVerifyResultTrue(
3183 "void x() { return; if (true) {} }",
3184 functionDecl(decl().bind("x"),
3185 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3186 new VerifyIdIsBoundTo<Decl>("x", 1)));
3187 EXPECT_TRUE(matchAndVerifyResultTrue(
3188 "namespace X { void b(int); void b(); }"
3189 "using X::b;",
3190 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3191 functionDecl(parameterCountIs(1))))),
3192 new VerifyIdIsBoundTo<Decl>("x", 1)));
3193 EXPECT_TRUE(matchAndVerifyResultTrue(
3194 "class A{}; class B{}; class C : B, A {};",
3195 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3196 new VerifyIdIsBoundTo<Decl>("x", 1)));
3197 EXPECT_TRUE(matchAndVerifyResultTrue(
3198 "class A{}; typedef A B; typedef A C; typedef A D;"
3199 "class E : A {};",
3200 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3201 new VerifyIdIsBoundTo<Decl>("x", 1)));
3202 EXPECT_TRUE(matchAndVerifyResultTrue(
3203 "class A { class B { void f() {} }; };",
3204 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3205 new VerifyIdIsBoundTo<Decl>("x", 1)));
3206 EXPECT_TRUE(matchAndVerifyResultTrue(
3207 "template <typename T> struct A { struct B {"
3208 " void f() { if(true) {} }"
3209 "}; };"
3210 "void t() { A<int>::B b; b.f(); }",
3211 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3212 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3213 EXPECT_TRUE(matchAndVerifyResultTrue(
3214 "class A {};",
3215 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3216 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimekba46fc02013-07-19 11:50:54 +00003217 EXPECT_TRUE(matchAndVerifyResultTrue(
3218 "class A { A() : s(), i(42) {} const char *s; int i; };",
3219 constructorDecl(hasName("::A::A"), decl().bind("x"),
3220 forEachConstructorInitializer(forField(hasName("i")))),
3221 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003222}
3223
Daniel Jasper33806cd2012-11-11 22:14:55 +00003224TEST(ForEachDescendant, BindsCorrectNodes) {
3225 EXPECT_TRUE(matchAndVerifyResultTrue(
3226 "class C { void f(); int i; };",
3227 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3228 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3229 EXPECT_TRUE(matchAndVerifyResultTrue(
3230 "class C { void f() {} int i; };",
3231 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3232 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3233}
3234
Manuel Klimekabf43712013-02-04 10:59:20 +00003235TEST(FindAll, BindsNodeOnMatch) {
3236 EXPECT_TRUE(matchAndVerifyResultTrue(
3237 "class A {};",
3238 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3239 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3240}
3241
3242TEST(FindAll, BindsDescendantNodeOnMatch) {
3243 EXPECT_TRUE(matchAndVerifyResultTrue(
3244 "class A { int a; int b; };",
3245 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3246 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3247}
3248
3249TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3250 EXPECT_TRUE(matchAndVerifyResultTrue(
3251 "class A { int a; int b; };",
3252 recordDecl(hasName("::A"),
3253 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3254 fieldDecl().bind("v"))))),
3255 new VerifyIdIsBoundTo<Decl>("v", 3)));
3256
3257 EXPECT_TRUE(matchAndVerifyResultTrue(
3258 "class A { class B {}; class C {}; };",
3259 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3260 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3261}
3262
Manuel Klimek88b95872013-02-04 09:42:38 +00003263TEST(EachOf, TriggersForEachMatch) {
3264 EXPECT_TRUE(matchAndVerifyResultTrue(
3265 "class A { int a; int b; };",
3266 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3267 has(fieldDecl(hasName("b")).bind("v")))),
3268 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3269}
3270
3271TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3272 EXPECT_TRUE(matchAndVerifyResultTrue(
3273 "class A { int a; int c; };",
3274 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3275 has(fieldDecl(hasName("b")).bind("v")))),
3276 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3277 EXPECT_TRUE(matchAndVerifyResultTrue(
3278 "class A { int c; int b; };",
3279 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3280 has(fieldDecl(hasName("b")).bind("v")))),
3281 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3282 EXPECT_TRUE(notMatches(
3283 "class A { int c; int d; };",
3284 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3285 has(fieldDecl(hasName("b")).bind("v"))))));
3286}
Manuel Klimek04616e42012-07-06 05:48:52 +00003287
3288TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3289 // Make sure that we can both match the class by name (::X) and by the type
3290 // the template was instantiated with (via a field).
3291
3292 EXPECT_TRUE(matches(
3293 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003294 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003295
3296 EXPECT_TRUE(matches(
3297 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003298 recordDecl(isTemplateInstantiation(), hasDescendant(
3299 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003300}
3301
3302TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3303 EXPECT_TRUE(matches(
3304 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003305 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek04616e42012-07-06 05:48:52 +00003306 isTemplateInstantiation())));
3307}
3308
3309TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3310 EXPECT_TRUE(matches(
3311 "template <typename T> class X { T t; }; class A {};"
3312 "template class X<A>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003313 recordDecl(isTemplateInstantiation(), hasDescendant(
3314 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003315}
3316
3317TEST(IsTemplateInstantiation,
3318 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3319 EXPECT_TRUE(matches(
3320 "template <typename T> class X {};"
3321 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003322 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003323}
3324
3325TEST(IsTemplateInstantiation,
3326 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3327 EXPECT_TRUE(matches(
3328 "class A {};"
3329 "class X {"
3330 " template <typename U> class Y { U u; };"
3331 " Y<A> y;"
3332 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003333 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003334}
3335
3336TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3337 // FIXME: Figure out whether this makes sense. It doesn't affect the
3338 // normal use case as long as the uppermost instantiation always is marked
3339 // as template instantiation, but it might be confusing as a predicate.
3340 EXPECT_TRUE(matches(
3341 "class A {};"
3342 "template <typename T> class X {"
3343 " template <typename U> class Y { U u; };"
3344 " Y<T> y;"
3345 "}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003346 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003347}
3348
3349TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3350 EXPECT_TRUE(notMatches(
3351 "template <typename T> class X {}; class A {};"
3352 "template <> class X<A> {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003353 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003354}
3355
3356TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3357 EXPECT_TRUE(notMatches(
3358 "class A {}; class Y { A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003359 recordDecl(isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003360}
3361
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003362TEST(IsExplicitTemplateSpecialization,
3363 DoesNotMatchPrimaryTemplate) {
3364 EXPECT_TRUE(notMatches(
3365 "template <typename T> class X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003366 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003367 EXPECT_TRUE(notMatches(
3368 "template <typename T> void f(T t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003369 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003370}
3371
3372TEST(IsExplicitTemplateSpecialization,
3373 DoesNotMatchExplicitTemplateInstantiations) {
3374 EXPECT_TRUE(notMatches(
3375 "template <typename T> class X {};"
3376 "template class X<int>; extern template class X<long>;",
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) {}"
3380 "template void f(int t); extern template void f(long t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003381 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003382}
3383
3384TEST(IsExplicitTemplateSpecialization,
3385 DoesNotMatchImplicitTemplateInstantiations) {
3386 EXPECT_TRUE(notMatches(
3387 "template <typename T> class X {}; X<int> x;",
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); void g() { f(10); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003391 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003392}
3393
3394TEST(IsExplicitTemplateSpecialization,
3395 MatchesExplicitTemplateSpecializations) {
3396 EXPECT_TRUE(matches(
3397 "template <typename T> class X {};"
3398 "template<> class X<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003399 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003400 EXPECT_TRUE(matches(
3401 "template <typename T> void f(T t) {}"
3402 "template<> void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003403 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003404}
3405
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003406TEST(HasAncenstor, MatchesDeclarationAncestors) {
3407 EXPECT_TRUE(matches(
3408 "class A { class B { class C {}; }; };",
3409 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3410}
3411
3412TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3413 EXPECT_TRUE(notMatches(
3414 "class A { class B { class C {}; }; };",
3415 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3416}
3417
3418TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3419 EXPECT_TRUE(matches(
3420 "class A { class B { void f() { C c; } class C {}; }; };",
3421 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3422 hasAncestor(recordDecl(hasName("A"))))))));
3423}
3424
3425TEST(HasAncenstor, MatchesStatementAncestors) {
3426 EXPECT_TRUE(matches(
3427 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003428 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003429}
3430
3431TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3432 EXPECT_TRUE(matches(
3433 "void f() { if (true) { int x = 42; } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003434 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003435}
3436
3437TEST(HasAncestor, BindsRecursiveCombinations) {
3438 EXPECT_TRUE(matchAndVerifyResultTrue(
3439 "class C { class D { class E { class F { int y; }; }; }; };",
3440 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003441 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003442}
3443
3444TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3445 EXPECT_TRUE(matchAndVerifyResultTrue(
3446 "class C { class D { class E { class F { int y; }; }; }; };",
3447 fieldDecl(hasAncestor(
3448 decl(
3449 hasDescendant(recordDecl(isDefinition(),
3450 hasAncestor(recordDecl())))
3451 ).bind("d")
3452 )),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003453 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003454}
3455
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003456TEST(HasAncestor, MatchesClosestAncestor) {
3457 EXPECT_TRUE(matchAndVerifyResultTrue(
3458 "template <typename T> struct C {"
3459 " void f(int) {"
3460 " struct I { void g(T) { int x; } } i; i.g(42);"
3461 " }"
3462 "};"
3463 "template struct C<int>;",
3464 varDecl(hasName("x"),
3465 hasAncestor(functionDecl(hasParameter(
3466 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3467 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3468}
3469
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003470TEST(HasAncestor, MatchesInTemplateInstantiations) {
3471 EXPECT_TRUE(matches(
3472 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3473 "A<int>::B::C a;",
3474 fieldDecl(hasType(asString("int")),
3475 hasAncestor(recordDecl(hasName("A"))))));
3476}
3477
3478TEST(HasAncestor, MatchesInImplicitCode) {
3479 EXPECT_TRUE(matches(
3480 "struct X {}; struct A { A() {} X x; };",
3481 constructorDecl(
3482 hasAnyConstructorInitializer(withInitializer(expr(
3483 hasAncestor(recordDecl(hasName("A")))))))));
3484}
3485
Daniel Jasper632aea92012-10-22 16:26:51 +00003486TEST(HasParent, MatchesOnlyParent) {
3487 EXPECT_TRUE(matches(
3488 "void f() { if (true) { int x = 42; } }",
3489 compoundStmt(hasParent(ifStmt()))));
3490 EXPECT_TRUE(notMatches(
3491 "void f() { for (;;) { int x = 42; } }",
3492 compoundStmt(hasParent(ifStmt()))));
3493 EXPECT_TRUE(notMatches(
3494 "void f() { if (true) for (;;) { int x = 42; } }",
3495 compoundStmt(hasParent(ifStmt()))));
3496}
3497
Manuel Klimekc844a462012-12-06 14:42:48 +00003498TEST(HasAncestor, MatchesAllAncestors) {
3499 EXPECT_TRUE(matches(
3500 "template <typename T> struct C { static void f() { 42; } };"
3501 "void t() { C<int>::f(); }",
3502 integerLiteral(
3503 equals(42),
3504 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3505 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3506}
3507
3508TEST(HasParent, MatchesAllParents) {
3509 EXPECT_TRUE(matches(
3510 "template <typename T> struct C { static void f() { 42; } };"
3511 "void t() { C<int>::f(); }",
3512 integerLiteral(
3513 equals(42),
3514 hasParent(compoundStmt(hasParent(functionDecl(
3515 hasParent(recordDecl(isTemplateInstantiation())))))))));
3516 EXPECT_TRUE(matches(
3517 "template <typename T> struct C { static void f() { 42; } };"
3518 "void t() { C<int>::f(); }",
3519 integerLiteral(
3520 equals(42),
3521 hasParent(compoundStmt(hasParent(functionDecl(
3522 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3523 EXPECT_TRUE(matches(
3524 "template <typename T> struct C { static void f() { 42; } };"
3525 "void t() { C<int>::f(); }",
3526 integerLiteral(equals(42),
3527 hasParent(compoundStmt(allOf(
3528 hasParent(functionDecl(
3529 hasParent(recordDecl(isTemplateInstantiation())))),
3530 hasParent(functionDecl(hasParent(recordDecl(
3531 unless(isTemplateInstantiation())))))))))));
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003532 EXPECT_TRUE(
3533 notMatches("template <typename T> struct C { static void f() {} };"
3534 "void t() { C<int>::f(); }",
3535 compoundStmt(hasParent(recordDecl()))));
Manuel Klimekc844a462012-12-06 14:42:48 +00003536}
3537
Daniel Jasper516b02e2012-10-17 08:52:59 +00003538TEST(TypeMatching, MatchesTypes) {
3539 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3540}
3541
3542TEST(TypeMatching, MatchesArrayTypes) {
3543 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3544 EXPECT_TRUE(matches("int a[42];", arrayType()));
3545 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3546
3547 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3548 arrayType(hasElementType(builtinType()))));
3549
3550 EXPECT_TRUE(matches(
3551 "int const a[] = { 2, 3 };",
3552 qualType(arrayType(hasElementType(builtinType())))));
3553 EXPECT_TRUE(matches(
3554 "int const a[] = { 2, 3 };",
3555 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3556 EXPECT_TRUE(matches(
3557 "typedef const int T; T x[] = { 1, 2 };",
3558 qualType(isConstQualified(), arrayType())));
3559
3560 EXPECT_TRUE(notMatches(
3561 "int a[] = { 2, 3 };",
3562 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3563 EXPECT_TRUE(notMatches(
3564 "int a[] = { 2, 3 };",
3565 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3566 EXPECT_TRUE(notMatches(
3567 "int const a[] = { 2, 3 };",
3568 qualType(arrayType(hasElementType(builtinType())),
3569 unless(isConstQualified()))));
3570
3571 EXPECT_TRUE(matches("int a[2];",
3572 constantArrayType(hasElementType(builtinType()))));
3573 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3574}
3575
3576TEST(TypeMatching, MatchesComplexTypes) {
3577 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3578 EXPECT_TRUE(matches(
3579 "_Complex float f;",
3580 complexType(hasElementType(builtinType()))));
3581 EXPECT_TRUE(notMatches(
3582 "_Complex float f;",
3583 complexType(hasElementType(isInteger()))));
3584}
3585
3586TEST(TypeMatching, MatchesConstantArrayTypes) {
3587 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3588 EXPECT_TRUE(notMatches(
3589 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3590 constantArrayType(hasElementType(builtinType()))));
3591
3592 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3593 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3594 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3595}
3596
3597TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3598 EXPECT_TRUE(matches(
3599 "template <typename T, int Size> class array { T data[Size]; };",
3600 dependentSizedArrayType()));
3601 EXPECT_TRUE(notMatches(
3602 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3603 dependentSizedArrayType()));
3604}
3605
3606TEST(TypeMatching, MatchesIncompleteArrayType) {
3607 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3608 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3609
3610 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3611 incompleteArrayType()));
3612}
3613
3614TEST(TypeMatching, MatchesVariableArrayType) {
3615 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3616 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3617
3618 EXPECT_TRUE(matches(
3619 "void f(int b) { int a[b]; }",
3620 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3621 varDecl(hasName("b")))))))));
3622}
3623
3624TEST(TypeMatching, MatchesAtomicTypes) {
3625 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3626
3627 EXPECT_TRUE(matches("_Atomic(int) i;",
3628 atomicType(hasValueType(isInteger()))));
3629 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3630 atomicType(hasValueType(isInteger()))));
3631}
3632
3633TEST(TypeMatching, MatchesAutoTypes) {
3634 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3635 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3636 autoType()));
3637
Richard Smith061f1e22013-04-30 21:23:01 +00003638 // FIXME: Matching against the type-as-written can't work here, because the
3639 // type as written was not deduced.
3640 //EXPECT_TRUE(matches("auto a = 1;",
3641 // autoType(hasDeducedType(isInteger()))));
3642 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3643 // autoType(hasDeducedType(isInteger()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003644}
3645
Daniel Jasperd29d5fa2012-10-29 10:14:44 +00003646TEST(TypeMatching, MatchesFunctionTypes) {
3647 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3648 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3649}
3650
Edwin Vaneec074802013-04-01 18:33:34 +00003651TEST(TypeMatching, MatchesParenType) {
3652 EXPECT_TRUE(
3653 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3654 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3655
3656 EXPECT_TRUE(matches(
3657 "int (*ptr_to_func)(int);",
3658 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3659 EXPECT_TRUE(notMatches(
3660 "int (*ptr_to_array)[4];",
3661 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3662}
3663
Daniel Jasper516b02e2012-10-17 08:52:59 +00003664TEST(TypeMatching, PointerTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003665 // FIXME: Reactive when these tests can be more specific (not matching
3666 // implicit code on certain platforms), likely when we have hasDescendant for
3667 // Types/TypeLocs.
3668 //EXPECT_TRUE(matchAndVerifyResultTrue(
3669 // "int* a;",
3670 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3671 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3672 //EXPECT_TRUE(matchAndVerifyResultTrue(
3673 // "int* a;",
3674 // pointerTypeLoc().bind("loc"),
3675 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003676 EXPECT_TRUE(matches(
3677 "int** a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003678 loc(pointerType(pointee(qualType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003679 EXPECT_TRUE(matches(
3680 "int** a;",
3681 loc(pointerType(pointee(pointerType())))));
3682 EXPECT_TRUE(matches(
3683 "int* b; int* * const a = &b;",
3684 loc(qualType(isConstQualified(), pointerType()))));
3685
3686 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper7943eb52012-10-17 13:35:36 +00003687 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3688 hasType(blockPointerType()))));
3689 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3690 hasType(memberPointerType()))));
3691 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3692 hasType(pointerType()))));
3693 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3694 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003695 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3696 hasType(lValueReferenceType()))));
3697 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3698 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003699
Daniel Jasper7943eb52012-10-17 13:35:36 +00003700 Fragment = "int *ptr;";
3701 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3702 hasType(blockPointerType()))));
3703 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3704 hasType(memberPointerType()))));
3705 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3706 hasType(pointerType()))));
3707 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3708 hasType(referenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003709
Daniel Jasper7943eb52012-10-17 13:35:36 +00003710 Fragment = "int a; int &ref = a;";
3711 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3712 hasType(blockPointerType()))));
3713 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3714 hasType(memberPointerType()))));
3715 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3716 hasType(pointerType()))));
3717 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3718 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003719 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3720 hasType(lValueReferenceType()))));
3721 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3722 hasType(rValueReferenceType()))));
3723
3724 Fragment = "int &&ref = 2;";
3725 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3726 hasType(blockPointerType()))));
3727 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3728 hasType(memberPointerType()))));
3729 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3730 hasType(pointerType()))));
3731 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3732 hasType(referenceType()))));
3733 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3734 hasType(lValueReferenceType()))));
3735 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3736 hasType(rValueReferenceType()))));
3737}
3738
3739TEST(TypeMatching, AutoRefTypes) {
3740 std::string Fragment = "auto a = 1;"
3741 "auto b = a;"
3742 "auto &c = a;"
3743 "auto &&d = c;"
3744 "auto &&e = 2;";
3745 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3746 hasType(referenceType()))));
3747 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3748 hasType(referenceType()))));
3749 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3750 hasType(referenceType()))));
3751 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3752 hasType(lValueReferenceType()))));
3753 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3754 hasType(rValueReferenceType()))));
3755 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3756 hasType(referenceType()))));
3757 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3758 hasType(lValueReferenceType()))));
3759 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3760 hasType(rValueReferenceType()))));
3761 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3762 hasType(referenceType()))));
3763 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3764 hasType(lValueReferenceType()))));
3765 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3766 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003767}
3768
3769TEST(TypeMatching, PointeeTypes) {
3770 EXPECT_TRUE(matches("int b; int &a = b;",
3771 referenceType(pointee(builtinType()))));
3772 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3773
3774 EXPECT_TRUE(matches("int *a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003775 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003776
3777 EXPECT_TRUE(matches(
3778 "int const *A;",
3779 pointerType(pointee(isConstQualified(), builtinType()))));
3780 EXPECT_TRUE(notMatches(
3781 "int *A;",
3782 pointerType(pointee(isConstQualified(), builtinType()))));
3783}
3784
3785TEST(TypeMatching, MatchesPointersToConstTypes) {
3786 EXPECT_TRUE(matches("int b; int * const a = &b;",
3787 loc(pointerType())));
3788 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003789 loc(pointerType())));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003790 EXPECT_TRUE(matches(
3791 "int b; const int * a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003792 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003793 EXPECT_TRUE(matches(
3794 "int b; const int * a = &b;",
3795 pointerType(pointee(builtinType()))));
3796}
3797
3798TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003799 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3800 hasType(typedefType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003801}
3802
Edwin Vanef901b712013-02-25 14:49:29 +00003803TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vaneb6eae142013-02-25 20:43:32 +00003804 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vanef901b712013-02-25 14:49:29 +00003805 templateSpecializationType()));
3806}
3807
Edwin Vaneb6eae142013-02-25 20:43:32 +00003808TEST(TypeMatching, MatchesRecordType) {
3809 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek59b0af62013-02-27 11:56:58 +00003810 EXPECT_TRUE(matches("struct S{}; S s;",
3811 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3812 EXPECT_TRUE(notMatches("int i;",
3813 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003814}
3815
3816TEST(TypeMatching, MatchesElaboratedType) {
3817 EXPECT_TRUE(matches(
3818 "namespace N {"
3819 " namespace M {"
3820 " class D {};"
3821 " }"
3822 "}"
3823 "N::M::D d;", elaboratedType()));
3824 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3825 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3826}
3827
3828TEST(ElaboratedTypeNarrowing, hasQualifier) {
3829 EXPECT_TRUE(matches(
3830 "namespace N {"
3831 " namespace M {"
3832 " class D {};"
3833 " }"
3834 "}"
3835 "N::M::D d;",
3836 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3837 EXPECT_TRUE(notMatches(
3838 "namespace M {"
3839 " class D {};"
3840 "}"
3841 "M::D d;",
3842 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vane6972f6d2013-03-04 17:51:00 +00003843 EXPECT_TRUE(notMatches(
3844 "struct D {"
3845 "} d;",
3846 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003847}
3848
3849TEST(ElaboratedTypeNarrowing, namesType) {
3850 EXPECT_TRUE(matches(
3851 "namespace N {"
3852 " namespace M {"
3853 " class D {};"
3854 " }"
3855 "}"
3856 "N::M::D d;",
3857 elaboratedType(elaboratedType(namesType(recordType(
3858 hasDeclaration(namedDecl(hasName("D")))))))));
3859 EXPECT_TRUE(notMatches(
3860 "namespace M {"
3861 " class D {};"
3862 "}"
3863 "M::D d;",
3864 elaboratedType(elaboratedType(namesType(typedefType())))));
3865}
3866
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003867TEST(NNS, MatchesNestedNameSpecifiers) {
3868 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3869 nestedNameSpecifier()));
3870 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3871 nestedNameSpecifier()));
3872 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3873 nestedNameSpecifier()));
3874
3875 EXPECT_TRUE(matches(
3876 "struct A { static void f() {} }; void g() { A::f(); }",
3877 nestedNameSpecifier()));
3878 EXPECT_TRUE(notMatches(
3879 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3880 nestedNameSpecifier()));
3881}
3882
Daniel Jasper87c3d362012-09-20 14:12:57 +00003883TEST(NullStatement, SimpleCases) {
3884 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3885 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3886}
3887
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003888TEST(NNS, MatchesTypes) {
3889 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3890 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3891 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3892 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3893 Matcher));
3894 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3895}
3896
3897TEST(NNS, MatchesNamespaceDecls) {
3898 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3899 specifiesNamespace(hasName("ns")));
3900 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3901 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3902 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3903}
3904
3905TEST(NNS, BindsNestedNameSpecifiers) {
3906 EXPECT_TRUE(matchAndVerifyResultTrue(
3907 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3908 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3909 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3910}
3911
3912TEST(NNS, BindsNestedNameSpecifierLocs) {
3913 EXPECT_TRUE(matchAndVerifyResultTrue(
3914 "namespace ns { struct B {}; } ns::B b;",
3915 loc(nestedNameSpecifier()).bind("loc"),
3916 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3917}
3918
3919TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3920 EXPECT_TRUE(matches(
3921 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3922 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3923 EXPECT_TRUE(matches(
3924 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasper516b02e2012-10-17 08:52:59 +00003925 nestedNameSpecifierLoc(hasPrefix(
3926 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003927}
3928
Daniel Jasper6fc34332012-10-30 15:42:00 +00003929TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3930 std::string Fragment =
3931 "namespace a { struct A { struct B { struct C {}; }; }; };"
3932 "void f() { a::A::B::C c; }";
3933 EXPECT_TRUE(matches(
3934 Fragment,
3935 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3936 hasDescendant(nestedNameSpecifier(
3937 specifiesNamespace(hasName("a")))))));
3938 EXPECT_TRUE(notMatches(
3939 Fragment,
3940 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3941 has(nestedNameSpecifier(
3942 specifiesNamespace(hasName("a")))))));
3943 EXPECT_TRUE(matches(
3944 Fragment,
3945 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3946 has(nestedNameSpecifier(
3947 specifiesNamespace(hasName("a")))))));
3948
3949 // Not really useful because a NestedNameSpecifier can af at most one child,
3950 // but to complete the interface.
3951 EXPECT_TRUE(matchAndVerifyResultTrue(
3952 Fragment,
3953 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3954 forEach(nestedNameSpecifier().bind("x"))),
3955 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3956}
3957
3958TEST(NNS, NestedNameSpecifiersAsDescendants) {
3959 std::string Fragment =
3960 "namespace a { struct A { struct B { struct C {}; }; }; };"
3961 "void f() { a::A::B::C c; }";
3962 EXPECT_TRUE(matches(
3963 Fragment,
3964 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3965 asString("struct a::A")))))));
3966 EXPECT_TRUE(matchAndVerifyResultTrue(
3967 Fragment,
3968 functionDecl(hasName("f"),
3969 forEachDescendant(nestedNameSpecifier().bind("x"))),
3970 // Nested names: a, a::A and a::A::B.
3971 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3972}
3973
3974TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3975 std::string Fragment =
3976 "namespace a { struct A { struct B { struct C {}; }; }; };"
3977 "void f() { a::A::B::C c; }";
3978 EXPECT_TRUE(matches(
3979 Fragment,
3980 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3981 hasDescendant(loc(nestedNameSpecifier(
3982 specifiesNamespace(hasName("a"))))))));
3983 EXPECT_TRUE(notMatches(
3984 Fragment,
3985 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3986 has(loc(nestedNameSpecifier(
3987 specifiesNamespace(hasName("a"))))))));
3988 EXPECT_TRUE(matches(
3989 Fragment,
3990 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3991 has(loc(nestedNameSpecifier(
3992 specifiesNamespace(hasName("a"))))))));
3993
3994 EXPECT_TRUE(matchAndVerifyResultTrue(
3995 Fragment,
3996 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3997 forEach(nestedNameSpecifierLoc().bind("x"))),
3998 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3999}
4000
4001TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
4002 std::string Fragment =
4003 "namespace a { struct A { struct B { struct C {}; }; }; };"
4004 "void f() { a::A::B::C c; }";
4005 EXPECT_TRUE(matches(
4006 Fragment,
4007 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4008 asString("struct a::A"))))))));
4009 EXPECT_TRUE(matchAndVerifyResultTrue(
4010 Fragment,
4011 functionDecl(hasName("f"),
4012 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4013 // Nested names: a, a::A and a::A::B.
4014 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4015}
4016
Manuel Klimek191c0932013-02-01 13:41:35 +00004017template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimekc2687452012-10-24 14:47:44 +00004018public:
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004019 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4020 StringRef InnerId)
4021 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jaspere9aa6872012-10-29 10:48:25 +00004022 }
4023
Manuel Klimek191c0932013-02-01 13:41:35 +00004024 virtual bool run(const BoundNodes *Nodes) { return false; }
4025
Manuel Klimekc2687452012-10-24 14:47:44 +00004026 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4027 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004028 return selectFirst<const T>(InnerId,
4029 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimekc2687452012-10-24 14:47:44 +00004030 }
4031private:
4032 std::string Id;
4033 internal::Matcher<T> InnerMatcher;
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004034 std::string InnerId;
Manuel Klimekc2687452012-10-24 14:47:44 +00004035};
4036
4037TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004038 EXPECT_TRUE(matchAndVerifyResultTrue(
4039 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4040 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004041 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4042 "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004043 EXPECT_TRUE(matchAndVerifyResultFalse(
4044 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4045 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004046 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4047 "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004048}
4049
4050TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004051 EXPECT_TRUE(matchAndVerifyResultTrue(
4052 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004053 new VerifyMatchOnNode<clang::Stmt>(
4054 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004055 EXPECT_TRUE(matchAndVerifyResultFalse(
4056 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004057 new VerifyMatchOnNode<clang::Stmt>(
4058 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004059}
4060
4061TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4062 EXPECT_TRUE(matchAndVerifyResultTrue(
4063 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4064 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004065 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004066 EXPECT_TRUE(matchAndVerifyResultFalse(
4067 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4068 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004069 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004070}
4071
Manuel Klimekbee08572013-02-07 12:42:10 +00004072template <typename T>
4073class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4074public:
4075 virtual bool run(const BoundNodes *Nodes) { return false; }
4076
4077 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4078 const T *Node = Nodes->getNodeAs<T>("");
4079 return verify(*Nodes, *Context, Node);
4080 }
4081
4082 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4083 return selectFirst<const T>(
4084 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4085 *Node, Context)) != NULL;
4086 }
4087 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4088 return selectFirst<const T>(
4089 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4090 *Node, Context)) != NULL;
4091 }
4092};
4093
4094TEST(IsEqualTo, MatchesNodesByIdentity) {
4095 EXPECT_TRUE(matchAndVerifyResultTrue(
4096 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4097 new VerifyAncestorHasChildIsEqual<Decl>()));
4098 EXPECT_TRUE(
4099 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4100 new VerifyAncestorHasChildIsEqual<Stmt>()));
4101}
4102
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004103class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4104public:
4105 VerifyStartOfTranslationUnit() : Called(false) {}
4106 virtual void run(const MatchFinder::MatchResult &Result) {
4107 EXPECT_TRUE(Called);
4108 }
4109 virtual void onStartOfTranslationUnit() {
4110 Called = true;
4111 }
4112 bool Called;
4113};
4114
4115TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4116 MatchFinder Finder;
4117 VerifyStartOfTranslationUnit VerifyCallback;
4118 Finder.addMatcher(decl(), &VerifyCallback);
4119 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4120 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4121 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004122
4123 VerifyCallback.Called = false;
4124 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4125 ASSERT_TRUE(AST.get());
4126 Finder.matchAST(AST->getASTContext());
4127 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004128}
4129
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004130class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4131public:
4132 VerifyEndOfTranslationUnit() : Called(false) {}
4133 virtual void run(const MatchFinder::MatchResult &Result) {
4134 EXPECT_FALSE(Called);
4135 }
4136 virtual void onEndOfTranslationUnit() {
4137 Called = true;
4138 }
4139 bool Called;
4140};
4141
4142TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4143 MatchFinder Finder;
4144 VerifyEndOfTranslationUnit VerifyCallback;
4145 Finder.addMatcher(decl(), &VerifyCallback);
4146 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4147 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4148 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004149
4150 VerifyCallback.Called = false;
4151 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4152 ASSERT_TRUE(AST.get());
4153 Finder.matchAST(AST->getASTContext());
4154 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004155}
4156
Manuel Klimekbbb75852013-06-20 14:06:32 +00004157TEST(EqualsBoundNodeMatcher, QualType) {
4158 EXPECT_TRUE(matches(
4159 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4160 hasInitializer(ignoringParenImpCasts(
4161 hasType(qualType(equalsBoundNode("type"))))))));
4162 EXPECT_TRUE(notMatches("int i = 1.f;",
4163 varDecl(hasType(qualType().bind("type")),
4164 hasInitializer(ignoringParenImpCasts(hasType(
4165 qualType(equalsBoundNode("type"))))))));
4166}
4167
4168TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4169 EXPECT_TRUE(notMatches(
4170 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4171 hasInitializer(ignoringParenImpCasts(
4172 hasType(qualType(equalsBoundNode("type"))))))));
4173}
4174
4175TEST(EqualsBoundNodeMatcher, Stmt) {
4176 EXPECT_TRUE(
4177 matches("void f() { if(true) {} }",
4178 stmt(allOf(ifStmt().bind("if"),
4179 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4180
4181 EXPECT_TRUE(notMatches(
4182 "void f() { if(true) { if (true) {} } }",
4183 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4184}
4185
4186TEST(EqualsBoundNodeMatcher, Decl) {
4187 EXPECT_TRUE(matches(
4188 "class X { class Y {}; };",
4189 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4190 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4191
4192 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4193 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4194 has(decl(equalsBoundNode("record")))))));
4195}
4196
4197TEST(EqualsBoundNodeMatcher, Type) {
4198 EXPECT_TRUE(matches(
4199 "class X { int a; int b; };",
4200 recordDecl(
4201 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4202 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4203
4204 EXPECT_TRUE(notMatches(
4205 "class X { int a; double b; };",
4206 recordDecl(
4207 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4208 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4209}
4210
4211TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4212
4213 EXPECT_TRUE(matchAndVerifyResultTrue(
4214 "int f() {"
4215 " if (1) {"
4216 " int i = 9;"
4217 " }"
4218 " int j = 10;"
4219 " {"
4220 " float k = 9.0;"
4221 " }"
4222 " return 0;"
4223 "}",
4224 // Look for variable declarations within functions whose type is the same
4225 // as the function return type.
4226 functionDecl(returns(qualType().bind("type")),
4227 forEachDescendant(varDecl(hasType(
4228 qualType(equalsBoundNode("type")))).bind("decl"))),
4229 // Only i and j should match, not k.
4230 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4231}
4232
4233TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4234 EXPECT_TRUE(matchAndVerifyResultTrue(
4235 "void f() {"
4236 " int x;"
4237 " double d;"
4238 " x = d + x - d + x;"
4239 "}",
4240 functionDecl(
4241 hasName("f"), forEachDescendant(varDecl().bind("d")),
4242 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4243 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4244}
4245
Manuel Klimek04616e42012-07-06 05:48:52 +00004246} // end namespace ast_matchers
4247} // end namespace clang