blob: d458431ecbb2bd9d0f018fc47d36972a4fc47ea2 [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"
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +000015#include "llvm/ADT/Triple.h"
16#include "llvm/Support/Host.h"
Manuel Klimek04616e42012-07-06 05:48:52 +000017#include "gtest/gtest.h"
18
19namespace clang {
20namespace ast_matchers {
21
Benjamin Kramer60d7f5a2012-07-10 17:30:44 +000022#if GTEST_HAS_DEATH_TEST
Manuel Klimek04616e42012-07-06 05:48:52 +000023TEST(HasNameDeathTest, DiesOnEmptyName) {
24 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000025 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek04616e42012-07-06 05:48:52 +000026 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
27 }, "");
28}
29
Daniel Jasper1dad1832012-07-10 20:20:19 +000030TEST(HasNameDeathTest, DiesOnEmptyPattern) {
31 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000032 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jasper1dad1832012-07-10 20:20:19 +000033 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
34 }, "");
35}
36
Manuel Klimek04616e42012-07-06 05:48:52 +000037TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
38 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000039 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek04616e42012-07-06 05:48:52 +000040 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
41 }, "");
42}
Benjamin Kramer60d7f5a2012-07-10 17:30:44 +000043#endif
Manuel Klimek04616e42012-07-06 05:48:52 +000044
Peter Collingbourne2b9471302013-11-07 22:30:32 +000045TEST(Finder, DynamicOnlyAcceptsSomeMatchers) {
46 MatchFinder Finder;
Craig Topper416fa342014-06-08 08:38:12 +000047 EXPECT_TRUE(Finder.addDynamicMatcher(decl(), nullptr));
48 EXPECT_TRUE(Finder.addDynamicMatcher(callExpr(), nullptr));
49 EXPECT_TRUE(Finder.addDynamicMatcher(constantArrayType(hasSize(42)),
50 nullptr));
Peter Collingbourne2b9471302013-11-07 22:30:32 +000051
52 // Do not accept non-toplevel matchers.
Craig Topper416fa342014-06-08 08:38:12 +000053 EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), nullptr));
54 EXPECT_FALSE(Finder.addDynamicMatcher(hasSize(2), nullptr));
55 EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), nullptr));
Peter Collingbourne2b9471302013-11-07 22:30:32 +000056}
57
Manuel Klimeke9235692012-07-25 10:02:02 +000058TEST(Decl, MatchesDeclarations) {
59 EXPECT_TRUE(notMatches("", decl(usingDecl())));
60 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
61 decl(usingDecl())));
62}
63
Manuel Klimek04616e42012-07-06 05:48:52 +000064TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000065 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +000066 EXPECT_TRUE(matches("typedef int X;", NamedX));
67 EXPECT_TRUE(matches("int X;", NamedX));
68 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
69 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
70 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
71 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jasper1dad1832012-07-10 20:20:19 +000072 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek04616e42012-07-06 05:48:52 +000073
74 EXPECT_TRUE(notMatches("#define X 1", NamedX));
75}
76
Daniel Jasper1dad1832012-07-10 20:20:19 +000077TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000078 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000079 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
80 EXPECT_TRUE(matches("int Xb;", NamedX));
81 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
82 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
83 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
84 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
85 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
86
87 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
88
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000089 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000090 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
91 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
92
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000093 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000094 EXPECT_TRUE(matches("int abc;", Abc));
95 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
96 EXPECT_TRUE(notMatches("int cab;", Abc));
97 EXPECT_TRUE(matches("int cabc;", Abc));
Manuel Klimeke792efd2012-12-10 07:08:53 +000098
99 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
100 EXPECT_TRUE(matches("int k;", StartsWithK));
101 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
102 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
103 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
104 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000105}
106
Manuel Klimek04616e42012-07-06 05:48:52 +0000107TEST(DeclarationMatcher, MatchClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000108 DeclarationMatcher ClassMatcher(recordDecl());
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000109 llvm::Triple Triple(llvm::sys::getDefaultTargetTriple());
110 if (Triple.getOS() != llvm::Triple::Win32 ||
111 Triple.getEnvironment() != llvm::Triple::MSVC)
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +0000112 EXPECT_FALSE(matches("", ClassMatcher));
113 else
114 // Matches class type_info.
115 EXPECT_TRUE(matches("", ClassMatcher));
Manuel Klimek04616e42012-07-06 05:48:52 +0000116
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000117 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000118 EXPECT_TRUE(matches("class X;", ClassX));
119 EXPECT_TRUE(matches("class X {};", ClassX));
120 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
121 EXPECT_TRUE(notMatches("", ClassX));
122}
123
124TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000125 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000126
127 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasperf49d1e02012-09-07 12:48:17 +0000128 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
129 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek04616e42012-07-06 05:48:52 +0000130 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
131 EXPECT_TRUE(notMatches("", IsDerivedFromX));
132
Daniel Jasperd6b82cb2012-09-12 21:14:15 +0000133 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasperf49d1e02012-09-07 12:48:17 +0000134
135 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
136 EXPECT_TRUE(matches("class X {};", IsAX));
137 EXPECT_TRUE(matches("class X;", IsAX));
138 EXPECT_TRUE(notMatches("class Y;", IsAX));
139 EXPECT_TRUE(notMatches("", IsAX));
140
Manuel Klimek04616e42012-07-06 05:48:52 +0000141 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000142 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000143 EXPECT_TRUE(
144 matches("class X {}; class Y : public X {}; class Z : public Y {};",
145 ZIsDerivedFromX));
146 EXPECT_TRUE(
147 matches("class X {};"
148 "template<class T> class Y : public X {};"
149 "class Z : public Y<int> {};", ZIsDerivedFromX));
150 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
151 ZIsDerivedFromX));
152 EXPECT_TRUE(
153 matches("template<class T> class X {}; "
154 "template<class T> class Z : public X<T> {};",
155 ZIsDerivedFromX));
156 EXPECT_TRUE(
157 matches("template<class T, class U=T> class X {}; "
158 "template<class T> class Z : public X<T> {};",
159 ZIsDerivedFromX));
160 EXPECT_TRUE(
161 notMatches("template<class X> class A { class Z : public X {}; };",
162 ZIsDerivedFromX));
163 EXPECT_TRUE(
164 matches("template<class X> class A { public: class Z : public X {}; }; "
165 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
166 EXPECT_TRUE(
167 matches("template <class T> class X {}; "
168 "template<class Y> class A { class Z : public X<Y> {}; };",
169 ZIsDerivedFromX));
170 EXPECT_TRUE(
171 notMatches("template<template<class T> class X> class A { "
172 " class Z : public X<int> {}; };", ZIsDerivedFromX));
173 EXPECT_TRUE(
174 matches("template<template<class T> class X> class A { "
175 " public: class Z : public X<int> {}; }; "
176 "template<class T> class X {}; void y() { A<X>::Z z; }",
177 ZIsDerivedFromX));
178 EXPECT_TRUE(
179 notMatches("template<class X> class A { class Z : public X::D {}; };",
180 ZIsDerivedFromX));
181 EXPECT_TRUE(
182 matches("template<class X> class A { public: "
183 " class Z : public X::D {}; }; "
184 "class Y { public: class X {}; typedef X D; }; "
185 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
186 EXPECT_TRUE(
187 matches("class X {}; typedef X Y; class Z : public Y {};",
188 ZIsDerivedFromX));
189 EXPECT_TRUE(
190 matches("template<class T> class Y { typedef typename T::U X; "
191 " class Z : public X {}; };", ZIsDerivedFromX));
192 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
193 ZIsDerivedFromX));
194 EXPECT_TRUE(
195 notMatches("template<class T> class X {}; "
196 "template<class T> class A { class Z : public X<T>::D {}; };",
197 ZIsDerivedFromX));
198 EXPECT_TRUE(
199 matches("template<class T> class X { public: typedef X<T> D; }; "
200 "template<class T> class A { public: "
201 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
202 ZIsDerivedFromX));
203 EXPECT_TRUE(
204 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
205 ZIsDerivedFromX));
206 EXPECT_TRUE(
207 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
208 ZIsDerivedFromX));
209 EXPECT_TRUE(
210 matches("class X {}; class Y : public X {}; "
211 "typedef Y V; typedef V W; class Z : public W {};",
212 ZIsDerivedFromX));
213 EXPECT_TRUE(
214 matches("template<class T, class U> class X {}; "
215 "template<class T> class A { class Z : public X<T, int> {}; };",
216 ZIsDerivedFromX));
217 EXPECT_TRUE(
218 notMatches("template<class X> class D { typedef X A; typedef A B; "
219 " typedef B C; class Z : public C {}; };",
220 ZIsDerivedFromX));
221 EXPECT_TRUE(
222 matches("class X {}; typedef X A; typedef A B; "
223 "class Z : public B {};", ZIsDerivedFromX));
224 EXPECT_TRUE(
225 matches("class X {}; typedef X A; typedef A B; typedef B C; "
226 "class Z : public C {};", ZIsDerivedFromX));
227 EXPECT_TRUE(
228 matches("class U {}; typedef U X; typedef X V; "
229 "class Z : public V {};", ZIsDerivedFromX));
230 EXPECT_TRUE(
231 matches("class Base {}; typedef Base X; "
232 "class Z : public Base {};", ZIsDerivedFromX));
233 EXPECT_TRUE(
234 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
235 "class Z : public Base {};", ZIsDerivedFromX));
236 EXPECT_TRUE(
237 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
238 "class Z : public Base {};", ZIsDerivedFromX));
239 EXPECT_TRUE(
240 matches("class A {}; typedef A X; typedef A Y; "
241 "class Z : public Y {};", ZIsDerivedFromX));
242 EXPECT_TRUE(
243 notMatches("template <typename T> class Z;"
244 "template <> class Z<void> {};"
245 "template <typename T> class Z : public Z<void> {};",
246 IsDerivedFromX));
247 EXPECT_TRUE(
248 matches("template <typename T> class X;"
249 "template <> class X<void> {};"
250 "template <typename T> class X : public X<void> {};",
251 IsDerivedFromX));
252 EXPECT_TRUE(matches(
253 "class X {};"
254 "template <typename T> class Z;"
255 "template <> class Z<void> {};"
256 "template <typename T> class Z : public Z<void>, public X {};",
257 ZIsDerivedFromX));
Manuel Klimek5472a522012-12-04 13:40:29 +0000258 EXPECT_TRUE(
259 notMatches("template<int> struct X;"
260 "template<int i> struct X : public X<i-1> {};",
261 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
262 EXPECT_TRUE(matches(
263 "struct A {};"
264 "template<int> struct X;"
265 "template<int i> struct X : public X<i-1> {};"
266 "template<> struct X<0> : public A {};"
267 "struct B : public X<42> {};",
268 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000269
270 // FIXME: Once we have better matchers for template type matching,
271 // get rid of the Variable(...) matching and match the right template
272 // declarations directly.
273 const char *RecursiveTemplateOneParameter =
274 "class Base1 {}; class Base2 {};"
275 "template <typename T> class Z;"
276 "template <> class Z<void> : public Base1 {};"
277 "template <> class Z<int> : public Base2 {};"
278 "template <> class Z<float> : public Z<void> {};"
279 "template <> class Z<double> : public Z<int> {};"
280 "template <typename T> class Z : public Z<float>, public Z<double> {};"
281 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
282 EXPECT_TRUE(matches(
283 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000284 varDecl(hasName("z_float"),
285 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000286 EXPECT_TRUE(notMatches(
287 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000288 varDecl(hasName("z_float"),
289 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000290 EXPECT_TRUE(matches(
291 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000292 varDecl(hasName("z_char"),
293 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
294 isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000295
296 const char *RecursiveTemplateTwoParameters =
297 "class Base1 {}; class Base2 {};"
298 "template <typename T1, typename T2> class Z;"
299 "template <typename T> class Z<void, T> : public Base1 {};"
300 "template <typename T> class Z<int, T> : public Base2 {};"
301 "template <typename T> class Z<float, T> : public Z<void, T> {};"
302 "template <typename T> class Z<double, T> : public Z<int, T> {};"
303 "template <typename T1, typename T2> class Z : "
304 " public Z<float, T2>, public Z<double, T2> {};"
305 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
306 " Z<char, void> z_char; }";
307 EXPECT_TRUE(matches(
308 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000309 varDecl(hasName("z_float"),
310 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000311 EXPECT_TRUE(notMatches(
312 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000313 varDecl(hasName("z_float"),
314 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000315 EXPECT_TRUE(matches(
316 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000317 varDecl(hasName("z_char"),
318 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
319 isDerivedFrom("Base2")))))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000320 EXPECT_TRUE(matches(
321 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000322 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000323 EXPECT_TRUE(notMatches(
324 "class X {}; class Y : public X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000325 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000326
327 EXPECT_TRUE(matches(
328 "class X {}; class Y : public X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000329 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek1863e502013-08-02 21:24:09 +0000330
331 EXPECT_TRUE(matches(
332 "template<typename T> class X {};"
333 "template<typename T> using Z = X<T>;"
334 "template <typename T> class Y : Z<T> {};",
335 recordDecl(isDerivedFrom(namedDecl(hasName("X"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000336}
337
Edwin Vane0a4836e2013-03-06 17:02:57 +0000338TEST(DeclarationMatcher, hasMethod) {
339 EXPECT_TRUE(matches("class A { void func(); };",
340 recordDecl(hasMethod(hasName("func")))));
341 EXPECT_TRUE(notMatches("class A { void func(); };",
342 recordDecl(hasMethod(isPublic()))));
343}
344
Daniel Jasper83dafaf2012-09-18 14:17:42 +0000345TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
346 EXPECT_TRUE(matches(
347 "template <typename T> struct A {"
348 " template <typename T2> struct F {};"
349 "};"
350 "template <typename T> struct B : A<T>::template F<T> {};"
351 "B<int> b;",
352 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
353}
354
Edwin Vaneb6eae142013-02-25 20:43:32 +0000355TEST(DeclarationMatcher, hasDeclContext) {
356 EXPECT_TRUE(matches(
357 "namespace N {"
358 " namespace M {"
359 " class D {};"
360 " }"
361 "}",
Daniel Jasper9fcdc462013-04-08 16:44:05 +0000362 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +0000363 EXPECT_TRUE(notMatches(
364 "namespace N {"
365 " namespace M {"
366 " class D {};"
367 " }"
368 "}",
Daniel Jasper9fcdc462013-04-08 16:44:05 +0000369 recordDecl(hasDeclContext(namespaceDecl(hasName("N"))))));
370
371 EXPECT_TRUE(matches("namespace {"
372 " namespace M {"
373 " class D {};"
374 " }"
375 "}",
376 recordDecl(hasDeclContext(namespaceDecl(
377 hasName("M"), hasDeclContext(namespaceDecl()))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +0000378}
379
Manuel Klimek94ad0bf2014-09-04 08:51:06 +0000380TEST(DeclarationMatcher, LinkageSpecification) {
381 EXPECT_TRUE(matches("extern \"C\" { void foo() {}; }", linkageSpecDecl()));
382 EXPECT_TRUE(notMatches("void foo() {};", linkageSpecDecl()));
383}
384
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000385TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000386 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000387 EXPECT_TRUE(notMatches("class X;", ClassX));
388 EXPECT_TRUE(notMatches("class X {};", ClassX));
389}
390
391TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000392 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000393 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
394 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
395}
396
397TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
398 EXPECT_TRUE(notMatches("template<typename T> class X { };"
399 "template<> class X<int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000400 classTemplateDecl(hasName("X"),
401 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000402}
403
404TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
405 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
406 "template<typename T> class X<T, int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000407 classTemplateDecl(hasName("X"),
408 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000409}
410
Daniel Jasper4e566c42012-07-12 08:50:38 +0000411TEST(AllOf, AllOverloadsWork) {
412 const char Program[] =
Edwin Vanee9dd3602013-02-12 13:55:40 +0000413 "struct T { };"
414 "int f(int, T*, int, int);"
415 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper4e566c42012-07-12 08:50:38 +0000416 EXPECT_TRUE(matches(Program,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000417 callExpr(allOf(callee(functionDecl(hasName("f"))),
418 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +0000419 EXPECT_TRUE(matches(Program,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000420 callExpr(allOf(callee(functionDecl(hasName("f"))),
421 hasArgument(0, declRefExpr(to(varDecl()))),
422 hasArgument(1, hasType(pointsTo(
423 recordDecl(hasName("T")))))))));
Edwin Vanee9dd3602013-02-12 13:55:40 +0000424 EXPECT_TRUE(matches(Program,
425 callExpr(allOf(callee(functionDecl(hasName("f"))),
426 hasArgument(0, declRefExpr(to(varDecl()))),
427 hasArgument(1, hasType(pointsTo(
428 recordDecl(hasName("T"))))),
429 hasArgument(2, integerLiteral(equals(3)))))));
430 EXPECT_TRUE(matches(Program,
431 callExpr(allOf(callee(functionDecl(hasName("f"))),
432 hasArgument(0, declRefExpr(to(varDecl()))),
433 hasArgument(1, hasType(pointsTo(
434 recordDecl(hasName("T"))))),
435 hasArgument(2, integerLiteral(equals(3))),
436 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +0000437}
438
Manuel Klimek04616e42012-07-06 05:48:52 +0000439TEST(DeclarationMatcher, MatchAnyOf) {
440 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000441 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000442 EXPECT_TRUE(
443 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
444 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
445 EXPECT_TRUE(
446 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
447 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
448
Daniel Jasper84c763e2012-07-15 19:57:12 +0000449 DeclarationMatcher XOrYOrZOrU =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000450 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasper84c763e2012-07-15 19:57:12 +0000451 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
452 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
453
Manuel Klimek04616e42012-07-06 05:48:52 +0000454 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000455 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
456 hasName("V")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000457 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
458 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
459 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
460 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
461 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
462 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
Samuel Benzaquena1170022014-10-06 13:14:30 +0000463
464 StatementMatcher MixedTypes = stmt(anyOf(ifStmt(), binaryOperator()));
465 EXPECT_TRUE(matches("int F() { return 1 + 2; }", MixedTypes));
466 EXPECT_TRUE(matches("int F() { if (true) return 1; }", MixedTypes));
467 EXPECT_TRUE(notMatches("int F() { return 1; }", MixedTypes));
Manuel Klimek04616e42012-07-06 05:48:52 +0000468}
469
470TEST(DeclarationMatcher, MatchHas) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000471 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000472 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
473 EXPECT_TRUE(matches("class X {};", HasClassX));
474
475 DeclarationMatcher YHasClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000476 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000477 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
478 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
479 EXPECT_TRUE(
480 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
481}
482
483TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
484 DeclarationMatcher Recursive =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000485 recordDecl(
486 has(recordDecl(
487 has(recordDecl(hasName("X"))),
488 has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000489 hasName("Z"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000490 has(recordDecl(
491 has(recordDecl(hasName("A"))),
492 has(recordDecl(hasName("B"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000493 hasName("C"))),
494 hasName("F"));
495
496 EXPECT_TRUE(matches(
497 "class F {"
498 " class Z {"
499 " class X {};"
500 " class Y {};"
501 " };"
502 " class C {"
503 " class A {};"
504 " class B {};"
505 " };"
506 "};", Recursive));
507
508 EXPECT_TRUE(matches(
509 "class F {"
510 " class Z {"
511 " class A {};"
512 " class X {};"
513 " class Y {};"
514 " };"
515 " class C {"
516 " class X {};"
517 " class A {};"
518 " class B {};"
519 " };"
520 "};", Recursive));
521
522 EXPECT_TRUE(matches(
523 "class O1 {"
524 " class O2 {"
525 " class F {"
526 " class Z {"
527 " class A {};"
528 " class X {};"
529 " class Y {};"
530 " };"
531 " class C {"
532 " class X {};"
533 " class A {};"
534 " class B {};"
535 " };"
536 " };"
537 " };"
538 "};", Recursive));
539}
540
541TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
542 DeclarationMatcher Recursive =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000543 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000544 anyOf(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000545 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000546 anyOf(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000547 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000548 hasName("X"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000549 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000550 hasName("Y"))),
551 hasName("Z")))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000552 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000553 anyOf(
554 hasName("C"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000555 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000556 hasName("A"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000557 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000558 hasName("B")))))),
559 hasName("F")));
560
561 EXPECT_TRUE(matches("class F {};", Recursive));
562 EXPECT_TRUE(matches("class Z {};", Recursive));
563 EXPECT_TRUE(matches("class C {};", Recursive));
564 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
565 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
566 EXPECT_TRUE(
567 matches("class O1 { class O2 {"
568 " class M { class N { class B {}; }; }; "
569 "}; };", Recursive));
570}
571
572TEST(DeclarationMatcher, MatchNot) {
573 DeclarationMatcher NotClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000574 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000575 isDerivedFrom("Y"),
Manuel Klimek04616e42012-07-06 05:48:52 +0000576 unless(hasName("X")));
577 EXPECT_TRUE(notMatches("", NotClassX));
578 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
579 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
580 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
581 EXPECT_TRUE(
582 notMatches("class Y {}; class Z {}; class X : public Y {};",
583 NotClassX));
584
585 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000586 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000587 hasName("X"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000588 has(recordDecl(hasName("Z"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000589 unless(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000590 has(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000591 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
592 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
593 ClassXHasNotClassY));
Samuel Benzaquen20099602014-10-13 17:38:12 +0000594
595 DeclarationMatcher NamedNotRecord =
596 namedDecl(hasName("Foo"), unless(recordDecl()));
597 EXPECT_TRUE(matches("void Foo(){}", NamedNotRecord));
598 EXPECT_TRUE(notMatches("struct Foo {};", NamedNotRecord));
Manuel Klimek04616e42012-07-06 05:48:52 +0000599}
600
601TEST(DeclarationMatcher, HasDescendant) {
602 DeclarationMatcher ZDescendantClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000603 recordDecl(
604 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000605 hasName("Z"));
606 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
607 EXPECT_TRUE(
608 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
609 EXPECT_TRUE(
610 matches("class Z { class A { class Y { class X {}; }; }; };",
611 ZDescendantClassX));
612 EXPECT_TRUE(
613 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
614 ZDescendantClassX));
615 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
616
617 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000618 recordDecl(
619 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000620 hasName("X"))),
621 hasName("Z"));
622 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
623 ZDescendantClassXHasClassY));
624 EXPECT_TRUE(
625 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
626 ZDescendantClassXHasClassY));
627 EXPECT_TRUE(notMatches(
628 "class Z {"
629 " class A {"
630 " class B {"
631 " class X {"
632 " class C {"
633 " class Y {};"
634 " };"
635 " };"
636 " }; "
637 " };"
638 "};", ZDescendantClassXHasClassY));
639
640 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000641 recordDecl(
642 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
643 hasName("X"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000644 hasName("Z"));
645 EXPECT_TRUE(
646 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
647 ZDescendantClassXDescendantClassY));
648 EXPECT_TRUE(matches(
649 "class Z {"
650 " class A {"
651 " class X {"
652 " class B {"
653 " class Y {};"
654 " };"
655 " class Y {};"
656 " };"
657 " };"
658 "};", ZDescendantClassXDescendantClassY));
659}
660
Daniel Jasper3dfa09b2014-07-23 13:17:47 +0000661TEST(DeclarationMatcher, HasDescendantMemoization) {
662 DeclarationMatcher CannotMemoize =
663 decl(hasDescendant(typeLoc().bind("x")), has(decl()));
664 EXPECT_TRUE(matches("void f() { int i; }", CannotMemoize));
665}
666
Samuel Benzaquenf28d9972014-10-01 15:08:07 +0000667TEST(DeclarationMatcher, HasDescendantMemoizationUsesRestrictKind) {
668 auto Name = hasName("i");
669 auto VD = internal::Matcher<VarDecl>(Name).dynCastTo<Decl>();
670 auto RD = internal::Matcher<RecordDecl>(Name).dynCastTo<Decl>();
671 // Matching VD first should not make a cache hit for RD.
672 EXPECT_TRUE(notMatches("void f() { int i; }",
673 decl(hasDescendant(VD), hasDescendant(RD))));
674 EXPECT_TRUE(notMatches("void f() { int i; }",
675 decl(hasDescendant(RD), hasDescendant(VD))));
676 // Not matching RD first should not make a cache hit for VD either.
677 EXPECT_TRUE(matches("void f() { int i; }",
678 decl(anyOf(hasDescendant(RD), hasDescendant(VD)))));
679}
680
Manuel Klimek3fe8a382014-08-25 11:23:50 +0000681TEST(DeclarationMatcher, HasAttr) {
682 EXPECT_TRUE(matches("struct __attribute__((warn_unused)) X {};",
683 decl(hasAttr(clang::attr::WarnUnused))));
684 EXPECT_FALSE(matches("struct X {};",
685 decl(hasAttr(clang::attr::WarnUnused))));
686}
687
Manuel Klimekd52a3b82014-08-05 09:45:53 +0000688TEST(DeclarationMatcher, MatchCudaDecl) {
689 EXPECT_TRUE(matchesWithCuda("__global__ void f() { }"
690 "void g() { f<<<1, 2>>>(); }",
691 CUDAKernelCallExpr()));
692 EXPECT_TRUE(matchesWithCuda("__attribute__((device)) void f() {}",
Manuel Klimek3fe8a382014-08-25 11:23:50 +0000693 hasAttr(clang::attr::CUDADevice)));
Manuel Klimekd52a3b82014-08-05 09:45:53 +0000694 EXPECT_TRUE(notMatchesWithCuda("void f() {}",
Manuel Klimek3fe8a382014-08-25 11:23:50 +0000695 CUDAKernelCallExpr()));
Manuel Klimekd52a3b82014-08-05 09:45:53 +0000696 EXPECT_FALSE(notMatchesWithCuda("__attribute__((global)) void f() {}",
Manuel Klimek3fe8a382014-08-25 11:23:50 +0000697 hasAttr(clang::attr::CUDAGlobal)));
Manuel Klimekd52a3b82014-08-05 09:45:53 +0000698}
699
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000700// Implements a run method that returns whether BoundNodes contains a
701// Decl bound to Id that can be dynamically cast to T.
702// Optionally checks that the check succeeded a specific number of times.
703template <typename T>
704class VerifyIdIsBoundTo : public BoundNodesCallback {
705public:
706 // Create an object that checks that a node of type \c T was bound to \c Id.
707 // Does not check for a certain number of matches.
708 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
709 : Id(Id), ExpectedCount(-1), Count(0) {}
710
711 // Create an object that checks that a node of type \c T was bound to \c Id.
712 // Checks that there were exactly \c ExpectedCount matches.
713 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
714 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
715
716 // Create an object that checks that a node of type \c T was bound to \c Id.
717 // Checks that there was exactly one match with the name \c ExpectedName.
718 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimekb64d6b72013-03-14 16:33:21 +0000719 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
720 int ExpectedCount = 1)
721 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
722 ExpectedName(ExpectedName) {}
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000723
Craig Toppera798a9d2014-03-02 09:32:10 +0000724 void onEndOfTranslationUnit() override {
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000725 if (ExpectedCount != -1)
726 EXPECT_EQ(ExpectedCount, Count);
727 if (!ExpectedName.empty())
728 EXPECT_EQ(ExpectedName, Name);
Peter Collingbourne2b9471302013-11-07 22:30:32 +0000729 Count = 0;
730 Name.clear();
731 }
732
733 ~VerifyIdIsBoundTo() {
734 EXPECT_EQ(0, Count);
735 EXPECT_EQ("", Name);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000736 }
737
Fariborz Jahanian5afc8692014-10-01 16:56:40 +0000738 virtual bool run(const BoundNodes *Nodes) override {
Peter Collingbourne093a7292013-11-06 00:27:07 +0000739 const BoundNodes::IDToNodeMap &M = Nodes->getMap();
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000740 if (Nodes->getNodeAs<T>(Id)) {
741 ++Count;
742 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
743 Name = Named->getNameAsString();
744 } else if (const NestedNameSpecifier *NNS =
745 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
746 llvm::raw_string_ostream OS(Name);
747 NNS->print(OS, PrintingPolicy(LangOptions()));
748 }
Peter Collingbourne093a7292013-11-06 00:27:07 +0000749 BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
750 EXPECT_NE(M.end(), I);
751 if (I != M.end())
752 EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000753 return true;
754 }
Craig Topper416fa342014-06-08 08:38:12 +0000755 EXPECT_TRUE(M.count(Id) == 0 ||
756 M.find(Id)->second.template get<T>() == nullptr);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000757 return false;
758 }
759
Fariborz Jahanian5afc8692014-10-01 16:56:40 +0000760 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) override {
Daniel Jaspere9aa6872012-10-29 10:48:25 +0000761 return run(Nodes);
762 }
763
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000764private:
765 const std::string Id;
766 const int ExpectedCount;
767 int Count;
768 const std::string ExpectedName;
769 std::string Name;
770};
771
772TEST(HasDescendant, MatchesDescendantTypes) {
773 EXPECT_TRUE(matches("void f() { int i = 3; }",
774 decl(hasDescendant(loc(builtinType())))));
775 EXPECT_TRUE(matches("void f() { int i = 3; }",
776 stmt(hasDescendant(builtinType()))));
777
778 EXPECT_TRUE(matches("void f() { int i = 3; }",
779 stmt(hasDescendant(loc(builtinType())))));
780 EXPECT_TRUE(matches("void f() { int i = 3; }",
781 stmt(hasDescendant(qualType(builtinType())))));
782
783 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
784 stmt(hasDescendant(isInteger()))));
785
786 EXPECT_TRUE(matchAndVerifyResultTrue(
787 "void f() { int a; float c; int d; int e; }",
788 functionDecl(forEachDescendant(
789 varDecl(hasDescendant(isInteger())).bind("x"))),
790 new VerifyIdIsBoundTo<Decl>("x", 3)));
791}
792
793TEST(HasDescendant, MatchesDescendantsOfTypes) {
794 EXPECT_TRUE(matches("void f() { int*** i; }",
795 qualType(hasDescendant(builtinType()))));
796 EXPECT_TRUE(matches("void f() { int*** i; }",
797 qualType(hasDescendant(
798 pointerType(pointee(builtinType()))))));
799 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikieb61d0872013-02-18 19:04:16 +0000800 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000801
802 EXPECT_TRUE(matchAndVerifyResultTrue(
803 "void f() { int*** i; }",
804 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
805 new VerifyIdIsBoundTo<Type>("x", 2)));
806}
807
808TEST(Has, MatchesChildrenOfTypes) {
809 EXPECT_TRUE(matches("int i;",
810 varDecl(hasName("i"), has(isInteger()))));
811 EXPECT_TRUE(notMatches("int** i;",
812 varDecl(hasName("i"), has(isInteger()))));
813 EXPECT_TRUE(matchAndVerifyResultTrue(
814 "int (*f)(float, int);",
815 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
816 new VerifyIdIsBoundTo<QualType>("x", 2)));
817}
818
819TEST(Has, MatchesChildTypes) {
820 EXPECT_TRUE(matches(
821 "int* i;",
822 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
823 EXPECT_TRUE(notMatches(
824 "int* i;",
825 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
826}
827
Daniel Jasper1dad1832012-07-10 20:20:19 +0000828TEST(Enum, DoesNotMatchClasses) {
829 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
830}
831
832TEST(Enum, MatchesEnums) {
833 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
834}
835
836TEST(EnumConstant, Matches) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000837 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000838 EXPECT_TRUE(matches("enum X{ A };", Matcher));
839 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
840 EXPECT_TRUE(notMatches("enum X {};", Matcher));
841}
842
Manuel Klimek04616e42012-07-06 05:48:52 +0000843TEST(StatementMatcher, Has) {
844 StatementMatcher HasVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000845 expr(hasType(pointsTo(recordDecl(hasName("X")))),
846 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000847
848 EXPECT_TRUE(matches(
849 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
850 EXPECT_TRUE(notMatches(
851 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
852}
853
854TEST(StatementMatcher, HasDescendant) {
855 StatementMatcher HasDescendantVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000856 expr(hasType(pointsTo(recordDecl(hasName("X")))),
857 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000858
859 EXPECT_TRUE(matches(
860 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
861 HasDescendantVariableI));
862 EXPECT_TRUE(notMatches(
863 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
864 HasDescendantVariableI));
865}
866
867TEST(TypeMatcher, MatchesClassType) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000868 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000869
870 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
871 EXPECT_TRUE(notMatches("class A {};", TypeA));
872
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000873 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000874
875 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
876 TypeDerivedFromA));
877 EXPECT_TRUE(notMatches("class A {};", TypeA));
878
879 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000880 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000881
882 EXPECT_TRUE(
883 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
884}
885
Manuel Klimek04616e42012-07-06 05:48:52 +0000886TEST(Matcher, BindMatchedNodes) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000887 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000888
889 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000890 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000891
892 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000893 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000894
895 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000896 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000897
898 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
899 TypeAHasClassB,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000900 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000901
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000902 StatementMatcher MethodX =
903 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek04616e42012-07-06 05:48:52 +0000904
905 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
906 MethodX,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000907 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000908}
909
910TEST(Matcher, BindTheSameNameInAlternatives) {
911 StatementMatcher matcher = anyOf(
912 binaryOperator(hasOperatorName("+"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000913 hasLHS(expr().bind("x")),
Daniel Jasper1dad1832012-07-10 20:20:19 +0000914 hasRHS(integerLiteral(equals(0)))),
915 binaryOperator(hasOperatorName("+"),
916 hasLHS(integerLiteral(equals(0))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000917 hasRHS(expr().bind("x"))));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000918
919 EXPECT_TRUE(matchAndVerifyResultTrue(
920 // The first branch of the matcher binds x to 0 but then fails.
921 // The second branch binds x to f() and succeeds.
922 "int f() { return 0 + f(); }",
923 matcher,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000924 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000925}
926
Manuel Klimekfdf98762012-08-30 19:41:06 +0000927TEST(Matcher, BindsIDForMemoizedResults) {
928 // Using the same matcher in two match expressions will make memoization
929 // kick in.
930 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
931 EXPECT_TRUE(matchAndVerifyResultTrue(
932 "class A { class B { class X {}; }; };",
933 DeclarationMatcher(anyOf(
934 recordDecl(hasName("A"), hasDescendant(ClassX)),
935 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000936 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimekfdf98762012-08-30 19:41:06 +0000937}
938
Daniel Jasper856194d02012-12-03 15:43:25 +0000939TEST(HasDeclaration, HasDeclarationOfEnumType) {
940 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
941 expr(hasType(pointsTo(
942 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
943}
944
Edwin Vaneed936452013-02-25 14:32:42 +0000945TEST(HasDeclaration, HasGetDeclTraitTest) {
946 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
947 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
948 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
949}
950
Edwin Vane2c197e02013-02-19 17:14:34 +0000951TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
952 EXPECT_TRUE(matches("typedef int X; X a;",
953 varDecl(hasName("a"),
954 hasType(typedefType(hasDeclaration(decl()))))));
955
956 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
957}
958
Edwin Vanef901b712013-02-25 14:49:29 +0000959TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
960 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
961 varDecl(hasType(templateSpecializationType(
962 hasDeclaration(namedDecl(hasName("A"))))))));
963}
964
Manuel Klimek04616e42012-07-06 05:48:52 +0000965TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000966 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000967 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000968 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000969 EXPECT_TRUE(
970 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000971 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000972 EXPECT_TRUE(
973 matches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000974 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000975}
976
977TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000978 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000979 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000980 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000981 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000982 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000983 EXPECT_TRUE(
984 matches("class X {}; void y() { X *x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000985 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000986}
987
988TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000989 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000990 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000991 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000992 EXPECT_TRUE(
993 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000994 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000995}
996
997TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000998 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000999 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001000 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001001 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001002 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001003}
1004
Manuel Klimekc16c6522013-06-20 13:08:29 +00001005TEST(HasTypeLoc, MatchesDeclaratorDecls) {
1006 EXPECT_TRUE(matches("int x;",
1007 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
1008
1009 // Make sure we don't crash on implicit constructors.
1010 EXPECT_TRUE(notMatches("class X {}; X x;",
1011 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
1012}
1013
Manuel Klimek04616e42012-07-06 05:48:52 +00001014TEST(Matcher, Call) {
1015 // FIXME: Do we want to overload Call() to directly take
Daniel Jasper1dad1832012-07-10 20:20:19 +00001016 // Matcher<Decl>, too?
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001017 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001018
1019 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
1020 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
1021
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001022 StatementMatcher MethodOnY =
1023 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001024
1025 EXPECT_TRUE(
1026 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1027 MethodOnY));
1028 EXPECT_TRUE(
1029 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1030 MethodOnY));
1031 EXPECT_TRUE(
1032 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1033 MethodOnY));
1034 EXPECT_TRUE(
1035 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1036 MethodOnY));
1037 EXPECT_TRUE(
1038 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1039 MethodOnY));
1040
1041 StatementMatcher MethodOnYPointer =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001042 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001043
1044 EXPECT_TRUE(
1045 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1046 MethodOnYPointer));
1047 EXPECT_TRUE(
1048 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1049 MethodOnYPointer));
1050 EXPECT_TRUE(
1051 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1052 MethodOnYPointer));
1053 EXPECT_TRUE(
1054 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1055 MethodOnYPointer));
1056 EXPECT_TRUE(
1057 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1058 MethodOnYPointer));
1059}
1060
Daniel Jasper5901e472012-10-01 13:40:41 +00001061TEST(Matcher, Lambda) {
Richard Smith3d584b02014-02-06 21:49:08 +00001062 EXPECT_TRUE(matches("auto f = [] (int i) { return i; };",
Daniel Jasper5901e472012-10-01 13:40:41 +00001063 lambdaExpr()));
1064}
1065
1066TEST(Matcher, ForRange) {
Daniel Jasper6f595392012-10-01 15:05:34 +00001067 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
1068 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00001069 forRangeStmt()));
1070 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
1071 forRangeStmt()));
1072}
1073
Alexander Kornienko9e41b5c2014-06-29 22:18:53 +00001074TEST(Matcher, SubstNonTypeTemplateParm) {
1075 EXPECT_FALSE(matches("template<int N>\n"
1076 "struct A { static const int n = 0; };\n"
1077 "struct B : public A<42> {};",
1078 substNonTypeTemplateParmExpr()));
1079 EXPECT_TRUE(matches("template<int N>\n"
1080 "struct A { static const int n = N; };\n"
1081 "struct B : public A<42> {};",
1082 substNonTypeTemplateParmExpr()));
1083}
1084
Daniel Jasper5901e472012-10-01 13:40:41 +00001085TEST(Matcher, UserDefinedLiteral) {
1086 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
1087 " return i + 1;"
1088 "}"
1089 "char c = 'a'_inc;",
1090 userDefinedLiteral()));
1091}
1092
Daniel Jasper87c3d362012-09-20 14:12:57 +00001093TEST(Matcher, FlowControl) {
1094 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
1095 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
1096 continueStmt()));
1097 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
1098 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
1099 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
1100}
1101
Daniel Jasper1dad1832012-07-10 20:20:19 +00001102TEST(HasType, MatchesAsString) {
1103 EXPECT_TRUE(
1104 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001105 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001106 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001107 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001108 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001109 fieldDecl(hasType(asString("ns::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001110 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
David Blaikieabe1a392014-04-02 05:58:29 +00001111 fieldDecl(hasType(asString("struct (anonymous namespace)::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001112}
1113
Manuel Klimek04616e42012-07-06 05:48:52 +00001114TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001115 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001116 // Unary operator
1117 EXPECT_TRUE(matches("class Y { }; "
1118 "bool operator!(Y x) { return false; }; "
1119 "Y y; bool c = !y;", OpCall));
1120 // No match -- special operators like "new", "delete"
1121 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1122 // we need to figure out include paths in the test.
1123 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1124 // "class Y { }; "
1125 // "void *operator new(size_t size) { return 0; } "
1126 // "Y *y = new Y;", OpCall));
1127 EXPECT_TRUE(notMatches("class Y { }; "
1128 "void operator delete(void *p) { } "
1129 "void a() {Y *y = new Y; delete y;}", OpCall));
1130 // Binary operator
1131 EXPECT_TRUE(matches("class Y { }; "
1132 "bool operator&&(Y x, Y y) { return true; }; "
1133 "Y a; Y b; bool c = a && b;",
1134 OpCall));
1135 // No match -- normal operator, not an overloaded one.
1136 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1137 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1138}
1139
1140TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1141 StatementMatcher OpCallAndAnd =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001142 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek04616e42012-07-06 05:48:52 +00001143 EXPECT_TRUE(matches("class Y { }; "
1144 "bool operator&&(Y x, Y y) { return true; }; "
1145 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1146 StatementMatcher OpCallLessLess =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001147 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek04616e42012-07-06 05:48:52 +00001148 EXPECT_TRUE(notMatches("class Y { }; "
1149 "bool operator&&(Y x, Y y) { return true; }; "
1150 "Y a; Y b; bool c = a && b;",
1151 OpCallLessLess));
Benjamin Kramer09514492014-07-14 14:05:02 +00001152 StatementMatcher OpStarCall =
1153 operatorCallExpr(hasOverloadedOperatorName("*"));
1154 EXPECT_TRUE(matches("class Y; int operator*(Y &); void f(Y &y) { *y; }",
1155 OpStarCall));
Edwin Vane0a4836e2013-03-06 17:02:57 +00001156 DeclarationMatcher ClassWithOpStar =
1157 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1158 EXPECT_TRUE(matches("class Y { int operator*(); };",
1159 ClassWithOpStar));
1160 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1161 ClassWithOpStar)) ;
Benjamin Kramer09514492014-07-14 14:05:02 +00001162 DeclarationMatcher AnyOpStar = functionDecl(hasOverloadedOperatorName("*"));
1163 EXPECT_TRUE(matches("class Y; int operator*(Y &);", AnyOpStar));
1164 EXPECT_TRUE(matches("class Y { int operator*(); };", AnyOpStar));
Manuel Klimek04616e42012-07-06 05:48:52 +00001165}
1166
Daniel Jasper0f9f0192012-11-15 03:29:05 +00001167TEST(Matcher, NestedOverloadedOperatorCalls) {
1168 EXPECT_TRUE(matchAndVerifyResultTrue(
1169 "class Y { }; "
1170 "Y& operator&&(Y& x, Y& y) { return x; }; "
1171 "Y a; Y b; Y c; Y d = a && b && c;",
1172 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1173 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1174 EXPECT_TRUE(matches(
1175 "class Y { }; "
1176 "Y& operator&&(Y& x, Y& y) { return x; }; "
1177 "Y a; Y b; Y c; Y d = a && b && c;",
1178 operatorCallExpr(hasParent(operatorCallExpr()))));
1179 EXPECT_TRUE(matches(
1180 "class Y { }; "
1181 "Y& operator&&(Y& x, Y& y) { return x; }; "
1182 "Y a; Y b; Y c; Y d = a && b && c;",
1183 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1184}
1185
Manuel Klimek04616e42012-07-06 05:48:52 +00001186TEST(Matcher, ThisPointerType) {
Manuel Klimek86f8bbc2012-07-24 13:37:29 +00001187 StatementMatcher MethodOnY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001188 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001189
1190 EXPECT_TRUE(
1191 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1192 MethodOnY));
1193 EXPECT_TRUE(
1194 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1195 MethodOnY));
1196 EXPECT_TRUE(
1197 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1198 MethodOnY));
1199 EXPECT_TRUE(
1200 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1201 MethodOnY));
1202 EXPECT_TRUE(
1203 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1204 MethodOnY));
1205
1206 EXPECT_TRUE(matches(
1207 "class Y {"
1208 " public: virtual void x();"
1209 "};"
1210 "class X : public Y {"
1211 " public: virtual void x();"
1212 "};"
1213 "void z() { X *x; x->Y::x(); }", MethodOnY));
1214}
1215
1216TEST(Matcher, VariableUsage) {
1217 StatementMatcher Reference =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001218 declRefExpr(to(
1219 varDecl(hasInitializer(
1220 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001221
1222 EXPECT_TRUE(matches(
1223 "class Y {"
1224 " public:"
1225 " bool x() const;"
1226 "};"
1227 "void z(const Y &y) {"
1228 " bool b = y.x();"
1229 " if (b) {}"
1230 "}", Reference));
1231
1232 EXPECT_TRUE(notMatches(
1233 "class Y {"
1234 " public:"
1235 " bool x() const;"
1236 "};"
1237 "void z(const Y &y) {"
1238 " bool b = y.x();"
1239 "}", Reference));
1240}
1241
Samuel Benzaquenf56a2992014-06-05 18:22:14 +00001242TEST(Matcher, VarDecl_Storage) {
1243 auto M = varDecl(hasName("X"), hasLocalStorage());
1244 EXPECT_TRUE(matches("void f() { int X; }", M));
1245 EXPECT_TRUE(notMatches("int X;", M));
1246 EXPECT_TRUE(notMatches("void f() { static int X; }", M));
1247
1248 M = varDecl(hasName("X"), hasGlobalStorage());
1249 EXPECT_TRUE(notMatches("void f() { int X; }", M));
1250 EXPECT_TRUE(matches("int X;", M));
1251 EXPECT_TRUE(matches("void f() { static int X; }", M));
1252}
1253
Manuel Klimek61379422012-12-04 14:42:08 +00001254TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001255 EXPECT_TRUE(matches(
1256 "void f(int i) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001257 varDecl(hasName("i"))));
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001258}
1259
Manuel Klimek04616e42012-07-06 05:48:52 +00001260TEST(Matcher, CalledVariable) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001261 StatementMatcher CallOnVariableY =
1262 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001263
1264 EXPECT_TRUE(matches(
1265 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1266 EXPECT_TRUE(matches(
1267 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1268 EXPECT_TRUE(matches(
1269 "class Y { public: void x(); };"
1270 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1271 EXPECT_TRUE(matches(
1272 "class Y { public: void x(); };"
1273 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1274 EXPECT_TRUE(notMatches(
1275 "class Y { public: void x(); };"
1276 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1277 CallOnVariableY));
1278}
1279
Daniel Jasper1dad1832012-07-10 20:20:19 +00001280TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1281 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1282 unaryExprOrTypeTraitExpr()));
1283 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1284 alignOfExpr(anything())));
1285 // FIXME: Uncomment once alignof is enabled.
1286 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1287 // unaryExprOrTypeTraitExpr()));
1288 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1289 // sizeOfExpr()));
1290}
1291
1292TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1293 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1294 hasArgumentOfType(asString("int")))));
1295 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1296 hasArgumentOfType(asString("float")))));
1297 EXPECT_TRUE(matches(
1298 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001299 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001300 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001301 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001302}
1303
Manuel Klimek04616e42012-07-06 05:48:52 +00001304TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001305 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001306}
1307
1308TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001309 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001310}
1311
1312TEST(MemberExpression, MatchesVariable) {
1313 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001314 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001315 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001316 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001317 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001318 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001319}
1320
1321TEST(MemberExpression, MatchesStaticVariable) {
1322 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001323 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001324 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001325 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001326 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001327 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001328}
1329
Daniel Jasper4e566c42012-07-12 08:50:38 +00001330TEST(IsInteger, MatchesIntegers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001331 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1332 EXPECT_TRUE(matches(
1333 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1334 callExpr(hasArgument(0, declRefExpr(
1335 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001336}
1337
1338TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001339 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001340 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001341 callExpr(hasArgument(0, declRefExpr(
1342 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001343}
1344
Manuel Klimek04616e42012-07-06 05:48:52 +00001345TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1346 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001347 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001348 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001349 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001350 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001351 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001352}
1353
1354TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1355 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001356 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001357 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001358 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001359 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001360 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001361}
1362
1363TEST(IsArrow, MatchesMemberCallsViaArrow) {
1364 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001365 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001366 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001367 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001368 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001369 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001370}
1371
1372TEST(Callee, MatchesDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001373 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001374
1375 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1376 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1377}
1378
1379TEST(Callee, MatchesMemberExpressions) {
1380 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001381 callExpr(callee(memberExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001382 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001383 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001384}
1385
1386TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001387 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001388
1389 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1390 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1391
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +00001392 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
1393 llvm::Triple::Win32) {
1394 // FIXME: Make this work for MSVC.
1395 // Dependent contexts, but a non-dependent call.
1396 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1397 CallFunctionF));
1398 EXPECT_TRUE(
1399 matches("void f(); template <int N> struct S { void g() { f(); } };",
1400 CallFunctionF));
1401 }
Manuel Klimek04616e42012-07-06 05:48:52 +00001402
1403 // Depedent calls don't match.
1404 EXPECT_TRUE(
1405 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1406 CallFunctionF));
1407 EXPECT_TRUE(
1408 notMatches("void f(int);"
1409 "template <typename T> struct S { void g(T t) { f(t); } };",
1410 CallFunctionF));
1411}
1412
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001413TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1414 EXPECT_TRUE(
1415 matches("template <typename T> void f(T t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001416 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001417}
1418
1419TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1420 EXPECT_TRUE(
1421 notMatches("void f(double d); void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001422 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001423}
1424
1425TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1426 EXPECT_TRUE(
1427 notMatches("void g(); template <typename T> void f(T t) {}"
1428 "template <> void f(int t) { g(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001429 functionTemplateDecl(hasName("f"),
1430 hasDescendant(declRefExpr(to(
1431 functionDecl(hasName("g"))))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001432}
1433
Manuel Klimek04616e42012-07-06 05:48:52 +00001434TEST(Matcher, Argument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001435 StatementMatcher CallArgumentY = callExpr(
1436 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001437
1438 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1439 EXPECT_TRUE(
1440 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1441 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1442
Daniel Jasper848cbe12012-09-18 13:09:13 +00001443 StatementMatcher WrongIndex = callExpr(
1444 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001445 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1446}
1447
1448TEST(Matcher, AnyArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001449 StatementMatcher CallArgumentY = callExpr(
1450 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001451 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1452 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1453 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1454}
1455
1456TEST(Matcher, ArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001457 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001458
1459 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1460 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1461 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1462}
1463
Daniel Jasper9f501292012-12-04 11:54:27 +00001464TEST(Matcher, ParameterCount) {
1465 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1466 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1467 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1468 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1469 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1470}
1471
Manuel Klimek04616e42012-07-06 05:48:52 +00001472TEST(Matcher, References) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001473 DeclarationMatcher ReferenceClassX = varDecl(
1474 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001475 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1476 ReferenceClassX));
1477 EXPECT_TRUE(
1478 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
Michael Hanc90d12d2013-09-11 15:53:29 +00001479 // The match here is on the implicit copy constructor code for
1480 // class X, not on code 'X x = y'.
Manuel Klimek04616e42012-07-06 05:48:52 +00001481 EXPECT_TRUE(
Michael Hanc90d12d2013-09-11 15:53:29 +00001482 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1483 EXPECT_TRUE(
1484 notMatches("class X {}; extern X x;", ReferenceClassX));
Manuel Klimek04616e42012-07-06 05:48:52 +00001485 EXPECT_TRUE(
1486 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1487}
1488
Edwin Vane0a4836e2013-03-06 17:02:57 +00001489TEST(QualType, hasCanonicalType) {
1490 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1491 "int a;"
1492 "int_ref b = a;",
1493 varDecl(hasType(qualType(referenceType())))));
1494 EXPECT_TRUE(
1495 matches("typedef int &int_ref;"
1496 "int a;"
1497 "int_ref b = a;",
1498 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1499}
1500
Edwin Vane119d3df2013-04-02 18:15:55 +00001501TEST(QualType, hasLocalQualifiers) {
1502 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1503 varDecl(hasType(hasLocalQualifiers()))));
1504 EXPECT_TRUE(matches("int *const j = nullptr;",
1505 varDecl(hasType(hasLocalQualifiers()))));
1506 EXPECT_TRUE(matches("int *volatile k;",
1507 varDecl(hasType(hasLocalQualifiers()))));
1508 EXPECT_TRUE(notMatches("int m;",
1509 varDecl(hasType(hasLocalQualifiers()))));
1510}
1511
Manuel Klimek04616e42012-07-06 05:48:52 +00001512TEST(HasParameter, CallsInnerMatcher) {
1513 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001514 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001515 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001516 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001517}
1518
1519TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1520 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001521 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001522}
1523
1524TEST(HasType, MatchesParameterVariableTypesStrictly) {
1525 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001526 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001527 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001528 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001529 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001530 methodDecl(hasParameter(0,
1531 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001532 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001533 methodDecl(hasParameter(0,
1534 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001535}
1536
1537TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1538 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001539 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001540 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001541 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001542}
1543
Daniel Jasper1dad1832012-07-10 20:20:19 +00001544TEST(Returns, MatchesReturnTypes) {
1545 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001546 functionDecl(returns(asString("int")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001547 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001548 functionDecl(returns(asString("float")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001549 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001550 functionDecl(returns(hasDeclaration(
1551 recordDecl(hasName("Y")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001552}
1553
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001554TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001555 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1556 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1557 functionDecl(isExternC())));
1558 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001559}
1560
Samuel Benzaquen8e7f9962014-08-15 14:20:59 +00001561TEST(IsDeleted, MatchesDeletedFunctionDeclarations) {
1562 EXPECT_TRUE(
1563 notMatches("void Func();", functionDecl(hasName("Func"), isDeleted())));
1564 EXPECT_TRUE(matches("void Func() = delete;",
1565 functionDecl(hasName("Func"), isDeleted())));
1566}
1567
Manuel Klimek04616e42012-07-06 05:48:52 +00001568TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1569 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001570 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001571}
1572
1573TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1574 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001575 methodDecl(hasAnyParameter(hasType(pointsTo(
1576 recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001577}
1578
Alp Toker8db6e7a2014-01-05 06:38:57 +00001579TEST(HasName, MatchesParameterVariableDeclarations) {
Manuel Klimek04616e42012-07-06 05:48:52 +00001580 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001581 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001582 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001583 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001584}
1585
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001586TEST(Matcher, MatchesClassTemplateSpecialization) {
1587 EXPECT_TRUE(matches("template<typename T> struct A {};"
1588 "template<> struct A<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001589 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001590 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001591 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001592 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001593 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001594}
1595
Manuel Klimekc16c6522013-06-20 13:08:29 +00001596TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1597 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1598 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1599}
1600
1601TEST(ParmVarDecl, MatchesParmVars) {
1602 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1603 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1604}
1605
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001606TEST(Matcher, MatchesTypeTemplateArgument) {
1607 EXPECT_TRUE(matches(
1608 "template<typename T> struct B {};"
1609 "B<int> b;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001610 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001611 asString("int"))))));
1612}
1613
1614TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1615 EXPECT_TRUE(matches(
1616 "struct B { int next; };"
1617 "template<int(B::*next_ptr)> struct A {};"
1618 "A<&B::next> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001619 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1620 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper0c303372012-09-29 15:55:18 +00001621
1622 EXPECT_TRUE(notMatches(
1623 "template <typename T> struct A {};"
1624 "A<int> a;",
1625 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1626 refersToDeclaration(decl())))));
Peter Collingbourne564597f2014-02-20 19:18:03 +00001627
1628 EXPECT_TRUE(matches(
1629 "struct B { int next; };"
1630 "template<int(B::*next_ptr)> struct A {};"
1631 "A<&B::next> a;",
1632 templateSpecializationType(hasAnyTemplateArgument(isExpr(
1633 hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))));
1634
1635 EXPECT_TRUE(notMatches(
1636 "template <typename T> struct A {};"
1637 "A<int> a;",
1638 templateSpecializationType(hasAnyTemplateArgument(
1639 refersToDeclaration(decl())))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001640}
1641
1642TEST(Matcher, MatchesSpecificArgument) {
1643 EXPECT_TRUE(matches(
1644 "template<typename T, typename U> class A {};"
1645 "A<bool, int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001646 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001647 1, refersToType(asString("int"))))));
1648 EXPECT_TRUE(notMatches(
1649 "template<typename T, typename U> class A {};"
1650 "A<int, bool> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001651 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001652 1, refersToType(asString("int"))))));
Peter Collingbourne564597f2014-02-20 19:18:03 +00001653
1654 EXPECT_TRUE(matches(
1655 "template<typename T, typename U> class A {};"
1656 "A<bool, int> a;",
1657 templateSpecializationType(hasTemplateArgument(
1658 1, refersToType(asString("int"))))));
1659 EXPECT_TRUE(notMatches(
1660 "template<typename T, typename U> class A {};"
1661 "A<int, bool> a;",
1662 templateSpecializationType(hasTemplateArgument(
1663 1, refersToType(asString("int"))))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001664}
1665
Manuel Klimek7735e402014-10-09 13:06:22 +00001666TEST(TemplateArgument, Matches) {
1667 EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;",
1668 classTemplateSpecializationDecl(
1669 hasAnyTemplateArgument(templateArgument()))));
1670 EXPECT_TRUE(matches(
1671 "template<typename T> struct C {}; C<int> c;",
1672 templateSpecializationType(hasAnyTemplateArgument(templateArgument()))));
1673}
1674
1675TEST(TemplateArgumentCountIs, Matches) {
1676 EXPECT_TRUE(
1677 matches("template<typename T> struct C {}; C<int> c;",
1678 classTemplateSpecializationDecl(templateArgumentCountIs(1))));
1679 EXPECT_TRUE(
1680 notMatches("template<typename T> struct C {}; C<int> c;",
1681 classTemplateSpecializationDecl(templateArgumentCountIs(2))));
1682
1683 EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;",
1684 templateSpecializationType(templateArgumentCountIs(1))));
1685 EXPECT_TRUE(
1686 notMatches("template<typename T> struct C {}; C<int> c;",
1687 templateSpecializationType(templateArgumentCountIs(2))));
1688}
1689
1690TEST(IsIntegral, Matches) {
1691 EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;",
1692 classTemplateSpecializationDecl(
1693 hasAnyTemplateArgument(isIntegral()))));
1694 EXPECT_TRUE(notMatches("template<typename T> struct C {}; C<int> c;",
1695 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1696 templateArgument(isIntegral())))));
1697}
1698
1699TEST(RefersToIntegralType, Matches) {
1700 EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;",
1701 classTemplateSpecializationDecl(
1702 hasAnyTemplateArgument(refersToIntegralType(
1703 asString("int"))))));
1704 EXPECT_TRUE(notMatches("template<unsigned T> struct C {}; C<42> c;",
1705 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1706 refersToIntegralType(asString("int"))))));
1707}
1708
1709TEST(EqualsIntegralValue, Matches) {
1710 EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;",
1711 classTemplateSpecializationDecl(
1712 hasAnyTemplateArgument(equalsIntegralValue("42")))));
1713 EXPECT_TRUE(matches("template<int T> struct C {}; C<-42> c;",
1714 classTemplateSpecializationDecl(
1715 hasAnyTemplateArgument(equalsIntegralValue("-42")))));
1716 EXPECT_TRUE(matches("template<int T> struct C {}; C<-0042> c;",
1717 classTemplateSpecializationDecl(
1718 hasAnyTemplateArgument(equalsIntegralValue("-34")))));
1719 EXPECT_TRUE(notMatches("template<int T> struct C {}; C<42> c;",
1720 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1721 equalsIntegralValue("0042")))));
1722}
1723
Daniel Jasper639522c2013-02-25 12:02:08 +00001724TEST(Matcher, MatchesAccessSpecDecls) {
1725 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1726 EXPECT_TRUE(
1727 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1728 EXPECT_TRUE(
1729 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1730 EXPECT_TRUE(
1731 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1732
1733 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1734}
1735
Edwin Vane37ee1d72013-04-09 20:46:36 +00001736TEST(Matcher, MatchesVirtualMethod) {
1737 EXPECT_TRUE(matches("class X { virtual int f(); };",
1738 methodDecl(isVirtual(), hasName("::X::f"))));
1739 EXPECT_TRUE(notMatches("class X { int f(); };",
1740 methodDecl(isVirtual())));
1741}
1742
Dmitri Gribenko51c1b552014-02-24 09:27:46 +00001743TEST(Matcher, MatchesPureMethod) {
1744 EXPECT_TRUE(matches("class X { virtual int f() = 0; };",
1745 methodDecl(isPure(), hasName("::X::f"))));
1746 EXPECT_TRUE(notMatches("class X { int f(); };",
1747 methodDecl(isPure())));
1748}
1749
Edwin Vanefc4f7dc2013-05-09 17:00:17 +00001750TEST(Matcher, MatchesConstMethod) {
1751 EXPECT_TRUE(matches("struct A { void foo() const; };",
1752 methodDecl(isConst())));
1753 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1754 methodDecl(isConst())));
1755}
1756
Edwin Vane37ee1d72013-04-09 20:46:36 +00001757TEST(Matcher, MatchesOverridingMethod) {
1758 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1759 "class Y : public X { int f(); };",
1760 methodDecl(isOverride(), hasName("::Y::f"))));
1761 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1762 "class Y : public X { int f(); };",
1763 methodDecl(isOverride(), hasName("::X::f"))));
1764 EXPECT_TRUE(notMatches("class X { int f(); }; "
1765 "class Y : public X { int f(); };",
1766 methodDecl(isOverride())));
1767 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1768 methodDecl(isOverride())));
1769}
1770
Manuel Klimek04616e42012-07-06 05:48:52 +00001771TEST(Matcher, ConstructorCall) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001772 StatementMatcher Constructor = constructExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001773
1774 EXPECT_TRUE(
1775 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1776 EXPECT_TRUE(
1777 matches("class X { public: X(); }; void x() { X x = X(); }",
1778 Constructor));
1779 EXPECT_TRUE(
1780 matches("class X { public: X(int); }; void x() { X x = 0; }",
1781 Constructor));
1782 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1783}
1784
1785TEST(Matcher, ConstructorArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001786 StatementMatcher Constructor = constructExpr(
1787 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001788
1789 EXPECT_TRUE(
1790 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1791 Constructor));
1792 EXPECT_TRUE(
1793 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1794 Constructor));
1795 EXPECT_TRUE(
1796 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1797 Constructor));
1798 EXPECT_TRUE(
1799 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1800 Constructor));
1801
Daniel Jasper848cbe12012-09-18 13:09:13 +00001802 StatementMatcher WrongIndex = constructExpr(
1803 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001804 EXPECT_TRUE(
1805 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1806 WrongIndex));
1807}
1808
1809TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001810 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001811
1812 EXPECT_TRUE(
1813 matches("class X { public: X(int); }; void x() { X x(0); }",
1814 Constructor1Arg));
1815 EXPECT_TRUE(
1816 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1817 Constructor1Arg));
1818 EXPECT_TRUE(
1819 matches("class X { public: X(int); }; void x() { X x = 0; }",
1820 Constructor1Arg));
1821 EXPECT_TRUE(
1822 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1823 Constructor1Arg));
1824}
1825
Peter Collingbourne1fec3df2014-02-06 21:52:24 +00001826TEST(Matcher, ConstructorListInitialization) {
1827 StatementMatcher ConstructorListInit = constructExpr(isListInitialization());
1828
1829 EXPECT_TRUE(
1830 matches("class X { public: X(int); }; void x() { X x{0}; }",
1831 ConstructorListInit));
1832 EXPECT_FALSE(
1833 matches("class X { public: X(int); }; void x() { X x(0); }",
1834 ConstructorListInit));
1835}
1836
Manuel Klimek7fca93b2012-10-23 10:40:50 +00001837TEST(Matcher,ThisExpr) {
1838 EXPECT_TRUE(
1839 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1840 EXPECT_TRUE(
1841 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1842}
1843
Manuel Klimek04616e42012-07-06 05:48:52 +00001844TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001845 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001846
1847 std::string ClassString = "class string { public: string(); ~string(); }; ";
1848
1849 EXPECT_TRUE(
1850 matches(ClassString +
1851 "string GetStringByValue();"
1852 "void FunctionTakesString(string s);"
1853 "void run() { FunctionTakesString(GetStringByValue()); }",
1854 TempExpression));
1855
1856 EXPECT_TRUE(
1857 notMatches(ClassString +
1858 "string* GetStringPointer(); "
1859 "void FunctionTakesStringPtr(string* s);"
1860 "void run() {"
1861 " string* s = GetStringPointer();"
1862 " FunctionTakesStringPtr(GetStringPointer());"
1863 " FunctionTakesStringPtr(s);"
1864 "}",
1865 TempExpression));
1866
1867 EXPECT_TRUE(
1868 notMatches("class no_dtor {};"
1869 "no_dtor GetObjByValue();"
1870 "void ConsumeObj(no_dtor param);"
1871 "void run() { ConsumeObj(GetObjByValue()); }",
1872 TempExpression));
1873}
1874
Sam Panzer68a35af2012-08-24 22:04:44 +00001875TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1876 std::string ClassString =
1877 "class string { public: string(); int length(); }; ";
1878
1879 EXPECT_TRUE(
1880 matches(ClassString +
1881 "string GetStringByValue();"
1882 "void FunctionTakesString(string s);"
1883 "void run() { FunctionTakesString(GetStringByValue()); }",
1884 materializeTemporaryExpr()));
1885
1886 EXPECT_TRUE(
1887 notMatches(ClassString +
1888 "string* GetStringPointer(); "
1889 "void FunctionTakesStringPtr(string* s);"
1890 "void run() {"
1891 " string* s = GetStringPointer();"
1892 " FunctionTakesStringPtr(GetStringPointer());"
1893 " FunctionTakesStringPtr(s);"
1894 "}",
1895 materializeTemporaryExpr()));
1896
1897 EXPECT_TRUE(
1898 notMatches(ClassString +
1899 "string GetStringByValue();"
1900 "void run() { int k = GetStringByValue().length(); }",
1901 materializeTemporaryExpr()));
1902
1903 EXPECT_TRUE(
1904 notMatches(ClassString +
1905 "string GetStringByValue();"
1906 "void run() { GetStringByValue(); }",
1907 materializeTemporaryExpr()));
1908}
1909
Manuel Klimek04616e42012-07-06 05:48:52 +00001910TEST(ConstructorDeclaration, SimpleCase) {
1911 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001912 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001913 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001914 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001915}
1916
1917TEST(ConstructorDeclaration, IsImplicit) {
1918 // This one doesn't match because the constructor is not added by the
1919 // compiler (it is not needed).
1920 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001921 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001922 // The compiler added the implicit default constructor.
1923 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001924 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001925 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001926 constructorDecl(unless(isImplicit()))));
Joey Gouly0d9a2b22014-05-16 19:31:08 +00001927 // The compiler added an implicit assignment operator.
1928 EXPECT_TRUE(matches("struct A { int x; } a = {0}, b = a; void f() { a = b; }",
1929 methodDecl(isImplicit(), hasName("operator="))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001930}
1931
Daniel Jasper1dad1832012-07-10 20:20:19 +00001932TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1933 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001934 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001935}
1936
1937TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001938 EXPECT_TRUE(notMatches("class Foo {};",
1939 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001940}
1941
Manuel Klimek04616e42012-07-06 05:48:52 +00001942TEST(HasAnyConstructorInitializer, SimpleCase) {
1943 EXPECT_TRUE(notMatches(
1944 "class Foo { Foo() { } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001945 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001946 EXPECT_TRUE(matches(
1947 "class Foo {"
1948 " Foo() : foo_() { }"
1949 " int foo_;"
1950 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001951 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001952}
1953
1954TEST(HasAnyConstructorInitializer, ForField) {
1955 static const char Code[] =
1956 "class Baz { };"
1957 "class Foo {"
1958 " Foo() : foo_() { }"
1959 " Baz foo_;"
1960 " Baz bar_;"
1961 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001962 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1963 forField(hasType(recordDecl(hasName("Baz"))))))));
1964 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001965 forField(hasName("foo_"))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001966 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1967 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001968}
1969
1970TEST(HasAnyConstructorInitializer, WithInitializer) {
1971 static const char Code[] =
1972 "class Foo {"
1973 " Foo() : foo_(0) { }"
1974 " int foo_;"
1975 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001976 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001977 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001978 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001979 withInitializer(integerLiteral(equals(1)))))));
1980}
1981
1982TEST(HasAnyConstructorInitializer, IsWritten) {
1983 static const char Code[] =
1984 "struct Bar { Bar(){} };"
1985 "class Foo {"
1986 " Foo() : foo_() { }"
1987 " Bar foo_;"
1988 " Bar bar_;"
1989 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001990 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001991 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001992 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001993 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001994 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001995 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1996}
1997
1998TEST(Matcher, NewExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001999 StatementMatcher New = newExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00002000
2001 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
2002 EXPECT_TRUE(
2003 matches("class X { public: X(); }; void x() { new X(); }", New));
2004 EXPECT_TRUE(
2005 matches("class X { public: X(int); }; void x() { new X(0); }", New));
2006 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
2007}
2008
2009TEST(Matcher, NewExpressionArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002010 StatementMatcher New = constructExpr(
2011 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002012
2013 EXPECT_TRUE(
2014 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
2015 New));
2016 EXPECT_TRUE(
2017 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
2018 New));
2019 EXPECT_TRUE(
2020 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
2021 New));
2022
Daniel Jasper848cbe12012-09-18 13:09:13 +00002023 StatementMatcher WrongIndex = constructExpr(
2024 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002025 EXPECT_TRUE(
2026 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
2027 WrongIndex));
2028}
2029
2030TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002031 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00002032
2033 EXPECT_TRUE(
2034 matches("class X { public: X(int); }; void x() { new X(0); }", New));
2035 EXPECT_TRUE(
2036 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
2037 New));
2038}
2039
Daniel Jasper1dad1832012-07-10 20:20:19 +00002040TEST(Matcher, DeleteExpression) {
2041 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002042 deleteExpr()));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002043}
2044
Manuel Klimek04616e42012-07-06 05:48:52 +00002045TEST(Matcher, DefaultArgument) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002046 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00002047
2048 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
2049 EXPECT_TRUE(
2050 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
2051 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
2052}
2053
2054TEST(Matcher, StringLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002055 StatementMatcher Literal = stringLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00002056 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
2057 // wide string
2058 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
2059 // with escaped characters
2060 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
2061 // no matching -- though the data type is the same, there is no string literal
2062 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
2063}
2064
2065TEST(Matcher, CharacterLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002066 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00002067 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
2068 // wide character
2069 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
2070 // wide character, Hex encoded, NOT MATCHED!
2071 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
2072 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
2073}
2074
2075TEST(Matcher, IntegerLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002076 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00002077 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
2078 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
2079 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
2080 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
2081
2082 // Non-matching cases (character literals, float and double)
2083 EXPECT_TRUE(notMatches("int i = L'a';",
2084 HasIntLiteral)); // this is actually a character
2085 // literal cast to int
2086 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
2087 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
2088 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
2089}
2090
Daniel Jasper91f1c8c2013-07-26 18:52:58 +00002091TEST(Matcher, FloatLiterals) {
2092 StatementMatcher HasFloatLiteral = floatLiteral();
2093 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
2094 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
2095 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
2096 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
2097 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
2098
2099 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
2100}
2101
Daniel Jasper5901e472012-10-01 13:40:41 +00002102TEST(Matcher, NullPtrLiteral) {
2103 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
2104}
2105
Daniel Jasper87c3d362012-09-20 14:12:57 +00002106TEST(Matcher, AsmStatement) {
2107 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
2108}
2109
Manuel Klimek04616e42012-07-06 05:48:52 +00002110TEST(Matcher, Conditions) {
2111 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
2112
2113 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
2114 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
2115 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
2116 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
2117 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
2118}
2119
Manuel Klimek909b5c942014-05-27 10:04:12 +00002120TEST(IfStmt, ChildTraversalMatchers) {
2121 EXPECT_TRUE(matches("void f() { if (false) true; else false; }",
2122 ifStmt(hasThen(boolLiteral(equals(true))))));
2123 EXPECT_TRUE(notMatches("void f() { if (false) false; else true; }",
2124 ifStmt(hasThen(boolLiteral(equals(true))))));
2125 EXPECT_TRUE(matches("void f() { if (false) false; else true; }",
2126 ifStmt(hasElse(boolLiteral(equals(true))))));
2127 EXPECT_TRUE(notMatches("void f() { if (false) true; else false; }",
2128 ifStmt(hasElse(boolLiteral(equals(true))))));
2129}
2130
Manuel Klimek04616e42012-07-06 05:48:52 +00002131TEST(MatchBinaryOperator, HasOperatorName) {
2132 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
2133
2134 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
2135 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
2136}
2137
2138TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
2139 StatementMatcher OperatorTrueFalse =
2140 binaryOperator(hasLHS(boolLiteral(equals(true))),
2141 hasRHS(boolLiteral(equals(false))));
2142
2143 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
2144 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
2145 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
2146}
2147
2148TEST(MatchBinaryOperator, HasEitherOperand) {
2149 StatementMatcher HasOperand =
2150 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
2151
2152 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
2153 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
2154 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
2155}
2156
2157TEST(Matcher, BinaryOperatorTypes) {
2158 // Integration test that verifies the AST provides all binary operators in
2159 // a way we expect.
2160 // FIXME: Operator ','
2161 EXPECT_TRUE(
2162 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
2163 EXPECT_TRUE(
2164 matches("bool b; bool c = (b = true);",
2165 binaryOperator(hasOperatorName("="))));
2166 EXPECT_TRUE(
2167 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
2168 EXPECT_TRUE(
2169 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
2170 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
2171 EXPECT_TRUE(
2172 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
2173 EXPECT_TRUE(
2174 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
2175 EXPECT_TRUE(
2176 matches("int i = 1; int j = (i <<= 2);",
2177 binaryOperator(hasOperatorName("<<="))));
2178 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
2179 EXPECT_TRUE(
2180 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
2181 EXPECT_TRUE(
2182 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
2183 EXPECT_TRUE(
2184 matches("int i = 1; int j = (i >>= 2);",
2185 binaryOperator(hasOperatorName(">>="))));
2186 EXPECT_TRUE(
2187 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
2188 EXPECT_TRUE(
2189 matches("int i = 42; int j = (i ^= 42);",
2190 binaryOperator(hasOperatorName("^="))));
2191 EXPECT_TRUE(
2192 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
2193 EXPECT_TRUE(
2194 matches("int i = 42; int j = (i %= 42);",
2195 binaryOperator(hasOperatorName("%="))));
2196 EXPECT_TRUE(
2197 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
2198 EXPECT_TRUE(
2199 matches("bool b = true && false;",
2200 binaryOperator(hasOperatorName("&&"))));
2201 EXPECT_TRUE(
2202 matches("bool b = true; bool c = (b &= false);",
2203 binaryOperator(hasOperatorName("&="))));
2204 EXPECT_TRUE(
2205 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
2206 EXPECT_TRUE(
2207 matches("bool b = true || false;",
2208 binaryOperator(hasOperatorName("||"))));
2209 EXPECT_TRUE(
2210 matches("bool b = true; bool c = (b |= false);",
2211 binaryOperator(hasOperatorName("|="))));
2212 EXPECT_TRUE(
2213 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2214 EXPECT_TRUE(
2215 matches("int i = 42; int j = (i *= 23);",
2216 binaryOperator(hasOperatorName("*="))));
2217 EXPECT_TRUE(
2218 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
2219 EXPECT_TRUE(
2220 matches("int i = 42; int j = (i /= 23);",
2221 binaryOperator(hasOperatorName("/="))));
2222 EXPECT_TRUE(
2223 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
2224 EXPECT_TRUE(
2225 matches("int i = 42; int j = (i += 23);",
2226 binaryOperator(hasOperatorName("+="))));
2227 EXPECT_TRUE(
2228 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
2229 EXPECT_TRUE(
2230 matches("int i = 42; int j = (i -= 23);",
2231 binaryOperator(hasOperatorName("-="))));
2232 EXPECT_TRUE(
2233 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2234 binaryOperator(hasOperatorName("->*"))));
2235 EXPECT_TRUE(
2236 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2237 binaryOperator(hasOperatorName(".*"))));
2238
2239 // Member expressions as operators are not supported in matches.
2240 EXPECT_TRUE(
2241 notMatches("struct A { void x(A *a) { a->x(this); } };",
2242 binaryOperator(hasOperatorName("->"))));
2243
2244 // Initializer assignments are not represented as operator equals.
2245 EXPECT_TRUE(
2246 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2247
2248 // Array indexing is not represented as operator.
2249 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2250
2251 // Overloaded operators do not match at all.
2252 EXPECT_TRUE(notMatches(
2253 "struct A { bool operator&&(const A &a) const { return false; } };"
2254 "void x() { A a, b; a && b; }",
2255 binaryOperator()));
2256}
2257
2258TEST(MatchUnaryOperator, HasOperatorName) {
2259 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2260
2261 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2262 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2263}
2264
2265TEST(MatchUnaryOperator, HasUnaryOperand) {
2266 StatementMatcher OperatorOnFalse =
2267 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2268
2269 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2270 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2271}
2272
2273TEST(Matcher, UnaryOperatorTypes) {
2274 // Integration test that verifies the AST provides all unary operators in
2275 // a way we expect.
2276 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2277 EXPECT_TRUE(
2278 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2279 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2280 EXPECT_TRUE(
2281 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2282 EXPECT_TRUE(
2283 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2284 EXPECT_TRUE(
2285 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2286 EXPECT_TRUE(
2287 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2288 EXPECT_TRUE(
2289 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2290 EXPECT_TRUE(
2291 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2292 EXPECT_TRUE(
2293 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2294
2295 // We don't match conversion operators.
2296 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2297
2298 // Function calls are not represented as operator.
2299 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2300
2301 // Overloaded operators do not match at all.
2302 // FIXME: We probably want to add that.
2303 EXPECT_TRUE(notMatches(
2304 "struct A { bool operator!() const { return false; } };"
2305 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2306}
2307
2308TEST(Matcher, ConditionalOperator) {
2309 StatementMatcher Conditional = conditionalOperator(
2310 hasCondition(boolLiteral(equals(true))),
2311 hasTrueExpression(boolLiteral(equals(false))));
2312
2313 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2314 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2315 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2316
2317 StatementMatcher ConditionalFalse = conditionalOperator(
2318 hasFalseExpression(boolLiteral(equals(false))));
2319
2320 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2321 EXPECT_TRUE(
2322 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2323}
2324
Daniel Jasper1dad1832012-07-10 20:20:19 +00002325TEST(ArraySubscriptMatchers, ArraySubscripts) {
2326 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2327 arraySubscriptExpr()));
2328 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2329 arraySubscriptExpr()));
2330}
2331
2332TEST(ArraySubscriptMatchers, ArrayIndex) {
2333 EXPECT_TRUE(matches(
2334 "int i[2]; void f() { i[1] = 1; }",
2335 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2336 EXPECT_TRUE(matches(
2337 "int i[2]; void f() { 1[i] = 1; }",
2338 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2339 EXPECT_TRUE(notMatches(
2340 "int i[2]; void f() { i[1] = 1; }",
2341 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2342}
2343
2344TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2345 EXPECT_TRUE(matches(
2346 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002347 arraySubscriptExpr(hasBase(implicitCastExpr(
2348 hasSourceExpression(declRefExpr()))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002349}
2350
Manuel Klimek04616e42012-07-06 05:48:52 +00002351TEST(Matcher, HasNameSupportsNamespaces) {
2352 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002353 recordDecl(hasName("a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002354 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002355 recordDecl(hasName("::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002356 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002357 recordDecl(hasName("b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002358 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002359 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002360 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002361 recordDecl(hasName("c::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002362 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002363 recordDecl(hasName("a::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002364 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002365 recordDecl(hasName("a::b::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002366 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002367 recordDecl(hasName("::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002368 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002369 recordDecl(hasName("::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002370 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002371 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002372 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002373 recordDecl(hasName("a+b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002374 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002375 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002376}
2377
2378TEST(Matcher, HasNameSupportsOuterClasses) {
2379 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002380 matches("class A { class B { class C; }; };",
2381 recordDecl(hasName("A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002382 EXPECT_TRUE(
2383 matches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002384 recordDecl(hasName("::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002385 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002386 matches("class A { class B { class C; }; };",
2387 recordDecl(hasName("B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002388 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002389 matches("class A { class B { class C; }; };",
2390 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002391 EXPECT_TRUE(
2392 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002393 recordDecl(hasName("c::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002394 EXPECT_TRUE(
2395 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002396 recordDecl(hasName("A::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002397 EXPECT_TRUE(
2398 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002399 recordDecl(hasName("A::B::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002400 EXPECT_TRUE(
2401 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002402 recordDecl(hasName("::C"))));
2403 EXPECT_TRUE(
2404 notMatches("class A { class B { class C; }; };",
2405 recordDecl(hasName("::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002406 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002407 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002408 EXPECT_TRUE(
2409 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002410 recordDecl(hasName("A+B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002411}
2412
2413TEST(Matcher, IsDefinition) {
2414 DeclarationMatcher DefinitionOfClassA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002415 recordDecl(hasName("A"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002416 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2417 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2418
2419 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002420 varDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002421 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2422 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2423
2424 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002425 methodDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002426 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2427 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2428}
2429
2430TEST(Matcher, OfClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002431 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +00002432 ofClass(hasName("X")))));
2433
2434 EXPECT_TRUE(
2435 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2436 EXPECT_TRUE(
2437 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2438 Constructor));
2439 EXPECT_TRUE(
2440 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2441 Constructor));
2442}
2443
2444TEST(Matcher, VisitsTemplateInstantiations) {
2445 EXPECT_TRUE(matches(
2446 "class A { public: void x(); };"
2447 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002448 "void f() { B<A> b; b.y(); }",
2449 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002450
2451 EXPECT_TRUE(matches(
2452 "class A { public: void x(); };"
2453 "class C {"
2454 " public:"
2455 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2456 "};"
2457 "void f() {"
2458 " C::B<A> b; b.y();"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002459 "}",
2460 recordDecl(hasName("C"),
2461 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002462}
2463
Daniel Jasper1dad1832012-07-10 20:20:19 +00002464TEST(Matcher, HandlesNullQualTypes) {
2465 // FIXME: Add a Type matcher so we can replace uses of this
2466 // variable with Type(True())
2467 const TypeMatcher AnyType = anything();
2468
2469 // We don't really care whether this matcher succeeds; we're testing that
2470 // it completes without crashing.
2471 EXPECT_TRUE(matches(
2472 "struct A { };"
2473 "template <typename T>"
2474 "void f(T t) {"
2475 " T local_t(t /* this becomes a null QualType in the AST */);"
2476 "}"
2477 "void g() {"
2478 " f(0);"
2479 "}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002480 expr(hasType(TypeMatcher(
Daniel Jasper1dad1832012-07-10 20:20:19 +00002481 anyOf(
2482 TypeMatcher(hasDeclaration(anything())),
2483 pointsTo(AnyType),
2484 references(AnyType)
2485 // Other QualType matchers should go here.
2486 ))))));
2487}
2488
Manuel Klimek04616e42012-07-06 05:48:52 +00002489// For testing AST_MATCHER_P().
Daniel Jasper1dad1832012-07-10 20:20:19 +00002490AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002491 // Make sure all special variables are used: node, match_finder,
2492 // bound_nodes_builder, and the parameter named 'AMatcher'.
2493 return AMatcher.matches(Node, Finder, Builder);
2494}
2495
2496TEST(AstMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002497 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002498
2499 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002500 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002501
2502 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002503 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002504
2505 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002506 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002507}
2508
2509AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +00002510 polymorphicHas,
2511 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2512 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002513 return Finder->matchesChildOf(
Manuel Klimekeb958de2012-09-05 12:12:07 +00002514 Node, AMatcher, Builder,
Manuel Klimek04616e42012-07-06 05:48:52 +00002515 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2516 ASTMatchFinder::BK_First);
2517}
2518
2519TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002520 DeclarationMatcher HasClassB =
2521 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek04616e42012-07-06 05:48:52 +00002522
2523 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002524 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002525
2526 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002527 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002528
2529 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002530 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002531
2532 StatementMatcher StatementHasClassB =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002533 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002534
2535 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2536}
2537
2538TEST(For, FindsForLoops) {
2539 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2540 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper6f595392012-10-01 15:05:34 +00002541 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2542 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00002543 forStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002544}
2545
Daniel Jasper4e566c42012-07-12 08:50:38 +00002546TEST(For, ForLoopInternals) {
2547 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2548 forStmt(hasCondition(anything()))));
2549 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2550 forStmt(hasLoopInit(anything()))));
2551}
2552
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002553TEST(For, ForRangeLoopInternals) {
2554 EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2555 forRangeStmt(hasLoopVariable(anything()))));
Manuel Klimek86510812014-05-23 17:49:03 +00002556 EXPECT_TRUE(matches(
2557 "void f(){ int a[] {1, 2}; for (int i : a); }",
2558 forRangeStmt(hasRangeInit(declRefExpr(to(varDecl(hasName("a"))))))));
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002559}
2560
Daniel Jasper4e566c42012-07-12 08:50:38 +00002561TEST(For, NegativeForLoopInternals) {
2562 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002563 forStmt(hasCondition(expr()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002564 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2565 forStmt(hasLoopInit(anything()))));
2566}
2567
Manuel Klimek04616e42012-07-06 05:48:52 +00002568TEST(For, ReportsNoFalsePositives) {
2569 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2570 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2571}
2572
2573TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002574 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2575 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2576 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002577}
2578
2579TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2580 // It's not a compound statement just because there's "{}" in the source
2581 // text. This is an AST search, not grep.
2582 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002583 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002584 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002585 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002586}
2587
Daniel Jasper4e566c42012-07-12 08:50:38 +00002588TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002589 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002590 forStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002591 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002592 forStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002593 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002594 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002595 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002596 doStmt(hasBody(compoundStmt()))));
Manuel Klimek2af0a912014-05-27 07:45:18 +00002597 EXPECT_TRUE(matches("void f() { int p[2]; for (auto x : p) {} }",
2598 forRangeStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002599}
2600
2601TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2602 // The simplest case: every compound statement is in a function
2603 // definition, and the function body itself must be a compound
2604 // statement.
2605 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002606 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002607}
2608
2609TEST(HasAnySubstatement, IsNotRecursive) {
2610 // It's really "has any immediate substatement".
2611 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002612 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002613}
2614
2615TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2616 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002617 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002618}
2619
2620TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2621 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002622 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002623}
2624
2625TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2626 EXPECT_TRUE(matches("void f() { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002627 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002628 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002629 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002630}
2631
2632TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2633 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002634 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002635 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002636 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002637 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002638 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002639}
2640
2641TEST(StatementCountIs, WorksWithMultipleStatements) {
2642 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002643 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002644}
2645
2646TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2647 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002648 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002649 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002650 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002651 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002652 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002653 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002654 compoundStmt(statementCountIs(4))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002655}
2656
2657TEST(Member, WorksInSimplestCase) {
2658 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002659 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002660}
2661
2662TEST(Member, DoesNotMatchTheBaseExpression) {
2663 // Don't pick out the wrong part of the member expression, this should
2664 // be checking the member (name) only.
2665 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002666 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002667}
2668
2669TEST(Member, MatchesInMemberFunctionCall) {
2670 EXPECT_TRUE(matches("void f() {"
2671 " struct { void first() {}; } s;"
2672 " s.first();"
2673 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002674 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002675}
2676
Daniel Jasperb0c7b612012-10-23 15:46:39 +00002677TEST(Member, MatchesMember) {
2678 EXPECT_TRUE(matches(
2679 "struct A { int i; }; void f() { A a; a.i = 2; }",
2680 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2681 EXPECT_TRUE(notMatches(
2682 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2683 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2684}
2685
Daniel Jasper639522c2013-02-25 12:02:08 +00002686TEST(Member, UnderstandsAccess) {
2687 EXPECT_TRUE(matches(
2688 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2689 EXPECT_TRUE(notMatches(
2690 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2691 EXPECT_TRUE(notMatches(
2692 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2693
2694 EXPECT_TRUE(notMatches(
2695 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2696 EXPECT_TRUE(notMatches(
2697 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2698 EXPECT_TRUE(matches(
2699 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2700
2701 EXPECT_TRUE(notMatches(
2702 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2703 EXPECT_TRUE(matches("class A { protected: int i; };",
2704 fieldDecl(isProtected(), hasName("i"))));
2705 EXPECT_TRUE(notMatches(
2706 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2707
2708 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2709 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2710 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2711 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2712}
2713
Dmitri Gribenko06963042012-08-18 00:29:27 +00002714TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper5901e472012-10-01 13:40:41 +00002715 // Fails in C++11 mode
2716 EXPECT_TRUE(matchesConditionally(
2717 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2718 "class X { void *operator new(std::size_t); };",
2719 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002720
2721 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002722 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002723
Daniel Jasper5901e472012-10-01 13:40:41 +00002724 // Fails in C++11 mode
2725 EXPECT_TRUE(matchesConditionally(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002726 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2727 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper5901e472012-10-01 13:40:41 +00002728 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002729}
2730
Manuel Klimek04616e42012-07-06 05:48:52 +00002731TEST(HasObjectExpression, DoesNotMatchMember) {
2732 EXPECT_TRUE(notMatches(
2733 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002734 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002735}
2736
2737TEST(HasObjectExpression, MatchesBaseOfVariable) {
2738 EXPECT_TRUE(matches(
2739 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002740 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002741 EXPECT_TRUE(matches(
2742 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002743 memberExpr(hasObjectExpression(
2744 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002745}
2746
2747TEST(HasObjectExpression,
2748 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2749 EXPECT_TRUE(matches(
2750 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002751 memberExpr(hasObjectExpression(
2752 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002753 EXPECT_TRUE(matches(
2754 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002755 memberExpr(hasObjectExpression(
2756 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002757}
2758
2759TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002760 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2761 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2762 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2763 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002764}
2765
2766TEST(Field, MatchesField) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002767 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002768}
2769
2770TEST(IsConstQualified, MatchesConstInt) {
2771 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002772 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002773}
2774
2775TEST(IsConstQualified, MatchesConstPointer) {
2776 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002777 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002778}
2779
2780TEST(IsConstQualified, MatchesThroughTypedef) {
2781 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002782 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002783 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002784 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002785}
2786
2787TEST(IsConstQualified, DoesNotMatchInappropriately) {
2788 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002789 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002790 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002791 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002792}
2793
Sam Panzer80c13772012-08-16 16:58:10 +00002794TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002795 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2796 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2797 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2798 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002799}
2800TEST(CastExpression, MatchesImplicitCasts) {
2801 // This test creates an implicit cast from int to char.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002802 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002803 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002804 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002805}
2806
2807TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002808 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2809 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2810 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2811 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002812}
2813
Manuel Klimek04616e42012-07-06 05:48:52 +00002814TEST(ReinterpretCast, MatchesSimpleCase) {
2815 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002816 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002817}
2818
2819TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002820 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002821 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002822 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002823 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002824 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002825 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2826 "B b;"
2827 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002828 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002829}
2830
2831TEST(FunctionalCast, MatchesSimpleCase) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002832 std::string foo_class = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002833 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002834 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002835}
2836
2837TEST(FunctionalCast, DoesNotMatchOtherCasts) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002838 std::string FooClass = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002839 EXPECT_TRUE(
2840 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002841 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002842 EXPECT_TRUE(
2843 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002844 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002845}
2846
2847TEST(DynamicCast, MatchesSimpleCase) {
2848 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2849 "B b;"
2850 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002851 dynamicCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002852}
2853
2854TEST(StaticCast, MatchesSimpleCase) {
2855 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002856 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002857}
2858
2859TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002860 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002861 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002862 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002863 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002864 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002865 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2866 "B b;"
2867 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002868 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002869}
2870
Daniel Jasper417f7762012-09-18 13:36:17 +00002871TEST(CStyleCast, MatchesSimpleCase) {
2872 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2873}
2874
2875TEST(CStyleCast, DoesNotMatchOtherCasts) {
2876 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2877 "char q, *r = const_cast<char*>(&q);"
2878 "void* s = reinterpret_cast<char*>(&s);"
2879 "struct B { virtual ~B() {} }; struct D : B {};"
2880 "B b;"
2881 "D* t = dynamic_cast<D*>(&b);",
2882 cStyleCastExpr()));
2883}
2884
Manuel Klimek04616e42012-07-06 05:48:52 +00002885TEST(HasDestinationType, MatchesSimpleCase) {
2886 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002887 staticCastExpr(hasDestinationType(
2888 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002889}
2890
Sam Panzer80c13772012-08-16 16:58:10 +00002891TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2892 // This test creates an implicit const cast.
2893 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002894 implicitCastExpr(
2895 hasImplicitDestinationType(isInteger()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002896 // This test creates an implicit array-to-pointer cast.
2897 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002898 implicitCastExpr(hasImplicitDestinationType(
2899 pointsTo(TypeMatcher(anything()))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002900}
2901
2902TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2903 // This test creates an implicit cast from int to char.
2904 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002905 implicitCastExpr(hasImplicitDestinationType(
2906 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002907 // This test creates an implicit array-to-pointer cast.
2908 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002909 implicitCastExpr(hasImplicitDestinationType(
2910 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002911}
2912
2913TEST(ImplicitCast, MatchesSimpleCase) {
2914 // This test creates an implicit const cast.
2915 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002916 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002917 // This test creates an implicit cast from int to char.
2918 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002919 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002920 // This test creates an implicit array-to-pointer cast.
2921 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002922 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002923}
2924
2925TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002926 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer80c13772012-08-16 16:58:10 +00002927 // are present, and that it ignores explicit and paren casts.
2928
2929 // These two test cases have no casts.
2930 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002931 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002932 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002933 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002934
2935 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002936 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002937 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002938 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002939
2940 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002941 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002942}
2943
2944TEST(IgnoringImpCasts, MatchesImpCasts) {
2945 // This test checks that ignoringImpCasts matches when implicit casts are
2946 // present and its inner matcher alone does not match.
2947 // Note that this test creates an implicit const cast.
2948 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002949 varDecl(hasInitializer(ignoringImpCasts(
2950 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002951 // This test creates an implict cast from int to char.
2952 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002953 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002954 integerLiteral(equals(0)))))));
2955}
2956
2957TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2958 // These tests verify that ignoringImpCasts does not match if the inner
2959 // matcher does not match.
2960 // Note that the first test creates an implicit const cast.
2961 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002962 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002963 unless(anything()))))));
2964 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002965 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002966 unless(anything()))))));
2967
2968 // These tests verify that ignoringImplictCasts does not look through explicit
2969 // casts or parentheses.
2970 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002971 varDecl(hasInitializer(ignoringImpCasts(
2972 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002973 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002974 varDecl(hasInitializer(ignoringImpCasts(
2975 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002976 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002977 varDecl(hasInitializer(ignoringImpCasts(
2978 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002979 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002980 varDecl(hasInitializer(ignoringImpCasts(
2981 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002982}
2983
2984TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2985 // This test verifies that expressions that do not have implicit casts
2986 // still match the inner matcher.
2987 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002988 varDecl(hasInitializer(ignoringImpCasts(
2989 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002990}
2991
2992TEST(IgnoringParenCasts, MatchesParenCasts) {
2993 // This test checks that ignoringParenCasts matches when parentheses and/or
2994 // casts are present and its inner matcher alone does not match.
2995 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002996 varDecl(hasInitializer(ignoringParenCasts(
2997 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002998 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002999 varDecl(hasInitializer(ignoringParenCasts(
3000 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003001
3002 // This test creates an implict cast from int to char in addition to the
3003 // parentheses.
3004 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003005 varDecl(hasInitializer(ignoringParenCasts(
3006 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003007
3008 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003009 varDecl(hasInitializer(ignoringParenCasts(
3010 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003011 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003012 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003013 integerLiteral(equals(0)))))));
3014}
3015
3016TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
3017 // This test verifies that expressions that do not have any casts still match.
3018 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003019 varDecl(hasInitializer(ignoringParenCasts(
3020 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003021}
3022
3023TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
3024 // These tests verify that ignoringImpCasts does not match if the inner
3025 // matcher does not match.
3026 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003027 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003028 unless(anything()))))));
3029
3030 // This test creates an implicit cast from int to char in addition to the
3031 // parentheses.
3032 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003033 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003034 unless(anything()))))));
3035
3036 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003037 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003038 unless(anything()))))));
3039}
3040
3041TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
3042 // This test checks that ignoringParenAndImpCasts matches when
3043 // parentheses and/or implicit casts are present and its inner matcher alone
3044 // does not match.
3045 // Note that this test creates an implicit const cast.
3046 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003047 varDecl(hasInitializer(ignoringParenImpCasts(
3048 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003049 // This test creates an implicit cast from int to char.
3050 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003051 varDecl(hasInitializer(ignoringParenImpCasts(
3052 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003053}
3054
3055TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
3056 // This test verifies that expressions that do not have parentheses or
3057 // implicit casts still match.
3058 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003059 varDecl(hasInitializer(ignoringParenImpCasts(
3060 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003061 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003062 varDecl(hasInitializer(ignoringParenImpCasts(
3063 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003064}
3065
3066TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
3067 // These tests verify that ignoringParenImpCasts does not match if
3068 // the inner matcher does not match.
3069 // This test creates an implicit cast.
3070 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003071 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003072 unless(anything()))))));
3073 // These tests verify that ignoringParenAndImplictCasts does not look
3074 // through explicit casts.
3075 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003076 varDecl(hasInitializer(ignoringParenImpCasts(
3077 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00003078 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003079 varDecl(hasInitializer(ignoringParenImpCasts(
3080 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00003081 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003082 varDecl(hasInitializer(ignoringParenImpCasts(
3083 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00003084}
3085
Manuel Klimeke9235692012-07-25 10:02:02 +00003086TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek04616e42012-07-06 05:48:52 +00003087 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
3088 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003089 implicitCastExpr(
3090 hasSourceExpression(constructExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003091}
3092
Manuel Klimeke9235692012-07-25 10:02:02 +00003093TEST(HasSourceExpression, MatchesExplicitCasts) {
3094 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003095 explicitCastExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003096 hasSourceExpression(hasDescendant(
Daniel Jasper848cbe12012-09-18 13:09:13 +00003097 expr(integerLiteral()))))));
Manuel Klimeke9235692012-07-25 10:02:02 +00003098}
3099
Manuel Klimek04616e42012-07-06 05:48:52 +00003100TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003101 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00003102}
3103
3104TEST(Statement, MatchesCompoundStatments) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003105 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00003106}
3107
3108TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003109 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00003110}
3111
3112TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003113 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00003114}
3115
Samuel Benzaquenf1066292014-04-02 13:12:14 +00003116TEST(ExprWithCleanups, MatchesExprWithCleanups) {
3117 EXPECT_TRUE(matches("struct Foo { ~Foo(); };"
3118 "const Foo f = Foo();",
3119 varDecl(hasInitializer(exprWithCleanups()))));
3120 EXPECT_FALSE(matches("struct Foo { };"
3121 "const Foo f = Foo();",
3122 varDecl(hasInitializer(exprWithCleanups()))));
3123}
3124
Daniel Jasper1dad1832012-07-10 20:20:19 +00003125TEST(InitListExpression, MatchesInitListExpression) {
3126 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
3127 initListExpr(hasType(asString("int [2]")))));
3128 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003129 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003130}
3131
3132TEST(UsingDeclaration, MatchesUsingDeclarations) {
3133 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
3134 usingDecl()));
3135}
3136
3137TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
3138 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
3139 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
3140}
3141
3142TEST(UsingDeclaration, MatchesSpecificTarget) {
3143 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
3144 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003145 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003146 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
3147 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003148 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003149}
3150
3151TEST(UsingDeclaration, ThroughUsingDeclaration) {
3152 EXPECT_TRUE(matches(
3153 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003154 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003155 EXPECT_TRUE(notMatches(
3156 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003157 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003158}
3159
Benjamin Kramer73ef2162014-07-16 14:14:51 +00003160TEST(UsingDirectiveDeclaration, MatchesUsingNamespace) {
3161 EXPECT_TRUE(matches("namespace X { int x; } using namespace X;",
3162 usingDirectiveDecl()));
3163 EXPECT_FALSE(
3164 matches("namespace X { int x; } using X::x;", usingDirectiveDecl()));
3165}
3166
Sam Panzerd624bfb2012-08-16 17:20:59 +00003167TEST(SingleDecl, IsSingleDecl) {
3168 StatementMatcher SingleDeclStmt =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003169 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003170 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
3171 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
3172 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
3173 SingleDeclStmt));
3174}
3175
3176TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003177 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003178
3179 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003180 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003181 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003182 declStmt(containsDeclaration(0, MatchesInit),
3183 containsDeclaration(1, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003184 unsigned WrongIndex = 42;
3185 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003186 declStmt(containsDeclaration(WrongIndex,
Sam Panzerd624bfb2012-08-16 17:20:59 +00003187 MatchesInit))));
3188}
3189
3190TEST(DeclCount, DeclCountIsCorrect) {
3191 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003192 declStmt(declCountIs(2))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003193 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003194 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003195 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003196 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003197}
3198
Manuel Klimek04616e42012-07-06 05:48:52 +00003199TEST(While, MatchesWhileLoops) {
3200 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
3201 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
3202 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
3203}
3204
3205TEST(Do, MatchesDoLoops) {
3206 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
3207 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
3208}
3209
3210TEST(Do, DoesNotMatchWhileLoops) {
3211 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
3212}
3213
3214TEST(SwitchCase, MatchesCase) {
3215 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
3216 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
3217 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
3218 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
3219}
3220
Daniel Jasper87c3d362012-09-20 14:12:57 +00003221TEST(SwitchCase, MatchesSwitch) {
3222 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
3223 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
3224 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
3225 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
3226}
3227
Peter Collingbourne3154a102013-05-10 11:52:02 +00003228TEST(SwitchCase, MatchesEachCase) {
3229 EXPECT_TRUE(notMatches("void x() { switch(42); }",
3230 switchStmt(forEachSwitchCase(caseStmt()))));
3231 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
3232 switchStmt(forEachSwitchCase(caseStmt()))));
3233 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
3234 switchStmt(forEachSwitchCase(caseStmt()))));
3235 EXPECT_TRUE(notMatches(
3236 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3237 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3238 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3239 switchStmt(forEachSwitchCase(
3240 caseStmt(hasCaseConstant(integerLiteral()))))));
3241 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3242 switchStmt(forEachSwitchCase(
3243 caseStmt(hasCaseConstant(integerLiteral()))))));
3244 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3245 switchStmt(forEachSwitchCase(
3246 caseStmt(hasCaseConstant(integerLiteral()))))));
3247 EXPECT_TRUE(matchAndVerifyResultTrue(
3248 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3249 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3250 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3251}
3252
Manuel Klimekba46fc02013-07-19 11:50:54 +00003253TEST(ForEachConstructorInitializer, MatchesInitializers) {
3254 EXPECT_TRUE(matches(
3255 "struct X { X() : i(42), j(42) {} int i, j; };",
3256 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3257}
3258
Daniel Jasper87c3d362012-09-20 14:12:57 +00003259TEST(ExceptionHandling, SimpleCases) {
3260 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3261 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3262 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3263 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3264 throwExpr()));
3265 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3266 throwExpr()));
3267}
3268
Manuel Klimek04616e42012-07-06 05:48:52 +00003269TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3270 EXPECT_TRUE(notMatches(
3271 "void x() { if(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003272 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003273 EXPECT_TRUE(notMatches(
3274 "void x() { int x; if((x = 42)) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003275 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003276}
3277
3278TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3279 EXPECT_TRUE(matches(
3280 "void x() { if(int* a = 0) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003281 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003282}
3283
3284TEST(ForEach, BindsOneNode) {
3285 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003286 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003287 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003288}
3289
3290TEST(ForEach, BindsMultipleNodes) {
3291 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003292 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003293 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003294}
3295
3296TEST(ForEach, BindsRecursiveCombinations) {
3297 EXPECT_TRUE(matchAndVerifyResultTrue(
3298 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003299 recordDecl(hasName("C"),
3300 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003301 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003302}
3303
3304TEST(ForEachDescendant, BindsOneNode) {
3305 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003306 recordDecl(hasName("C"),
3307 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003308 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003309}
3310
Daniel Jasper94a56852012-11-16 18:39:22 +00003311TEST(ForEachDescendant, NestedForEachDescendant) {
3312 DeclarationMatcher m = recordDecl(
3313 isDefinition(), decl().bind("x"), hasName("C"));
3314 EXPECT_TRUE(matchAndVerifyResultTrue(
3315 "class A { class B { class C {}; }; };",
3316 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3317 new VerifyIdIsBoundTo<Decl>("x", "C")));
3318
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003319 // Check that a partial match of 'm' that binds 'x' in the
3320 // first part of anyOf(m, anything()) will not overwrite the
3321 // binding created by the earlier binding in the hasDescendant.
3322 EXPECT_TRUE(matchAndVerifyResultTrue(
3323 "class A { class B { class C {}; }; };",
3324 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3325 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper94a56852012-11-16 18:39:22 +00003326}
3327
Manuel Klimek04616e42012-07-06 05:48:52 +00003328TEST(ForEachDescendant, BindsMultipleNodes) {
3329 EXPECT_TRUE(matchAndVerifyResultTrue(
3330 "class C { class D { int x; int y; }; "
3331 " class E { class F { int y; int z; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003332 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003333 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003334}
3335
3336TEST(ForEachDescendant, BindsRecursiveCombinations) {
3337 EXPECT_TRUE(matchAndVerifyResultTrue(
3338 "class C { class D { "
3339 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003340 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3341 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003342 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003343}
3344
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003345TEST(ForEachDescendant, BindsCombinations) {
3346 EXPECT_TRUE(matchAndVerifyResultTrue(
3347 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3348 "(true) {} }",
3349 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3350 forEachDescendant(whileStmt().bind("while"))),
3351 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3352}
3353
3354TEST(Has, DoesNotDeleteBindings) {
3355 EXPECT_TRUE(matchAndVerifyResultTrue(
3356 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3357 new VerifyIdIsBoundTo<Decl>("x", 1)));
3358}
3359
3360TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3361 // Those matchers cover all the cases where an inner matcher is called
3362 // and there is not a 1:1 relationship between the match of the outer
3363 // matcher and the match of the inner matcher.
3364 // The pattern to look for is:
3365 // ... return InnerMatcher.matches(...); ...
3366 // In which case no special handling is needed.
3367 //
3368 // On the other hand, if there are multiple alternative matches
3369 // (for example forEach*) or matches might be discarded (for example has*)
3370 // the implementation must make sure that the discarded matches do not
3371 // affect the bindings.
3372 // When new such matchers are added, add a test here that:
3373 // - matches a simple node, and binds it as the first thing in the matcher:
3374 // recordDecl(decl().bind("x"), hasName("X")))
3375 // - uses the matcher under test afterwards in a way that not the first
3376 // alternative is matched; for anyOf, that means the first branch
3377 // would need to return false; for hasAncestor, it means that not
3378 // the direct parent matches the inner matcher.
3379
3380 EXPECT_TRUE(matchAndVerifyResultTrue(
3381 "class X { int y; };",
3382 recordDecl(
3383 recordDecl().bind("x"), hasName("::X"),
3384 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3385 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3386 EXPECT_TRUE(matchAndVerifyResultTrue(
3387 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3388 anyOf(unless(anything()), anything())),
3389 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3390 EXPECT_TRUE(matchAndVerifyResultTrue(
3391 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3392 classTemplateSpecializationDecl(
3393 decl().bind("x"),
3394 hasAnyTemplateArgument(refersToType(asString("int")))),
3395 new VerifyIdIsBoundTo<Decl>("x", 1)));
3396 EXPECT_TRUE(matchAndVerifyResultTrue(
3397 "class X { void f(); void g(); };",
3398 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3399 new VerifyIdIsBoundTo<Decl>("x", 1)));
3400 EXPECT_TRUE(matchAndVerifyResultTrue(
3401 "class X { X() : a(1), b(2) {} double a; int b; };",
3402 recordDecl(decl().bind("x"),
3403 has(constructorDecl(
3404 hasAnyConstructorInitializer(forField(hasName("b")))))),
3405 new VerifyIdIsBoundTo<Decl>("x", 1)));
3406 EXPECT_TRUE(matchAndVerifyResultTrue(
3407 "void x(int, int) { x(0, 42); }",
3408 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3409 new VerifyIdIsBoundTo<Expr>("x", 1)));
3410 EXPECT_TRUE(matchAndVerifyResultTrue(
3411 "void x(int, int y) {}",
3412 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3413 new VerifyIdIsBoundTo<Decl>("x", 1)));
3414 EXPECT_TRUE(matchAndVerifyResultTrue(
3415 "void x() { return; if (true) {} }",
3416 functionDecl(decl().bind("x"),
3417 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3418 new VerifyIdIsBoundTo<Decl>("x", 1)));
3419 EXPECT_TRUE(matchAndVerifyResultTrue(
3420 "namespace X { void b(int); void b(); }"
3421 "using X::b;",
3422 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3423 functionDecl(parameterCountIs(1))))),
3424 new VerifyIdIsBoundTo<Decl>("x", 1)));
3425 EXPECT_TRUE(matchAndVerifyResultTrue(
3426 "class A{}; class B{}; class C : B, A {};",
3427 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3428 new VerifyIdIsBoundTo<Decl>("x", 1)));
3429 EXPECT_TRUE(matchAndVerifyResultTrue(
3430 "class A{}; typedef A B; typedef A C; typedef A D;"
3431 "class E : A {};",
3432 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3433 new VerifyIdIsBoundTo<Decl>("x", 1)));
3434 EXPECT_TRUE(matchAndVerifyResultTrue(
3435 "class A { class B { void f() {} }; };",
3436 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3437 new VerifyIdIsBoundTo<Decl>("x", 1)));
3438 EXPECT_TRUE(matchAndVerifyResultTrue(
3439 "template <typename T> struct A { struct B {"
3440 " void f() { if(true) {} }"
3441 "}; };"
3442 "void t() { A<int>::B b; b.f(); }",
3443 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3444 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3445 EXPECT_TRUE(matchAndVerifyResultTrue(
3446 "class A {};",
3447 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3448 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimekba46fc02013-07-19 11:50:54 +00003449 EXPECT_TRUE(matchAndVerifyResultTrue(
3450 "class A { A() : s(), i(42) {} const char *s; int i; };",
3451 constructorDecl(hasName("::A::A"), decl().bind("x"),
3452 forEachConstructorInitializer(forField(hasName("i")))),
3453 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003454}
3455
Daniel Jasper33806cd2012-11-11 22:14:55 +00003456TEST(ForEachDescendant, BindsCorrectNodes) {
3457 EXPECT_TRUE(matchAndVerifyResultTrue(
3458 "class C { void f(); int i; };",
3459 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3460 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3461 EXPECT_TRUE(matchAndVerifyResultTrue(
3462 "class C { void f() {} int i; };",
3463 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3464 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3465}
3466
Manuel Klimekabf43712013-02-04 10:59:20 +00003467TEST(FindAll, BindsNodeOnMatch) {
3468 EXPECT_TRUE(matchAndVerifyResultTrue(
3469 "class A {};",
3470 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3471 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3472}
3473
3474TEST(FindAll, BindsDescendantNodeOnMatch) {
3475 EXPECT_TRUE(matchAndVerifyResultTrue(
3476 "class A { int a; int b; };",
3477 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3478 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3479}
3480
3481TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3482 EXPECT_TRUE(matchAndVerifyResultTrue(
3483 "class A { int a; int b; };",
3484 recordDecl(hasName("::A"),
3485 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3486 fieldDecl().bind("v"))))),
3487 new VerifyIdIsBoundTo<Decl>("v", 3)));
3488
3489 EXPECT_TRUE(matchAndVerifyResultTrue(
3490 "class A { class B {}; class C {}; };",
3491 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3492 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3493}
3494
Manuel Klimek88b95872013-02-04 09:42:38 +00003495TEST(EachOf, TriggersForEachMatch) {
3496 EXPECT_TRUE(matchAndVerifyResultTrue(
3497 "class A { int a; int b; };",
3498 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3499 has(fieldDecl(hasName("b")).bind("v")))),
3500 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3501}
3502
3503TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3504 EXPECT_TRUE(matchAndVerifyResultTrue(
3505 "class A { int a; int c; };",
3506 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3507 has(fieldDecl(hasName("b")).bind("v")))),
3508 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3509 EXPECT_TRUE(matchAndVerifyResultTrue(
3510 "class A { int c; int b; };",
3511 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3512 has(fieldDecl(hasName("b")).bind("v")))),
3513 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3514 EXPECT_TRUE(notMatches(
3515 "class A { int c; int d; };",
3516 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3517 has(fieldDecl(hasName("b")).bind("v"))))));
3518}
Manuel Klimek04616e42012-07-06 05:48:52 +00003519
3520TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3521 // Make sure that we can both match the class by name (::X) and by the type
3522 // the template was instantiated with (via a field).
3523
3524 EXPECT_TRUE(matches(
3525 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003526 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003527
3528 EXPECT_TRUE(matches(
3529 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003530 recordDecl(isTemplateInstantiation(), hasDescendant(
3531 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003532}
3533
3534TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3535 EXPECT_TRUE(matches(
3536 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003537 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek04616e42012-07-06 05:48:52 +00003538 isTemplateInstantiation())));
3539}
3540
3541TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3542 EXPECT_TRUE(matches(
3543 "template <typename T> class X { T t; }; class A {};"
3544 "template class X<A>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003545 recordDecl(isTemplateInstantiation(), hasDescendant(
3546 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003547}
3548
3549TEST(IsTemplateInstantiation,
3550 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3551 EXPECT_TRUE(matches(
3552 "template <typename T> class X {};"
3553 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003554 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003555}
3556
3557TEST(IsTemplateInstantiation,
3558 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3559 EXPECT_TRUE(matches(
3560 "class A {};"
3561 "class X {"
3562 " template <typename U> class Y { U u; };"
3563 " Y<A> y;"
3564 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003565 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003566}
3567
3568TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3569 // FIXME: Figure out whether this makes sense. It doesn't affect the
3570 // normal use case as long as the uppermost instantiation always is marked
3571 // as template instantiation, but it might be confusing as a predicate.
3572 EXPECT_TRUE(matches(
3573 "class A {};"
3574 "template <typename T> class X {"
3575 " template <typename U> class Y { U u; };"
3576 " Y<T> y;"
3577 "}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003578 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003579}
3580
3581TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3582 EXPECT_TRUE(notMatches(
3583 "template <typename T> class X {}; class A {};"
3584 "template <> class X<A> {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003585 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003586}
3587
3588TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3589 EXPECT_TRUE(notMatches(
3590 "class A {}; class Y { A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003591 recordDecl(isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003592}
3593
Benjamin Kramer7ab84762014-09-03 12:08:14 +00003594TEST(IsInstantiated, MatchesInstantiation) {
3595 EXPECT_TRUE(
3596 matches("template<typename T> class A { T i; }; class Y { A<int> a; };",
3597 recordDecl(isInstantiated())));
3598}
3599
3600TEST(IsInstantiated, NotMatchesDefinition) {
3601 EXPECT_TRUE(notMatches("template<typename T> class A { T i; };",
3602 recordDecl(isInstantiated())));
3603}
3604
3605TEST(IsInTemplateInstantiation, MatchesInstantiationStmt) {
3606 EXPECT_TRUE(matches("template<typename T> struct A { A() { T i; } };"
3607 "class Y { A<int> a; }; Y y;",
3608 declStmt(isInTemplateInstantiation())));
3609}
3610
3611TEST(IsInTemplateInstantiation, NotMatchesDefinitionStmt) {
3612 EXPECT_TRUE(notMatches("template<typename T> struct A { void x() { T i; } };",
3613 declStmt(isInTemplateInstantiation())));
3614}
3615
3616TEST(IsInstantiated, MatchesFunctionInstantiation) {
3617 EXPECT_TRUE(
3618 matches("template<typename T> void A(T t) { T i; } void x() { A(0); }",
3619 functionDecl(isInstantiated())));
3620}
3621
3622TEST(IsInstantiated, NotMatchesFunctionDefinition) {
3623 EXPECT_TRUE(notMatches("template<typename T> void A(T t) { T i; }",
3624 varDecl(isInstantiated())));
3625}
3626
3627TEST(IsInTemplateInstantiation, MatchesFunctionInstantiationStmt) {
3628 EXPECT_TRUE(
3629 matches("template<typename T> void A(T t) { T i; } void x() { A(0); }",
3630 declStmt(isInTemplateInstantiation())));
3631}
3632
3633TEST(IsInTemplateInstantiation, NotMatchesFunctionDefinitionStmt) {
3634 EXPECT_TRUE(notMatches("template<typename T> void A(T t) { T i; }",
3635 declStmt(isInTemplateInstantiation())));
3636}
3637
3638TEST(IsInTemplateInstantiation, Sharing) {
3639 auto Matcher = binaryOperator(unless(isInTemplateInstantiation()));
3640 // FIXME: Node sharing is an implementation detail, exposing it is ugly
3641 // and makes the matcher behave in non-obvious ways.
3642 EXPECT_TRUE(notMatches(
3643 "int j; template<typename T> void A(T t) { j += 42; } void x() { A(0); }",
3644 Matcher));
3645 EXPECT_TRUE(matches(
3646 "int j; template<typename T> void A(T t) { j += t; } void x() { A(0); }",
3647 Matcher));
3648}
3649
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003650TEST(IsExplicitTemplateSpecialization,
3651 DoesNotMatchPrimaryTemplate) {
3652 EXPECT_TRUE(notMatches(
3653 "template <typename T> class X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003654 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003655 EXPECT_TRUE(notMatches(
3656 "template <typename T> void f(T t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003657 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003658}
3659
3660TEST(IsExplicitTemplateSpecialization,
3661 DoesNotMatchExplicitTemplateInstantiations) {
3662 EXPECT_TRUE(notMatches(
3663 "template <typename T> class X {};"
3664 "template class X<int>; extern template class X<long>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003665 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003666 EXPECT_TRUE(notMatches(
3667 "template <typename T> void f(T t) {}"
3668 "template void f(int t); extern template void f(long t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003669 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003670}
3671
3672TEST(IsExplicitTemplateSpecialization,
3673 DoesNotMatchImplicitTemplateInstantiations) {
3674 EXPECT_TRUE(notMatches(
3675 "template <typename T> class X {}; X<int> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003676 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003677 EXPECT_TRUE(notMatches(
3678 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003679 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003680}
3681
3682TEST(IsExplicitTemplateSpecialization,
3683 MatchesExplicitTemplateSpecializations) {
3684 EXPECT_TRUE(matches(
3685 "template <typename T> class X {};"
3686 "template<> class X<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003687 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003688 EXPECT_TRUE(matches(
3689 "template <typename T> void f(T t) {}"
3690 "template<> void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003691 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003692}
3693
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003694TEST(HasAncenstor, MatchesDeclarationAncestors) {
3695 EXPECT_TRUE(matches(
3696 "class A { class B { class C {}; }; };",
3697 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3698}
3699
3700TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3701 EXPECT_TRUE(notMatches(
3702 "class A { class B { class C {}; }; };",
3703 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3704}
3705
3706TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3707 EXPECT_TRUE(matches(
3708 "class A { class B { void f() { C c; } class C {}; }; };",
3709 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3710 hasAncestor(recordDecl(hasName("A"))))))));
3711}
3712
3713TEST(HasAncenstor, MatchesStatementAncestors) {
3714 EXPECT_TRUE(matches(
3715 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003716 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003717}
3718
3719TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3720 EXPECT_TRUE(matches(
3721 "void f() { if (true) { int x = 42; } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003722 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003723}
3724
3725TEST(HasAncestor, BindsRecursiveCombinations) {
3726 EXPECT_TRUE(matchAndVerifyResultTrue(
3727 "class C { class D { class E { class F { int y; }; }; }; };",
3728 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003729 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003730}
3731
3732TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3733 EXPECT_TRUE(matchAndVerifyResultTrue(
3734 "class C { class D { class E { class F { int y; }; }; }; };",
3735 fieldDecl(hasAncestor(
3736 decl(
3737 hasDescendant(recordDecl(isDefinition(),
3738 hasAncestor(recordDecl())))
3739 ).bind("d")
3740 )),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003741 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003742}
3743
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003744TEST(HasAncestor, MatchesClosestAncestor) {
3745 EXPECT_TRUE(matchAndVerifyResultTrue(
3746 "template <typename T> struct C {"
3747 " void f(int) {"
3748 " struct I { void g(T) { int x; } } i; i.g(42);"
3749 " }"
3750 "};"
3751 "template struct C<int>;",
3752 varDecl(hasName("x"),
3753 hasAncestor(functionDecl(hasParameter(
3754 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3755 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3756}
3757
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003758TEST(HasAncestor, MatchesInTemplateInstantiations) {
3759 EXPECT_TRUE(matches(
3760 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3761 "A<int>::B::C a;",
3762 fieldDecl(hasType(asString("int")),
3763 hasAncestor(recordDecl(hasName("A"))))));
3764}
3765
3766TEST(HasAncestor, MatchesInImplicitCode) {
3767 EXPECT_TRUE(matches(
3768 "struct X {}; struct A { A() {} X x; };",
3769 constructorDecl(
3770 hasAnyConstructorInitializer(withInitializer(expr(
3771 hasAncestor(recordDecl(hasName("A")))))))));
3772}
3773
Daniel Jasper632aea92012-10-22 16:26:51 +00003774TEST(HasParent, MatchesOnlyParent) {
3775 EXPECT_TRUE(matches(
3776 "void f() { if (true) { int x = 42; } }",
3777 compoundStmt(hasParent(ifStmt()))));
3778 EXPECT_TRUE(notMatches(
3779 "void f() { for (;;) { int x = 42; } }",
3780 compoundStmt(hasParent(ifStmt()))));
3781 EXPECT_TRUE(notMatches(
3782 "void f() { if (true) for (;;) { int x = 42; } }",
3783 compoundStmt(hasParent(ifStmt()))));
3784}
3785
Manuel Klimekc844a462012-12-06 14:42:48 +00003786TEST(HasAncestor, MatchesAllAncestors) {
3787 EXPECT_TRUE(matches(
3788 "template <typename T> struct C { static void f() { 42; } };"
3789 "void t() { C<int>::f(); }",
3790 integerLiteral(
3791 equals(42),
3792 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3793 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3794}
3795
3796TEST(HasParent, MatchesAllParents) {
3797 EXPECT_TRUE(matches(
3798 "template <typename T> struct C { static void f() { 42; } };"
3799 "void t() { C<int>::f(); }",
3800 integerLiteral(
3801 equals(42),
3802 hasParent(compoundStmt(hasParent(functionDecl(
3803 hasParent(recordDecl(isTemplateInstantiation())))))))));
3804 EXPECT_TRUE(matches(
3805 "template <typename T> struct C { static void f() { 42; } };"
3806 "void t() { C<int>::f(); }",
3807 integerLiteral(
3808 equals(42),
3809 hasParent(compoundStmt(hasParent(functionDecl(
3810 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3811 EXPECT_TRUE(matches(
3812 "template <typename T> struct C { static void f() { 42; } };"
3813 "void t() { C<int>::f(); }",
3814 integerLiteral(equals(42),
3815 hasParent(compoundStmt(allOf(
3816 hasParent(functionDecl(
3817 hasParent(recordDecl(isTemplateInstantiation())))),
3818 hasParent(functionDecl(hasParent(recordDecl(
3819 unless(isTemplateInstantiation())))))))))));
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003820 EXPECT_TRUE(
3821 notMatches("template <typename T> struct C { static void f() {} };"
3822 "void t() { C<int>::f(); }",
3823 compoundStmt(hasParent(recordDecl()))));
Manuel Klimekc844a462012-12-06 14:42:48 +00003824}
3825
Samuel Benzaquen3ca0a7b2014-06-13 13:31:40 +00003826TEST(HasParent, NoDuplicateParents) {
3827 class HasDuplicateParents : public BoundNodesCallback {
3828 public:
3829 bool run(const BoundNodes *Nodes) override { return false; }
3830 bool run(const BoundNodes *Nodes, ASTContext *Context) override {
3831 const Stmt *Node = Nodes->getNodeAs<Stmt>("node");
3832 std::set<const void *> Parents;
3833 for (const auto &Parent : Context->getParents(*Node)) {
3834 if (!Parents.insert(Parent.getMemoizationData()).second) {
3835 return true;
3836 }
3837 }
3838 return false;
3839 }
3840 };
3841 EXPECT_FALSE(matchAndVerifyResultTrue(
3842 "template <typename T> int Foo() { return 1 + 2; }\n"
3843 "int x = Foo<int>() + Foo<unsigned>();",
3844 stmt().bind("node"), new HasDuplicateParents()));
3845}
3846
Daniel Jasper516b02e2012-10-17 08:52:59 +00003847TEST(TypeMatching, MatchesTypes) {
3848 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3849}
3850
3851TEST(TypeMatching, MatchesArrayTypes) {
3852 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3853 EXPECT_TRUE(matches("int a[42];", arrayType()));
3854 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3855
3856 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3857 arrayType(hasElementType(builtinType()))));
3858
3859 EXPECT_TRUE(matches(
3860 "int const a[] = { 2, 3 };",
3861 qualType(arrayType(hasElementType(builtinType())))));
3862 EXPECT_TRUE(matches(
3863 "int const a[] = { 2, 3 };",
3864 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3865 EXPECT_TRUE(matches(
3866 "typedef const int T; T x[] = { 1, 2 };",
3867 qualType(isConstQualified(), arrayType())));
3868
3869 EXPECT_TRUE(notMatches(
3870 "int a[] = { 2, 3 };",
3871 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3872 EXPECT_TRUE(notMatches(
3873 "int a[] = { 2, 3 };",
3874 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3875 EXPECT_TRUE(notMatches(
3876 "int const a[] = { 2, 3 };",
3877 qualType(arrayType(hasElementType(builtinType())),
3878 unless(isConstQualified()))));
3879
3880 EXPECT_TRUE(matches("int a[2];",
3881 constantArrayType(hasElementType(builtinType()))));
3882 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3883}
3884
3885TEST(TypeMatching, MatchesComplexTypes) {
3886 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3887 EXPECT_TRUE(matches(
3888 "_Complex float f;",
3889 complexType(hasElementType(builtinType()))));
3890 EXPECT_TRUE(notMatches(
3891 "_Complex float f;",
3892 complexType(hasElementType(isInteger()))));
3893}
3894
3895TEST(TypeMatching, MatchesConstantArrayTypes) {
3896 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3897 EXPECT_TRUE(notMatches(
3898 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3899 constantArrayType(hasElementType(builtinType()))));
3900
3901 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3902 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3903 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3904}
3905
3906TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3907 EXPECT_TRUE(matches(
3908 "template <typename T, int Size> class array { T data[Size]; };",
3909 dependentSizedArrayType()));
3910 EXPECT_TRUE(notMatches(
3911 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3912 dependentSizedArrayType()));
3913}
3914
3915TEST(TypeMatching, MatchesIncompleteArrayType) {
3916 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3917 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3918
3919 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3920 incompleteArrayType()));
3921}
3922
3923TEST(TypeMatching, MatchesVariableArrayType) {
3924 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3925 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3926
3927 EXPECT_TRUE(matches(
3928 "void f(int b) { int a[b]; }",
3929 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3930 varDecl(hasName("b")))))))));
3931}
3932
3933TEST(TypeMatching, MatchesAtomicTypes) {
David Majnemer197e2102014-03-05 06:32:38 +00003934 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
3935 llvm::Triple::Win32) {
3936 // FIXME: Make this work for MSVC.
3937 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003938
David Majnemer197e2102014-03-05 06:32:38 +00003939 EXPECT_TRUE(matches("_Atomic(int) i;",
3940 atomicType(hasValueType(isInteger()))));
3941 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3942 atomicType(hasValueType(isInteger()))));
3943 }
Daniel Jasper516b02e2012-10-17 08:52:59 +00003944}
3945
3946TEST(TypeMatching, MatchesAutoTypes) {
3947 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3948 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3949 autoType()));
3950
Richard Smith061f1e22013-04-30 21:23:01 +00003951 // FIXME: Matching against the type-as-written can't work here, because the
3952 // type as written was not deduced.
3953 //EXPECT_TRUE(matches("auto a = 1;",
3954 // autoType(hasDeducedType(isInteger()))));
3955 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3956 // autoType(hasDeducedType(isInteger()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003957}
3958
Daniel Jasperd29d5fa2012-10-29 10:14:44 +00003959TEST(TypeMatching, MatchesFunctionTypes) {
3960 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3961 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3962}
3963
Edwin Vaneec074802013-04-01 18:33:34 +00003964TEST(TypeMatching, MatchesParenType) {
3965 EXPECT_TRUE(
3966 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3967 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3968
3969 EXPECT_TRUE(matches(
3970 "int (*ptr_to_func)(int);",
3971 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3972 EXPECT_TRUE(notMatches(
3973 "int (*ptr_to_array)[4];",
3974 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3975}
3976
Daniel Jasper516b02e2012-10-17 08:52:59 +00003977TEST(TypeMatching, PointerTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003978 // FIXME: Reactive when these tests can be more specific (not matching
3979 // implicit code on certain platforms), likely when we have hasDescendant for
3980 // Types/TypeLocs.
3981 //EXPECT_TRUE(matchAndVerifyResultTrue(
3982 // "int* a;",
3983 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3984 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3985 //EXPECT_TRUE(matchAndVerifyResultTrue(
3986 // "int* a;",
3987 // pointerTypeLoc().bind("loc"),
3988 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003989 EXPECT_TRUE(matches(
3990 "int** a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003991 loc(pointerType(pointee(qualType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003992 EXPECT_TRUE(matches(
3993 "int** a;",
3994 loc(pointerType(pointee(pointerType())))));
3995 EXPECT_TRUE(matches(
3996 "int* b; int* * const a = &b;",
3997 loc(qualType(isConstQualified(), pointerType()))));
3998
3999 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper7943eb52012-10-17 13:35:36 +00004000 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4001 hasType(blockPointerType()))));
4002 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
4003 hasType(memberPointerType()))));
4004 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4005 hasType(pointerType()))));
4006 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4007 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00004008 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4009 hasType(lValueReferenceType()))));
4010 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4011 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004012
Daniel Jasper7943eb52012-10-17 13:35:36 +00004013 Fragment = "int *ptr;";
4014 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4015 hasType(blockPointerType()))));
4016 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4017 hasType(memberPointerType()))));
4018 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
4019 hasType(pointerType()))));
4020 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4021 hasType(referenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004022
Daniel Jasper7943eb52012-10-17 13:35:36 +00004023 Fragment = "int a; int &ref = a;";
4024 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4025 hasType(blockPointerType()))));
4026 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4027 hasType(memberPointerType()))));
4028 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4029 hasType(pointerType()))));
4030 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
4031 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00004032 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
4033 hasType(lValueReferenceType()))));
4034 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4035 hasType(rValueReferenceType()))));
4036
4037 Fragment = "int &&ref = 2;";
4038 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4039 hasType(blockPointerType()))));
4040 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4041 hasType(memberPointerType()))));
4042 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4043 hasType(pointerType()))));
4044 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
4045 hasType(referenceType()))));
4046 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4047 hasType(lValueReferenceType()))));
4048 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
4049 hasType(rValueReferenceType()))));
4050}
4051
4052TEST(TypeMatching, AutoRefTypes) {
4053 std::string Fragment = "auto a = 1;"
4054 "auto b = a;"
4055 "auto &c = a;"
4056 "auto &&d = c;"
4057 "auto &&e = 2;";
4058 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
4059 hasType(referenceType()))));
4060 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
4061 hasType(referenceType()))));
4062 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
4063 hasType(referenceType()))));
4064 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
4065 hasType(lValueReferenceType()))));
4066 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
4067 hasType(rValueReferenceType()))));
4068 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
4069 hasType(referenceType()))));
4070 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
4071 hasType(lValueReferenceType()))));
4072 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
4073 hasType(rValueReferenceType()))));
4074 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
4075 hasType(referenceType()))));
4076 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
4077 hasType(lValueReferenceType()))));
4078 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
4079 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004080}
4081
4082TEST(TypeMatching, PointeeTypes) {
4083 EXPECT_TRUE(matches("int b; int &a = b;",
4084 referenceType(pointee(builtinType()))));
4085 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
4086
4087 EXPECT_TRUE(matches("int *a;",
David Blaikieb61d0872013-02-18 19:04:16 +00004088 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004089
4090 EXPECT_TRUE(matches(
4091 "int const *A;",
4092 pointerType(pointee(isConstQualified(), builtinType()))));
4093 EXPECT_TRUE(notMatches(
4094 "int *A;",
4095 pointerType(pointee(isConstQualified(), builtinType()))));
4096}
4097
4098TEST(TypeMatching, MatchesPointersToConstTypes) {
4099 EXPECT_TRUE(matches("int b; int * const a = &b;",
4100 loc(pointerType())));
4101 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00004102 loc(pointerType())));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004103 EXPECT_TRUE(matches(
4104 "int b; const int * a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00004105 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004106 EXPECT_TRUE(matches(
4107 "int b; const int * a = &b;",
4108 pointerType(pointee(builtinType()))));
4109}
4110
4111TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00004112 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
4113 hasType(typedefType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004114}
4115
Edwin Vanef901b712013-02-25 14:49:29 +00004116TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vaneb6eae142013-02-25 20:43:32 +00004117 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vanef901b712013-02-25 14:49:29 +00004118 templateSpecializationType()));
4119}
4120
Edwin Vaneb6eae142013-02-25 20:43:32 +00004121TEST(TypeMatching, MatchesRecordType) {
4122 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek59b0af62013-02-27 11:56:58 +00004123 EXPECT_TRUE(matches("struct S{}; S s;",
4124 recordType(hasDeclaration(recordDecl(hasName("S"))))));
4125 EXPECT_TRUE(notMatches("int i;",
4126 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00004127}
4128
4129TEST(TypeMatching, MatchesElaboratedType) {
4130 EXPECT_TRUE(matches(
4131 "namespace N {"
4132 " namespace M {"
4133 " class D {};"
4134 " }"
4135 "}"
4136 "N::M::D d;", elaboratedType()));
4137 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
4138 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
4139}
4140
4141TEST(ElaboratedTypeNarrowing, hasQualifier) {
4142 EXPECT_TRUE(matches(
4143 "namespace N {"
4144 " namespace M {"
4145 " class D {};"
4146 " }"
4147 "}"
4148 "N::M::D d;",
4149 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
4150 EXPECT_TRUE(notMatches(
4151 "namespace M {"
4152 " class D {};"
4153 "}"
4154 "M::D d;",
4155 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vane6972f6d2013-03-04 17:51:00 +00004156 EXPECT_TRUE(notMatches(
4157 "struct D {"
4158 "} d;",
4159 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00004160}
4161
4162TEST(ElaboratedTypeNarrowing, namesType) {
4163 EXPECT_TRUE(matches(
4164 "namespace N {"
4165 " namespace M {"
4166 " class D {};"
4167 " }"
4168 "}"
4169 "N::M::D d;",
4170 elaboratedType(elaboratedType(namesType(recordType(
4171 hasDeclaration(namedDecl(hasName("D")))))))));
4172 EXPECT_TRUE(notMatches(
4173 "namespace M {"
4174 " class D {};"
4175 "}"
4176 "M::D d;",
4177 elaboratedType(elaboratedType(namesType(typedefType())))));
4178}
4179
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00004180TEST(NNS, MatchesNestedNameSpecifiers) {
4181 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
4182 nestedNameSpecifier()));
4183 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
4184 nestedNameSpecifier()));
4185 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
4186 nestedNameSpecifier()));
4187
4188 EXPECT_TRUE(matches(
4189 "struct A { static void f() {} }; void g() { A::f(); }",
4190 nestedNameSpecifier()));
4191 EXPECT_TRUE(notMatches(
4192 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
4193 nestedNameSpecifier()));
4194}
4195
Daniel Jasper87c3d362012-09-20 14:12:57 +00004196TEST(NullStatement, SimpleCases) {
4197 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
4198 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
4199}
4200
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00004201TEST(NNS, MatchesTypes) {
4202 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
4203 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
4204 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
4205 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
4206 Matcher));
4207 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
4208}
4209
4210TEST(NNS, MatchesNamespaceDecls) {
4211 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
4212 specifiesNamespace(hasName("ns")));
4213 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
4214 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
4215 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
4216}
4217
4218TEST(NNS, BindsNestedNameSpecifiers) {
4219 EXPECT_TRUE(matchAndVerifyResultTrue(
4220 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
4221 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
4222 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
4223}
4224
4225TEST(NNS, BindsNestedNameSpecifierLocs) {
4226 EXPECT_TRUE(matchAndVerifyResultTrue(
4227 "namespace ns { struct B {}; } ns::B b;",
4228 loc(nestedNameSpecifier()).bind("loc"),
4229 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
4230}
4231
4232TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
4233 EXPECT_TRUE(matches(
4234 "struct A { struct B { struct C {}; }; }; A::B::C c;",
4235 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
4236 EXPECT_TRUE(matches(
4237 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasper516b02e2012-10-17 08:52:59 +00004238 nestedNameSpecifierLoc(hasPrefix(
4239 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00004240}
4241
Daniel Jasper6fc34332012-10-30 15:42:00 +00004242TEST(NNS, DescendantsOfNestedNameSpecifiers) {
4243 std::string Fragment =
4244 "namespace a { struct A { struct B { struct C {}; }; }; };"
4245 "void f() { a::A::B::C c; }";
4246 EXPECT_TRUE(matches(
4247 Fragment,
4248 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
4249 hasDescendant(nestedNameSpecifier(
4250 specifiesNamespace(hasName("a")))))));
4251 EXPECT_TRUE(notMatches(
4252 Fragment,
4253 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
4254 has(nestedNameSpecifier(
4255 specifiesNamespace(hasName("a")))))));
4256 EXPECT_TRUE(matches(
4257 Fragment,
4258 nestedNameSpecifier(specifiesType(asString("struct a::A")),
4259 has(nestedNameSpecifier(
4260 specifiesNamespace(hasName("a")))))));
4261
4262 // Not really useful because a NestedNameSpecifier can af at most one child,
4263 // but to complete the interface.
4264 EXPECT_TRUE(matchAndVerifyResultTrue(
4265 Fragment,
4266 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
4267 forEach(nestedNameSpecifier().bind("x"))),
4268 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
4269}
4270
4271TEST(NNS, NestedNameSpecifiersAsDescendants) {
4272 std::string Fragment =
4273 "namespace a { struct A { struct B { struct C {}; }; }; };"
4274 "void f() { a::A::B::C c; }";
4275 EXPECT_TRUE(matches(
4276 Fragment,
4277 decl(hasDescendant(nestedNameSpecifier(specifiesType(
4278 asString("struct a::A")))))));
4279 EXPECT_TRUE(matchAndVerifyResultTrue(
4280 Fragment,
4281 functionDecl(hasName("f"),
4282 forEachDescendant(nestedNameSpecifier().bind("x"))),
4283 // Nested names: a, a::A and a::A::B.
4284 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
4285}
4286
4287TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
4288 std::string Fragment =
4289 "namespace a { struct A { struct B { struct C {}; }; }; };"
4290 "void f() { a::A::B::C c; }";
4291 EXPECT_TRUE(matches(
4292 Fragment,
4293 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4294 hasDescendant(loc(nestedNameSpecifier(
4295 specifiesNamespace(hasName("a"))))))));
4296 EXPECT_TRUE(notMatches(
4297 Fragment,
4298 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4299 has(loc(nestedNameSpecifier(
4300 specifiesNamespace(hasName("a"))))))));
4301 EXPECT_TRUE(matches(
4302 Fragment,
4303 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
4304 has(loc(nestedNameSpecifier(
4305 specifiesNamespace(hasName("a"))))))));
4306
4307 EXPECT_TRUE(matchAndVerifyResultTrue(
4308 Fragment,
4309 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4310 forEach(nestedNameSpecifierLoc().bind("x"))),
4311 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
4312}
4313
4314TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
4315 std::string Fragment =
4316 "namespace a { struct A { struct B { struct C {}; }; }; };"
4317 "void f() { a::A::B::C c; }";
4318 EXPECT_TRUE(matches(
4319 Fragment,
4320 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4321 asString("struct a::A"))))))));
4322 EXPECT_TRUE(matchAndVerifyResultTrue(
4323 Fragment,
4324 functionDecl(hasName("f"),
4325 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4326 // Nested names: a, a::A and a::A::B.
4327 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4328}
4329
Manuel Klimek191c0932013-02-01 13:41:35 +00004330template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimekc2687452012-10-24 14:47:44 +00004331public:
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004332 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4333 StringRef InnerId)
4334 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jaspere9aa6872012-10-29 10:48:25 +00004335 }
4336
Manuel Klimek191c0932013-02-01 13:41:35 +00004337 virtual bool run(const BoundNodes *Nodes) { return false; }
4338
Manuel Klimekc2687452012-10-24 14:47:44 +00004339 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4340 const T *Node = Nodes->getNodeAs<T>(Id);
Benjamin Kramer76645582014-07-23 11:41:44 +00004341 return selectFirst<T>(InnerId, match(InnerMatcher, *Node, *Context)) !=
4342 nullptr;
Manuel Klimekc2687452012-10-24 14:47:44 +00004343 }
4344private:
4345 std::string Id;
4346 internal::Matcher<T> InnerMatcher;
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004347 std::string InnerId;
Manuel Klimekc2687452012-10-24 14:47:44 +00004348};
4349
4350TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004351 EXPECT_TRUE(matchAndVerifyResultTrue(
4352 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4353 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004354 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4355 "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004356 EXPECT_TRUE(matchAndVerifyResultFalse(
4357 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4358 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004359 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4360 "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004361}
4362
4363TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004364 EXPECT_TRUE(matchAndVerifyResultTrue(
4365 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004366 new VerifyMatchOnNode<clang::Stmt>(
4367 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004368 EXPECT_TRUE(matchAndVerifyResultFalse(
4369 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004370 new VerifyMatchOnNode<clang::Stmt>(
4371 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004372}
4373
4374TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4375 EXPECT_TRUE(matchAndVerifyResultTrue(
4376 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4377 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004378 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004379 EXPECT_TRUE(matchAndVerifyResultFalse(
4380 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4381 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004382 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004383}
4384
Manuel Klimekbee08572013-02-07 12:42:10 +00004385template <typename T>
4386class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4387public:
4388 virtual bool run(const BoundNodes *Nodes) { return false; }
4389
4390 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4391 const T *Node = Nodes->getNodeAs<T>("");
4392 return verify(*Nodes, *Context, Node);
4393 }
4394
4395 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004396 // Use the original typed pointer to verify we can pass pointers to subtypes
4397 // to equalsNode.
4398 const T *TypedNode = cast<T>(Node);
Benjamin Kramer76645582014-07-23 11:41:44 +00004399 return selectFirst<T>(
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004400 "", match(stmt(hasParent(
4401 stmt(has(stmt(equalsNode(TypedNode)))).bind(""))),
Craig Topper416fa342014-06-08 08:38:12 +00004402 *Node, Context)) != nullptr;
Manuel Klimekbee08572013-02-07 12:42:10 +00004403 }
4404 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004405 // Use the original typed pointer to verify we can pass pointers to subtypes
4406 // to equalsNode.
4407 const T *TypedNode = cast<T>(Node);
Benjamin Kramer76645582014-07-23 11:41:44 +00004408 return selectFirst<T>(
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004409 "", match(decl(hasParent(
4410 decl(has(decl(equalsNode(TypedNode)))).bind(""))),
Craig Topper416fa342014-06-08 08:38:12 +00004411 *Node, Context)) != nullptr;
Manuel Klimekbee08572013-02-07 12:42:10 +00004412 }
4413};
4414
4415TEST(IsEqualTo, MatchesNodesByIdentity) {
4416 EXPECT_TRUE(matchAndVerifyResultTrue(
4417 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004418 new VerifyAncestorHasChildIsEqual<CXXRecordDecl>()));
4419 EXPECT_TRUE(matchAndVerifyResultTrue(
4420 "void f() { if (true) if(true) {} }", ifStmt().bind(""),
4421 new VerifyAncestorHasChildIsEqual<IfStmt>()));
Manuel Klimekbee08572013-02-07 12:42:10 +00004422}
4423
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004424class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4425public:
4426 VerifyStartOfTranslationUnit() : Called(false) {}
4427 virtual void run(const MatchFinder::MatchResult &Result) {
4428 EXPECT_TRUE(Called);
4429 }
4430 virtual void onStartOfTranslationUnit() {
4431 Called = true;
4432 }
4433 bool Called;
4434};
4435
4436TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4437 MatchFinder Finder;
4438 VerifyStartOfTranslationUnit VerifyCallback;
4439 Finder.addMatcher(decl(), &VerifyCallback);
Ahmed Charlesb8984322014-03-07 20:03:18 +00004440 std::unique_ptr<FrontendActionFactory> Factory(
4441 newFrontendActionFactory(&Finder));
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004442 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4443 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004444
4445 VerifyCallback.Called = false;
Ahmed Charlesb8984322014-03-07 20:03:18 +00004446 std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
Peter Collingbournea2334162013-11-07 22:30:36 +00004447 ASSERT_TRUE(AST.get());
4448 Finder.matchAST(AST->getASTContext());
4449 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004450}
4451
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004452class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4453public:
4454 VerifyEndOfTranslationUnit() : Called(false) {}
4455 virtual void run(const MatchFinder::MatchResult &Result) {
4456 EXPECT_FALSE(Called);
4457 }
4458 virtual void onEndOfTranslationUnit() {
4459 Called = true;
4460 }
4461 bool Called;
4462};
4463
4464TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4465 MatchFinder Finder;
4466 VerifyEndOfTranslationUnit VerifyCallback;
4467 Finder.addMatcher(decl(), &VerifyCallback);
Ahmed Charlesb8984322014-03-07 20:03:18 +00004468 std::unique_ptr<FrontendActionFactory> Factory(
4469 newFrontendActionFactory(&Finder));
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004470 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4471 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004472
4473 VerifyCallback.Called = false;
Ahmed Charlesb8984322014-03-07 20:03:18 +00004474 std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
Peter Collingbournea2334162013-11-07 22:30:36 +00004475 ASSERT_TRUE(AST.get());
4476 Finder.matchAST(AST->getASTContext());
4477 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004478}
4479
Manuel Klimekbbb75852013-06-20 14:06:32 +00004480TEST(EqualsBoundNodeMatcher, QualType) {
4481 EXPECT_TRUE(matches(
4482 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4483 hasInitializer(ignoringParenImpCasts(
4484 hasType(qualType(equalsBoundNode("type"))))))));
4485 EXPECT_TRUE(notMatches("int i = 1.f;",
4486 varDecl(hasType(qualType().bind("type")),
4487 hasInitializer(ignoringParenImpCasts(hasType(
4488 qualType(equalsBoundNode("type"))))))));
4489}
4490
4491TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4492 EXPECT_TRUE(notMatches(
4493 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4494 hasInitializer(ignoringParenImpCasts(
4495 hasType(qualType(equalsBoundNode("type"))))))));
4496}
4497
4498TEST(EqualsBoundNodeMatcher, Stmt) {
4499 EXPECT_TRUE(
4500 matches("void f() { if(true) {} }",
4501 stmt(allOf(ifStmt().bind("if"),
4502 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4503
4504 EXPECT_TRUE(notMatches(
4505 "void f() { if(true) { if (true) {} } }",
4506 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4507}
4508
4509TEST(EqualsBoundNodeMatcher, Decl) {
4510 EXPECT_TRUE(matches(
4511 "class X { class Y {}; };",
4512 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4513 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4514
4515 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4516 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4517 has(decl(equalsBoundNode("record")))))));
4518}
4519
4520TEST(EqualsBoundNodeMatcher, Type) {
4521 EXPECT_TRUE(matches(
4522 "class X { int a; int b; };",
4523 recordDecl(
4524 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4525 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4526
4527 EXPECT_TRUE(notMatches(
4528 "class X { int a; double b; };",
4529 recordDecl(
4530 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4531 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4532}
4533
4534TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
Manuel Klimekbbb75852013-06-20 14:06:32 +00004535 EXPECT_TRUE(matchAndVerifyResultTrue(
4536 "int f() {"
4537 " if (1) {"
4538 " int i = 9;"
4539 " }"
4540 " int j = 10;"
4541 " {"
4542 " float k = 9.0;"
4543 " }"
4544 " return 0;"
4545 "}",
4546 // Look for variable declarations within functions whose type is the same
4547 // as the function return type.
4548 functionDecl(returns(qualType().bind("type")),
4549 forEachDescendant(varDecl(hasType(
4550 qualType(equalsBoundNode("type")))).bind("decl"))),
4551 // Only i and j should match, not k.
4552 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4553}
4554
4555TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4556 EXPECT_TRUE(matchAndVerifyResultTrue(
4557 "void f() {"
4558 " int x;"
4559 " double d;"
4560 " x = d + x - d + x;"
4561 "}",
4562 functionDecl(
4563 hasName("f"), forEachDescendant(varDecl().bind("d")),
4564 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4565 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4566}
4567
Manuel Klimekce68f772014-03-25 14:39:26 +00004568TEST(EqualsBoundNodeMatcher, UnlessDescendantsOfAncestorsMatch) {
4569 EXPECT_TRUE(matchAndVerifyResultTrue(
4570 "struct StringRef { int size() const; const char* data() const; };"
4571 "void f(StringRef v) {"
4572 " v.data();"
4573 "}",
4574 memberCallExpr(
4575 callee(methodDecl(hasName("data"))),
4576 on(declRefExpr(to(varDecl(hasType(recordDecl(hasName("StringRef"))))
4577 .bind("var")))),
4578 unless(hasAncestor(stmt(hasDescendant(memberCallExpr(
4579 callee(methodDecl(anyOf(hasName("size"), hasName("length")))),
4580 on(declRefExpr(to(varDecl(equalsBoundNode("var")))))))))))
4581 .bind("data"),
4582 new VerifyIdIsBoundTo<Expr>("data", 1)));
4583
4584 EXPECT_FALSE(matches(
4585 "struct StringRef { int size() const; const char* data() const; };"
4586 "void f(StringRef v) {"
4587 " v.data();"
4588 " v.size();"
4589 "}",
4590 memberCallExpr(
4591 callee(methodDecl(hasName("data"))),
4592 on(declRefExpr(to(varDecl(hasType(recordDecl(hasName("StringRef"))))
4593 .bind("var")))),
4594 unless(hasAncestor(stmt(hasDescendant(memberCallExpr(
4595 callee(methodDecl(anyOf(hasName("size"), hasName("length")))),
4596 on(declRefExpr(to(varDecl(equalsBoundNode("var")))))))))))
4597 .bind("data")));
4598}
4599
Manuel Klimek04616e42012-07-06 05:48:52 +00004600} // end namespace ast_matchers
4601} // end namespace clang