blob: 2195604799bc971c6a8f7033ab61fbb24665d728 [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));
594}
595
596TEST(DeclarationMatcher, HasDescendant) {
597 DeclarationMatcher ZDescendantClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000598 recordDecl(
599 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000600 hasName("Z"));
601 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
602 EXPECT_TRUE(
603 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
604 EXPECT_TRUE(
605 matches("class Z { class A { class Y { class X {}; }; }; };",
606 ZDescendantClassX));
607 EXPECT_TRUE(
608 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
609 ZDescendantClassX));
610 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
611
612 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000613 recordDecl(
614 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000615 hasName("X"))),
616 hasName("Z"));
617 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
618 ZDescendantClassXHasClassY));
619 EXPECT_TRUE(
620 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
621 ZDescendantClassXHasClassY));
622 EXPECT_TRUE(notMatches(
623 "class Z {"
624 " class A {"
625 " class B {"
626 " class X {"
627 " class C {"
628 " class Y {};"
629 " };"
630 " };"
631 " }; "
632 " };"
633 "};", ZDescendantClassXHasClassY));
634
635 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000636 recordDecl(
637 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
638 hasName("X"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000639 hasName("Z"));
640 EXPECT_TRUE(
641 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
642 ZDescendantClassXDescendantClassY));
643 EXPECT_TRUE(matches(
644 "class Z {"
645 " class A {"
646 " class X {"
647 " class B {"
648 " class Y {};"
649 " };"
650 " class Y {};"
651 " };"
652 " };"
653 "};", ZDescendantClassXDescendantClassY));
654}
655
Daniel Jasper3dfa09b2014-07-23 13:17:47 +0000656TEST(DeclarationMatcher, HasDescendantMemoization) {
657 DeclarationMatcher CannotMemoize =
658 decl(hasDescendant(typeLoc().bind("x")), has(decl()));
659 EXPECT_TRUE(matches("void f() { int i; }", CannotMemoize));
660}
661
Samuel Benzaquenf28d9972014-10-01 15:08:07 +0000662TEST(DeclarationMatcher, HasDescendantMemoizationUsesRestrictKind) {
663 auto Name = hasName("i");
664 auto VD = internal::Matcher<VarDecl>(Name).dynCastTo<Decl>();
665 auto RD = internal::Matcher<RecordDecl>(Name).dynCastTo<Decl>();
666 // Matching VD first should not make a cache hit for RD.
667 EXPECT_TRUE(notMatches("void f() { int i; }",
668 decl(hasDescendant(VD), hasDescendant(RD))));
669 EXPECT_TRUE(notMatches("void f() { int i; }",
670 decl(hasDescendant(RD), hasDescendant(VD))));
671 // Not matching RD first should not make a cache hit for VD either.
672 EXPECT_TRUE(matches("void f() { int i; }",
673 decl(anyOf(hasDescendant(RD), hasDescendant(VD)))));
674}
675
Manuel Klimek3fe8a382014-08-25 11:23:50 +0000676TEST(DeclarationMatcher, HasAttr) {
677 EXPECT_TRUE(matches("struct __attribute__((warn_unused)) X {};",
678 decl(hasAttr(clang::attr::WarnUnused))));
679 EXPECT_FALSE(matches("struct X {};",
680 decl(hasAttr(clang::attr::WarnUnused))));
681}
682
Manuel Klimekd52a3b82014-08-05 09:45:53 +0000683TEST(DeclarationMatcher, MatchCudaDecl) {
684 EXPECT_TRUE(matchesWithCuda("__global__ void f() { }"
685 "void g() { f<<<1, 2>>>(); }",
686 CUDAKernelCallExpr()));
687 EXPECT_TRUE(matchesWithCuda("__attribute__((device)) void f() {}",
Manuel Klimek3fe8a382014-08-25 11:23:50 +0000688 hasAttr(clang::attr::CUDADevice)));
Manuel Klimekd52a3b82014-08-05 09:45:53 +0000689 EXPECT_TRUE(notMatchesWithCuda("void f() {}",
Manuel Klimek3fe8a382014-08-25 11:23:50 +0000690 CUDAKernelCallExpr()));
Manuel Klimekd52a3b82014-08-05 09:45:53 +0000691 EXPECT_FALSE(notMatchesWithCuda("__attribute__((global)) void f() {}",
Manuel Klimek3fe8a382014-08-25 11:23:50 +0000692 hasAttr(clang::attr::CUDAGlobal)));
Manuel Klimekd52a3b82014-08-05 09:45:53 +0000693}
694
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000695// Implements a run method that returns whether BoundNodes contains a
696// Decl bound to Id that can be dynamically cast to T.
697// Optionally checks that the check succeeded a specific number of times.
698template <typename T>
699class VerifyIdIsBoundTo : public BoundNodesCallback {
700public:
701 // Create an object that checks that a node of type \c T was bound to \c Id.
702 // Does not check for a certain number of matches.
703 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
704 : Id(Id), ExpectedCount(-1), Count(0) {}
705
706 // Create an object that checks that a node of type \c T was bound to \c Id.
707 // Checks that there were exactly \c ExpectedCount matches.
708 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
709 : Id(Id), ExpectedCount(ExpectedCount), 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 was exactly one match with the name \c ExpectedName.
713 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimekb64d6b72013-03-14 16:33:21 +0000714 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
715 int ExpectedCount = 1)
716 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
717 ExpectedName(ExpectedName) {}
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000718
Craig Toppera798a9d2014-03-02 09:32:10 +0000719 void onEndOfTranslationUnit() override {
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000720 if (ExpectedCount != -1)
721 EXPECT_EQ(ExpectedCount, Count);
722 if (!ExpectedName.empty())
723 EXPECT_EQ(ExpectedName, Name);
Peter Collingbourne2b9471302013-11-07 22:30:32 +0000724 Count = 0;
725 Name.clear();
726 }
727
728 ~VerifyIdIsBoundTo() {
729 EXPECT_EQ(0, Count);
730 EXPECT_EQ("", Name);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000731 }
732
Fariborz Jahanian5afc8692014-10-01 16:56:40 +0000733 virtual bool run(const BoundNodes *Nodes) override {
Peter Collingbourne093a7292013-11-06 00:27:07 +0000734 const BoundNodes::IDToNodeMap &M = Nodes->getMap();
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000735 if (Nodes->getNodeAs<T>(Id)) {
736 ++Count;
737 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
738 Name = Named->getNameAsString();
739 } else if (const NestedNameSpecifier *NNS =
740 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
741 llvm::raw_string_ostream OS(Name);
742 NNS->print(OS, PrintingPolicy(LangOptions()));
743 }
Peter Collingbourne093a7292013-11-06 00:27:07 +0000744 BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
745 EXPECT_NE(M.end(), I);
746 if (I != M.end())
747 EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000748 return true;
749 }
Craig Topper416fa342014-06-08 08:38:12 +0000750 EXPECT_TRUE(M.count(Id) == 0 ||
751 M.find(Id)->second.template get<T>() == nullptr);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000752 return false;
753 }
754
Fariborz Jahanian5afc8692014-10-01 16:56:40 +0000755 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) override {
Daniel Jaspere9aa6872012-10-29 10:48:25 +0000756 return run(Nodes);
757 }
758
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000759private:
760 const std::string Id;
761 const int ExpectedCount;
762 int Count;
763 const std::string ExpectedName;
764 std::string Name;
765};
766
767TEST(HasDescendant, MatchesDescendantTypes) {
768 EXPECT_TRUE(matches("void f() { int i = 3; }",
769 decl(hasDescendant(loc(builtinType())))));
770 EXPECT_TRUE(matches("void f() { int i = 3; }",
771 stmt(hasDescendant(builtinType()))));
772
773 EXPECT_TRUE(matches("void f() { int i = 3; }",
774 stmt(hasDescendant(loc(builtinType())))));
775 EXPECT_TRUE(matches("void f() { int i = 3; }",
776 stmt(hasDescendant(qualType(builtinType())))));
777
778 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
779 stmt(hasDescendant(isInteger()))));
780
781 EXPECT_TRUE(matchAndVerifyResultTrue(
782 "void f() { int a; float c; int d; int e; }",
783 functionDecl(forEachDescendant(
784 varDecl(hasDescendant(isInteger())).bind("x"))),
785 new VerifyIdIsBoundTo<Decl>("x", 3)));
786}
787
788TEST(HasDescendant, MatchesDescendantsOfTypes) {
789 EXPECT_TRUE(matches("void f() { int*** i; }",
790 qualType(hasDescendant(builtinType()))));
791 EXPECT_TRUE(matches("void f() { int*** i; }",
792 qualType(hasDescendant(
793 pointerType(pointee(builtinType()))))));
794 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikieb61d0872013-02-18 19:04:16 +0000795 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000796
797 EXPECT_TRUE(matchAndVerifyResultTrue(
798 "void f() { int*** i; }",
799 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
800 new VerifyIdIsBoundTo<Type>("x", 2)));
801}
802
803TEST(Has, MatchesChildrenOfTypes) {
804 EXPECT_TRUE(matches("int i;",
805 varDecl(hasName("i"), has(isInteger()))));
806 EXPECT_TRUE(notMatches("int** i;",
807 varDecl(hasName("i"), has(isInteger()))));
808 EXPECT_TRUE(matchAndVerifyResultTrue(
809 "int (*f)(float, int);",
810 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
811 new VerifyIdIsBoundTo<QualType>("x", 2)));
812}
813
814TEST(Has, MatchesChildTypes) {
815 EXPECT_TRUE(matches(
816 "int* i;",
817 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
818 EXPECT_TRUE(notMatches(
819 "int* i;",
820 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
821}
822
Daniel Jasper1dad1832012-07-10 20:20:19 +0000823TEST(Enum, DoesNotMatchClasses) {
824 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
825}
826
827TEST(Enum, MatchesEnums) {
828 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
829}
830
831TEST(EnumConstant, Matches) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000832 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000833 EXPECT_TRUE(matches("enum X{ A };", Matcher));
834 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
835 EXPECT_TRUE(notMatches("enum X {};", Matcher));
836}
837
Manuel Klimek04616e42012-07-06 05:48:52 +0000838TEST(StatementMatcher, Has) {
839 StatementMatcher HasVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000840 expr(hasType(pointsTo(recordDecl(hasName("X")))),
841 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000842
843 EXPECT_TRUE(matches(
844 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
845 EXPECT_TRUE(notMatches(
846 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
847}
848
849TEST(StatementMatcher, HasDescendant) {
850 StatementMatcher HasDescendantVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000851 expr(hasType(pointsTo(recordDecl(hasName("X")))),
852 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000853
854 EXPECT_TRUE(matches(
855 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
856 HasDescendantVariableI));
857 EXPECT_TRUE(notMatches(
858 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
859 HasDescendantVariableI));
860}
861
862TEST(TypeMatcher, MatchesClassType) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000863 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000864
865 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
866 EXPECT_TRUE(notMatches("class A {};", TypeA));
867
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000868 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000869
870 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
871 TypeDerivedFromA));
872 EXPECT_TRUE(notMatches("class A {};", TypeA));
873
874 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000875 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000876
877 EXPECT_TRUE(
878 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
879}
880
Manuel Klimek04616e42012-07-06 05:48:52 +0000881TEST(Matcher, BindMatchedNodes) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000882 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000883
884 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000885 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000886
887 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000888 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000889
890 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000891 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000892
893 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
894 TypeAHasClassB,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000895 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000896
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000897 StatementMatcher MethodX =
898 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek04616e42012-07-06 05:48:52 +0000899
900 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
901 MethodX,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000902 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000903}
904
905TEST(Matcher, BindTheSameNameInAlternatives) {
906 StatementMatcher matcher = anyOf(
907 binaryOperator(hasOperatorName("+"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000908 hasLHS(expr().bind("x")),
Daniel Jasper1dad1832012-07-10 20:20:19 +0000909 hasRHS(integerLiteral(equals(0)))),
910 binaryOperator(hasOperatorName("+"),
911 hasLHS(integerLiteral(equals(0))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000912 hasRHS(expr().bind("x"))));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000913
914 EXPECT_TRUE(matchAndVerifyResultTrue(
915 // The first branch of the matcher binds x to 0 but then fails.
916 // The second branch binds x to f() and succeeds.
917 "int f() { return 0 + f(); }",
918 matcher,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000919 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000920}
921
Manuel Klimekfdf98762012-08-30 19:41:06 +0000922TEST(Matcher, BindsIDForMemoizedResults) {
923 // Using the same matcher in two match expressions will make memoization
924 // kick in.
925 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
926 EXPECT_TRUE(matchAndVerifyResultTrue(
927 "class A { class B { class X {}; }; };",
928 DeclarationMatcher(anyOf(
929 recordDecl(hasName("A"), hasDescendant(ClassX)),
930 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000931 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimekfdf98762012-08-30 19:41:06 +0000932}
933
Daniel Jasper856194d02012-12-03 15:43:25 +0000934TEST(HasDeclaration, HasDeclarationOfEnumType) {
935 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
936 expr(hasType(pointsTo(
937 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
938}
939
Edwin Vaneed936452013-02-25 14:32:42 +0000940TEST(HasDeclaration, HasGetDeclTraitTest) {
941 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
942 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
943 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
944}
945
Edwin Vane2c197e02013-02-19 17:14:34 +0000946TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
947 EXPECT_TRUE(matches("typedef int X; X a;",
948 varDecl(hasName("a"),
949 hasType(typedefType(hasDeclaration(decl()))))));
950
951 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
952}
953
Edwin Vanef901b712013-02-25 14:49:29 +0000954TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
955 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
956 varDecl(hasType(templateSpecializationType(
957 hasDeclaration(namedDecl(hasName("A"))))))));
958}
959
Manuel Klimek04616e42012-07-06 05:48:52 +0000960TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000961 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000962 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000963 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000964 EXPECT_TRUE(
965 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000966 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000967 EXPECT_TRUE(
968 matches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000969 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000970}
971
972TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000973 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000974 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000975 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000976 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000977 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000978 EXPECT_TRUE(
979 matches("class X {}; void y() { X *x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000980 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000981}
982
983TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000984 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000985 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000986 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000987 EXPECT_TRUE(
988 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000989 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000990}
991
992TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000993 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000994 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000995 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000996 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000997 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000998}
999
Manuel Klimekc16c6522013-06-20 13:08:29 +00001000TEST(HasTypeLoc, MatchesDeclaratorDecls) {
1001 EXPECT_TRUE(matches("int x;",
1002 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
1003
1004 // Make sure we don't crash on implicit constructors.
1005 EXPECT_TRUE(notMatches("class X {}; X x;",
1006 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
1007}
1008
Manuel Klimek04616e42012-07-06 05:48:52 +00001009TEST(Matcher, Call) {
1010 // FIXME: Do we want to overload Call() to directly take
Daniel Jasper1dad1832012-07-10 20:20:19 +00001011 // Matcher<Decl>, too?
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001012 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001013
1014 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
1015 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
1016
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001017 StatementMatcher MethodOnY =
1018 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001019
1020 EXPECT_TRUE(
1021 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1022 MethodOnY));
1023 EXPECT_TRUE(
1024 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1025 MethodOnY));
1026 EXPECT_TRUE(
1027 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1028 MethodOnY));
1029 EXPECT_TRUE(
1030 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1031 MethodOnY));
1032 EXPECT_TRUE(
1033 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1034 MethodOnY));
1035
1036 StatementMatcher MethodOnYPointer =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001037 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001038
1039 EXPECT_TRUE(
1040 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1041 MethodOnYPointer));
1042 EXPECT_TRUE(
1043 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1044 MethodOnYPointer));
1045 EXPECT_TRUE(
1046 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1047 MethodOnYPointer));
1048 EXPECT_TRUE(
1049 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1050 MethodOnYPointer));
1051 EXPECT_TRUE(
1052 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1053 MethodOnYPointer));
1054}
1055
Daniel Jasper5901e472012-10-01 13:40:41 +00001056TEST(Matcher, Lambda) {
Richard Smith3d584b02014-02-06 21:49:08 +00001057 EXPECT_TRUE(matches("auto f = [] (int i) { return i; };",
Daniel Jasper5901e472012-10-01 13:40:41 +00001058 lambdaExpr()));
1059}
1060
1061TEST(Matcher, ForRange) {
Daniel Jasper6f595392012-10-01 15:05:34 +00001062 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
1063 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00001064 forRangeStmt()));
1065 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
1066 forRangeStmt()));
1067}
1068
Alexander Kornienko9e41b5c2014-06-29 22:18:53 +00001069TEST(Matcher, SubstNonTypeTemplateParm) {
1070 EXPECT_FALSE(matches("template<int N>\n"
1071 "struct A { static const int n = 0; };\n"
1072 "struct B : public A<42> {};",
1073 substNonTypeTemplateParmExpr()));
1074 EXPECT_TRUE(matches("template<int N>\n"
1075 "struct A { static const int n = N; };\n"
1076 "struct B : public A<42> {};",
1077 substNonTypeTemplateParmExpr()));
1078}
1079
Daniel Jasper5901e472012-10-01 13:40:41 +00001080TEST(Matcher, UserDefinedLiteral) {
1081 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
1082 " return i + 1;"
1083 "}"
1084 "char c = 'a'_inc;",
1085 userDefinedLiteral()));
1086}
1087
Daniel Jasper87c3d362012-09-20 14:12:57 +00001088TEST(Matcher, FlowControl) {
1089 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
1090 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
1091 continueStmt()));
1092 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
1093 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
1094 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
1095}
1096
Daniel Jasper1dad1832012-07-10 20:20:19 +00001097TEST(HasType, MatchesAsString) {
1098 EXPECT_TRUE(
1099 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001100 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001101 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001102 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001103 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001104 fieldDecl(hasType(asString("ns::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001105 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
David Blaikieabe1a392014-04-02 05:58:29 +00001106 fieldDecl(hasType(asString("struct (anonymous namespace)::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001107}
1108
Manuel Klimek04616e42012-07-06 05:48:52 +00001109TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001110 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001111 // Unary operator
1112 EXPECT_TRUE(matches("class Y { }; "
1113 "bool operator!(Y x) { return false; }; "
1114 "Y y; bool c = !y;", OpCall));
1115 // No match -- special operators like "new", "delete"
1116 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1117 // we need to figure out include paths in the test.
1118 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1119 // "class Y { }; "
1120 // "void *operator new(size_t size) { return 0; } "
1121 // "Y *y = new Y;", OpCall));
1122 EXPECT_TRUE(notMatches("class Y { }; "
1123 "void operator delete(void *p) { } "
1124 "void a() {Y *y = new Y; delete y;}", OpCall));
1125 // Binary operator
1126 EXPECT_TRUE(matches("class Y { }; "
1127 "bool operator&&(Y x, Y y) { return true; }; "
1128 "Y a; Y b; bool c = a && b;",
1129 OpCall));
1130 // No match -- normal operator, not an overloaded one.
1131 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1132 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1133}
1134
1135TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1136 StatementMatcher OpCallAndAnd =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001137 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek04616e42012-07-06 05:48:52 +00001138 EXPECT_TRUE(matches("class Y { }; "
1139 "bool operator&&(Y x, Y y) { return true; }; "
1140 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1141 StatementMatcher OpCallLessLess =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001142 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek04616e42012-07-06 05:48:52 +00001143 EXPECT_TRUE(notMatches("class Y { }; "
1144 "bool operator&&(Y x, Y y) { return true; }; "
1145 "Y a; Y b; bool c = a && b;",
1146 OpCallLessLess));
Benjamin Kramer09514492014-07-14 14:05:02 +00001147 StatementMatcher OpStarCall =
1148 operatorCallExpr(hasOverloadedOperatorName("*"));
1149 EXPECT_TRUE(matches("class Y; int operator*(Y &); void f(Y &y) { *y; }",
1150 OpStarCall));
Edwin Vane0a4836e2013-03-06 17:02:57 +00001151 DeclarationMatcher ClassWithOpStar =
1152 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1153 EXPECT_TRUE(matches("class Y { int operator*(); };",
1154 ClassWithOpStar));
1155 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1156 ClassWithOpStar)) ;
Benjamin Kramer09514492014-07-14 14:05:02 +00001157 DeclarationMatcher AnyOpStar = functionDecl(hasOverloadedOperatorName("*"));
1158 EXPECT_TRUE(matches("class Y; int operator*(Y &);", AnyOpStar));
1159 EXPECT_TRUE(matches("class Y { int operator*(); };", AnyOpStar));
Manuel Klimek04616e42012-07-06 05:48:52 +00001160}
1161
Daniel Jasper0f9f0192012-11-15 03:29:05 +00001162TEST(Matcher, NestedOverloadedOperatorCalls) {
1163 EXPECT_TRUE(matchAndVerifyResultTrue(
1164 "class Y { }; "
1165 "Y& operator&&(Y& x, Y& y) { return x; }; "
1166 "Y a; Y b; Y c; Y d = a && b && c;",
1167 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1168 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1169 EXPECT_TRUE(matches(
1170 "class Y { }; "
1171 "Y& operator&&(Y& x, Y& y) { return x; }; "
1172 "Y a; Y b; Y c; Y d = a && b && c;",
1173 operatorCallExpr(hasParent(operatorCallExpr()))));
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(hasDescendant(operatorCallExpr()))));
1179}
1180
Manuel Klimek04616e42012-07-06 05:48:52 +00001181TEST(Matcher, ThisPointerType) {
Manuel Klimek86f8bbc2012-07-24 13:37:29 +00001182 StatementMatcher MethodOnY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001183 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001184
1185 EXPECT_TRUE(
1186 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1187 MethodOnY));
1188 EXPECT_TRUE(
1189 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1190 MethodOnY));
1191 EXPECT_TRUE(
1192 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1193 MethodOnY));
1194 EXPECT_TRUE(
1195 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1196 MethodOnY));
1197 EXPECT_TRUE(
1198 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1199 MethodOnY));
1200
1201 EXPECT_TRUE(matches(
1202 "class Y {"
1203 " public: virtual void x();"
1204 "};"
1205 "class X : public Y {"
1206 " public: virtual void x();"
1207 "};"
1208 "void z() { X *x; x->Y::x(); }", MethodOnY));
1209}
1210
1211TEST(Matcher, VariableUsage) {
1212 StatementMatcher Reference =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001213 declRefExpr(to(
1214 varDecl(hasInitializer(
1215 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001216
1217 EXPECT_TRUE(matches(
1218 "class Y {"
1219 " public:"
1220 " bool x() const;"
1221 "};"
1222 "void z(const Y &y) {"
1223 " bool b = y.x();"
1224 " if (b) {}"
1225 "}", Reference));
1226
1227 EXPECT_TRUE(notMatches(
1228 "class Y {"
1229 " public:"
1230 " bool x() const;"
1231 "};"
1232 "void z(const Y &y) {"
1233 " bool b = y.x();"
1234 "}", Reference));
1235}
1236
Samuel Benzaquenf56a2992014-06-05 18:22:14 +00001237TEST(Matcher, VarDecl_Storage) {
1238 auto M = varDecl(hasName("X"), hasLocalStorage());
1239 EXPECT_TRUE(matches("void f() { int X; }", M));
1240 EXPECT_TRUE(notMatches("int X;", M));
1241 EXPECT_TRUE(notMatches("void f() { static int X; }", M));
1242
1243 M = varDecl(hasName("X"), hasGlobalStorage());
1244 EXPECT_TRUE(notMatches("void f() { int X; }", M));
1245 EXPECT_TRUE(matches("int X;", M));
1246 EXPECT_TRUE(matches("void f() { static int X; }", M));
1247}
1248
Manuel Klimek61379422012-12-04 14:42:08 +00001249TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001250 EXPECT_TRUE(matches(
1251 "void f(int i) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001252 varDecl(hasName("i"))));
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001253}
1254
Manuel Klimek04616e42012-07-06 05:48:52 +00001255TEST(Matcher, CalledVariable) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001256 StatementMatcher CallOnVariableY =
1257 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001258
1259 EXPECT_TRUE(matches(
1260 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1261 EXPECT_TRUE(matches(
1262 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1263 EXPECT_TRUE(matches(
1264 "class Y { public: void x(); };"
1265 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1266 EXPECT_TRUE(matches(
1267 "class Y { public: void x(); };"
1268 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1269 EXPECT_TRUE(notMatches(
1270 "class Y { public: void x(); };"
1271 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1272 CallOnVariableY));
1273}
1274
Daniel Jasper1dad1832012-07-10 20:20:19 +00001275TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1276 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1277 unaryExprOrTypeTraitExpr()));
1278 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1279 alignOfExpr(anything())));
1280 // FIXME: Uncomment once alignof is enabled.
1281 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1282 // unaryExprOrTypeTraitExpr()));
1283 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1284 // sizeOfExpr()));
1285}
1286
1287TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1288 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1289 hasArgumentOfType(asString("int")))));
1290 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1291 hasArgumentOfType(asString("float")))));
1292 EXPECT_TRUE(matches(
1293 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001294 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001295 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001296 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001297}
1298
Manuel Klimek04616e42012-07-06 05:48:52 +00001299TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001300 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001301}
1302
1303TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001304 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001305}
1306
1307TEST(MemberExpression, MatchesVariable) {
1308 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001309 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001310 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001311 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001312 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001313 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001314}
1315
1316TEST(MemberExpression, MatchesStaticVariable) {
1317 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001318 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001319 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001320 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001321 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001322 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001323}
1324
Daniel Jasper4e566c42012-07-12 08:50:38 +00001325TEST(IsInteger, MatchesIntegers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001326 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1327 EXPECT_TRUE(matches(
1328 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1329 callExpr(hasArgument(0, declRefExpr(
1330 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001331}
1332
1333TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001334 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001335 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001336 callExpr(hasArgument(0, declRefExpr(
1337 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001338}
1339
Manuel Klimek04616e42012-07-06 05:48:52 +00001340TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1341 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001342 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001343 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001344 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001345 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001346 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001347}
1348
1349TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1350 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001351 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001352 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001353 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001354 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001355 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001356}
1357
1358TEST(IsArrow, MatchesMemberCallsViaArrow) {
1359 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001360 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001361 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001362 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001363 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001364 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001365}
1366
1367TEST(Callee, MatchesDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001368 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001369
1370 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1371 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1372}
1373
1374TEST(Callee, MatchesMemberExpressions) {
1375 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001376 callExpr(callee(memberExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001377 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001378 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001379}
1380
1381TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001382 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001383
1384 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1385 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1386
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +00001387 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
1388 llvm::Triple::Win32) {
1389 // FIXME: Make this work for MSVC.
1390 // Dependent contexts, but a non-dependent call.
1391 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1392 CallFunctionF));
1393 EXPECT_TRUE(
1394 matches("void f(); template <int N> struct S { void g() { f(); } };",
1395 CallFunctionF));
1396 }
Manuel Klimek04616e42012-07-06 05:48:52 +00001397
1398 // Depedent calls don't match.
1399 EXPECT_TRUE(
1400 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1401 CallFunctionF));
1402 EXPECT_TRUE(
1403 notMatches("void f(int);"
1404 "template <typename T> struct S { void g(T t) { f(t); } };",
1405 CallFunctionF));
1406}
1407
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001408TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1409 EXPECT_TRUE(
1410 matches("template <typename T> void f(T t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001411 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001412}
1413
1414TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1415 EXPECT_TRUE(
1416 notMatches("void f(double d); void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001417 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001418}
1419
1420TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1421 EXPECT_TRUE(
1422 notMatches("void g(); template <typename T> void f(T t) {}"
1423 "template <> void f(int t) { g(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001424 functionTemplateDecl(hasName("f"),
1425 hasDescendant(declRefExpr(to(
1426 functionDecl(hasName("g"))))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001427}
1428
Manuel Klimek04616e42012-07-06 05:48:52 +00001429TEST(Matcher, Argument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001430 StatementMatcher CallArgumentY = callExpr(
1431 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001432
1433 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1434 EXPECT_TRUE(
1435 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1436 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1437
Daniel Jasper848cbe12012-09-18 13:09:13 +00001438 StatementMatcher WrongIndex = callExpr(
1439 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001440 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1441}
1442
1443TEST(Matcher, AnyArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001444 StatementMatcher CallArgumentY = callExpr(
1445 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001446 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1447 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1448 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1449}
1450
1451TEST(Matcher, ArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001452 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001453
1454 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1455 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1456 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1457}
1458
Daniel Jasper9f501292012-12-04 11:54:27 +00001459TEST(Matcher, ParameterCount) {
1460 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1461 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1462 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1463 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1464 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1465}
1466
Manuel Klimek04616e42012-07-06 05:48:52 +00001467TEST(Matcher, References) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001468 DeclarationMatcher ReferenceClassX = varDecl(
1469 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001470 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1471 ReferenceClassX));
1472 EXPECT_TRUE(
1473 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
Michael Hanc90d12d2013-09-11 15:53:29 +00001474 // The match here is on the implicit copy constructor code for
1475 // class X, not on code 'X x = y'.
Manuel Klimek04616e42012-07-06 05:48:52 +00001476 EXPECT_TRUE(
Michael Hanc90d12d2013-09-11 15:53:29 +00001477 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1478 EXPECT_TRUE(
1479 notMatches("class X {}; extern X x;", ReferenceClassX));
Manuel Klimek04616e42012-07-06 05:48:52 +00001480 EXPECT_TRUE(
1481 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1482}
1483
Edwin Vane0a4836e2013-03-06 17:02:57 +00001484TEST(QualType, hasCanonicalType) {
1485 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1486 "int a;"
1487 "int_ref b = a;",
1488 varDecl(hasType(qualType(referenceType())))));
1489 EXPECT_TRUE(
1490 matches("typedef int &int_ref;"
1491 "int a;"
1492 "int_ref b = a;",
1493 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1494}
1495
Edwin Vane119d3df2013-04-02 18:15:55 +00001496TEST(QualType, hasLocalQualifiers) {
1497 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1498 varDecl(hasType(hasLocalQualifiers()))));
1499 EXPECT_TRUE(matches("int *const j = nullptr;",
1500 varDecl(hasType(hasLocalQualifiers()))));
1501 EXPECT_TRUE(matches("int *volatile k;",
1502 varDecl(hasType(hasLocalQualifiers()))));
1503 EXPECT_TRUE(notMatches("int m;",
1504 varDecl(hasType(hasLocalQualifiers()))));
1505}
1506
Manuel Klimek04616e42012-07-06 05:48:52 +00001507TEST(HasParameter, CallsInnerMatcher) {
1508 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001509 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001510 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001511 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001512}
1513
1514TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1515 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001516 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001517}
1518
1519TEST(HasType, MatchesParameterVariableTypesStrictly) {
1520 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001521 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001522 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001523 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001524 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001525 methodDecl(hasParameter(0,
1526 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001527 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001528 methodDecl(hasParameter(0,
1529 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001530}
1531
1532TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1533 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001534 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001535 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001536 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001537}
1538
Daniel Jasper1dad1832012-07-10 20:20:19 +00001539TEST(Returns, MatchesReturnTypes) {
1540 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001541 functionDecl(returns(asString("int")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001542 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001543 functionDecl(returns(asString("float")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001544 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001545 functionDecl(returns(hasDeclaration(
1546 recordDecl(hasName("Y")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001547}
1548
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001549TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001550 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1551 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1552 functionDecl(isExternC())));
1553 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001554}
1555
Samuel Benzaquen8e7f9962014-08-15 14:20:59 +00001556TEST(IsDeleted, MatchesDeletedFunctionDeclarations) {
1557 EXPECT_TRUE(
1558 notMatches("void Func();", functionDecl(hasName("Func"), isDeleted())));
1559 EXPECT_TRUE(matches("void Func() = delete;",
1560 functionDecl(hasName("Func"), isDeleted())));
1561}
1562
Manuel Klimek04616e42012-07-06 05:48:52 +00001563TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1564 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001565 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001566}
1567
1568TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1569 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001570 methodDecl(hasAnyParameter(hasType(pointsTo(
1571 recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001572}
1573
Alp Toker8db6e7a2014-01-05 06:38:57 +00001574TEST(HasName, MatchesParameterVariableDeclarations) {
Manuel Klimek04616e42012-07-06 05:48:52 +00001575 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001576 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001577 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001578 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001579}
1580
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001581TEST(Matcher, MatchesClassTemplateSpecialization) {
1582 EXPECT_TRUE(matches("template<typename T> struct A {};"
1583 "template<> struct A<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001584 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001585 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001586 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001587 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001588 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001589}
1590
Manuel Klimekc16c6522013-06-20 13:08:29 +00001591TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1592 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1593 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1594}
1595
1596TEST(ParmVarDecl, MatchesParmVars) {
1597 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1598 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1599}
1600
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001601TEST(Matcher, MatchesTypeTemplateArgument) {
1602 EXPECT_TRUE(matches(
1603 "template<typename T> struct B {};"
1604 "B<int> b;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001605 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001606 asString("int"))))));
1607}
1608
1609TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1610 EXPECT_TRUE(matches(
1611 "struct B { int next; };"
1612 "template<int(B::*next_ptr)> struct A {};"
1613 "A<&B::next> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001614 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1615 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper0c303372012-09-29 15:55:18 +00001616
1617 EXPECT_TRUE(notMatches(
1618 "template <typename T> struct A {};"
1619 "A<int> a;",
1620 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1621 refersToDeclaration(decl())))));
Peter Collingbourne564597f2014-02-20 19:18:03 +00001622
1623 EXPECT_TRUE(matches(
1624 "struct B { int next; };"
1625 "template<int(B::*next_ptr)> struct A {};"
1626 "A<&B::next> a;",
1627 templateSpecializationType(hasAnyTemplateArgument(isExpr(
1628 hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))));
1629
1630 EXPECT_TRUE(notMatches(
1631 "template <typename T> struct A {};"
1632 "A<int> a;",
1633 templateSpecializationType(hasAnyTemplateArgument(
1634 refersToDeclaration(decl())))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001635}
1636
1637TEST(Matcher, MatchesSpecificArgument) {
1638 EXPECT_TRUE(matches(
1639 "template<typename T, typename U> class A {};"
1640 "A<bool, int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001641 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001642 1, refersToType(asString("int"))))));
1643 EXPECT_TRUE(notMatches(
1644 "template<typename T, typename U> class A {};"
1645 "A<int, bool> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001646 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001647 1, refersToType(asString("int"))))));
Peter Collingbourne564597f2014-02-20 19:18:03 +00001648
1649 EXPECT_TRUE(matches(
1650 "template<typename T, typename U> class A {};"
1651 "A<bool, int> a;",
1652 templateSpecializationType(hasTemplateArgument(
1653 1, refersToType(asString("int"))))));
1654 EXPECT_TRUE(notMatches(
1655 "template<typename T, typename U> class A {};"
1656 "A<int, bool> a;",
1657 templateSpecializationType(hasTemplateArgument(
1658 1, refersToType(asString("int"))))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001659}
1660
Manuel Klimek7735e402014-10-09 13:06:22 +00001661TEST(TemplateArgument, Matches) {
1662 EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;",
1663 classTemplateSpecializationDecl(
1664 hasAnyTemplateArgument(templateArgument()))));
1665 EXPECT_TRUE(matches(
1666 "template<typename T> struct C {}; C<int> c;",
1667 templateSpecializationType(hasAnyTemplateArgument(templateArgument()))));
1668}
1669
1670TEST(TemplateArgumentCountIs, Matches) {
1671 EXPECT_TRUE(
1672 matches("template<typename T> struct C {}; C<int> c;",
1673 classTemplateSpecializationDecl(templateArgumentCountIs(1))));
1674 EXPECT_TRUE(
1675 notMatches("template<typename T> struct C {}; C<int> c;",
1676 classTemplateSpecializationDecl(templateArgumentCountIs(2))));
1677
1678 EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;",
1679 templateSpecializationType(templateArgumentCountIs(1))));
1680 EXPECT_TRUE(
1681 notMatches("template<typename T> struct C {}; C<int> c;",
1682 templateSpecializationType(templateArgumentCountIs(2))));
1683}
1684
1685TEST(IsIntegral, Matches) {
1686 EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;",
1687 classTemplateSpecializationDecl(
1688 hasAnyTemplateArgument(isIntegral()))));
1689 EXPECT_TRUE(notMatches("template<typename T> struct C {}; C<int> c;",
1690 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1691 templateArgument(isIntegral())))));
1692}
1693
1694TEST(RefersToIntegralType, Matches) {
1695 EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;",
1696 classTemplateSpecializationDecl(
1697 hasAnyTemplateArgument(refersToIntegralType(
1698 asString("int"))))));
1699 EXPECT_TRUE(notMatches("template<unsigned T> struct C {}; C<42> c;",
1700 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1701 refersToIntegralType(asString("int"))))));
1702}
1703
1704TEST(EqualsIntegralValue, Matches) {
1705 EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;",
1706 classTemplateSpecializationDecl(
1707 hasAnyTemplateArgument(equalsIntegralValue("42")))));
1708 EXPECT_TRUE(matches("template<int T> struct C {}; C<-42> c;",
1709 classTemplateSpecializationDecl(
1710 hasAnyTemplateArgument(equalsIntegralValue("-42")))));
1711 EXPECT_TRUE(matches("template<int T> struct C {}; C<-0042> c;",
1712 classTemplateSpecializationDecl(
1713 hasAnyTemplateArgument(equalsIntegralValue("-34")))));
1714 EXPECT_TRUE(notMatches("template<int T> struct C {}; C<42> c;",
1715 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1716 equalsIntegralValue("0042")))));
1717}
1718
Daniel Jasper639522c2013-02-25 12:02:08 +00001719TEST(Matcher, MatchesAccessSpecDecls) {
1720 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1721 EXPECT_TRUE(
1722 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1723 EXPECT_TRUE(
1724 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1725 EXPECT_TRUE(
1726 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1727
1728 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1729}
1730
Edwin Vane37ee1d72013-04-09 20:46:36 +00001731TEST(Matcher, MatchesVirtualMethod) {
1732 EXPECT_TRUE(matches("class X { virtual int f(); };",
1733 methodDecl(isVirtual(), hasName("::X::f"))));
1734 EXPECT_TRUE(notMatches("class X { int f(); };",
1735 methodDecl(isVirtual())));
1736}
1737
Dmitri Gribenko51c1b552014-02-24 09:27:46 +00001738TEST(Matcher, MatchesPureMethod) {
1739 EXPECT_TRUE(matches("class X { virtual int f() = 0; };",
1740 methodDecl(isPure(), hasName("::X::f"))));
1741 EXPECT_TRUE(notMatches("class X { int f(); };",
1742 methodDecl(isPure())));
1743}
1744
Edwin Vanefc4f7dc2013-05-09 17:00:17 +00001745TEST(Matcher, MatchesConstMethod) {
1746 EXPECT_TRUE(matches("struct A { void foo() const; };",
1747 methodDecl(isConst())));
1748 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1749 methodDecl(isConst())));
1750}
1751
Edwin Vane37ee1d72013-04-09 20:46:36 +00001752TEST(Matcher, MatchesOverridingMethod) {
1753 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1754 "class Y : public X { int f(); };",
1755 methodDecl(isOverride(), hasName("::Y::f"))));
1756 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1757 "class Y : public X { int f(); };",
1758 methodDecl(isOverride(), hasName("::X::f"))));
1759 EXPECT_TRUE(notMatches("class X { int f(); }; "
1760 "class Y : public X { int f(); };",
1761 methodDecl(isOverride())));
1762 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1763 methodDecl(isOverride())));
1764}
1765
Manuel Klimek04616e42012-07-06 05:48:52 +00001766TEST(Matcher, ConstructorCall) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001767 StatementMatcher Constructor = constructExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001768
1769 EXPECT_TRUE(
1770 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1771 EXPECT_TRUE(
1772 matches("class X { public: X(); }; void x() { X x = X(); }",
1773 Constructor));
1774 EXPECT_TRUE(
1775 matches("class X { public: X(int); }; void x() { X x = 0; }",
1776 Constructor));
1777 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1778}
1779
1780TEST(Matcher, ConstructorArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001781 StatementMatcher Constructor = constructExpr(
1782 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001783
1784 EXPECT_TRUE(
1785 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1786 Constructor));
1787 EXPECT_TRUE(
1788 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1789 Constructor));
1790 EXPECT_TRUE(
1791 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1792 Constructor));
1793 EXPECT_TRUE(
1794 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1795 Constructor));
1796
Daniel Jasper848cbe12012-09-18 13:09:13 +00001797 StatementMatcher WrongIndex = constructExpr(
1798 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001799 EXPECT_TRUE(
1800 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1801 WrongIndex));
1802}
1803
1804TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001805 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001806
1807 EXPECT_TRUE(
1808 matches("class X { public: X(int); }; void x() { X x(0); }",
1809 Constructor1Arg));
1810 EXPECT_TRUE(
1811 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1812 Constructor1Arg));
1813 EXPECT_TRUE(
1814 matches("class X { public: X(int); }; void x() { X x = 0; }",
1815 Constructor1Arg));
1816 EXPECT_TRUE(
1817 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1818 Constructor1Arg));
1819}
1820
Peter Collingbourne1fec3df2014-02-06 21:52:24 +00001821TEST(Matcher, ConstructorListInitialization) {
1822 StatementMatcher ConstructorListInit = constructExpr(isListInitialization());
1823
1824 EXPECT_TRUE(
1825 matches("class X { public: X(int); }; void x() { X x{0}; }",
1826 ConstructorListInit));
1827 EXPECT_FALSE(
1828 matches("class X { public: X(int); }; void x() { X x(0); }",
1829 ConstructorListInit));
1830}
1831
Manuel Klimek7fca93b2012-10-23 10:40:50 +00001832TEST(Matcher,ThisExpr) {
1833 EXPECT_TRUE(
1834 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1835 EXPECT_TRUE(
1836 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1837}
1838
Manuel Klimek04616e42012-07-06 05:48:52 +00001839TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001840 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001841
1842 std::string ClassString = "class string { public: string(); ~string(); }; ";
1843
1844 EXPECT_TRUE(
1845 matches(ClassString +
1846 "string GetStringByValue();"
1847 "void FunctionTakesString(string s);"
1848 "void run() { FunctionTakesString(GetStringByValue()); }",
1849 TempExpression));
1850
1851 EXPECT_TRUE(
1852 notMatches(ClassString +
1853 "string* GetStringPointer(); "
1854 "void FunctionTakesStringPtr(string* s);"
1855 "void run() {"
1856 " string* s = GetStringPointer();"
1857 " FunctionTakesStringPtr(GetStringPointer());"
1858 " FunctionTakesStringPtr(s);"
1859 "}",
1860 TempExpression));
1861
1862 EXPECT_TRUE(
1863 notMatches("class no_dtor {};"
1864 "no_dtor GetObjByValue();"
1865 "void ConsumeObj(no_dtor param);"
1866 "void run() { ConsumeObj(GetObjByValue()); }",
1867 TempExpression));
1868}
1869
Sam Panzer68a35af2012-08-24 22:04:44 +00001870TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1871 std::string ClassString =
1872 "class string { public: string(); int length(); }; ";
1873
1874 EXPECT_TRUE(
1875 matches(ClassString +
1876 "string GetStringByValue();"
1877 "void FunctionTakesString(string s);"
1878 "void run() { FunctionTakesString(GetStringByValue()); }",
1879 materializeTemporaryExpr()));
1880
1881 EXPECT_TRUE(
1882 notMatches(ClassString +
1883 "string* GetStringPointer(); "
1884 "void FunctionTakesStringPtr(string* s);"
1885 "void run() {"
1886 " string* s = GetStringPointer();"
1887 " FunctionTakesStringPtr(GetStringPointer());"
1888 " FunctionTakesStringPtr(s);"
1889 "}",
1890 materializeTemporaryExpr()));
1891
1892 EXPECT_TRUE(
1893 notMatches(ClassString +
1894 "string GetStringByValue();"
1895 "void run() { int k = GetStringByValue().length(); }",
1896 materializeTemporaryExpr()));
1897
1898 EXPECT_TRUE(
1899 notMatches(ClassString +
1900 "string GetStringByValue();"
1901 "void run() { GetStringByValue(); }",
1902 materializeTemporaryExpr()));
1903}
1904
Manuel Klimek04616e42012-07-06 05:48:52 +00001905TEST(ConstructorDeclaration, SimpleCase) {
1906 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001907 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001908 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001909 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001910}
1911
1912TEST(ConstructorDeclaration, IsImplicit) {
1913 // This one doesn't match because the constructor is not added by the
1914 // compiler (it is not needed).
1915 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001916 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001917 // The compiler added the implicit default constructor.
1918 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001919 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001920 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001921 constructorDecl(unless(isImplicit()))));
Joey Gouly0d9a2b22014-05-16 19:31:08 +00001922 // The compiler added an implicit assignment operator.
1923 EXPECT_TRUE(matches("struct A { int x; } a = {0}, b = a; void f() { a = b; }",
1924 methodDecl(isImplicit(), hasName("operator="))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001925}
1926
Daniel Jasper1dad1832012-07-10 20:20:19 +00001927TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1928 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001929 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001930}
1931
1932TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001933 EXPECT_TRUE(notMatches("class Foo {};",
1934 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001935}
1936
Manuel Klimek04616e42012-07-06 05:48:52 +00001937TEST(HasAnyConstructorInitializer, SimpleCase) {
1938 EXPECT_TRUE(notMatches(
1939 "class Foo { Foo() { } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001940 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001941 EXPECT_TRUE(matches(
1942 "class Foo {"
1943 " Foo() : foo_() { }"
1944 " int foo_;"
1945 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001946 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001947}
1948
1949TEST(HasAnyConstructorInitializer, ForField) {
1950 static const char Code[] =
1951 "class Baz { };"
1952 "class Foo {"
1953 " Foo() : foo_() { }"
1954 " Baz foo_;"
1955 " Baz bar_;"
1956 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001957 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1958 forField(hasType(recordDecl(hasName("Baz"))))))));
1959 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001960 forField(hasName("foo_"))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001961 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1962 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001963}
1964
1965TEST(HasAnyConstructorInitializer, WithInitializer) {
1966 static const char Code[] =
1967 "class Foo {"
1968 " Foo() : foo_(0) { }"
1969 " int foo_;"
1970 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001971 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001972 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001973 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001974 withInitializer(integerLiteral(equals(1)))))));
1975}
1976
1977TEST(HasAnyConstructorInitializer, IsWritten) {
1978 static const char Code[] =
1979 "struct Bar { Bar(){} };"
1980 "class Foo {"
1981 " Foo() : foo_() { }"
1982 " Bar foo_;"
1983 " Bar bar_;"
1984 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001985 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001986 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001987 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001988 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001989 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001990 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1991}
1992
1993TEST(Matcher, NewExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001994 StatementMatcher New = newExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001995
1996 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1997 EXPECT_TRUE(
1998 matches("class X { public: X(); }; void x() { new X(); }", New));
1999 EXPECT_TRUE(
2000 matches("class X { public: X(int); }; void x() { new X(0); }", New));
2001 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
2002}
2003
2004TEST(Matcher, NewExpressionArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002005 StatementMatcher New = constructExpr(
2006 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002007
2008 EXPECT_TRUE(
2009 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
2010 New));
2011 EXPECT_TRUE(
2012 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
2013 New));
2014 EXPECT_TRUE(
2015 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
2016 New));
2017
Daniel Jasper848cbe12012-09-18 13:09:13 +00002018 StatementMatcher WrongIndex = constructExpr(
2019 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002020 EXPECT_TRUE(
2021 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
2022 WrongIndex));
2023}
2024
2025TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002026 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00002027
2028 EXPECT_TRUE(
2029 matches("class X { public: X(int); }; void x() { new X(0); }", New));
2030 EXPECT_TRUE(
2031 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
2032 New));
2033}
2034
Daniel Jasper1dad1832012-07-10 20:20:19 +00002035TEST(Matcher, DeleteExpression) {
2036 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002037 deleteExpr()));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002038}
2039
Manuel Klimek04616e42012-07-06 05:48:52 +00002040TEST(Matcher, DefaultArgument) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002041 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00002042
2043 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
2044 EXPECT_TRUE(
2045 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
2046 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
2047}
2048
2049TEST(Matcher, StringLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002050 StatementMatcher Literal = stringLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00002051 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
2052 // wide string
2053 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
2054 // with escaped characters
2055 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
2056 // no matching -- though the data type is the same, there is no string literal
2057 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
2058}
2059
2060TEST(Matcher, CharacterLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002061 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00002062 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
2063 // wide character
2064 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
2065 // wide character, Hex encoded, NOT MATCHED!
2066 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
2067 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
2068}
2069
2070TEST(Matcher, IntegerLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002071 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00002072 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
2073 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
2074 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
2075 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
2076
2077 // Non-matching cases (character literals, float and double)
2078 EXPECT_TRUE(notMatches("int i = L'a';",
2079 HasIntLiteral)); // this is actually a character
2080 // literal cast to int
2081 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
2082 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
2083 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
2084}
2085
Daniel Jasper91f1c8c2013-07-26 18:52:58 +00002086TEST(Matcher, FloatLiterals) {
2087 StatementMatcher HasFloatLiteral = floatLiteral();
2088 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
2089 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
2090 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
2091 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
2092 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
2093
2094 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
2095}
2096
Daniel Jasper5901e472012-10-01 13:40:41 +00002097TEST(Matcher, NullPtrLiteral) {
2098 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
2099}
2100
Daniel Jasper87c3d362012-09-20 14:12:57 +00002101TEST(Matcher, AsmStatement) {
2102 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
2103}
2104
Manuel Klimek04616e42012-07-06 05:48:52 +00002105TEST(Matcher, Conditions) {
2106 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
2107
2108 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
2109 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
2110 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
2111 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
2112 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
2113}
2114
Manuel Klimek909b5c942014-05-27 10:04:12 +00002115TEST(IfStmt, ChildTraversalMatchers) {
2116 EXPECT_TRUE(matches("void f() { if (false) true; else false; }",
2117 ifStmt(hasThen(boolLiteral(equals(true))))));
2118 EXPECT_TRUE(notMatches("void f() { if (false) false; else true; }",
2119 ifStmt(hasThen(boolLiteral(equals(true))))));
2120 EXPECT_TRUE(matches("void f() { if (false) false; else true; }",
2121 ifStmt(hasElse(boolLiteral(equals(true))))));
2122 EXPECT_TRUE(notMatches("void f() { if (false) true; else false; }",
2123 ifStmt(hasElse(boolLiteral(equals(true))))));
2124}
2125
Manuel Klimek04616e42012-07-06 05:48:52 +00002126TEST(MatchBinaryOperator, HasOperatorName) {
2127 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
2128
2129 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
2130 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
2131}
2132
2133TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
2134 StatementMatcher OperatorTrueFalse =
2135 binaryOperator(hasLHS(boolLiteral(equals(true))),
2136 hasRHS(boolLiteral(equals(false))));
2137
2138 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
2139 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
2140 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
2141}
2142
2143TEST(MatchBinaryOperator, HasEitherOperand) {
2144 StatementMatcher HasOperand =
2145 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
2146
2147 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
2148 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
2149 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
2150}
2151
2152TEST(Matcher, BinaryOperatorTypes) {
2153 // Integration test that verifies the AST provides all binary operators in
2154 // a way we expect.
2155 // FIXME: Operator ','
2156 EXPECT_TRUE(
2157 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
2158 EXPECT_TRUE(
2159 matches("bool b; bool c = (b = true);",
2160 binaryOperator(hasOperatorName("="))));
2161 EXPECT_TRUE(
2162 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
2163 EXPECT_TRUE(
2164 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
2165 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
2166 EXPECT_TRUE(
2167 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
2168 EXPECT_TRUE(
2169 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
2170 EXPECT_TRUE(
2171 matches("int i = 1; int j = (i <<= 2);",
2172 binaryOperator(hasOperatorName("<<="))));
2173 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
2174 EXPECT_TRUE(
2175 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
2176 EXPECT_TRUE(
2177 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
2178 EXPECT_TRUE(
2179 matches("int i = 1; int j = (i >>= 2);",
2180 binaryOperator(hasOperatorName(">>="))));
2181 EXPECT_TRUE(
2182 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
2183 EXPECT_TRUE(
2184 matches("int i = 42; int j = (i ^= 42);",
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("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
2193 EXPECT_TRUE(
2194 matches("bool b = true && false;",
2195 binaryOperator(hasOperatorName("&&"))));
2196 EXPECT_TRUE(
2197 matches("bool b = true; bool c = (b &= false);",
2198 binaryOperator(hasOperatorName("&="))));
2199 EXPECT_TRUE(
2200 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
2201 EXPECT_TRUE(
2202 matches("bool b = true || false;",
2203 binaryOperator(hasOperatorName("||"))));
2204 EXPECT_TRUE(
2205 matches("bool b = true; bool c = (b |= false);",
2206 binaryOperator(hasOperatorName("|="))));
2207 EXPECT_TRUE(
2208 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2209 EXPECT_TRUE(
2210 matches("int i = 42; int j = (i *= 23);",
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("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2229 binaryOperator(hasOperatorName("->*"))));
2230 EXPECT_TRUE(
2231 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2232 binaryOperator(hasOperatorName(".*"))));
2233
2234 // Member expressions as operators are not supported in matches.
2235 EXPECT_TRUE(
2236 notMatches("struct A { void x(A *a) { a->x(this); } };",
2237 binaryOperator(hasOperatorName("->"))));
2238
2239 // Initializer assignments are not represented as operator equals.
2240 EXPECT_TRUE(
2241 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2242
2243 // Array indexing is not represented as operator.
2244 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2245
2246 // Overloaded operators do not match at all.
2247 EXPECT_TRUE(notMatches(
2248 "struct A { bool operator&&(const A &a) const { return false; } };"
2249 "void x() { A a, b; a && b; }",
2250 binaryOperator()));
2251}
2252
2253TEST(MatchUnaryOperator, HasOperatorName) {
2254 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2255
2256 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2257 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2258}
2259
2260TEST(MatchUnaryOperator, HasUnaryOperand) {
2261 StatementMatcher OperatorOnFalse =
2262 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2263
2264 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2265 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2266}
2267
2268TEST(Matcher, UnaryOperatorTypes) {
2269 // Integration test that verifies the AST provides all unary operators in
2270 // a way we expect.
2271 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2272 EXPECT_TRUE(
2273 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2274 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2275 EXPECT_TRUE(
2276 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2277 EXPECT_TRUE(
2278 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2279 EXPECT_TRUE(
2280 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2281 EXPECT_TRUE(
2282 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2283 EXPECT_TRUE(
2284 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2285 EXPECT_TRUE(
2286 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2287 EXPECT_TRUE(
2288 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2289
2290 // We don't match conversion operators.
2291 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2292
2293 // Function calls are not represented as operator.
2294 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2295
2296 // Overloaded operators do not match at all.
2297 // FIXME: We probably want to add that.
2298 EXPECT_TRUE(notMatches(
2299 "struct A { bool operator!() const { return false; } };"
2300 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2301}
2302
2303TEST(Matcher, ConditionalOperator) {
2304 StatementMatcher Conditional = conditionalOperator(
2305 hasCondition(boolLiteral(equals(true))),
2306 hasTrueExpression(boolLiteral(equals(false))));
2307
2308 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2309 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2310 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2311
2312 StatementMatcher ConditionalFalse = conditionalOperator(
2313 hasFalseExpression(boolLiteral(equals(false))));
2314
2315 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2316 EXPECT_TRUE(
2317 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2318}
2319
Daniel Jasper1dad1832012-07-10 20:20:19 +00002320TEST(ArraySubscriptMatchers, ArraySubscripts) {
2321 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2322 arraySubscriptExpr()));
2323 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2324 arraySubscriptExpr()));
2325}
2326
2327TEST(ArraySubscriptMatchers, ArrayIndex) {
2328 EXPECT_TRUE(matches(
2329 "int i[2]; void f() { i[1] = 1; }",
2330 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2331 EXPECT_TRUE(matches(
2332 "int i[2]; void f() { 1[i] = 1; }",
2333 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2334 EXPECT_TRUE(notMatches(
2335 "int i[2]; void f() { i[1] = 1; }",
2336 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2337}
2338
2339TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2340 EXPECT_TRUE(matches(
2341 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002342 arraySubscriptExpr(hasBase(implicitCastExpr(
2343 hasSourceExpression(declRefExpr()))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002344}
2345
Manuel Klimek04616e42012-07-06 05:48:52 +00002346TEST(Matcher, HasNameSupportsNamespaces) {
2347 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002348 recordDecl(hasName("a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002349 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002350 recordDecl(hasName("::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002351 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002352 recordDecl(hasName("b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002353 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002354 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002355 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002356 recordDecl(hasName("c::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002357 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002358 recordDecl(hasName("a::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002359 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002360 recordDecl(hasName("a::b::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002361 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002362 recordDecl(hasName("::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002363 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002364 recordDecl(hasName("::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002365 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002366 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002367 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002368 recordDecl(hasName("a+b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002369 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002370 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002371}
2372
2373TEST(Matcher, HasNameSupportsOuterClasses) {
2374 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002375 matches("class A { class B { class C; }; };",
2376 recordDecl(hasName("A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002377 EXPECT_TRUE(
2378 matches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002379 recordDecl(hasName("::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002380 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002381 matches("class A { class B { class C; }; };",
2382 recordDecl(hasName("B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002383 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002384 matches("class A { class B { class C; }; };",
2385 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002386 EXPECT_TRUE(
2387 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002388 recordDecl(hasName("c::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002389 EXPECT_TRUE(
2390 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002391 recordDecl(hasName("A::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002392 EXPECT_TRUE(
2393 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002394 recordDecl(hasName("A::B::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002395 EXPECT_TRUE(
2396 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002397 recordDecl(hasName("::C"))));
2398 EXPECT_TRUE(
2399 notMatches("class A { class B { class C; }; };",
2400 recordDecl(hasName("::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002401 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002402 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002403 EXPECT_TRUE(
2404 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002405 recordDecl(hasName("A+B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002406}
2407
2408TEST(Matcher, IsDefinition) {
2409 DeclarationMatcher DefinitionOfClassA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002410 recordDecl(hasName("A"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002411 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2412 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2413
2414 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002415 varDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002416 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2417 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2418
2419 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002420 methodDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002421 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2422 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2423}
2424
2425TEST(Matcher, OfClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002426 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +00002427 ofClass(hasName("X")))));
2428
2429 EXPECT_TRUE(
2430 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2431 EXPECT_TRUE(
2432 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2433 Constructor));
2434 EXPECT_TRUE(
2435 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2436 Constructor));
2437}
2438
2439TEST(Matcher, VisitsTemplateInstantiations) {
2440 EXPECT_TRUE(matches(
2441 "class A { public: void x(); };"
2442 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002443 "void f() { B<A> b; b.y(); }",
2444 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002445
2446 EXPECT_TRUE(matches(
2447 "class A { public: void x(); };"
2448 "class C {"
2449 " public:"
2450 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2451 "};"
2452 "void f() {"
2453 " C::B<A> b; b.y();"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002454 "}",
2455 recordDecl(hasName("C"),
2456 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002457}
2458
Daniel Jasper1dad1832012-07-10 20:20:19 +00002459TEST(Matcher, HandlesNullQualTypes) {
2460 // FIXME: Add a Type matcher so we can replace uses of this
2461 // variable with Type(True())
2462 const TypeMatcher AnyType = anything();
2463
2464 // We don't really care whether this matcher succeeds; we're testing that
2465 // it completes without crashing.
2466 EXPECT_TRUE(matches(
2467 "struct A { };"
2468 "template <typename T>"
2469 "void f(T t) {"
2470 " T local_t(t /* this becomes a null QualType in the AST */);"
2471 "}"
2472 "void g() {"
2473 " f(0);"
2474 "}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002475 expr(hasType(TypeMatcher(
Daniel Jasper1dad1832012-07-10 20:20:19 +00002476 anyOf(
2477 TypeMatcher(hasDeclaration(anything())),
2478 pointsTo(AnyType),
2479 references(AnyType)
2480 // Other QualType matchers should go here.
2481 ))))));
2482}
2483
Manuel Klimek04616e42012-07-06 05:48:52 +00002484// For testing AST_MATCHER_P().
Daniel Jasper1dad1832012-07-10 20:20:19 +00002485AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002486 // Make sure all special variables are used: node, match_finder,
2487 // bound_nodes_builder, and the parameter named 'AMatcher'.
2488 return AMatcher.matches(Node, Finder, Builder);
2489}
2490
2491TEST(AstMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002492 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002493
2494 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002495 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002496
2497 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002498 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002499
2500 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002501 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002502}
2503
2504AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +00002505 polymorphicHas,
2506 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2507 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002508 return Finder->matchesChildOf(
Manuel Klimekeb958de2012-09-05 12:12:07 +00002509 Node, AMatcher, Builder,
Manuel Klimek04616e42012-07-06 05:48:52 +00002510 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2511 ASTMatchFinder::BK_First);
2512}
2513
2514TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002515 DeclarationMatcher HasClassB =
2516 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek04616e42012-07-06 05:48:52 +00002517
2518 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002519 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002520
2521 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002522 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002523
2524 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002525 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002526
2527 StatementMatcher StatementHasClassB =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002528 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002529
2530 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2531}
2532
2533TEST(For, FindsForLoops) {
2534 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2535 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper6f595392012-10-01 15:05:34 +00002536 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2537 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00002538 forStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002539}
2540
Daniel Jasper4e566c42012-07-12 08:50:38 +00002541TEST(For, ForLoopInternals) {
2542 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2543 forStmt(hasCondition(anything()))));
2544 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2545 forStmt(hasLoopInit(anything()))));
2546}
2547
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002548TEST(For, ForRangeLoopInternals) {
2549 EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2550 forRangeStmt(hasLoopVariable(anything()))));
Manuel Klimek86510812014-05-23 17:49:03 +00002551 EXPECT_TRUE(matches(
2552 "void f(){ int a[] {1, 2}; for (int i : a); }",
2553 forRangeStmt(hasRangeInit(declRefExpr(to(varDecl(hasName("a"))))))));
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002554}
2555
Daniel Jasper4e566c42012-07-12 08:50:38 +00002556TEST(For, NegativeForLoopInternals) {
2557 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002558 forStmt(hasCondition(expr()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002559 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2560 forStmt(hasLoopInit(anything()))));
2561}
2562
Manuel Klimek04616e42012-07-06 05:48:52 +00002563TEST(For, ReportsNoFalsePositives) {
2564 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2565 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2566}
2567
2568TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002569 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2570 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2571 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002572}
2573
2574TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2575 // It's not a compound statement just because there's "{}" in the source
2576 // text. This is an AST search, not grep.
2577 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002578 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002579 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002580 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002581}
2582
Daniel Jasper4e566c42012-07-12 08:50:38 +00002583TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002584 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002585 forStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002586 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002587 forStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002588 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002589 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002590 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002591 doStmt(hasBody(compoundStmt()))));
Manuel Klimek2af0a912014-05-27 07:45:18 +00002592 EXPECT_TRUE(matches("void f() { int p[2]; for (auto x : p) {} }",
2593 forRangeStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002594}
2595
2596TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2597 // The simplest case: every compound statement is in a function
2598 // definition, and the function body itself must be a compound
2599 // statement.
2600 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002601 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002602}
2603
2604TEST(HasAnySubstatement, IsNotRecursive) {
2605 // It's really "has any immediate substatement".
2606 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002607 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002608}
2609
2610TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2611 EXPECT_TRUE(matches("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, FindsSubstatementBetweenOthers) {
2616 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002617 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002618}
2619
2620TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2621 EXPECT_TRUE(matches("void f() { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002622 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002623 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002624 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002625}
2626
2627TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2628 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002629 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002630 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002631 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002632 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002633 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002634}
2635
2636TEST(StatementCountIs, WorksWithMultipleStatements) {
2637 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002638 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002639}
2640
2641TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2642 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002643 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002644 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002645 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002646 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002647 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002648 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002649 compoundStmt(statementCountIs(4))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002650}
2651
2652TEST(Member, WorksInSimplestCase) {
2653 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002654 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002655}
2656
2657TEST(Member, DoesNotMatchTheBaseExpression) {
2658 // Don't pick out the wrong part of the member expression, this should
2659 // be checking the member (name) only.
2660 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002661 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002662}
2663
2664TEST(Member, MatchesInMemberFunctionCall) {
2665 EXPECT_TRUE(matches("void f() {"
2666 " struct { void first() {}; } s;"
2667 " s.first();"
2668 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002669 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002670}
2671
Daniel Jasperb0c7b612012-10-23 15:46:39 +00002672TEST(Member, MatchesMember) {
2673 EXPECT_TRUE(matches(
2674 "struct A { int i; }; void f() { A a; a.i = 2; }",
2675 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2676 EXPECT_TRUE(notMatches(
2677 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2678 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2679}
2680
Daniel Jasper639522c2013-02-25 12:02:08 +00002681TEST(Member, UnderstandsAccess) {
2682 EXPECT_TRUE(matches(
2683 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2684 EXPECT_TRUE(notMatches(
2685 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2686 EXPECT_TRUE(notMatches(
2687 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2688
2689 EXPECT_TRUE(notMatches(
2690 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2691 EXPECT_TRUE(notMatches(
2692 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2693 EXPECT_TRUE(matches(
2694 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2695
2696 EXPECT_TRUE(notMatches(
2697 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2698 EXPECT_TRUE(matches("class A { protected: int i; };",
2699 fieldDecl(isProtected(), hasName("i"))));
2700 EXPECT_TRUE(notMatches(
2701 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2702
2703 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2704 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2705 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2706 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2707}
2708
Dmitri Gribenko06963042012-08-18 00:29:27 +00002709TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper5901e472012-10-01 13:40:41 +00002710 // Fails in C++11 mode
2711 EXPECT_TRUE(matchesConditionally(
2712 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2713 "class X { void *operator new(std::size_t); };",
2714 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002715
2716 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002717 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002718
Daniel Jasper5901e472012-10-01 13:40:41 +00002719 // Fails in C++11 mode
2720 EXPECT_TRUE(matchesConditionally(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002721 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2722 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper5901e472012-10-01 13:40:41 +00002723 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002724}
2725
Manuel Klimek04616e42012-07-06 05:48:52 +00002726TEST(HasObjectExpression, DoesNotMatchMember) {
2727 EXPECT_TRUE(notMatches(
2728 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002729 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002730}
2731
2732TEST(HasObjectExpression, MatchesBaseOfVariable) {
2733 EXPECT_TRUE(matches(
2734 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002735 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002736 EXPECT_TRUE(matches(
2737 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002738 memberExpr(hasObjectExpression(
2739 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002740}
2741
2742TEST(HasObjectExpression,
2743 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2744 EXPECT_TRUE(matches(
2745 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002746 memberExpr(hasObjectExpression(
2747 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002748 EXPECT_TRUE(matches(
2749 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002750 memberExpr(hasObjectExpression(
2751 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002752}
2753
2754TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002755 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2756 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2757 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2758 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002759}
2760
2761TEST(Field, MatchesField) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002762 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002763}
2764
2765TEST(IsConstQualified, MatchesConstInt) {
2766 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002767 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002768}
2769
2770TEST(IsConstQualified, MatchesConstPointer) {
2771 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002772 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002773}
2774
2775TEST(IsConstQualified, MatchesThroughTypedef) {
2776 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002777 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002778 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002779 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002780}
2781
2782TEST(IsConstQualified, DoesNotMatchInappropriately) {
2783 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002784 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002785 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002786 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002787}
2788
Sam Panzer80c13772012-08-16 16:58:10 +00002789TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002790 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2791 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2792 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2793 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002794}
2795TEST(CastExpression, MatchesImplicitCasts) {
2796 // This test creates an implicit cast from int to char.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002797 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002798 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002799 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002800}
2801
2802TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002803 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2804 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2805 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2806 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002807}
2808
Manuel Klimek04616e42012-07-06 05:48:52 +00002809TEST(ReinterpretCast, MatchesSimpleCase) {
2810 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002811 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002812}
2813
2814TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002815 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002816 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002817 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002818 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002819 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002820 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2821 "B b;"
2822 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002823 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002824}
2825
2826TEST(FunctionalCast, MatchesSimpleCase) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002827 std::string foo_class = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002828 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002829 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002830}
2831
2832TEST(FunctionalCast, DoesNotMatchOtherCasts) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002833 std::string FooClass = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002834 EXPECT_TRUE(
2835 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002836 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002837 EXPECT_TRUE(
2838 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002839 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002840}
2841
2842TEST(DynamicCast, MatchesSimpleCase) {
2843 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2844 "B b;"
2845 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002846 dynamicCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002847}
2848
2849TEST(StaticCast, MatchesSimpleCase) {
2850 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002851 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002852}
2853
2854TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002855 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002856 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002857 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002858 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002859 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002860 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2861 "B b;"
2862 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002863 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002864}
2865
Daniel Jasper417f7762012-09-18 13:36:17 +00002866TEST(CStyleCast, MatchesSimpleCase) {
2867 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2868}
2869
2870TEST(CStyleCast, DoesNotMatchOtherCasts) {
2871 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2872 "char q, *r = const_cast<char*>(&q);"
2873 "void* s = reinterpret_cast<char*>(&s);"
2874 "struct B { virtual ~B() {} }; struct D : B {};"
2875 "B b;"
2876 "D* t = dynamic_cast<D*>(&b);",
2877 cStyleCastExpr()));
2878}
2879
Manuel Klimek04616e42012-07-06 05:48:52 +00002880TEST(HasDestinationType, MatchesSimpleCase) {
2881 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002882 staticCastExpr(hasDestinationType(
2883 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002884}
2885
Sam Panzer80c13772012-08-16 16:58:10 +00002886TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2887 // This test creates an implicit const cast.
2888 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002889 implicitCastExpr(
2890 hasImplicitDestinationType(isInteger()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002891 // This test creates an implicit array-to-pointer cast.
2892 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002893 implicitCastExpr(hasImplicitDestinationType(
2894 pointsTo(TypeMatcher(anything()))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002895}
2896
2897TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2898 // This test creates an implicit cast from int to char.
2899 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002900 implicitCastExpr(hasImplicitDestinationType(
2901 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002902 // This test creates an implicit array-to-pointer cast.
2903 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002904 implicitCastExpr(hasImplicitDestinationType(
2905 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002906}
2907
2908TEST(ImplicitCast, MatchesSimpleCase) {
2909 // This test creates an implicit const cast.
2910 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002911 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002912 // This test creates an implicit cast from int to char.
2913 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002914 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002915 // This test creates an implicit array-to-pointer cast.
2916 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002917 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002918}
2919
2920TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002921 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer80c13772012-08-16 16:58:10 +00002922 // are present, and that it ignores explicit and paren casts.
2923
2924 // These two test cases have no casts.
2925 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002926 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002927 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002928 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002929
2930 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002931 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002932 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
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);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002936 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002937}
2938
2939TEST(IgnoringImpCasts, MatchesImpCasts) {
2940 // This test checks that ignoringImpCasts matches when implicit casts are
2941 // present and its inner matcher alone does not match.
2942 // Note that this test creates an implicit const cast.
2943 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002944 varDecl(hasInitializer(ignoringImpCasts(
2945 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002946 // This test creates an implict cast from int to char.
2947 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002948 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002949 integerLiteral(equals(0)))))));
2950}
2951
2952TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2953 // These tests verify that ignoringImpCasts does not match if the inner
2954 // matcher does not match.
2955 // Note that the first test creates an implicit const cast.
2956 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002957 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002958 unless(anything()))))));
2959 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002960 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002961 unless(anything()))))));
2962
2963 // These tests verify that ignoringImplictCasts does not look through explicit
2964 // casts or parentheses.
2965 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002966 varDecl(hasInitializer(ignoringImpCasts(
2967 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002968 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002969 varDecl(hasInitializer(ignoringImpCasts(
2970 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002971 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002972 varDecl(hasInitializer(ignoringImpCasts(
2973 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002974 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002975 varDecl(hasInitializer(ignoringImpCasts(
2976 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002977}
2978
2979TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2980 // This test verifies that expressions that do not have implicit casts
2981 // still match the inner matcher.
2982 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002983 varDecl(hasInitializer(ignoringImpCasts(
2984 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002985}
2986
2987TEST(IgnoringParenCasts, MatchesParenCasts) {
2988 // This test checks that ignoringParenCasts matches when parentheses and/or
2989 // casts are present and its inner matcher alone does not match.
2990 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002991 varDecl(hasInitializer(ignoringParenCasts(
2992 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002993 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002994 varDecl(hasInitializer(ignoringParenCasts(
2995 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002996
2997 // This test creates an implict cast from int to char in addition to the
2998 // parentheses.
2999 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003000 varDecl(hasInitializer(ignoringParenCasts(
3001 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003002
3003 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003004 varDecl(hasInitializer(ignoringParenCasts(
3005 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003006 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003007 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003008 integerLiteral(equals(0)))))));
3009}
3010
3011TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
3012 // This test verifies that expressions that do not have any casts still match.
3013 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003014 varDecl(hasInitializer(ignoringParenCasts(
3015 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003016}
3017
3018TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
3019 // These tests verify that ignoringImpCasts does not match if the inner
3020 // matcher does not match.
3021 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003022 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003023 unless(anything()))))));
3024
3025 // This test creates an implicit cast from int to char in addition to the
3026 // parentheses.
3027 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003028 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003029 unless(anything()))))));
3030
3031 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003032 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003033 unless(anything()))))));
3034}
3035
3036TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
3037 // This test checks that ignoringParenAndImpCasts matches when
3038 // parentheses and/or implicit casts are present and its inner matcher alone
3039 // does not match.
3040 // Note that this test creates an implicit const cast.
3041 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003042 varDecl(hasInitializer(ignoringParenImpCasts(
3043 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003044 // This test creates an implicit cast from int to char.
3045 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003046 varDecl(hasInitializer(ignoringParenImpCasts(
3047 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003048}
3049
3050TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
3051 // This test verifies that expressions that do not have parentheses or
3052 // implicit casts still match.
3053 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003054 varDecl(hasInitializer(ignoringParenImpCasts(
3055 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003056 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003057 varDecl(hasInitializer(ignoringParenImpCasts(
3058 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00003059}
3060
3061TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
3062 // These tests verify that ignoringParenImpCasts does not match if
3063 // the inner matcher does not match.
3064 // This test creates an implicit cast.
3065 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003066 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00003067 unless(anything()))))));
3068 // These tests verify that ignoringParenAndImplictCasts does not look
3069 // through explicit casts.
3070 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003071 varDecl(hasInitializer(ignoringParenImpCasts(
3072 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00003073 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003074 varDecl(hasInitializer(ignoringParenImpCasts(
3075 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00003076 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003077 varDecl(hasInitializer(ignoringParenImpCasts(
3078 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00003079}
3080
Manuel Klimeke9235692012-07-25 10:02:02 +00003081TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek04616e42012-07-06 05:48:52 +00003082 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
3083 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003084 implicitCastExpr(
3085 hasSourceExpression(constructExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003086}
3087
Manuel Klimeke9235692012-07-25 10:02:02 +00003088TEST(HasSourceExpression, MatchesExplicitCasts) {
3089 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003090 explicitCastExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003091 hasSourceExpression(hasDescendant(
Daniel Jasper848cbe12012-09-18 13:09:13 +00003092 expr(integerLiteral()))))));
Manuel Klimeke9235692012-07-25 10:02:02 +00003093}
3094
Manuel Klimek04616e42012-07-06 05:48:52 +00003095TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003096 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00003097}
3098
3099TEST(Statement, MatchesCompoundStatments) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003100 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00003101}
3102
3103TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003104 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00003105}
3106
3107TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003108 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00003109}
3110
Samuel Benzaquenf1066292014-04-02 13:12:14 +00003111TEST(ExprWithCleanups, MatchesExprWithCleanups) {
3112 EXPECT_TRUE(matches("struct Foo { ~Foo(); };"
3113 "const Foo f = Foo();",
3114 varDecl(hasInitializer(exprWithCleanups()))));
3115 EXPECT_FALSE(matches("struct Foo { };"
3116 "const Foo f = Foo();",
3117 varDecl(hasInitializer(exprWithCleanups()))));
3118}
3119
Daniel Jasper1dad1832012-07-10 20:20:19 +00003120TEST(InitListExpression, MatchesInitListExpression) {
3121 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
3122 initListExpr(hasType(asString("int [2]")))));
3123 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003124 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003125}
3126
3127TEST(UsingDeclaration, MatchesUsingDeclarations) {
3128 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
3129 usingDecl()));
3130}
3131
3132TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
3133 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
3134 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
3135}
3136
3137TEST(UsingDeclaration, MatchesSpecificTarget) {
3138 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
3139 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003140 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003141 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
3142 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003143 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003144}
3145
3146TEST(UsingDeclaration, ThroughUsingDeclaration) {
3147 EXPECT_TRUE(matches(
3148 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003149 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003150 EXPECT_TRUE(notMatches(
3151 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003152 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00003153}
3154
Benjamin Kramer73ef2162014-07-16 14:14:51 +00003155TEST(UsingDirectiveDeclaration, MatchesUsingNamespace) {
3156 EXPECT_TRUE(matches("namespace X { int x; } using namespace X;",
3157 usingDirectiveDecl()));
3158 EXPECT_FALSE(
3159 matches("namespace X { int x; } using X::x;", usingDirectiveDecl()));
3160}
3161
Sam Panzerd624bfb2012-08-16 17:20:59 +00003162TEST(SingleDecl, IsSingleDecl) {
3163 StatementMatcher SingleDeclStmt =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003164 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003165 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
3166 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
3167 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
3168 SingleDeclStmt));
3169}
3170
3171TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003172 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003173
3174 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003175 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003176 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003177 declStmt(containsDeclaration(0, MatchesInit),
3178 containsDeclaration(1, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003179 unsigned WrongIndex = 42;
3180 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003181 declStmt(containsDeclaration(WrongIndex,
Sam Panzerd624bfb2012-08-16 17:20:59 +00003182 MatchesInit))));
3183}
3184
3185TEST(DeclCount, DeclCountIsCorrect) {
3186 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003187 declStmt(declCountIs(2))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003188 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003189 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003190 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003191 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003192}
3193
Manuel Klimek04616e42012-07-06 05:48:52 +00003194TEST(While, MatchesWhileLoops) {
3195 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
3196 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
3197 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
3198}
3199
3200TEST(Do, MatchesDoLoops) {
3201 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
3202 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
3203}
3204
3205TEST(Do, DoesNotMatchWhileLoops) {
3206 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
3207}
3208
3209TEST(SwitchCase, MatchesCase) {
3210 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
3211 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
3212 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
3213 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
3214}
3215
Daniel Jasper87c3d362012-09-20 14:12:57 +00003216TEST(SwitchCase, MatchesSwitch) {
3217 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
3218 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
3219 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
3220 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
3221}
3222
Peter Collingbourne3154a102013-05-10 11:52:02 +00003223TEST(SwitchCase, MatchesEachCase) {
3224 EXPECT_TRUE(notMatches("void x() { switch(42); }",
3225 switchStmt(forEachSwitchCase(caseStmt()))));
3226 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
3227 switchStmt(forEachSwitchCase(caseStmt()))));
3228 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
3229 switchStmt(forEachSwitchCase(caseStmt()))));
3230 EXPECT_TRUE(notMatches(
3231 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3232 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3233 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3234 switchStmt(forEachSwitchCase(
3235 caseStmt(hasCaseConstant(integerLiteral()))))));
3236 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3237 switchStmt(forEachSwitchCase(
3238 caseStmt(hasCaseConstant(integerLiteral()))))));
3239 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3240 switchStmt(forEachSwitchCase(
3241 caseStmt(hasCaseConstant(integerLiteral()))))));
3242 EXPECT_TRUE(matchAndVerifyResultTrue(
3243 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3244 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3245 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3246}
3247
Manuel Klimekba46fc02013-07-19 11:50:54 +00003248TEST(ForEachConstructorInitializer, MatchesInitializers) {
3249 EXPECT_TRUE(matches(
3250 "struct X { X() : i(42), j(42) {} int i, j; };",
3251 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3252}
3253
Daniel Jasper87c3d362012-09-20 14:12:57 +00003254TEST(ExceptionHandling, SimpleCases) {
3255 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3256 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3257 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3258 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3259 throwExpr()));
3260 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3261 throwExpr()));
3262}
3263
Manuel Klimek04616e42012-07-06 05:48:52 +00003264TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3265 EXPECT_TRUE(notMatches(
3266 "void x() { if(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003267 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003268 EXPECT_TRUE(notMatches(
3269 "void x() { int x; if((x = 42)) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003270 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003271}
3272
3273TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3274 EXPECT_TRUE(matches(
3275 "void x() { if(int* a = 0) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003276 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003277}
3278
3279TEST(ForEach, BindsOneNode) {
3280 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003281 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003282 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003283}
3284
3285TEST(ForEach, BindsMultipleNodes) {
3286 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003287 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003288 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003289}
3290
3291TEST(ForEach, BindsRecursiveCombinations) {
3292 EXPECT_TRUE(matchAndVerifyResultTrue(
3293 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003294 recordDecl(hasName("C"),
3295 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003296 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003297}
3298
3299TEST(ForEachDescendant, BindsOneNode) {
3300 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003301 recordDecl(hasName("C"),
3302 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003303 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003304}
3305
Daniel Jasper94a56852012-11-16 18:39:22 +00003306TEST(ForEachDescendant, NestedForEachDescendant) {
3307 DeclarationMatcher m = recordDecl(
3308 isDefinition(), decl().bind("x"), hasName("C"));
3309 EXPECT_TRUE(matchAndVerifyResultTrue(
3310 "class A { class B { class C {}; }; };",
3311 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3312 new VerifyIdIsBoundTo<Decl>("x", "C")));
3313
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003314 // Check that a partial match of 'm' that binds 'x' in the
3315 // first part of anyOf(m, anything()) will not overwrite the
3316 // binding created by the earlier binding in the hasDescendant.
3317 EXPECT_TRUE(matchAndVerifyResultTrue(
3318 "class A { class B { class C {}; }; };",
3319 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3320 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper94a56852012-11-16 18:39:22 +00003321}
3322
Manuel Klimek04616e42012-07-06 05:48:52 +00003323TEST(ForEachDescendant, BindsMultipleNodes) {
3324 EXPECT_TRUE(matchAndVerifyResultTrue(
3325 "class C { class D { int x; int y; }; "
3326 " class E { class F { int y; int z; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003327 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003328 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003329}
3330
3331TEST(ForEachDescendant, BindsRecursiveCombinations) {
3332 EXPECT_TRUE(matchAndVerifyResultTrue(
3333 "class C { class D { "
3334 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003335 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3336 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003337 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003338}
3339
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003340TEST(ForEachDescendant, BindsCombinations) {
3341 EXPECT_TRUE(matchAndVerifyResultTrue(
3342 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3343 "(true) {} }",
3344 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3345 forEachDescendant(whileStmt().bind("while"))),
3346 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3347}
3348
3349TEST(Has, DoesNotDeleteBindings) {
3350 EXPECT_TRUE(matchAndVerifyResultTrue(
3351 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3352 new VerifyIdIsBoundTo<Decl>("x", 1)));
3353}
3354
3355TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3356 // Those matchers cover all the cases where an inner matcher is called
3357 // and there is not a 1:1 relationship between the match of the outer
3358 // matcher and the match of the inner matcher.
3359 // The pattern to look for is:
3360 // ... return InnerMatcher.matches(...); ...
3361 // In which case no special handling is needed.
3362 //
3363 // On the other hand, if there are multiple alternative matches
3364 // (for example forEach*) or matches might be discarded (for example has*)
3365 // the implementation must make sure that the discarded matches do not
3366 // affect the bindings.
3367 // When new such matchers are added, add a test here that:
3368 // - matches a simple node, and binds it as the first thing in the matcher:
3369 // recordDecl(decl().bind("x"), hasName("X")))
3370 // - uses the matcher under test afterwards in a way that not the first
3371 // alternative is matched; for anyOf, that means the first branch
3372 // would need to return false; for hasAncestor, it means that not
3373 // the direct parent matches the inner matcher.
3374
3375 EXPECT_TRUE(matchAndVerifyResultTrue(
3376 "class X { int y; };",
3377 recordDecl(
3378 recordDecl().bind("x"), hasName("::X"),
3379 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3380 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3381 EXPECT_TRUE(matchAndVerifyResultTrue(
3382 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3383 anyOf(unless(anything()), anything())),
3384 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3385 EXPECT_TRUE(matchAndVerifyResultTrue(
3386 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3387 classTemplateSpecializationDecl(
3388 decl().bind("x"),
3389 hasAnyTemplateArgument(refersToType(asString("int")))),
3390 new VerifyIdIsBoundTo<Decl>("x", 1)));
3391 EXPECT_TRUE(matchAndVerifyResultTrue(
3392 "class X { void f(); void g(); };",
3393 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3394 new VerifyIdIsBoundTo<Decl>("x", 1)));
3395 EXPECT_TRUE(matchAndVerifyResultTrue(
3396 "class X { X() : a(1), b(2) {} double a; int b; };",
3397 recordDecl(decl().bind("x"),
3398 has(constructorDecl(
3399 hasAnyConstructorInitializer(forField(hasName("b")))))),
3400 new VerifyIdIsBoundTo<Decl>("x", 1)));
3401 EXPECT_TRUE(matchAndVerifyResultTrue(
3402 "void x(int, int) { x(0, 42); }",
3403 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3404 new VerifyIdIsBoundTo<Expr>("x", 1)));
3405 EXPECT_TRUE(matchAndVerifyResultTrue(
3406 "void x(int, int y) {}",
3407 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3408 new VerifyIdIsBoundTo<Decl>("x", 1)));
3409 EXPECT_TRUE(matchAndVerifyResultTrue(
3410 "void x() { return; if (true) {} }",
3411 functionDecl(decl().bind("x"),
3412 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3413 new VerifyIdIsBoundTo<Decl>("x", 1)));
3414 EXPECT_TRUE(matchAndVerifyResultTrue(
3415 "namespace X { void b(int); void b(); }"
3416 "using X::b;",
3417 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3418 functionDecl(parameterCountIs(1))))),
3419 new VerifyIdIsBoundTo<Decl>("x", 1)));
3420 EXPECT_TRUE(matchAndVerifyResultTrue(
3421 "class A{}; class B{}; class C : B, A {};",
3422 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3423 new VerifyIdIsBoundTo<Decl>("x", 1)));
3424 EXPECT_TRUE(matchAndVerifyResultTrue(
3425 "class A{}; typedef A B; typedef A C; typedef A D;"
3426 "class E : A {};",
3427 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3428 new VerifyIdIsBoundTo<Decl>("x", 1)));
3429 EXPECT_TRUE(matchAndVerifyResultTrue(
3430 "class A { class B { void f() {} }; };",
3431 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3432 new VerifyIdIsBoundTo<Decl>("x", 1)));
3433 EXPECT_TRUE(matchAndVerifyResultTrue(
3434 "template <typename T> struct A { struct B {"
3435 " void f() { if(true) {} }"
3436 "}; };"
3437 "void t() { A<int>::B b; b.f(); }",
3438 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3439 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3440 EXPECT_TRUE(matchAndVerifyResultTrue(
3441 "class A {};",
3442 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3443 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimekba46fc02013-07-19 11:50:54 +00003444 EXPECT_TRUE(matchAndVerifyResultTrue(
3445 "class A { A() : s(), i(42) {} const char *s; int i; };",
3446 constructorDecl(hasName("::A::A"), decl().bind("x"),
3447 forEachConstructorInitializer(forField(hasName("i")))),
3448 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003449}
3450
Daniel Jasper33806cd2012-11-11 22:14:55 +00003451TEST(ForEachDescendant, BindsCorrectNodes) {
3452 EXPECT_TRUE(matchAndVerifyResultTrue(
3453 "class C { void f(); int i; };",
3454 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3455 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3456 EXPECT_TRUE(matchAndVerifyResultTrue(
3457 "class C { void f() {} int i; };",
3458 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3459 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3460}
3461
Manuel Klimekabf43712013-02-04 10:59:20 +00003462TEST(FindAll, BindsNodeOnMatch) {
3463 EXPECT_TRUE(matchAndVerifyResultTrue(
3464 "class A {};",
3465 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3466 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3467}
3468
3469TEST(FindAll, BindsDescendantNodeOnMatch) {
3470 EXPECT_TRUE(matchAndVerifyResultTrue(
3471 "class A { int a; int b; };",
3472 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3473 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3474}
3475
3476TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3477 EXPECT_TRUE(matchAndVerifyResultTrue(
3478 "class A { int a; int b; };",
3479 recordDecl(hasName("::A"),
3480 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3481 fieldDecl().bind("v"))))),
3482 new VerifyIdIsBoundTo<Decl>("v", 3)));
3483
3484 EXPECT_TRUE(matchAndVerifyResultTrue(
3485 "class A { class B {}; class C {}; };",
3486 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3487 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3488}
3489
Manuel Klimek88b95872013-02-04 09:42:38 +00003490TEST(EachOf, TriggersForEachMatch) {
3491 EXPECT_TRUE(matchAndVerifyResultTrue(
3492 "class A { int a; int b; };",
3493 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3494 has(fieldDecl(hasName("b")).bind("v")))),
3495 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3496}
3497
3498TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3499 EXPECT_TRUE(matchAndVerifyResultTrue(
3500 "class A { int a; int c; };",
3501 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3502 has(fieldDecl(hasName("b")).bind("v")))),
3503 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3504 EXPECT_TRUE(matchAndVerifyResultTrue(
3505 "class A { int c; int b; };",
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(notMatches(
3510 "class A { int c; int d; };",
3511 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3512 has(fieldDecl(hasName("b")).bind("v"))))));
3513}
Manuel Klimek04616e42012-07-06 05:48:52 +00003514
3515TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3516 // Make sure that we can both match the class by name (::X) and by the type
3517 // the template was instantiated with (via a field).
3518
3519 EXPECT_TRUE(matches(
3520 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003521 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003522
3523 EXPECT_TRUE(matches(
3524 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003525 recordDecl(isTemplateInstantiation(), hasDescendant(
3526 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003527}
3528
3529TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3530 EXPECT_TRUE(matches(
3531 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003532 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek04616e42012-07-06 05:48:52 +00003533 isTemplateInstantiation())));
3534}
3535
3536TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3537 EXPECT_TRUE(matches(
3538 "template <typename T> class X { T t; }; class A {};"
3539 "template class X<A>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003540 recordDecl(isTemplateInstantiation(), hasDescendant(
3541 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003542}
3543
3544TEST(IsTemplateInstantiation,
3545 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3546 EXPECT_TRUE(matches(
3547 "template <typename T> class X {};"
3548 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003549 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003550}
3551
3552TEST(IsTemplateInstantiation,
3553 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3554 EXPECT_TRUE(matches(
3555 "class A {};"
3556 "class X {"
3557 " template <typename U> class Y { U u; };"
3558 " Y<A> y;"
3559 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003560 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003561}
3562
3563TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3564 // FIXME: Figure out whether this makes sense. It doesn't affect the
3565 // normal use case as long as the uppermost instantiation always is marked
3566 // as template instantiation, but it might be confusing as a predicate.
3567 EXPECT_TRUE(matches(
3568 "class A {};"
3569 "template <typename T> class X {"
3570 " template <typename U> class Y { U u; };"
3571 " Y<T> y;"
3572 "}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003573 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003574}
3575
3576TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3577 EXPECT_TRUE(notMatches(
3578 "template <typename T> class X {}; class A {};"
3579 "template <> class X<A> {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003580 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003581}
3582
3583TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3584 EXPECT_TRUE(notMatches(
3585 "class A {}; class Y { A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003586 recordDecl(isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003587}
3588
Benjamin Kramer7ab84762014-09-03 12:08:14 +00003589TEST(IsInstantiated, MatchesInstantiation) {
3590 EXPECT_TRUE(
3591 matches("template<typename T> class A { T i; }; class Y { A<int> a; };",
3592 recordDecl(isInstantiated())));
3593}
3594
3595TEST(IsInstantiated, NotMatchesDefinition) {
3596 EXPECT_TRUE(notMatches("template<typename T> class A { T i; };",
3597 recordDecl(isInstantiated())));
3598}
3599
3600TEST(IsInTemplateInstantiation, MatchesInstantiationStmt) {
3601 EXPECT_TRUE(matches("template<typename T> struct A { A() { T i; } };"
3602 "class Y { A<int> a; }; Y y;",
3603 declStmt(isInTemplateInstantiation())));
3604}
3605
3606TEST(IsInTemplateInstantiation, NotMatchesDefinitionStmt) {
3607 EXPECT_TRUE(notMatches("template<typename T> struct A { void x() { T i; } };",
3608 declStmt(isInTemplateInstantiation())));
3609}
3610
3611TEST(IsInstantiated, MatchesFunctionInstantiation) {
3612 EXPECT_TRUE(
3613 matches("template<typename T> void A(T t) { T i; } void x() { A(0); }",
3614 functionDecl(isInstantiated())));
3615}
3616
3617TEST(IsInstantiated, NotMatchesFunctionDefinition) {
3618 EXPECT_TRUE(notMatches("template<typename T> void A(T t) { T i; }",
3619 varDecl(isInstantiated())));
3620}
3621
3622TEST(IsInTemplateInstantiation, MatchesFunctionInstantiationStmt) {
3623 EXPECT_TRUE(
3624 matches("template<typename T> void A(T t) { T i; } void x() { A(0); }",
3625 declStmt(isInTemplateInstantiation())));
3626}
3627
3628TEST(IsInTemplateInstantiation, NotMatchesFunctionDefinitionStmt) {
3629 EXPECT_TRUE(notMatches("template<typename T> void A(T t) { T i; }",
3630 declStmt(isInTemplateInstantiation())));
3631}
3632
3633TEST(IsInTemplateInstantiation, Sharing) {
3634 auto Matcher = binaryOperator(unless(isInTemplateInstantiation()));
3635 // FIXME: Node sharing is an implementation detail, exposing it is ugly
3636 // and makes the matcher behave in non-obvious ways.
3637 EXPECT_TRUE(notMatches(
3638 "int j; template<typename T> void A(T t) { j += 42; } void x() { A(0); }",
3639 Matcher));
3640 EXPECT_TRUE(matches(
3641 "int j; template<typename T> void A(T t) { j += t; } void x() { A(0); }",
3642 Matcher));
3643}
3644
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003645TEST(IsExplicitTemplateSpecialization,
3646 DoesNotMatchPrimaryTemplate) {
3647 EXPECT_TRUE(notMatches(
3648 "template <typename T> class X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003649 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003650 EXPECT_TRUE(notMatches(
3651 "template <typename T> void f(T t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003652 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003653}
3654
3655TEST(IsExplicitTemplateSpecialization,
3656 DoesNotMatchExplicitTemplateInstantiations) {
3657 EXPECT_TRUE(notMatches(
3658 "template <typename T> class X {};"
3659 "template class X<int>; extern template class X<long>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003660 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003661 EXPECT_TRUE(notMatches(
3662 "template <typename T> void f(T t) {}"
3663 "template void f(int t); extern template void f(long t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003664 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003665}
3666
3667TEST(IsExplicitTemplateSpecialization,
3668 DoesNotMatchImplicitTemplateInstantiations) {
3669 EXPECT_TRUE(notMatches(
3670 "template <typename T> class X {}; X<int> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003671 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003672 EXPECT_TRUE(notMatches(
3673 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003674 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003675}
3676
3677TEST(IsExplicitTemplateSpecialization,
3678 MatchesExplicitTemplateSpecializations) {
3679 EXPECT_TRUE(matches(
3680 "template <typename T> class X {};"
3681 "template<> class X<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003682 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003683 EXPECT_TRUE(matches(
3684 "template <typename T> void f(T t) {}"
3685 "template<> void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003686 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003687}
3688
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003689TEST(HasAncenstor, MatchesDeclarationAncestors) {
3690 EXPECT_TRUE(matches(
3691 "class A { class B { class C {}; }; };",
3692 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3693}
3694
3695TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3696 EXPECT_TRUE(notMatches(
3697 "class A { class B { class C {}; }; };",
3698 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3699}
3700
3701TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3702 EXPECT_TRUE(matches(
3703 "class A { class B { void f() { C c; } class C {}; }; };",
3704 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3705 hasAncestor(recordDecl(hasName("A"))))))));
3706}
3707
3708TEST(HasAncenstor, MatchesStatementAncestors) {
3709 EXPECT_TRUE(matches(
3710 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003711 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003712}
3713
3714TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3715 EXPECT_TRUE(matches(
3716 "void f() { if (true) { int x = 42; } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003717 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003718}
3719
3720TEST(HasAncestor, BindsRecursiveCombinations) {
3721 EXPECT_TRUE(matchAndVerifyResultTrue(
3722 "class C { class D { class E { class F { int y; }; }; }; };",
3723 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003724 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003725}
3726
3727TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3728 EXPECT_TRUE(matchAndVerifyResultTrue(
3729 "class C { class D { class E { class F { int y; }; }; }; };",
3730 fieldDecl(hasAncestor(
3731 decl(
3732 hasDescendant(recordDecl(isDefinition(),
3733 hasAncestor(recordDecl())))
3734 ).bind("d")
3735 )),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003736 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003737}
3738
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003739TEST(HasAncestor, MatchesClosestAncestor) {
3740 EXPECT_TRUE(matchAndVerifyResultTrue(
3741 "template <typename T> struct C {"
3742 " void f(int) {"
3743 " struct I { void g(T) { int x; } } i; i.g(42);"
3744 " }"
3745 "};"
3746 "template struct C<int>;",
3747 varDecl(hasName("x"),
3748 hasAncestor(functionDecl(hasParameter(
3749 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3750 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3751}
3752
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003753TEST(HasAncestor, MatchesInTemplateInstantiations) {
3754 EXPECT_TRUE(matches(
3755 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3756 "A<int>::B::C a;",
3757 fieldDecl(hasType(asString("int")),
3758 hasAncestor(recordDecl(hasName("A"))))));
3759}
3760
3761TEST(HasAncestor, MatchesInImplicitCode) {
3762 EXPECT_TRUE(matches(
3763 "struct X {}; struct A { A() {} X x; };",
3764 constructorDecl(
3765 hasAnyConstructorInitializer(withInitializer(expr(
3766 hasAncestor(recordDecl(hasName("A")))))))));
3767}
3768
Daniel Jasper632aea92012-10-22 16:26:51 +00003769TEST(HasParent, MatchesOnlyParent) {
3770 EXPECT_TRUE(matches(
3771 "void f() { if (true) { int x = 42; } }",
3772 compoundStmt(hasParent(ifStmt()))));
3773 EXPECT_TRUE(notMatches(
3774 "void f() { for (;;) { int x = 42; } }",
3775 compoundStmt(hasParent(ifStmt()))));
3776 EXPECT_TRUE(notMatches(
3777 "void f() { if (true) for (;;) { int x = 42; } }",
3778 compoundStmt(hasParent(ifStmt()))));
3779}
3780
Manuel Klimekc844a462012-12-06 14:42:48 +00003781TEST(HasAncestor, MatchesAllAncestors) {
3782 EXPECT_TRUE(matches(
3783 "template <typename T> struct C { static void f() { 42; } };"
3784 "void t() { C<int>::f(); }",
3785 integerLiteral(
3786 equals(42),
3787 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3788 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3789}
3790
3791TEST(HasParent, MatchesAllParents) {
3792 EXPECT_TRUE(matches(
3793 "template <typename T> struct C { static void f() { 42; } };"
3794 "void t() { C<int>::f(); }",
3795 integerLiteral(
3796 equals(42),
3797 hasParent(compoundStmt(hasParent(functionDecl(
3798 hasParent(recordDecl(isTemplateInstantiation())))))))));
3799 EXPECT_TRUE(matches(
3800 "template <typename T> struct C { static void f() { 42; } };"
3801 "void t() { C<int>::f(); }",
3802 integerLiteral(
3803 equals(42),
3804 hasParent(compoundStmt(hasParent(functionDecl(
3805 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3806 EXPECT_TRUE(matches(
3807 "template <typename T> struct C { static void f() { 42; } };"
3808 "void t() { C<int>::f(); }",
3809 integerLiteral(equals(42),
3810 hasParent(compoundStmt(allOf(
3811 hasParent(functionDecl(
3812 hasParent(recordDecl(isTemplateInstantiation())))),
3813 hasParent(functionDecl(hasParent(recordDecl(
3814 unless(isTemplateInstantiation())))))))))));
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003815 EXPECT_TRUE(
3816 notMatches("template <typename T> struct C { static void f() {} };"
3817 "void t() { C<int>::f(); }",
3818 compoundStmt(hasParent(recordDecl()))));
Manuel Klimekc844a462012-12-06 14:42:48 +00003819}
3820
Samuel Benzaquen3ca0a7b2014-06-13 13:31:40 +00003821TEST(HasParent, NoDuplicateParents) {
3822 class HasDuplicateParents : public BoundNodesCallback {
3823 public:
3824 bool run(const BoundNodes *Nodes) override { return false; }
3825 bool run(const BoundNodes *Nodes, ASTContext *Context) override {
3826 const Stmt *Node = Nodes->getNodeAs<Stmt>("node");
3827 std::set<const void *> Parents;
3828 for (const auto &Parent : Context->getParents(*Node)) {
3829 if (!Parents.insert(Parent.getMemoizationData()).second) {
3830 return true;
3831 }
3832 }
3833 return false;
3834 }
3835 };
3836 EXPECT_FALSE(matchAndVerifyResultTrue(
3837 "template <typename T> int Foo() { return 1 + 2; }\n"
3838 "int x = Foo<int>() + Foo<unsigned>();",
3839 stmt().bind("node"), new HasDuplicateParents()));
3840}
3841
Daniel Jasper516b02e2012-10-17 08:52:59 +00003842TEST(TypeMatching, MatchesTypes) {
3843 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3844}
3845
3846TEST(TypeMatching, MatchesArrayTypes) {
3847 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3848 EXPECT_TRUE(matches("int a[42];", arrayType()));
3849 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3850
3851 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3852 arrayType(hasElementType(builtinType()))));
3853
3854 EXPECT_TRUE(matches(
3855 "int const a[] = { 2, 3 };",
3856 qualType(arrayType(hasElementType(builtinType())))));
3857 EXPECT_TRUE(matches(
3858 "int const a[] = { 2, 3 };",
3859 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3860 EXPECT_TRUE(matches(
3861 "typedef const int T; T x[] = { 1, 2 };",
3862 qualType(isConstQualified(), arrayType())));
3863
3864 EXPECT_TRUE(notMatches(
3865 "int a[] = { 2, 3 };",
3866 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3867 EXPECT_TRUE(notMatches(
3868 "int a[] = { 2, 3 };",
3869 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3870 EXPECT_TRUE(notMatches(
3871 "int const a[] = { 2, 3 };",
3872 qualType(arrayType(hasElementType(builtinType())),
3873 unless(isConstQualified()))));
3874
3875 EXPECT_TRUE(matches("int a[2];",
3876 constantArrayType(hasElementType(builtinType()))));
3877 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3878}
3879
3880TEST(TypeMatching, MatchesComplexTypes) {
3881 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3882 EXPECT_TRUE(matches(
3883 "_Complex float f;",
3884 complexType(hasElementType(builtinType()))));
3885 EXPECT_TRUE(notMatches(
3886 "_Complex float f;",
3887 complexType(hasElementType(isInteger()))));
3888}
3889
3890TEST(TypeMatching, MatchesConstantArrayTypes) {
3891 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3892 EXPECT_TRUE(notMatches(
3893 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3894 constantArrayType(hasElementType(builtinType()))));
3895
3896 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3897 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3898 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3899}
3900
3901TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3902 EXPECT_TRUE(matches(
3903 "template <typename T, int Size> class array { T data[Size]; };",
3904 dependentSizedArrayType()));
3905 EXPECT_TRUE(notMatches(
3906 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3907 dependentSizedArrayType()));
3908}
3909
3910TEST(TypeMatching, MatchesIncompleteArrayType) {
3911 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3912 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3913
3914 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3915 incompleteArrayType()));
3916}
3917
3918TEST(TypeMatching, MatchesVariableArrayType) {
3919 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3920 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3921
3922 EXPECT_TRUE(matches(
3923 "void f(int b) { int a[b]; }",
3924 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3925 varDecl(hasName("b")))))))));
3926}
3927
3928TEST(TypeMatching, MatchesAtomicTypes) {
David Majnemer197e2102014-03-05 06:32:38 +00003929 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
3930 llvm::Triple::Win32) {
3931 // FIXME: Make this work for MSVC.
3932 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003933
David Majnemer197e2102014-03-05 06:32:38 +00003934 EXPECT_TRUE(matches("_Atomic(int) i;",
3935 atomicType(hasValueType(isInteger()))));
3936 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3937 atomicType(hasValueType(isInteger()))));
3938 }
Daniel Jasper516b02e2012-10-17 08:52:59 +00003939}
3940
3941TEST(TypeMatching, MatchesAutoTypes) {
3942 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3943 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3944 autoType()));
3945
Richard Smith061f1e22013-04-30 21:23:01 +00003946 // FIXME: Matching against the type-as-written can't work here, because the
3947 // type as written was not deduced.
3948 //EXPECT_TRUE(matches("auto a = 1;",
3949 // autoType(hasDeducedType(isInteger()))));
3950 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3951 // autoType(hasDeducedType(isInteger()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003952}
3953
Daniel Jasperd29d5fa2012-10-29 10:14:44 +00003954TEST(TypeMatching, MatchesFunctionTypes) {
3955 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3956 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3957}
3958
Edwin Vaneec074802013-04-01 18:33:34 +00003959TEST(TypeMatching, MatchesParenType) {
3960 EXPECT_TRUE(
3961 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3962 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3963
3964 EXPECT_TRUE(matches(
3965 "int (*ptr_to_func)(int);",
3966 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3967 EXPECT_TRUE(notMatches(
3968 "int (*ptr_to_array)[4];",
3969 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3970}
3971
Daniel Jasper516b02e2012-10-17 08:52:59 +00003972TEST(TypeMatching, PointerTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003973 // FIXME: Reactive when these tests can be more specific (not matching
3974 // implicit code on certain platforms), likely when we have hasDescendant for
3975 // Types/TypeLocs.
3976 //EXPECT_TRUE(matchAndVerifyResultTrue(
3977 // "int* a;",
3978 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3979 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3980 //EXPECT_TRUE(matchAndVerifyResultTrue(
3981 // "int* a;",
3982 // pointerTypeLoc().bind("loc"),
3983 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003984 EXPECT_TRUE(matches(
3985 "int** a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003986 loc(pointerType(pointee(qualType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003987 EXPECT_TRUE(matches(
3988 "int** a;",
3989 loc(pointerType(pointee(pointerType())))));
3990 EXPECT_TRUE(matches(
3991 "int* b; int* * const a = &b;",
3992 loc(qualType(isConstQualified(), pointerType()))));
3993
3994 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper7943eb52012-10-17 13:35:36 +00003995 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3996 hasType(blockPointerType()))));
3997 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3998 hasType(memberPointerType()))));
3999 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4000 hasType(pointerType()))));
4001 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4002 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00004003 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4004 hasType(lValueReferenceType()))));
4005 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4006 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004007
Daniel Jasper7943eb52012-10-17 13:35:36 +00004008 Fragment = "int *ptr;";
4009 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4010 hasType(blockPointerType()))));
4011 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4012 hasType(memberPointerType()))));
4013 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
4014 hasType(pointerType()))));
4015 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
4016 hasType(referenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004017
Daniel Jasper7943eb52012-10-17 13:35:36 +00004018 Fragment = "int a; int &ref = a;";
4019 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4020 hasType(blockPointerType()))));
4021 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4022 hasType(memberPointerType()))));
4023 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4024 hasType(pointerType()))));
4025 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
4026 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00004027 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
4028 hasType(lValueReferenceType()))));
4029 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4030 hasType(rValueReferenceType()))));
4031
4032 Fragment = "int &&ref = 2;";
4033 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4034 hasType(blockPointerType()))));
4035 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4036 hasType(memberPointerType()))));
4037 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4038 hasType(pointerType()))));
4039 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
4040 hasType(referenceType()))));
4041 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
4042 hasType(lValueReferenceType()))));
4043 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
4044 hasType(rValueReferenceType()))));
4045}
4046
4047TEST(TypeMatching, AutoRefTypes) {
4048 std::string Fragment = "auto a = 1;"
4049 "auto b = a;"
4050 "auto &c = a;"
4051 "auto &&d = c;"
4052 "auto &&e = 2;";
4053 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
4054 hasType(referenceType()))));
4055 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
4056 hasType(referenceType()))));
4057 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
4058 hasType(referenceType()))));
4059 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
4060 hasType(lValueReferenceType()))));
4061 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
4062 hasType(rValueReferenceType()))));
4063 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
4064 hasType(referenceType()))));
4065 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
4066 hasType(lValueReferenceType()))));
4067 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
4068 hasType(rValueReferenceType()))));
4069 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
4070 hasType(referenceType()))));
4071 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
4072 hasType(lValueReferenceType()))));
4073 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
4074 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004075}
4076
4077TEST(TypeMatching, PointeeTypes) {
4078 EXPECT_TRUE(matches("int b; int &a = b;",
4079 referenceType(pointee(builtinType()))));
4080 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
4081
4082 EXPECT_TRUE(matches("int *a;",
David Blaikieb61d0872013-02-18 19:04:16 +00004083 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004084
4085 EXPECT_TRUE(matches(
4086 "int const *A;",
4087 pointerType(pointee(isConstQualified(), builtinType()))));
4088 EXPECT_TRUE(notMatches(
4089 "int *A;",
4090 pointerType(pointee(isConstQualified(), builtinType()))));
4091}
4092
4093TEST(TypeMatching, MatchesPointersToConstTypes) {
4094 EXPECT_TRUE(matches("int b; int * const a = &b;",
4095 loc(pointerType())));
4096 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00004097 loc(pointerType())));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004098 EXPECT_TRUE(matches(
4099 "int b; const int * a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00004100 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004101 EXPECT_TRUE(matches(
4102 "int b; const int * a = &b;",
4103 pointerType(pointee(builtinType()))));
4104}
4105
4106TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00004107 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
4108 hasType(typedefType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00004109}
4110
Edwin Vanef901b712013-02-25 14:49:29 +00004111TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vaneb6eae142013-02-25 20:43:32 +00004112 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vanef901b712013-02-25 14:49:29 +00004113 templateSpecializationType()));
4114}
4115
Edwin Vaneb6eae142013-02-25 20:43:32 +00004116TEST(TypeMatching, MatchesRecordType) {
4117 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek59b0af62013-02-27 11:56:58 +00004118 EXPECT_TRUE(matches("struct S{}; S s;",
4119 recordType(hasDeclaration(recordDecl(hasName("S"))))));
4120 EXPECT_TRUE(notMatches("int i;",
4121 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00004122}
4123
4124TEST(TypeMatching, MatchesElaboratedType) {
4125 EXPECT_TRUE(matches(
4126 "namespace N {"
4127 " namespace M {"
4128 " class D {};"
4129 " }"
4130 "}"
4131 "N::M::D d;", elaboratedType()));
4132 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
4133 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
4134}
4135
4136TEST(ElaboratedTypeNarrowing, hasQualifier) {
4137 EXPECT_TRUE(matches(
4138 "namespace N {"
4139 " namespace M {"
4140 " class D {};"
4141 " }"
4142 "}"
4143 "N::M::D d;",
4144 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
4145 EXPECT_TRUE(notMatches(
4146 "namespace M {"
4147 " class D {};"
4148 "}"
4149 "M::D d;",
4150 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vane6972f6d2013-03-04 17:51:00 +00004151 EXPECT_TRUE(notMatches(
4152 "struct D {"
4153 "} d;",
4154 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00004155}
4156
4157TEST(ElaboratedTypeNarrowing, namesType) {
4158 EXPECT_TRUE(matches(
4159 "namespace N {"
4160 " namespace M {"
4161 " class D {};"
4162 " }"
4163 "}"
4164 "N::M::D d;",
4165 elaboratedType(elaboratedType(namesType(recordType(
4166 hasDeclaration(namedDecl(hasName("D")))))))));
4167 EXPECT_TRUE(notMatches(
4168 "namespace M {"
4169 " class D {};"
4170 "}"
4171 "M::D d;",
4172 elaboratedType(elaboratedType(namesType(typedefType())))));
4173}
4174
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00004175TEST(NNS, MatchesNestedNameSpecifiers) {
4176 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
4177 nestedNameSpecifier()));
4178 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
4179 nestedNameSpecifier()));
4180 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
4181 nestedNameSpecifier()));
4182
4183 EXPECT_TRUE(matches(
4184 "struct A { static void f() {} }; void g() { A::f(); }",
4185 nestedNameSpecifier()));
4186 EXPECT_TRUE(notMatches(
4187 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
4188 nestedNameSpecifier()));
4189}
4190
Daniel Jasper87c3d362012-09-20 14:12:57 +00004191TEST(NullStatement, SimpleCases) {
4192 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
4193 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
4194}
4195
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00004196TEST(NNS, MatchesTypes) {
4197 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
4198 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
4199 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
4200 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
4201 Matcher));
4202 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
4203}
4204
4205TEST(NNS, MatchesNamespaceDecls) {
4206 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
4207 specifiesNamespace(hasName("ns")));
4208 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
4209 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
4210 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
4211}
4212
4213TEST(NNS, BindsNestedNameSpecifiers) {
4214 EXPECT_TRUE(matchAndVerifyResultTrue(
4215 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
4216 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
4217 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
4218}
4219
4220TEST(NNS, BindsNestedNameSpecifierLocs) {
4221 EXPECT_TRUE(matchAndVerifyResultTrue(
4222 "namespace ns { struct B {}; } ns::B b;",
4223 loc(nestedNameSpecifier()).bind("loc"),
4224 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
4225}
4226
4227TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
4228 EXPECT_TRUE(matches(
4229 "struct A { struct B { struct C {}; }; }; A::B::C c;",
4230 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
4231 EXPECT_TRUE(matches(
4232 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasper516b02e2012-10-17 08:52:59 +00004233 nestedNameSpecifierLoc(hasPrefix(
4234 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00004235}
4236
Daniel Jasper6fc34332012-10-30 15:42:00 +00004237TEST(NNS, DescendantsOfNestedNameSpecifiers) {
4238 std::string Fragment =
4239 "namespace a { struct A { struct B { struct C {}; }; }; };"
4240 "void f() { a::A::B::C c; }";
4241 EXPECT_TRUE(matches(
4242 Fragment,
4243 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
4244 hasDescendant(nestedNameSpecifier(
4245 specifiesNamespace(hasName("a")))))));
4246 EXPECT_TRUE(notMatches(
4247 Fragment,
4248 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
4249 has(nestedNameSpecifier(
4250 specifiesNamespace(hasName("a")))))));
4251 EXPECT_TRUE(matches(
4252 Fragment,
4253 nestedNameSpecifier(specifiesType(asString("struct a::A")),
4254 has(nestedNameSpecifier(
4255 specifiesNamespace(hasName("a")))))));
4256
4257 // Not really useful because a NestedNameSpecifier can af at most one child,
4258 // but to complete the interface.
4259 EXPECT_TRUE(matchAndVerifyResultTrue(
4260 Fragment,
4261 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
4262 forEach(nestedNameSpecifier().bind("x"))),
4263 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
4264}
4265
4266TEST(NNS, NestedNameSpecifiersAsDescendants) {
4267 std::string Fragment =
4268 "namespace a { struct A { struct B { struct C {}; }; }; };"
4269 "void f() { a::A::B::C c; }";
4270 EXPECT_TRUE(matches(
4271 Fragment,
4272 decl(hasDescendant(nestedNameSpecifier(specifiesType(
4273 asString("struct a::A")))))));
4274 EXPECT_TRUE(matchAndVerifyResultTrue(
4275 Fragment,
4276 functionDecl(hasName("f"),
4277 forEachDescendant(nestedNameSpecifier().bind("x"))),
4278 // Nested names: a, a::A and a::A::B.
4279 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
4280}
4281
4282TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
4283 std::string Fragment =
4284 "namespace a { struct A { struct B { struct C {}; }; }; };"
4285 "void f() { a::A::B::C c; }";
4286 EXPECT_TRUE(matches(
4287 Fragment,
4288 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4289 hasDescendant(loc(nestedNameSpecifier(
4290 specifiesNamespace(hasName("a"))))))));
4291 EXPECT_TRUE(notMatches(
4292 Fragment,
4293 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4294 has(loc(nestedNameSpecifier(
4295 specifiesNamespace(hasName("a"))))))));
4296 EXPECT_TRUE(matches(
4297 Fragment,
4298 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
4299 has(loc(nestedNameSpecifier(
4300 specifiesNamespace(hasName("a"))))))));
4301
4302 EXPECT_TRUE(matchAndVerifyResultTrue(
4303 Fragment,
4304 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4305 forEach(nestedNameSpecifierLoc().bind("x"))),
4306 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
4307}
4308
4309TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
4310 std::string Fragment =
4311 "namespace a { struct A { struct B { struct C {}; }; }; };"
4312 "void f() { a::A::B::C c; }";
4313 EXPECT_TRUE(matches(
4314 Fragment,
4315 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4316 asString("struct a::A"))))))));
4317 EXPECT_TRUE(matchAndVerifyResultTrue(
4318 Fragment,
4319 functionDecl(hasName("f"),
4320 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4321 // Nested names: a, a::A and a::A::B.
4322 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4323}
4324
Manuel Klimek191c0932013-02-01 13:41:35 +00004325template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimekc2687452012-10-24 14:47:44 +00004326public:
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004327 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4328 StringRef InnerId)
4329 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jaspere9aa6872012-10-29 10:48:25 +00004330 }
4331
Manuel Klimek191c0932013-02-01 13:41:35 +00004332 virtual bool run(const BoundNodes *Nodes) { return false; }
4333
Manuel Klimekc2687452012-10-24 14:47:44 +00004334 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4335 const T *Node = Nodes->getNodeAs<T>(Id);
Benjamin Kramer76645582014-07-23 11:41:44 +00004336 return selectFirst<T>(InnerId, match(InnerMatcher, *Node, *Context)) !=
4337 nullptr;
Manuel Klimekc2687452012-10-24 14:47:44 +00004338 }
4339private:
4340 std::string Id;
4341 internal::Matcher<T> InnerMatcher;
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004342 std::string InnerId;
Manuel Klimekc2687452012-10-24 14:47:44 +00004343};
4344
4345TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004346 EXPECT_TRUE(matchAndVerifyResultTrue(
4347 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4348 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004349 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4350 "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004351 EXPECT_TRUE(matchAndVerifyResultFalse(
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::Z")).bind("Z"))),
4355 "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004356}
4357
4358TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004359 EXPECT_TRUE(matchAndVerifyResultTrue(
4360 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004361 new VerifyMatchOnNode<clang::Stmt>(
4362 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004363 EXPECT_TRUE(matchAndVerifyResultFalse(
4364 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004365 new VerifyMatchOnNode<clang::Stmt>(
4366 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004367}
4368
4369TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4370 EXPECT_TRUE(matchAndVerifyResultTrue(
4371 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4372 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004373 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004374 EXPECT_TRUE(matchAndVerifyResultFalse(
4375 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4376 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004377 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004378}
4379
Manuel Klimekbee08572013-02-07 12:42:10 +00004380template <typename T>
4381class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4382public:
4383 virtual bool run(const BoundNodes *Nodes) { return false; }
4384
4385 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4386 const T *Node = Nodes->getNodeAs<T>("");
4387 return verify(*Nodes, *Context, Node);
4388 }
4389
4390 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004391 // Use the original typed pointer to verify we can pass pointers to subtypes
4392 // to equalsNode.
4393 const T *TypedNode = cast<T>(Node);
Benjamin Kramer76645582014-07-23 11:41:44 +00004394 return selectFirst<T>(
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004395 "", match(stmt(hasParent(
4396 stmt(has(stmt(equalsNode(TypedNode)))).bind(""))),
Craig Topper416fa342014-06-08 08:38:12 +00004397 *Node, Context)) != nullptr;
Manuel Klimekbee08572013-02-07 12:42:10 +00004398 }
4399 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004400 // Use the original typed pointer to verify we can pass pointers to subtypes
4401 // to equalsNode.
4402 const T *TypedNode = cast<T>(Node);
Benjamin Kramer76645582014-07-23 11:41:44 +00004403 return selectFirst<T>(
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004404 "", match(decl(hasParent(
4405 decl(has(decl(equalsNode(TypedNode)))).bind(""))),
Craig Topper416fa342014-06-08 08:38:12 +00004406 *Node, Context)) != nullptr;
Manuel Klimekbee08572013-02-07 12:42:10 +00004407 }
4408};
4409
4410TEST(IsEqualTo, MatchesNodesByIdentity) {
4411 EXPECT_TRUE(matchAndVerifyResultTrue(
4412 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
Manuel Klimeka2c2a4f2014-05-27 12:31:10 +00004413 new VerifyAncestorHasChildIsEqual<CXXRecordDecl>()));
4414 EXPECT_TRUE(matchAndVerifyResultTrue(
4415 "void f() { if (true) if(true) {} }", ifStmt().bind(""),
4416 new VerifyAncestorHasChildIsEqual<IfStmt>()));
Manuel Klimekbee08572013-02-07 12:42:10 +00004417}
4418
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004419class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4420public:
4421 VerifyStartOfTranslationUnit() : Called(false) {}
4422 virtual void run(const MatchFinder::MatchResult &Result) {
4423 EXPECT_TRUE(Called);
4424 }
4425 virtual void onStartOfTranslationUnit() {
4426 Called = true;
4427 }
4428 bool Called;
4429};
4430
4431TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4432 MatchFinder Finder;
4433 VerifyStartOfTranslationUnit VerifyCallback;
4434 Finder.addMatcher(decl(), &VerifyCallback);
Ahmed Charlesb8984322014-03-07 20:03:18 +00004435 std::unique_ptr<FrontendActionFactory> Factory(
4436 newFrontendActionFactory(&Finder));
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004437 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4438 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004439
4440 VerifyCallback.Called = false;
Ahmed Charlesb8984322014-03-07 20:03:18 +00004441 std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
Peter Collingbournea2334162013-11-07 22:30:36 +00004442 ASSERT_TRUE(AST.get());
4443 Finder.matchAST(AST->getASTContext());
4444 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004445}
4446
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004447class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4448public:
4449 VerifyEndOfTranslationUnit() : Called(false) {}
4450 virtual void run(const MatchFinder::MatchResult &Result) {
4451 EXPECT_FALSE(Called);
4452 }
4453 virtual void onEndOfTranslationUnit() {
4454 Called = true;
4455 }
4456 bool Called;
4457};
4458
4459TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4460 MatchFinder Finder;
4461 VerifyEndOfTranslationUnit VerifyCallback;
4462 Finder.addMatcher(decl(), &VerifyCallback);
Ahmed Charlesb8984322014-03-07 20:03:18 +00004463 std::unique_ptr<FrontendActionFactory> Factory(
4464 newFrontendActionFactory(&Finder));
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004465 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4466 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004467
4468 VerifyCallback.Called = false;
Ahmed Charlesb8984322014-03-07 20:03:18 +00004469 std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
Peter Collingbournea2334162013-11-07 22:30:36 +00004470 ASSERT_TRUE(AST.get());
4471 Finder.matchAST(AST->getASTContext());
4472 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004473}
4474
Manuel Klimekbbb75852013-06-20 14:06:32 +00004475TEST(EqualsBoundNodeMatcher, QualType) {
4476 EXPECT_TRUE(matches(
4477 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4478 hasInitializer(ignoringParenImpCasts(
4479 hasType(qualType(equalsBoundNode("type"))))))));
4480 EXPECT_TRUE(notMatches("int i = 1.f;",
4481 varDecl(hasType(qualType().bind("type")),
4482 hasInitializer(ignoringParenImpCasts(hasType(
4483 qualType(equalsBoundNode("type"))))))));
4484}
4485
4486TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4487 EXPECT_TRUE(notMatches(
4488 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4489 hasInitializer(ignoringParenImpCasts(
4490 hasType(qualType(equalsBoundNode("type"))))))));
4491}
4492
4493TEST(EqualsBoundNodeMatcher, Stmt) {
4494 EXPECT_TRUE(
4495 matches("void f() { if(true) {} }",
4496 stmt(allOf(ifStmt().bind("if"),
4497 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4498
4499 EXPECT_TRUE(notMatches(
4500 "void f() { if(true) { if (true) {} } }",
4501 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4502}
4503
4504TEST(EqualsBoundNodeMatcher, Decl) {
4505 EXPECT_TRUE(matches(
4506 "class X { class Y {}; };",
4507 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4508 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4509
4510 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4511 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4512 has(decl(equalsBoundNode("record")))))));
4513}
4514
4515TEST(EqualsBoundNodeMatcher, Type) {
4516 EXPECT_TRUE(matches(
4517 "class X { int a; int b; };",
4518 recordDecl(
4519 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4520 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4521
4522 EXPECT_TRUE(notMatches(
4523 "class X { int a; double b; };",
4524 recordDecl(
4525 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4526 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4527}
4528
4529TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
Manuel Klimekbbb75852013-06-20 14:06:32 +00004530 EXPECT_TRUE(matchAndVerifyResultTrue(
4531 "int f() {"
4532 " if (1) {"
4533 " int i = 9;"
4534 " }"
4535 " int j = 10;"
4536 " {"
4537 " float k = 9.0;"
4538 " }"
4539 " return 0;"
4540 "}",
4541 // Look for variable declarations within functions whose type is the same
4542 // as the function return type.
4543 functionDecl(returns(qualType().bind("type")),
4544 forEachDescendant(varDecl(hasType(
4545 qualType(equalsBoundNode("type")))).bind("decl"))),
4546 // Only i and j should match, not k.
4547 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4548}
4549
4550TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4551 EXPECT_TRUE(matchAndVerifyResultTrue(
4552 "void f() {"
4553 " int x;"
4554 " double d;"
4555 " x = d + x - d + x;"
4556 "}",
4557 functionDecl(
4558 hasName("f"), forEachDescendant(varDecl().bind("d")),
4559 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4560 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4561}
4562
Manuel Klimekce68f772014-03-25 14:39:26 +00004563TEST(EqualsBoundNodeMatcher, UnlessDescendantsOfAncestorsMatch) {
4564 EXPECT_TRUE(matchAndVerifyResultTrue(
4565 "struct StringRef { int size() const; const char* data() const; };"
4566 "void f(StringRef v) {"
4567 " v.data();"
4568 "}",
4569 memberCallExpr(
4570 callee(methodDecl(hasName("data"))),
4571 on(declRefExpr(to(varDecl(hasType(recordDecl(hasName("StringRef"))))
4572 .bind("var")))),
4573 unless(hasAncestor(stmt(hasDescendant(memberCallExpr(
4574 callee(methodDecl(anyOf(hasName("size"), hasName("length")))),
4575 on(declRefExpr(to(varDecl(equalsBoundNode("var")))))))))))
4576 .bind("data"),
4577 new VerifyIdIsBoundTo<Expr>("data", 1)));
4578
4579 EXPECT_FALSE(matches(
4580 "struct StringRef { int size() const; const char* data() const; };"
4581 "void f(StringRef v) {"
4582 " v.data();"
4583 " v.size();"
4584 "}",
4585 memberCallExpr(
4586 callee(methodDecl(hasName("data"))),
4587 on(declRefExpr(to(varDecl(hasType(recordDecl(hasName("StringRef"))))
4588 .bind("var")))),
4589 unless(hasAncestor(stmt(hasDescendant(memberCallExpr(
4590 callee(methodDecl(anyOf(hasName("size"), hasName("length")))),
4591 on(declRefExpr(to(varDecl(equalsBoundNode("var")))))))))))
4592 .bind("data")));
4593}
4594
Manuel Klimek04616e42012-07-06 05:48:52 +00004595} // end namespace ast_matchers
4596} // end namespace clang