blob: d84e6a6ef9d1f7dc4ea3fde6e3b3b6a234a99a8f [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;
47 EXPECT_TRUE(Finder.addDynamicMatcher(decl(), NULL));
48 EXPECT_TRUE(Finder.addDynamicMatcher(callExpr(), NULL));
49 EXPECT_TRUE(Finder.addDynamicMatcher(constantArrayType(hasSize(42)), NULL));
50
51 // Do not accept non-toplevel matchers.
52 EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), NULL));
53 EXPECT_FALSE(Finder.addDynamicMatcher(hasSize(2), NULL));
54 EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), NULL));
55}
56
Manuel Klimeke9235692012-07-25 10:02:02 +000057TEST(Decl, MatchesDeclarations) {
58 EXPECT_TRUE(notMatches("", decl(usingDecl())));
59 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
60 decl(usingDecl())));
61}
62
Manuel Klimek04616e42012-07-06 05:48:52 +000063TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000064 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +000065 EXPECT_TRUE(matches("typedef int X;", NamedX));
66 EXPECT_TRUE(matches("int X;", NamedX));
67 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
68 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
69 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
70 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jasper1dad1832012-07-10 20:20:19 +000071 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek04616e42012-07-06 05:48:52 +000072
73 EXPECT_TRUE(notMatches("#define X 1", NamedX));
74}
75
Daniel Jasper1dad1832012-07-10 20:20:19 +000076TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000077 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000078 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
79 EXPECT_TRUE(matches("int Xb;", NamedX));
80 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
81 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
82 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
83 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
84 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
85
86 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
87
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000088 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000089 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
90 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
91
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000092 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000093 EXPECT_TRUE(matches("int abc;", Abc));
94 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
95 EXPECT_TRUE(notMatches("int cab;", Abc));
96 EXPECT_TRUE(matches("int cabc;", Abc));
Manuel Klimeke792efd2012-12-10 07:08:53 +000097
98 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
99 EXPECT_TRUE(matches("int k;", StartsWithK));
100 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
101 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
102 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
103 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000104}
105
Manuel Klimek04616e42012-07-06 05:48:52 +0000106TEST(DeclarationMatcher, MatchClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000107 DeclarationMatcher ClassMatcher(recordDecl());
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +0000108 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
109 llvm::Triple::Win32)
110 EXPECT_FALSE(matches("", ClassMatcher));
111 else
112 // Matches class type_info.
113 EXPECT_TRUE(matches("", ClassMatcher));
Manuel Klimek04616e42012-07-06 05:48:52 +0000114
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000115 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000116 EXPECT_TRUE(matches("class X;", ClassX));
117 EXPECT_TRUE(matches("class X {};", ClassX));
118 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
119 EXPECT_TRUE(notMatches("", ClassX));
120}
121
122TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000123 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000124
125 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasperf49d1e02012-09-07 12:48:17 +0000126 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
127 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek04616e42012-07-06 05:48:52 +0000128 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
129 EXPECT_TRUE(notMatches("", IsDerivedFromX));
130
Daniel Jasperd6b82cb2012-09-12 21:14:15 +0000131 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasperf49d1e02012-09-07 12:48:17 +0000132
133 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
134 EXPECT_TRUE(matches("class X {};", IsAX));
135 EXPECT_TRUE(matches("class X;", IsAX));
136 EXPECT_TRUE(notMatches("class Y;", IsAX));
137 EXPECT_TRUE(notMatches("", IsAX));
138
Manuel Klimek04616e42012-07-06 05:48:52 +0000139 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000140 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000141 EXPECT_TRUE(
142 matches("class X {}; class Y : public X {}; class Z : public Y {};",
143 ZIsDerivedFromX));
144 EXPECT_TRUE(
145 matches("class X {};"
146 "template<class T> class Y : public X {};"
147 "class Z : public Y<int> {};", ZIsDerivedFromX));
148 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
149 ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template<class T> class X {}; "
152 "template<class T> class Z : public X<T> {};",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 matches("template<class T, class U=T> class X {}; "
156 "template<class T> class Z : public X<T> {};",
157 ZIsDerivedFromX));
158 EXPECT_TRUE(
159 notMatches("template<class X> class A { class Z : public X {}; };",
160 ZIsDerivedFromX));
161 EXPECT_TRUE(
162 matches("template<class X> class A { public: class Z : public X {}; }; "
163 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
164 EXPECT_TRUE(
165 matches("template <class T> class X {}; "
166 "template<class Y> class A { class Z : public X<Y> {}; };",
167 ZIsDerivedFromX));
168 EXPECT_TRUE(
169 notMatches("template<template<class T> class X> class A { "
170 " class Z : public X<int> {}; };", ZIsDerivedFromX));
171 EXPECT_TRUE(
172 matches("template<template<class T> class X> class A { "
173 " public: class Z : public X<int> {}; }; "
174 "template<class T> class X {}; void y() { A<X>::Z z; }",
175 ZIsDerivedFromX));
176 EXPECT_TRUE(
177 notMatches("template<class X> class A { class Z : public X::D {}; };",
178 ZIsDerivedFromX));
179 EXPECT_TRUE(
180 matches("template<class X> class A { public: "
181 " class Z : public X::D {}; }; "
182 "class Y { public: class X {}; typedef X D; }; "
183 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
184 EXPECT_TRUE(
185 matches("class X {}; typedef X Y; class Z : public Y {};",
186 ZIsDerivedFromX));
187 EXPECT_TRUE(
188 matches("template<class T> class Y { typedef typename T::U X; "
189 " class Z : public X {}; };", ZIsDerivedFromX));
190 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
191 ZIsDerivedFromX));
192 EXPECT_TRUE(
193 notMatches("template<class T> class X {}; "
194 "template<class T> class A { class Z : public X<T>::D {}; };",
195 ZIsDerivedFromX));
196 EXPECT_TRUE(
197 matches("template<class T> class X { public: typedef X<T> D; }; "
198 "template<class T> class A { public: "
199 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
200 ZIsDerivedFromX));
201 EXPECT_TRUE(
202 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
203 ZIsDerivedFromX));
204 EXPECT_TRUE(
205 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
206 ZIsDerivedFromX));
207 EXPECT_TRUE(
208 matches("class X {}; class Y : public X {}; "
209 "typedef Y V; typedef V W; class Z : public W {};",
210 ZIsDerivedFromX));
211 EXPECT_TRUE(
212 matches("template<class T, class U> class X {}; "
213 "template<class T> class A { class Z : public X<T, int> {}; };",
214 ZIsDerivedFromX));
215 EXPECT_TRUE(
216 notMatches("template<class X> class D { typedef X A; typedef A B; "
217 " typedef B C; class Z : public C {}; };",
218 ZIsDerivedFromX));
219 EXPECT_TRUE(
220 matches("class X {}; typedef X A; typedef A B; "
221 "class Z : public B {};", ZIsDerivedFromX));
222 EXPECT_TRUE(
223 matches("class X {}; typedef X A; typedef A B; typedef B C; "
224 "class Z : public C {};", ZIsDerivedFromX));
225 EXPECT_TRUE(
226 matches("class U {}; typedef U X; typedef X V; "
227 "class Z : public V {};", ZIsDerivedFromX));
228 EXPECT_TRUE(
229 matches("class Base {}; typedef Base X; "
230 "class Z : public Base {};", ZIsDerivedFromX));
231 EXPECT_TRUE(
232 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
233 "class Z : public Base {};", ZIsDerivedFromX));
234 EXPECT_TRUE(
235 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
236 "class Z : public Base {};", ZIsDerivedFromX));
237 EXPECT_TRUE(
238 matches("class A {}; typedef A X; typedef A Y; "
239 "class Z : public Y {};", ZIsDerivedFromX));
240 EXPECT_TRUE(
241 notMatches("template <typename T> class Z;"
242 "template <> class Z<void> {};"
243 "template <typename T> class Z : public Z<void> {};",
244 IsDerivedFromX));
245 EXPECT_TRUE(
246 matches("template <typename T> class X;"
247 "template <> class X<void> {};"
248 "template <typename T> class X : public X<void> {};",
249 IsDerivedFromX));
250 EXPECT_TRUE(matches(
251 "class X {};"
252 "template <typename T> class Z;"
253 "template <> class Z<void> {};"
254 "template <typename T> class Z : public Z<void>, public X {};",
255 ZIsDerivedFromX));
Manuel Klimek5472a522012-12-04 13:40:29 +0000256 EXPECT_TRUE(
257 notMatches("template<int> struct X;"
258 "template<int i> struct X : public X<i-1> {};",
259 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
260 EXPECT_TRUE(matches(
261 "struct A {};"
262 "template<int> struct X;"
263 "template<int i> struct X : public X<i-1> {};"
264 "template<> struct X<0> : public A {};"
265 "struct B : public X<42> {};",
266 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000267
268 // FIXME: Once we have better matchers for template type matching,
269 // get rid of the Variable(...) matching and match the right template
270 // declarations directly.
271 const char *RecursiveTemplateOneParameter =
272 "class Base1 {}; class Base2 {};"
273 "template <typename T> class Z;"
274 "template <> class Z<void> : public Base1 {};"
275 "template <> class Z<int> : public Base2 {};"
276 "template <> class Z<float> : public Z<void> {};"
277 "template <> class Z<double> : public Z<int> {};"
278 "template <typename T> class Z : public Z<float>, public Z<double> {};"
279 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
280 EXPECT_TRUE(matches(
281 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000282 varDecl(hasName("z_float"),
283 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000284 EXPECT_TRUE(notMatches(
285 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000286 varDecl(hasName("z_float"),
287 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000288 EXPECT_TRUE(matches(
289 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000290 varDecl(hasName("z_char"),
291 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
292 isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000293
294 const char *RecursiveTemplateTwoParameters =
295 "class Base1 {}; class Base2 {};"
296 "template <typename T1, typename T2> class Z;"
297 "template <typename T> class Z<void, T> : public Base1 {};"
298 "template <typename T> class Z<int, T> : public Base2 {};"
299 "template <typename T> class Z<float, T> : public Z<void, T> {};"
300 "template <typename T> class Z<double, T> : public Z<int, T> {};"
301 "template <typename T1, typename T2> class Z : "
302 " public Z<float, T2>, public Z<double, T2> {};"
303 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
304 " Z<char, void> z_char; }";
305 EXPECT_TRUE(matches(
306 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000307 varDecl(hasName("z_float"),
308 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000309 EXPECT_TRUE(notMatches(
310 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000311 varDecl(hasName("z_float"),
312 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000313 EXPECT_TRUE(matches(
314 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000315 varDecl(hasName("z_char"),
316 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
317 isDerivedFrom("Base2")))))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000318 EXPECT_TRUE(matches(
319 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000320 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000321 EXPECT_TRUE(notMatches(
322 "class X {}; class Y : public X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000323 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000324
325 EXPECT_TRUE(matches(
326 "class X {}; class Y : public X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000327 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek1863e502013-08-02 21:24:09 +0000328
329 EXPECT_TRUE(matches(
330 "template<typename T> class X {};"
331 "template<typename T> using Z = X<T>;"
332 "template <typename T> class Y : Z<T> {};",
333 recordDecl(isDerivedFrom(namedDecl(hasName("X"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000334}
335
Edwin Vane0a4836e2013-03-06 17:02:57 +0000336TEST(DeclarationMatcher, hasMethod) {
337 EXPECT_TRUE(matches("class A { void func(); };",
338 recordDecl(hasMethod(hasName("func")))));
339 EXPECT_TRUE(notMatches("class A { void func(); };",
340 recordDecl(hasMethod(isPublic()))));
341}
342
Daniel Jasper83dafaf2012-09-18 14:17:42 +0000343TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
344 EXPECT_TRUE(matches(
345 "template <typename T> struct A {"
346 " template <typename T2> struct F {};"
347 "};"
348 "template <typename T> struct B : A<T>::template F<T> {};"
349 "B<int> b;",
350 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
351}
352
Edwin Vaneb6eae142013-02-25 20:43:32 +0000353TEST(DeclarationMatcher, hasDeclContext) {
354 EXPECT_TRUE(matches(
355 "namespace N {"
356 " namespace M {"
357 " class D {};"
358 " }"
359 "}",
Daniel Jasper9fcdc462013-04-08 16:44:05 +0000360 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +0000361 EXPECT_TRUE(notMatches(
362 "namespace N {"
363 " namespace M {"
364 " class D {};"
365 " }"
366 "}",
Daniel Jasper9fcdc462013-04-08 16:44:05 +0000367 recordDecl(hasDeclContext(namespaceDecl(hasName("N"))))));
368
369 EXPECT_TRUE(matches("namespace {"
370 " namespace M {"
371 " class D {};"
372 " }"
373 "}",
374 recordDecl(hasDeclContext(namespaceDecl(
375 hasName("M"), hasDeclContext(namespaceDecl()))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +0000376}
377
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000378TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000379 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000380 EXPECT_TRUE(notMatches("class X;", ClassX));
381 EXPECT_TRUE(notMatches("class X {};", ClassX));
382}
383
384TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000385 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000386 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
387 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
388}
389
390TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
391 EXPECT_TRUE(notMatches("template<typename T> class X { };"
392 "template<> class X<int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000393 classTemplateDecl(hasName("X"),
394 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000395}
396
397TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
398 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
399 "template<typename T> class X<T, 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
Daniel Jasper4e566c42012-07-12 08:50:38 +0000404TEST(AllOf, AllOverloadsWork) {
405 const char Program[] =
Edwin Vanee9dd3602013-02-12 13:55:40 +0000406 "struct T { };"
407 "int f(int, T*, int, int);"
408 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper4e566c42012-07-12 08:50:38 +0000409 EXPECT_TRUE(matches(Program,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000410 callExpr(allOf(callee(functionDecl(hasName("f"))),
411 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +0000412 EXPECT_TRUE(matches(Program,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000413 callExpr(allOf(callee(functionDecl(hasName("f"))),
414 hasArgument(0, declRefExpr(to(varDecl()))),
415 hasArgument(1, hasType(pointsTo(
416 recordDecl(hasName("T")))))))));
Edwin Vanee9dd3602013-02-12 13:55:40 +0000417 EXPECT_TRUE(matches(Program,
418 callExpr(allOf(callee(functionDecl(hasName("f"))),
419 hasArgument(0, declRefExpr(to(varDecl()))),
420 hasArgument(1, hasType(pointsTo(
421 recordDecl(hasName("T"))))),
422 hasArgument(2, integerLiteral(equals(3)))))));
423 EXPECT_TRUE(matches(Program,
424 callExpr(allOf(callee(functionDecl(hasName("f"))),
425 hasArgument(0, declRefExpr(to(varDecl()))),
426 hasArgument(1, hasType(pointsTo(
427 recordDecl(hasName("T"))))),
428 hasArgument(2, integerLiteral(equals(3))),
429 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +0000430}
431
Manuel Klimek04616e42012-07-06 05:48:52 +0000432TEST(DeclarationMatcher, MatchAnyOf) {
433 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000434 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000435 EXPECT_TRUE(
436 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
437 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
438 EXPECT_TRUE(
439 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
440 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
441
Daniel Jasper84c763e2012-07-15 19:57:12 +0000442 DeclarationMatcher XOrYOrZOrU =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000443 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasper84c763e2012-07-15 19:57:12 +0000444 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
445 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
446
Manuel Klimek04616e42012-07-06 05:48:52 +0000447 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000448 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
449 hasName("V")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000450 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
451 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
452 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
453 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
454 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
455 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
456}
457
458TEST(DeclarationMatcher, MatchHas) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000459 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000460 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
461 EXPECT_TRUE(matches("class X {};", HasClassX));
462
463 DeclarationMatcher YHasClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000464 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000465 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
466 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
467 EXPECT_TRUE(
468 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
469}
470
471TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
472 DeclarationMatcher Recursive =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000473 recordDecl(
474 has(recordDecl(
475 has(recordDecl(hasName("X"))),
476 has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000477 hasName("Z"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000478 has(recordDecl(
479 has(recordDecl(hasName("A"))),
480 has(recordDecl(hasName("B"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000481 hasName("C"))),
482 hasName("F"));
483
484 EXPECT_TRUE(matches(
485 "class F {"
486 " class Z {"
487 " class X {};"
488 " class Y {};"
489 " };"
490 " class C {"
491 " class A {};"
492 " class B {};"
493 " };"
494 "};", Recursive));
495
496 EXPECT_TRUE(matches(
497 "class F {"
498 " class Z {"
499 " class A {};"
500 " class X {};"
501 " class Y {};"
502 " };"
503 " class C {"
504 " class X {};"
505 " class A {};"
506 " class B {};"
507 " };"
508 "};", Recursive));
509
510 EXPECT_TRUE(matches(
511 "class O1 {"
512 " class O2 {"
513 " class F {"
514 " class Z {"
515 " class A {};"
516 " class X {};"
517 " class Y {};"
518 " };"
519 " class C {"
520 " class X {};"
521 " class A {};"
522 " class B {};"
523 " };"
524 " };"
525 " };"
526 "};", Recursive));
527}
528
529TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
530 DeclarationMatcher Recursive =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000531 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000532 anyOf(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000533 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000534 anyOf(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000535 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000536 hasName("X"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000537 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000538 hasName("Y"))),
539 hasName("Z")))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000540 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000541 anyOf(
542 hasName("C"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000543 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000544 hasName("A"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000545 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000546 hasName("B")))))),
547 hasName("F")));
548
549 EXPECT_TRUE(matches("class F {};", Recursive));
550 EXPECT_TRUE(matches("class Z {};", Recursive));
551 EXPECT_TRUE(matches("class C {};", Recursive));
552 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
553 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
554 EXPECT_TRUE(
555 matches("class O1 { class O2 {"
556 " class M { class N { class B {}; }; }; "
557 "}; };", Recursive));
558}
559
560TEST(DeclarationMatcher, MatchNot) {
561 DeclarationMatcher NotClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000562 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000563 isDerivedFrom("Y"),
Manuel Klimek04616e42012-07-06 05:48:52 +0000564 unless(hasName("X")));
565 EXPECT_TRUE(notMatches("", NotClassX));
566 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
567 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
568 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
569 EXPECT_TRUE(
570 notMatches("class Y {}; class Z {}; class X : public Y {};",
571 NotClassX));
572
573 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000574 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000575 hasName("X"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000576 has(recordDecl(hasName("Z"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000577 unless(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000578 has(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000579 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
580 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
581 ClassXHasNotClassY));
582}
583
584TEST(DeclarationMatcher, HasDescendant) {
585 DeclarationMatcher ZDescendantClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000586 recordDecl(
587 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000588 hasName("Z"));
589 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
590 EXPECT_TRUE(
591 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
592 EXPECT_TRUE(
593 matches("class Z { class A { class Y { class X {}; }; }; };",
594 ZDescendantClassX));
595 EXPECT_TRUE(
596 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
597 ZDescendantClassX));
598 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
599
600 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000601 recordDecl(
602 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000603 hasName("X"))),
604 hasName("Z"));
605 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
606 ZDescendantClassXHasClassY));
607 EXPECT_TRUE(
608 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
609 ZDescendantClassXHasClassY));
610 EXPECT_TRUE(notMatches(
611 "class Z {"
612 " class A {"
613 " class B {"
614 " class X {"
615 " class C {"
616 " class Y {};"
617 " };"
618 " };"
619 " }; "
620 " };"
621 "};", ZDescendantClassXHasClassY));
622
623 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000624 recordDecl(
625 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
626 hasName("X"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000627 hasName("Z"));
628 EXPECT_TRUE(
629 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
630 ZDescendantClassXDescendantClassY));
631 EXPECT_TRUE(matches(
632 "class Z {"
633 " class A {"
634 " class X {"
635 " class B {"
636 " class Y {};"
637 " };"
638 " class Y {};"
639 " };"
640 " };"
641 "};", ZDescendantClassXDescendantClassY));
642}
643
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000644// Implements a run method that returns whether BoundNodes contains a
645// Decl bound to Id that can be dynamically cast to T.
646// Optionally checks that the check succeeded a specific number of times.
647template <typename T>
648class VerifyIdIsBoundTo : public BoundNodesCallback {
649public:
650 // Create an object that checks that a node of type \c T was bound to \c Id.
651 // Does not check for a certain number of matches.
652 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
653 : Id(Id), ExpectedCount(-1), Count(0) {}
654
655 // Create an object that checks that a node of type \c T was bound to \c Id.
656 // Checks that there were exactly \c ExpectedCount matches.
657 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
658 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
659
660 // Create an object that checks that a node of type \c T was bound to \c Id.
661 // Checks that there was exactly one match with the name \c ExpectedName.
662 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimekb64d6b72013-03-14 16:33:21 +0000663 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
664 int ExpectedCount = 1)
665 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
666 ExpectedName(ExpectedName) {}
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000667
Peter Collingbourne2b9471302013-11-07 22:30:32 +0000668 void onEndOfTranslationUnit() LLVM_OVERRIDE {
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000669 if (ExpectedCount != -1)
670 EXPECT_EQ(ExpectedCount, Count);
671 if (!ExpectedName.empty())
672 EXPECT_EQ(ExpectedName, Name);
Peter Collingbourne2b9471302013-11-07 22:30:32 +0000673 Count = 0;
674 Name.clear();
675 }
676
677 ~VerifyIdIsBoundTo() {
678 EXPECT_EQ(0, Count);
679 EXPECT_EQ("", Name);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000680 }
681
682 virtual bool run(const BoundNodes *Nodes) {
Peter Collingbourne093a7292013-11-06 00:27:07 +0000683 const BoundNodes::IDToNodeMap &M = Nodes->getMap();
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000684 if (Nodes->getNodeAs<T>(Id)) {
685 ++Count;
686 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
687 Name = Named->getNameAsString();
688 } else if (const NestedNameSpecifier *NNS =
689 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
690 llvm::raw_string_ostream OS(Name);
691 NNS->print(OS, PrintingPolicy(LangOptions()));
692 }
Peter Collingbourne093a7292013-11-06 00:27:07 +0000693 BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
694 EXPECT_NE(M.end(), I);
695 if (I != M.end())
696 EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000697 return true;
698 }
Peter Collingbourne093a7292013-11-06 00:27:07 +0000699 EXPECT_TRUE(M.count(Id) == 0 || M.find(Id)->second.template get<T>() == 0);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000700 return false;
701 }
702
Daniel Jaspere9aa6872012-10-29 10:48:25 +0000703 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
704 return run(Nodes);
705 }
706
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000707private:
708 const std::string Id;
709 const int ExpectedCount;
710 int Count;
711 const std::string ExpectedName;
712 std::string Name;
713};
714
715TEST(HasDescendant, MatchesDescendantTypes) {
716 EXPECT_TRUE(matches("void f() { int i = 3; }",
717 decl(hasDescendant(loc(builtinType())))));
718 EXPECT_TRUE(matches("void f() { int i = 3; }",
719 stmt(hasDescendant(builtinType()))));
720
721 EXPECT_TRUE(matches("void f() { int i = 3; }",
722 stmt(hasDescendant(loc(builtinType())))));
723 EXPECT_TRUE(matches("void f() { int i = 3; }",
724 stmt(hasDescendant(qualType(builtinType())))));
725
726 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
727 stmt(hasDescendant(isInteger()))));
728
729 EXPECT_TRUE(matchAndVerifyResultTrue(
730 "void f() { int a; float c; int d; int e; }",
731 functionDecl(forEachDescendant(
732 varDecl(hasDescendant(isInteger())).bind("x"))),
733 new VerifyIdIsBoundTo<Decl>("x", 3)));
734}
735
736TEST(HasDescendant, MatchesDescendantsOfTypes) {
737 EXPECT_TRUE(matches("void f() { int*** i; }",
738 qualType(hasDescendant(builtinType()))));
739 EXPECT_TRUE(matches("void f() { int*** i; }",
740 qualType(hasDescendant(
741 pointerType(pointee(builtinType()))))));
742 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikieb61d0872013-02-18 19:04:16 +0000743 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000744
745 EXPECT_TRUE(matchAndVerifyResultTrue(
746 "void f() { int*** i; }",
747 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
748 new VerifyIdIsBoundTo<Type>("x", 2)));
749}
750
751TEST(Has, MatchesChildrenOfTypes) {
752 EXPECT_TRUE(matches("int i;",
753 varDecl(hasName("i"), has(isInteger()))));
754 EXPECT_TRUE(notMatches("int** i;",
755 varDecl(hasName("i"), has(isInteger()))));
756 EXPECT_TRUE(matchAndVerifyResultTrue(
757 "int (*f)(float, int);",
758 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
759 new VerifyIdIsBoundTo<QualType>("x", 2)));
760}
761
762TEST(Has, MatchesChildTypes) {
763 EXPECT_TRUE(matches(
764 "int* i;",
765 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
766 EXPECT_TRUE(notMatches(
767 "int* i;",
768 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
769}
770
Daniel Jasper1dad1832012-07-10 20:20:19 +0000771TEST(Enum, DoesNotMatchClasses) {
772 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
773}
774
775TEST(Enum, MatchesEnums) {
776 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
777}
778
779TEST(EnumConstant, Matches) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000780 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000781 EXPECT_TRUE(matches("enum X{ A };", Matcher));
782 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
783 EXPECT_TRUE(notMatches("enum X {};", Matcher));
784}
785
Manuel Klimek04616e42012-07-06 05:48:52 +0000786TEST(StatementMatcher, Has) {
787 StatementMatcher HasVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000788 expr(hasType(pointsTo(recordDecl(hasName("X")))),
789 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000790
791 EXPECT_TRUE(matches(
792 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
793 EXPECT_TRUE(notMatches(
794 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
795}
796
797TEST(StatementMatcher, HasDescendant) {
798 StatementMatcher HasDescendantVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000799 expr(hasType(pointsTo(recordDecl(hasName("X")))),
800 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000801
802 EXPECT_TRUE(matches(
803 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
804 HasDescendantVariableI));
805 EXPECT_TRUE(notMatches(
806 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
807 HasDescendantVariableI));
808}
809
810TEST(TypeMatcher, MatchesClassType) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000811 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000812
813 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
814 EXPECT_TRUE(notMatches("class A {};", TypeA));
815
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000816 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000817
818 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
819 TypeDerivedFromA));
820 EXPECT_TRUE(notMatches("class A {};", TypeA));
821
822 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000823 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000824
825 EXPECT_TRUE(
826 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
827}
828
Manuel Klimek04616e42012-07-06 05:48:52 +0000829TEST(Matcher, BindMatchedNodes) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000830 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000831
832 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000833 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000834
835 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000836 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000837
838 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000839 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000840
841 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
842 TypeAHasClassB,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000843 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000844
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000845 StatementMatcher MethodX =
846 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek04616e42012-07-06 05:48:52 +0000847
848 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
849 MethodX,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000850 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000851}
852
853TEST(Matcher, BindTheSameNameInAlternatives) {
854 StatementMatcher matcher = anyOf(
855 binaryOperator(hasOperatorName("+"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000856 hasLHS(expr().bind("x")),
Daniel Jasper1dad1832012-07-10 20:20:19 +0000857 hasRHS(integerLiteral(equals(0)))),
858 binaryOperator(hasOperatorName("+"),
859 hasLHS(integerLiteral(equals(0))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000860 hasRHS(expr().bind("x"))));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000861
862 EXPECT_TRUE(matchAndVerifyResultTrue(
863 // The first branch of the matcher binds x to 0 but then fails.
864 // The second branch binds x to f() and succeeds.
865 "int f() { return 0 + f(); }",
866 matcher,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000867 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000868}
869
Manuel Klimekfdf98762012-08-30 19:41:06 +0000870TEST(Matcher, BindsIDForMemoizedResults) {
871 // Using the same matcher in two match expressions will make memoization
872 // kick in.
873 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
874 EXPECT_TRUE(matchAndVerifyResultTrue(
875 "class A { class B { class X {}; }; };",
876 DeclarationMatcher(anyOf(
877 recordDecl(hasName("A"), hasDescendant(ClassX)),
878 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000879 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimekfdf98762012-08-30 19:41:06 +0000880}
881
Daniel Jasper856194d02012-12-03 15:43:25 +0000882TEST(HasDeclaration, HasDeclarationOfEnumType) {
883 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
884 expr(hasType(pointsTo(
885 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
886}
887
Edwin Vaneed936452013-02-25 14:32:42 +0000888TEST(HasDeclaration, HasGetDeclTraitTest) {
889 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
890 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
891 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
892}
893
Edwin Vane2c197e02013-02-19 17:14:34 +0000894TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
895 EXPECT_TRUE(matches("typedef int X; X a;",
896 varDecl(hasName("a"),
897 hasType(typedefType(hasDeclaration(decl()))))));
898
899 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
900}
901
Edwin Vanef901b712013-02-25 14:49:29 +0000902TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
903 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
904 varDecl(hasType(templateSpecializationType(
905 hasDeclaration(namedDecl(hasName("A"))))))));
906}
907
Manuel Klimek04616e42012-07-06 05:48:52 +0000908TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000909 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000910 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000911 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000912 EXPECT_TRUE(
913 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000914 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000915 EXPECT_TRUE(
916 matches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000917 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000918}
919
920TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000921 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000922 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000923 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000924 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000925 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000926 EXPECT_TRUE(
927 matches("class X {}; void y() { X *x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000928 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000929}
930
931TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000932 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000933 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000934 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000935 EXPECT_TRUE(
936 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000937 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000938}
939
940TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000941 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000942 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000943 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000944 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000945 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000946}
947
Manuel Klimekc16c6522013-06-20 13:08:29 +0000948TEST(HasTypeLoc, MatchesDeclaratorDecls) {
949 EXPECT_TRUE(matches("int x;",
950 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
951
952 // Make sure we don't crash on implicit constructors.
953 EXPECT_TRUE(notMatches("class X {}; X x;",
954 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
955}
956
Manuel Klimek04616e42012-07-06 05:48:52 +0000957TEST(Matcher, Call) {
958 // FIXME: Do we want to overload Call() to directly take
Daniel Jasper1dad1832012-07-10 20:20:19 +0000959 // Matcher<Decl>, too?
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000960 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000961
962 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
963 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
964
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000965 StatementMatcher MethodOnY =
966 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000967
968 EXPECT_TRUE(
969 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
970 MethodOnY));
971 EXPECT_TRUE(
972 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
973 MethodOnY));
974 EXPECT_TRUE(
975 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
976 MethodOnY));
977 EXPECT_TRUE(
978 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
979 MethodOnY));
980 EXPECT_TRUE(
981 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
982 MethodOnY));
983
984 StatementMatcher MethodOnYPointer =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000985 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000986
987 EXPECT_TRUE(
988 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
989 MethodOnYPointer));
990 EXPECT_TRUE(
991 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
992 MethodOnYPointer));
993 EXPECT_TRUE(
994 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
995 MethodOnYPointer));
996 EXPECT_TRUE(
997 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
998 MethodOnYPointer));
999 EXPECT_TRUE(
1000 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1001 MethodOnYPointer));
1002}
1003
Daniel Jasper5901e472012-10-01 13:40:41 +00001004TEST(Matcher, Lambda) {
Richard Smith3d584b02014-02-06 21:49:08 +00001005 EXPECT_TRUE(matches("auto f = [] (int i) { return i; };",
Daniel Jasper5901e472012-10-01 13:40:41 +00001006 lambdaExpr()));
1007}
1008
1009TEST(Matcher, ForRange) {
Daniel Jasper6f595392012-10-01 15:05:34 +00001010 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
1011 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00001012 forRangeStmt()));
1013 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
1014 forRangeStmt()));
1015}
1016
1017TEST(Matcher, UserDefinedLiteral) {
1018 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
1019 " return i + 1;"
1020 "}"
1021 "char c = 'a'_inc;",
1022 userDefinedLiteral()));
1023}
1024
Daniel Jasper87c3d362012-09-20 14:12:57 +00001025TEST(Matcher, FlowControl) {
1026 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
1027 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
1028 continueStmt()));
1029 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
1030 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
1031 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
1032}
1033
Daniel Jasper1dad1832012-07-10 20:20:19 +00001034TEST(HasType, MatchesAsString) {
1035 EXPECT_TRUE(
1036 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001037 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001038 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001039 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001040 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001041 fieldDecl(hasType(asString("ns::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001042 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
David Blaikie8972f4e2014-02-14 22:12:54 +00001043 fieldDecl(hasType(asString("struct <anonymous namespace>::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001044}
1045
Manuel Klimek04616e42012-07-06 05:48:52 +00001046TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001047 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001048 // Unary operator
1049 EXPECT_TRUE(matches("class Y { }; "
1050 "bool operator!(Y x) { return false; }; "
1051 "Y y; bool c = !y;", OpCall));
1052 // No match -- special operators like "new", "delete"
1053 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1054 // we need to figure out include paths in the test.
1055 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1056 // "class Y { }; "
1057 // "void *operator new(size_t size) { return 0; } "
1058 // "Y *y = new Y;", OpCall));
1059 EXPECT_TRUE(notMatches("class Y { }; "
1060 "void operator delete(void *p) { } "
1061 "void a() {Y *y = new Y; delete y;}", OpCall));
1062 // Binary operator
1063 EXPECT_TRUE(matches("class Y { }; "
1064 "bool operator&&(Y x, Y y) { return true; }; "
1065 "Y a; Y b; bool c = a && b;",
1066 OpCall));
1067 // No match -- normal operator, not an overloaded one.
1068 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1069 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1070}
1071
1072TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1073 StatementMatcher OpCallAndAnd =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001074 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek04616e42012-07-06 05:48:52 +00001075 EXPECT_TRUE(matches("class Y { }; "
1076 "bool operator&&(Y x, Y y) { return true; }; "
1077 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1078 StatementMatcher OpCallLessLess =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001079 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek04616e42012-07-06 05:48:52 +00001080 EXPECT_TRUE(notMatches("class Y { }; "
1081 "bool operator&&(Y x, Y y) { return true; }; "
1082 "Y a; Y b; bool c = a && b;",
1083 OpCallLessLess));
Edwin Vane0a4836e2013-03-06 17:02:57 +00001084 DeclarationMatcher ClassWithOpStar =
1085 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1086 EXPECT_TRUE(matches("class Y { int operator*(); };",
1087 ClassWithOpStar));
1088 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1089 ClassWithOpStar)) ;
Manuel Klimek04616e42012-07-06 05:48:52 +00001090}
1091
Daniel Jasper0f9f0192012-11-15 03:29:05 +00001092TEST(Matcher, NestedOverloadedOperatorCalls) {
1093 EXPECT_TRUE(matchAndVerifyResultTrue(
1094 "class Y { }; "
1095 "Y& operator&&(Y& x, Y& y) { return x; }; "
1096 "Y a; Y b; Y c; Y d = a && b && c;",
1097 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1098 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1099 EXPECT_TRUE(matches(
1100 "class Y { }; "
1101 "Y& operator&&(Y& x, Y& y) { return x; }; "
1102 "Y a; Y b; Y c; Y d = a && b && c;",
1103 operatorCallExpr(hasParent(operatorCallExpr()))));
1104 EXPECT_TRUE(matches(
1105 "class Y { }; "
1106 "Y& operator&&(Y& x, Y& y) { return x; }; "
1107 "Y a; Y b; Y c; Y d = a && b && c;",
1108 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1109}
1110
Manuel Klimek04616e42012-07-06 05:48:52 +00001111TEST(Matcher, ThisPointerType) {
Manuel Klimek86f8bbc2012-07-24 13:37:29 +00001112 StatementMatcher MethodOnY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001113 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001114
1115 EXPECT_TRUE(
1116 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1117 MethodOnY));
1118 EXPECT_TRUE(
1119 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1120 MethodOnY));
1121 EXPECT_TRUE(
1122 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1123 MethodOnY));
1124 EXPECT_TRUE(
1125 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1126 MethodOnY));
1127 EXPECT_TRUE(
1128 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1129 MethodOnY));
1130
1131 EXPECT_TRUE(matches(
1132 "class Y {"
1133 " public: virtual void x();"
1134 "};"
1135 "class X : public Y {"
1136 " public: virtual void x();"
1137 "};"
1138 "void z() { X *x; x->Y::x(); }", MethodOnY));
1139}
1140
1141TEST(Matcher, VariableUsage) {
1142 StatementMatcher Reference =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001143 declRefExpr(to(
1144 varDecl(hasInitializer(
1145 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001146
1147 EXPECT_TRUE(matches(
1148 "class Y {"
1149 " public:"
1150 " bool x() const;"
1151 "};"
1152 "void z(const Y &y) {"
1153 " bool b = y.x();"
1154 " if (b) {}"
1155 "}", Reference));
1156
1157 EXPECT_TRUE(notMatches(
1158 "class Y {"
1159 " public:"
1160 " bool x() const;"
1161 "};"
1162 "void z(const Y &y) {"
1163 " bool b = y.x();"
1164 "}", Reference));
1165}
1166
Manuel Klimek61379422012-12-04 14:42:08 +00001167TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001168 EXPECT_TRUE(matches(
1169 "void f(int i) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001170 varDecl(hasName("i"))));
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001171}
1172
Manuel Klimek04616e42012-07-06 05:48:52 +00001173TEST(Matcher, CalledVariable) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001174 StatementMatcher CallOnVariableY =
1175 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001176
1177 EXPECT_TRUE(matches(
1178 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1179 EXPECT_TRUE(matches(
1180 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1181 EXPECT_TRUE(matches(
1182 "class Y { public: void x(); };"
1183 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1184 EXPECT_TRUE(matches(
1185 "class Y { public: void x(); };"
1186 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1187 EXPECT_TRUE(notMatches(
1188 "class Y { public: void x(); };"
1189 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1190 CallOnVariableY));
1191}
1192
Daniel Jasper1dad1832012-07-10 20:20:19 +00001193TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1194 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1195 unaryExprOrTypeTraitExpr()));
1196 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1197 alignOfExpr(anything())));
1198 // FIXME: Uncomment once alignof is enabled.
1199 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1200 // unaryExprOrTypeTraitExpr()));
1201 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1202 // sizeOfExpr()));
1203}
1204
1205TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1206 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1207 hasArgumentOfType(asString("int")))));
1208 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1209 hasArgumentOfType(asString("float")))));
1210 EXPECT_TRUE(matches(
1211 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001212 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001213 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001214 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001215}
1216
Manuel Klimek04616e42012-07-06 05:48:52 +00001217TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001218 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001219}
1220
1221TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001222 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001223}
1224
1225TEST(MemberExpression, MatchesVariable) {
1226 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001227 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001228 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001229 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001230 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001231 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001232}
1233
1234TEST(MemberExpression, MatchesStaticVariable) {
1235 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001236 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001237 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001238 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001239 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001240 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001241}
1242
Daniel Jasper4e566c42012-07-12 08:50:38 +00001243TEST(IsInteger, MatchesIntegers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001244 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1245 EXPECT_TRUE(matches(
1246 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1247 callExpr(hasArgument(0, declRefExpr(
1248 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001249}
1250
1251TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001252 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001253 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001254 callExpr(hasArgument(0, declRefExpr(
1255 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001256}
1257
Manuel Klimek04616e42012-07-06 05:48:52 +00001258TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1259 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001260 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001261 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001262 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001263 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001264 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001265}
1266
1267TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1268 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001269 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001270 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001271 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001272 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001273 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001274}
1275
1276TEST(IsArrow, MatchesMemberCallsViaArrow) {
1277 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001278 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001279 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001280 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001281 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001282 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001283}
1284
1285TEST(Callee, MatchesDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001286 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001287
1288 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1289 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1290}
1291
1292TEST(Callee, MatchesMemberExpressions) {
1293 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001294 callExpr(callee(memberExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001295 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001296 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001297}
1298
1299TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001300 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001301
1302 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1303 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1304
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +00001305 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
1306 llvm::Triple::Win32) {
1307 // FIXME: Make this work for MSVC.
1308 // Dependent contexts, but a non-dependent call.
1309 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1310 CallFunctionF));
1311 EXPECT_TRUE(
1312 matches("void f(); template <int N> struct S { void g() { f(); } };",
1313 CallFunctionF));
1314 }
Manuel Klimek04616e42012-07-06 05:48:52 +00001315
1316 // Depedent calls don't match.
1317 EXPECT_TRUE(
1318 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1319 CallFunctionF));
1320 EXPECT_TRUE(
1321 notMatches("void f(int);"
1322 "template <typename T> struct S { void g(T t) { f(t); } };",
1323 CallFunctionF));
1324}
1325
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001326TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1327 EXPECT_TRUE(
1328 matches("template <typename T> void f(T t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001329 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001330}
1331
1332TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1333 EXPECT_TRUE(
1334 notMatches("void f(double d); void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001335 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001336}
1337
1338TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1339 EXPECT_TRUE(
1340 notMatches("void g(); template <typename T> void f(T t) {}"
1341 "template <> void f(int t) { g(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001342 functionTemplateDecl(hasName("f"),
1343 hasDescendant(declRefExpr(to(
1344 functionDecl(hasName("g"))))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001345}
1346
Manuel Klimek04616e42012-07-06 05:48:52 +00001347TEST(Matcher, Argument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001348 StatementMatcher CallArgumentY = callExpr(
1349 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001350
1351 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1352 EXPECT_TRUE(
1353 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1354 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1355
Daniel Jasper848cbe12012-09-18 13:09:13 +00001356 StatementMatcher WrongIndex = callExpr(
1357 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001358 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1359}
1360
1361TEST(Matcher, AnyArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001362 StatementMatcher CallArgumentY = callExpr(
1363 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001364 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1365 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1366 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1367}
1368
1369TEST(Matcher, ArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001370 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001371
1372 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1373 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1374 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1375}
1376
Daniel Jasper9f501292012-12-04 11:54:27 +00001377TEST(Matcher, ParameterCount) {
1378 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1379 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1380 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1381 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1382 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1383}
1384
Manuel Klimek04616e42012-07-06 05:48:52 +00001385TEST(Matcher, References) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001386 DeclarationMatcher ReferenceClassX = varDecl(
1387 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001388 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1389 ReferenceClassX));
1390 EXPECT_TRUE(
1391 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
Michael Hanc90d12d2013-09-11 15:53:29 +00001392 // The match here is on the implicit copy constructor code for
1393 // class X, not on code 'X x = y'.
Manuel Klimek04616e42012-07-06 05:48:52 +00001394 EXPECT_TRUE(
Michael Hanc90d12d2013-09-11 15:53:29 +00001395 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1396 EXPECT_TRUE(
1397 notMatches("class X {}; extern X x;", ReferenceClassX));
Manuel Klimek04616e42012-07-06 05:48:52 +00001398 EXPECT_TRUE(
1399 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1400}
1401
Edwin Vane0a4836e2013-03-06 17:02:57 +00001402TEST(QualType, hasCanonicalType) {
1403 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1404 "int a;"
1405 "int_ref b = a;",
1406 varDecl(hasType(qualType(referenceType())))));
1407 EXPECT_TRUE(
1408 matches("typedef int &int_ref;"
1409 "int a;"
1410 "int_ref b = a;",
1411 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1412}
1413
Edwin Vane119d3df2013-04-02 18:15:55 +00001414TEST(QualType, hasLocalQualifiers) {
1415 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1416 varDecl(hasType(hasLocalQualifiers()))));
1417 EXPECT_TRUE(matches("int *const j = nullptr;",
1418 varDecl(hasType(hasLocalQualifiers()))));
1419 EXPECT_TRUE(matches("int *volatile k;",
1420 varDecl(hasType(hasLocalQualifiers()))));
1421 EXPECT_TRUE(notMatches("int m;",
1422 varDecl(hasType(hasLocalQualifiers()))));
1423}
1424
Manuel Klimek04616e42012-07-06 05:48:52 +00001425TEST(HasParameter, CallsInnerMatcher) {
1426 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001427 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001428 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001429 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001430}
1431
1432TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1433 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001434 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001435}
1436
1437TEST(HasType, MatchesParameterVariableTypesStrictly) {
1438 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001439 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001440 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001441 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001442 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001443 methodDecl(hasParameter(0,
1444 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001445 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001446 methodDecl(hasParameter(0,
1447 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001448}
1449
1450TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1451 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001452 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001453 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001454 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001455}
1456
Daniel Jasper1dad1832012-07-10 20:20:19 +00001457TEST(Returns, MatchesReturnTypes) {
1458 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001459 functionDecl(returns(asString("int")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001460 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001461 functionDecl(returns(asString("float")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001462 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001463 functionDecl(returns(hasDeclaration(
1464 recordDecl(hasName("Y")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001465}
1466
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001467TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001468 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1469 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1470 functionDecl(isExternC())));
1471 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001472}
1473
Manuel Klimek04616e42012-07-06 05:48:52 +00001474TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1475 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001476 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001477}
1478
1479TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1480 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001481 methodDecl(hasAnyParameter(hasType(pointsTo(
1482 recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001483}
1484
Alp Toker8db6e7a2014-01-05 06:38:57 +00001485TEST(HasName, MatchesParameterVariableDeclarations) {
Manuel Klimek04616e42012-07-06 05:48:52 +00001486 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001487 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001488 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001489 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001490}
1491
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001492TEST(Matcher, MatchesClassTemplateSpecialization) {
1493 EXPECT_TRUE(matches("template<typename T> struct A {};"
1494 "template<> struct A<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001495 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001496 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001497 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001498 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001499 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001500}
1501
Manuel Klimekc16c6522013-06-20 13:08:29 +00001502TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1503 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1504 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1505}
1506
1507TEST(ParmVarDecl, MatchesParmVars) {
1508 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1509 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1510}
1511
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001512TEST(Matcher, MatchesTypeTemplateArgument) {
1513 EXPECT_TRUE(matches(
1514 "template<typename T> struct B {};"
1515 "B<int> b;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001516 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001517 asString("int"))))));
1518}
1519
1520TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1521 EXPECT_TRUE(matches(
1522 "struct B { int next; };"
1523 "template<int(B::*next_ptr)> struct A {};"
1524 "A<&B::next> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001525 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1526 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper0c303372012-09-29 15:55:18 +00001527
1528 EXPECT_TRUE(notMatches(
1529 "template <typename T> struct A {};"
1530 "A<int> a;",
1531 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1532 refersToDeclaration(decl())))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001533}
1534
1535TEST(Matcher, MatchesSpecificArgument) {
1536 EXPECT_TRUE(matches(
1537 "template<typename T, typename U> class A {};"
1538 "A<bool, int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001539 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001540 1, refersToType(asString("int"))))));
1541 EXPECT_TRUE(notMatches(
1542 "template<typename T, typename U> class A {};"
1543 "A<int, bool> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001544 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001545 1, refersToType(asString("int"))))));
1546}
1547
Daniel Jasper639522c2013-02-25 12:02:08 +00001548TEST(Matcher, MatchesAccessSpecDecls) {
1549 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1550 EXPECT_TRUE(
1551 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1552 EXPECT_TRUE(
1553 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1554 EXPECT_TRUE(
1555 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1556
1557 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1558}
1559
Edwin Vane37ee1d72013-04-09 20:46:36 +00001560TEST(Matcher, MatchesVirtualMethod) {
1561 EXPECT_TRUE(matches("class X { virtual int f(); };",
1562 methodDecl(isVirtual(), hasName("::X::f"))));
1563 EXPECT_TRUE(notMatches("class X { int f(); };",
1564 methodDecl(isVirtual())));
1565}
1566
Edwin Vanefc4f7dc2013-05-09 17:00:17 +00001567TEST(Matcher, MatchesConstMethod) {
1568 EXPECT_TRUE(matches("struct A { void foo() const; };",
1569 methodDecl(isConst())));
1570 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1571 methodDecl(isConst())));
1572}
1573
Edwin Vane37ee1d72013-04-09 20:46:36 +00001574TEST(Matcher, MatchesOverridingMethod) {
1575 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1576 "class Y : public X { int f(); };",
1577 methodDecl(isOverride(), hasName("::Y::f"))));
1578 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1579 "class Y : public X { int f(); };",
1580 methodDecl(isOverride(), hasName("::X::f"))));
1581 EXPECT_TRUE(notMatches("class X { int f(); }; "
1582 "class Y : public X { int f(); };",
1583 methodDecl(isOverride())));
1584 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1585 methodDecl(isOverride())));
1586}
1587
Manuel Klimek04616e42012-07-06 05:48:52 +00001588TEST(Matcher, ConstructorCall) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001589 StatementMatcher Constructor = constructExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001590
1591 EXPECT_TRUE(
1592 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1593 EXPECT_TRUE(
1594 matches("class X { public: X(); }; void x() { X x = X(); }",
1595 Constructor));
1596 EXPECT_TRUE(
1597 matches("class X { public: X(int); }; void x() { X x = 0; }",
1598 Constructor));
1599 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1600}
1601
1602TEST(Matcher, ConstructorArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001603 StatementMatcher Constructor = constructExpr(
1604 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001605
1606 EXPECT_TRUE(
1607 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1608 Constructor));
1609 EXPECT_TRUE(
1610 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1611 Constructor));
1612 EXPECT_TRUE(
1613 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1614 Constructor));
1615 EXPECT_TRUE(
1616 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1617 Constructor));
1618
Daniel Jasper848cbe12012-09-18 13:09:13 +00001619 StatementMatcher WrongIndex = constructExpr(
1620 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001621 EXPECT_TRUE(
1622 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1623 WrongIndex));
1624}
1625
1626TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001627 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001628
1629 EXPECT_TRUE(
1630 matches("class X { public: X(int); }; void x() { X x(0); }",
1631 Constructor1Arg));
1632 EXPECT_TRUE(
1633 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1634 Constructor1Arg));
1635 EXPECT_TRUE(
1636 matches("class X { public: X(int); }; void x() { X x = 0; }",
1637 Constructor1Arg));
1638 EXPECT_TRUE(
1639 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1640 Constructor1Arg));
1641}
1642
Peter Collingbourne1fec3df2014-02-06 21:52:24 +00001643TEST(Matcher, ConstructorListInitialization) {
1644 StatementMatcher ConstructorListInit = constructExpr(isListInitialization());
1645
1646 EXPECT_TRUE(
1647 matches("class X { public: X(int); }; void x() { X x{0}; }",
1648 ConstructorListInit));
1649 EXPECT_FALSE(
1650 matches("class X { public: X(int); }; void x() { X x(0); }",
1651 ConstructorListInit));
1652}
1653
Manuel Klimek7fca93b2012-10-23 10:40:50 +00001654TEST(Matcher,ThisExpr) {
1655 EXPECT_TRUE(
1656 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1657 EXPECT_TRUE(
1658 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1659}
1660
Manuel Klimek04616e42012-07-06 05:48:52 +00001661TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001662 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001663
1664 std::string ClassString = "class string { public: string(); ~string(); }; ";
1665
1666 EXPECT_TRUE(
1667 matches(ClassString +
1668 "string GetStringByValue();"
1669 "void FunctionTakesString(string s);"
1670 "void run() { FunctionTakesString(GetStringByValue()); }",
1671 TempExpression));
1672
1673 EXPECT_TRUE(
1674 notMatches(ClassString +
1675 "string* GetStringPointer(); "
1676 "void FunctionTakesStringPtr(string* s);"
1677 "void run() {"
1678 " string* s = GetStringPointer();"
1679 " FunctionTakesStringPtr(GetStringPointer());"
1680 " FunctionTakesStringPtr(s);"
1681 "}",
1682 TempExpression));
1683
1684 EXPECT_TRUE(
1685 notMatches("class no_dtor {};"
1686 "no_dtor GetObjByValue();"
1687 "void ConsumeObj(no_dtor param);"
1688 "void run() { ConsumeObj(GetObjByValue()); }",
1689 TempExpression));
1690}
1691
Sam Panzer68a35af2012-08-24 22:04:44 +00001692TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1693 std::string ClassString =
1694 "class string { public: string(); int length(); }; ";
1695
1696 EXPECT_TRUE(
1697 matches(ClassString +
1698 "string GetStringByValue();"
1699 "void FunctionTakesString(string s);"
1700 "void run() { FunctionTakesString(GetStringByValue()); }",
1701 materializeTemporaryExpr()));
1702
1703 EXPECT_TRUE(
1704 notMatches(ClassString +
1705 "string* GetStringPointer(); "
1706 "void FunctionTakesStringPtr(string* s);"
1707 "void run() {"
1708 " string* s = GetStringPointer();"
1709 " FunctionTakesStringPtr(GetStringPointer());"
1710 " FunctionTakesStringPtr(s);"
1711 "}",
1712 materializeTemporaryExpr()));
1713
1714 EXPECT_TRUE(
1715 notMatches(ClassString +
1716 "string GetStringByValue();"
1717 "void run() { int k = GetStringByValue().length(); }",
1718 materializeTemporaryExpr()));
1719
1720 EXPECT_TRUE(
1721 notMatches(ClassString +
1722 "string GetStringByValue();"
1723 "void run() { GetStringByValue(); }",
1724 materializeTemporaryExpr()));
1725}
1726
Manuel Klimek04616e42012-07-06 05:48:52 +00001727TEST(ConstructorDeclaration, SimpleCase) {
1728 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001729 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001730 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001731 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001732}
1733
1734TEST(ConstructorDeclaration, IsImplicit) {
1735 // This one doesn't match because the constructor is not added by the
1736 // compiler (it is not needed).
1737 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001738 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001739 // The compiler added the implicit default constructor.
1740 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001741 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001742 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001743 constructorDecl(unless(isImplicit()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001744}
1745
Daniel Jasper1dad1832012-07-10 20:20:19 +00001746TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1747 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001748 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001749}
1750
1751TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001752 EXPECT_TRUE(notMatches("class Foo {};",
1753 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001754}
1755
Manuel Klimek04616e42012-07-06 05:48:52 +00001756TEST(HasAnyConstructorInitializer, SimpleCase) {
1757 EXPECT_TRUE(notMatches(
1758 "class Foo { Foo() { } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001759 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001760 EXPECT_TRUE(matches(
1761 "class Foo {"
1762 " Foo() : foo_() { }"
1763 " int foo_;"
1764 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001765 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001766}
1767
1768TEST(HasAnyConstructorInitializer, ForField) {
1769 static const char Code[] =
1770 "class Baz { };"
1771 "class Foo {"
1772 " Foo() : foo_() { }"
1773 " Baz foo_;"
1774 " Baz bar_;"
1775 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001776 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1777 forField(hasType(recordDecl(hasName("Baz"))))))));
1778 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001779 forField(hasName("foo_"))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001780 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1781 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001782}
1783
1784TEST(HasAnyConstructorInitializer, WithInitializer) {
1785 static const char Code[] =
1786 "class Foo {"
1787 " Foo() : foo_(0) { }"
1788 " int foo_;"
1789 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001790 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001791 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001792 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001793 withInitializer(integerLiteral(equals(1)))))));
1794}
1795
1796TEST(HasAnyConstructorInitializer, IsWritten) {
1797 static const char Code[] =
1798 "struct Bar { Bar(){} };"
1799 "class Foo {"
1800 " Foo() : foo_() { }"
1801 " Bar foo_;"
1802 " Bar bar_;"
1803 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001804 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001805 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001806 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001807 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001808 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001809 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1810}
1811
1812TEST(Matcher, NewExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001813 StatementMatcher New = newExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001814
1815 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1816 EXPECT_TRUE(
1817 matches("class X { public: X(); }; void x() { new X(); }", New));
1818 EXPECT_TRUE(
1819 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1820 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1821}
1822
1823TEST(Matcher, NewExpressionArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001824 StatementMatcher New = constructExpr(
1825 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001826
1827 EXPECT_TRUE(
1828 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1829 New));
1830 EXPECT_TRUE(
1831 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1832 New));
1833 EXPECT_TRUE(
1834 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1835 New));
1836
Daniel Jasper848cbe12012-09-18 13:09:13 +00001837 StatementMatcher WrongIndex = constructExpr(
1838 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001839 EXPECT_TRUE(
1840 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1841 WrongIndex));
1842}
1843
1844TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001845 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001846
1847 EXPECT_TRUE(
1848 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1849 EXPECT_TRUE(
1850 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1851 New));
1852}
1853
Daniel Jasper1dad1832012-07-10 20:20:19 +00001854TEST(Matcher, DeleteExpression) {
1855 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001856 deleteExpr()));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001857}
1858
Manuel Klimek04616e42012-07-06 05:48:52 +00001859TEST(Matcher, DefaultArgument) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001860 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001861
1862 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1863 EXPECT_TRUE(
1864 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1865 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1866}
1867
1868TEST(Matcher, StringLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001869 StatementMatcher Literal = stringLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001870 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1871 // wide string
1872 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1873 // with escaped characters
1874 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1875 // no matching -- though the data type is the same, there is no string literal
1876 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1877}
1878
1879TEST(Matcher, CharacterLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001880 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001881 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1882 // wide character
1883 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1884 // wide character, Hex encoded, NOT MATCHED!
1885 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1886 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1887}
1888
1889TEST(Matcher, IntegerLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001890 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001891 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1892 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1893 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1894 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1895
1896 // Non-matching cases (character literals, float and double)
1897 EXPECT_TRUE(notMatches("int i = L'a';",
1898 HasIntLiteral)); // this is actually a character
1899 // literal cast to int
1900 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1901 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1902 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1903}
1904
Daniel Jasper91f1c8c2013-07-26 18:52:58 +00001905TEST(Matcher, FloatLiterals) {
1906 StatementMatcher HasFloatLiteral = floatLiteral();
1907 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1908 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1909 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1910 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1911 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1912
1913 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1914}
1915
Daniel Jasper5901e472012-10-01 13:40:41 +00001916TEST(Matcher, NullPtrLiteral) {
1917 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1918}
1919
Daniel Jasper87c3d362012-09-20 14:12:57 +00001920TEST(Matcher, AsmStatement) {
1921 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1922}
1923
Manuel Klimek04616e42012-07-06 05:48:52 +00001924TEST(Matcher, Conditions) {
1925 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1926
1927 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1928 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1929 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1930 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1931 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1932}
1933
1934TEST(MatchBinaryOperator, HasOperatorName) {
1935 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1936
1937 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1938 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1939}
1940
1941TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1942 StatementMatcher OperatorTrueFalse =
1943 binaryOperator(hasLHS(boolLiteral(equals(true))),
1944 hasRHS(boolLiteral(equals(false))));
1945
1946 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1947 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1948 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1949}
1950
1951TEST(MatchBinaryOperator, HasEitherOperand) {
1952 StatementMatcher HasOperand =
1953 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1954
1955 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1956 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1957 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1958}
1959
1960TEST(Matcher, BinaryOperatorTypes) {
1961 // Integration test that verifies the AST provides all binary operators in
1962 // a way we expect.
1963 // FIXME: Operator ','
1964 EXPECT_TRUE(
1965 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1966 EXPECT_TRUE(
1967 matches("bool b; bool c = (b = true);",
1968 binaryOperator(hasOperatorName("="))));
1969 EXPECT_TRUE(
1970 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1971 EXPECT_TRUE(
1972 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1973 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1974 EXPECT_TRUE(
1975 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1976 EXPECT_TRUE(
1977 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1978 EXPECT_TRUE(
1979 matches("int i = 1; int j = (i <<= 2);",
1980 binaryOperator(hasOperatorName("<<="))));
1981 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1982 EXPECT_TRUE(
1983 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1984 EXPECT_TRUE(
1985 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1986 EXPECT_TRUE(
1987 matches("int i = 1; int j = (i >>= 2);",
1988 binaryOperator(hasOperatorName(">>="))));
1989 EXPECT_TRUE(
1990 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1991 EXPECT_TRUE(
1992 matches("int i = 42; int j = (i ^= 42);",
1993 binaryOperator(hasOperatorName("^="))));
1994 EXPECT_TRUE(
1995 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1996 EXPECT_TRUE(
1997 matches("int i = 42; int j = (i %= 42);",
1998 binaryOperator(hasOperatorName("%="))));
1999 EXPECT_TRUE(
2000 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
2001 EXPECT_TRUE(
2002 matches("bool b = true && false;",
2003 binaryOperator(hasOperatorName("&&"))));
2004 EXPECT_TRUE(
2005 matches("bool b = true; bool c = (b &= false);",
2006 binaryOperator(hasOperatorName("&="))));
2007 EXPECT_TRUE(
2008 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
2009 EXPECT_TRUE(
2010 matches("bool b = true || false;",
2011 binaryOperator(hasOperatorName("||"))));
2012 EXPECT_TRUE(
2013 matches("bool b = true; bool c = (b |= false);",
2014 binaryOperator(hasOperatorName("|="))));
2015 EXPECT_TRUE(
2016 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2017 EXPECT_TRUE(
2018 matches("int i = 42; int j = (i *= 23);",
2019 binaryOperator(hasOperatorName("*="))));
2020 EXPECT_TRUE(
2021 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
2022 EXPECT_TRUE(
2023 matches("int i = 42; int j = (i /= 23);",
2024 binaryOperator(hasOperatorName("/="))));
2025 EXPECT_TRUE(
2026 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
2027 EXPECT_TRUE(
2028 matches("int i = 42; int j = (i += 23);",
2029 binaryOperator(hasOperatorName("+="))));
2030 EXPECT_TRUE(
2031 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
2032 EXPECT_TRUE(
2033 matches("int i = 42; int j = (i -= 23);",
2034 binaryOperator(hasOperatorName("-="))));
2035 EXPECT_TRUE(
2036 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2037 binaryOperator(hasOperatorName("->*"))));
2038 EXPECT_TRUE(
2039 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2040 binaryOperator(hasOperatorName(".*"))));
2041
2042 // Member expressions as operators are not supported in matches.
2043 EXPECT_TRUE(
2044 notMatches("struct A { void x(A *a) { a->x(this); } };",
2045 binaryOperator(hasOperatorName("->"))));
2046
2047 // Initializer assignments are not represented as operator equals.
2048 EXPECT_TRUE(
2049 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2050
2051 // Array indexing is not represented as operator.
2052 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2053
2054 // Overloaded operators do not match at all.
2055 EXPECT_TRUE(notMatches(
2056 "struct A { bool operator&&(const A &a) const { return false; } };"
2057 "void x() { A a, b; a && b; }",
2058 binaryOperator()));
2059}
2060
2061TEST(MatchUnaryOperator, HasOperatorName) {
2062 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2063
2064 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2065 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2066}
2067
2068TEST(MatchUnaryOperator, HasUnaryOperand) {
2069 StatementMatcher OperatorOnFalse =
2070 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2071
2072 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2073 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2074}
2075
2076TEST(Matcher, UnaryOperatorTypes) {
2077 // Integration test that verifies the AST provides all unary operators in
2078 // a way we expect.
2079 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2080 EXPECT_TRUE(
2081 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2082 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2083 EXPECT_TRUE(
2084 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2085 EXPECT_TRUE(
2086 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2087 EXPECT_TRUE(
2088 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2089 EXPECT_TRUE(
2090 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2091 EXPECT_TRUE(
2092 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2093 EXPECT_TRUE(
2094 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2095 EXPECT_TRUE(
2096 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2097
2098 // We don't match conversion operators.
2099 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2100
2101 // Function calls are not represented as operator.
2102 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2103
2104 // Overloaded operators do not match at all.
2105 // FIXME: We probably want to add that.
2106 EXPECT_TRUE(notMatches(
2107 "struct A { bool operator!() const { return false; } };"
2108 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2109}
2110
2111TEST(Matcher, ConditionalOperator) {
2112 StatementMatcher Conditional = conditionalOperator(
2113 hasCondition(boolLiteral(equals(true))),
2114 hasTrueExpression(boolLiteral(equals(false))));
2115
2116 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2117 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2118 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2119
2120 StatementMatcher ConditionalFalse = conditionalOperator(
2121 hasFalseExpression(boolLiteral(equals(false))));
2122
2123 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2124 EXPECT_TRUE(
2125 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2126}
2127
Daniel Jasper1dad1832012-07-10 20:20:19 +00002128TEST(ArraySubscriptMatchers, ArraySubscripts) {
2129 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2130 arraySubscriptExpr()));
2131 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2132 arraySubscriptExpr()));
2133}
2134
2135TEST(ArraySubscriptMatchers, ArrayIndex) {
2136 EXPECT_TRUE(matches(
2137 "int i[2]; void f() { i[1] = 1; }",
2138 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2139 EXPECT_TRUE(matches(
2140 "int i[2]; void f() { 1[i] = 1; }",
2141 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2142 EXPECT_TRUE(notMatches(
2143 "int i[2]; void f() { i[1] = 1; }",
2144 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2145}
2146
2147TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2148 EXPECT_TRUE(matches(
2149 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002150 arraySubscriptExpr(hasBase(implicitCastExpr(
2151 hasSourceExpression(declRefExpr()))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002152}
2153
Manuel Klimek04616e42012-07-06 05:48:52 +00002154TEST(Matcher, HasNameSupportsNamespaces) {
2155 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002156 recordDecl(hasName("a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002157 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002158 recordDecl(hasName("::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002159 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002160 recordDecl(hasName("b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002161 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002162 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002163 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002164 recordDecl(hasName("c::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002165 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002166 recordDecl(hasName("a::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002167 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002168 recordDecl(hasName("a::b::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002169 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002170 recordDecl(hasName("::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002171 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002172 recordDecl(hasName("::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002173 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002174 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002175 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002176 recordDecl(hasName("a+b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002177 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002178 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002179}
2180
2181TEST(Matcher, HasNameSupportsOuterClasses) {
2182 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002183 matches("class A { class B { class C; }; };",
2184 recordDecl(hasName("A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002185 EXPECT_TRUE(
2186 matches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002187 recordDecl(hasName("::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002188 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002189 matches("class A { class B { class C; }; };",
2190 recordDecl(hasName("B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002191 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002192 matches("class A { class B { class C; }; };",
2193 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002194 EXPECT_TRUE(
2195 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002196 recordDecl(hasName("c::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002197 EXPECT_TRUE(
2198 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002199 recordDecl(hasName("A::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002200 EXPECT_TRUE(
2201 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002202 recordDecl(hasName("A::B::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002203 EXPECT_TRUE(
2204 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002205 recordDecl(hasName("::C"))));
2206 EXPECT_TRUE(
2207 notMatches("class A { class B { class C; }; };",
2208 recordDecl(hasName("::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002209 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002210 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002211 EXPECT_TRUE(
2212 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002213 recordDecl(hasName("A+B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002214}
2215
2216TEST(Matcher, IsDefinition) {
2217 DeclarationMatcher DefinitionOfClassA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002218 recordDecl(hasName("A"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002219 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2220 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2221
2222 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002223 varDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002224 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2225 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2226
2227 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002228 methodDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002229 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2230 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2231}
2232
2233TEST(Matcher, OfClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002234 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +00002235 ofClass(hasName("X")))));
2236
2237 EXPECT_TRUE(
2238 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2239 EXPECT_TRUE(
2240 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2241 Constructor));
2242 EXPECT_TRUE(
2243 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2244 Constructor));
2245}
2246
2247TEST(Matcher, VisitsTemplateInstantiations) {
2248 EXPECT_TRUE(matches(
2249 "class A { public: void x(); };"
2250 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002251 "void f() { B<A> b; b.y(); }",
2252 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002253
2254 EXPECT_TRUE(matches(
2255 "class A { public: void x(); };"
2256 "class C {"
2257 " public:"
2258 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2259 "};"
2260 "void f() {"
2261 " C::B<A> b; b.y();"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002262 "}",
2263 recordDecl(hasName("C"),
2264 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002265}
2266
Daniel Jasper1dad1832012-07-10 20:20:19 +00002267TEST(Matcher, HandlesNullQualTypes) {
2268 // FIXME: Add a Type matcher so we can replace uses of this
2269 // variable with Type(True())
2270 const TypeMatcher AnyType = anything();
2271
2272 // We don't really care whether this matcher succeeds; we're testing that
2273 // it completes without crashing.
2274 EXPECT_TRUE(matches(
2275 "struct A { };"
2276 "template <typename T>"
2277 "void f(T t) {"
2278 " T local_t(t /* this becomes a null QualType in the AST */);"
2279 "}"
2280 "void g() {"
2281 " f(0);"
2282 "}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002283 expr(hasType(TypeMatcher(
Daniel Jasper1dad1832012-07-10 20:20:19 +00002284 anyOf(
2285 TypeMatcher(hasDeclaration(anything())),
2286 pointsTo(AnyType),
2287 references(AnyType)
2288 // Other QualType matchers should go here.
2289 ))))));
2290}
2291
Manuel Klimek04616e42012-07-06 05:48:52 +00002292// For testing AST_MATCHER_P().
Daniel Jasper1dad1832012-07-10 20:20:19 +00002293AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002294 // Make sure all special variables are used: node, match_finder,
2295 // bound_nodes_builder, and the parameter named 'AMatcher'.
2296 return AMatcher.matches(Node, Finder, Builder);
2297}
2298
2299TEST(AstMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002300 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002301
2302 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002303 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002304
2305 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002306 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002307
2308 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002309 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002310}
2311
2312AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +00002313 polymorphicHas,
2314 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2315 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002316 return Finder->matchesChildOf(
Manuel Klimekeb958de2012-09-05 12:12:07 +00002317 Node, AMatcher, Builder,
Manuel Klimek04616e42012-07-06 05:48:52 +00002318 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2319 ASTMatchFinder::BK_First);
2320}
2321
2322TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002323 DeclarationMatcher HasClassB =
2324 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek04616e42012-07-06 05:48:52 +00002325
2326 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002327 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002328
2329 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002330 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002331
2332 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002333 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002334
2335 StatementMatcher StatementHasClassB =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002336 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002337
2338 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2339}
2340
2341TEST(For, FindsForLoops) {
2342 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2343 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper6f595392012-10-01 15:05:34 +00002344 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2345 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00002346 forStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002347}
2348
Daniel Jasper4e566c42012-07-12 08:50:38 +00002349TEST(For, ForLoopInternals) {
2350 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2351 forStmt(hasCondition(anything()))));
2352 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2353 forStmt(hasLoopInit(anything()))));
2354}
2355
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002356TEST(For, ForRangeLoopInternals) {
2357 EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2358 forRangeStmt(hasLoopVariable(anything()))));
2359}
2360
Daniel Jasper4e566c42012-07-12 08:50:38 +00002361TEST(For, NegativeForLoopInternals) {
2362 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002363 forStmt(hasCondition(expr()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002364 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2365 forStmt(hasLoopInit(anything()))));
2366}
2367
Manuel Klimek04616e42012-07-06 05:48:52 +00002368TEST(For, ReportsNoFalsePositives) {
2369 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2370 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2371}
2372
2373TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002374 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2375 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2376 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002377}
2378
2379TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2380 // It's not a compound statement just because there's "{}" in the source
2381 // text. This is an AST search, not grep.
2382 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002383 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002384 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002385 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002386}
2387
Daniel Jasper4e566c42012-07-12 08:50:38 +00002388TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002389 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002390 forStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002391 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002392 forStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002393 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002394 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002395 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002396 doStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002397}
2398
2399TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2400 // The simplest case: every compound statement is in a function
2401 // definition, and the function body itself must be a compound
2402 // statement.
2403 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002404 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002405}
2406
2407TEST(HasAnySubstatement, IsNotRecursive) {
2408 // It's really "has any immediate substatement".
2409 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002410 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002411}
2412
2413TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2414 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002415 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002416}
2417
2418TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2419 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002420 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002421}
2422
2423TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2424 EXPECT_TRUE(matches("void f() { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002425 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002426 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002427 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002428}
2429
2430TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2431 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002432 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002433 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002434 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002435 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002436 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002437}
2438
2439TEST(StatementCountIs, WorksWithMultipleStatements) {
2440 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002441 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002442}
2443
2444TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2445 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002446 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002447 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002448 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002449 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002450 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002451 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002452 compoundStmt(statementCountIs(4))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002453}
2454
2455TEST(Member, WorksInSimplestCase) {
2456 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002457 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002458}
2459
2460TEST(Member, DoesNotMatchTheBaseExpression) {
2461 // Don't pick out the wrong part of the member expression, this should
2462 // be checking the member (name) only.
2463 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002464 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002465}
2466
2467TEST(Member, MatchesInMemberFunctionCall) {
2468 EXPECT_TRUE(matches("void f() {"
2469 " struct { void first() {}; } s;"
2470 " s.first();"
2471 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002472 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002473}
2474
Daniel Jasperb0c7b612012-10-23 15:46:39 +00002475TEST(Member, MatchesMember) {
2476 EXPECT_TRUE(matches(
2477 "struct A { int i; }; void f() { A a; a.i = 2; }",
2478 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2479 EXPECT_TRUE(notMatches(
2480 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2481 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2482}
2483
Daniel Jasper639522c2013-02-25 12:02:08 +00002484TEST(Member, UnderstandsAccess) {
2485 EXPECT_TRUE(matches(
2486 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2487 EXPECT_TRUE(notMatches(
2488 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2489 EXPECT_TRUE(notMatches(
2490 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2491
2492 EXPECT_TRUE(notMatches(
2493 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2494 EXPECT_TRUE(notMatches(
2495 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2496 EXPECT_TRUE(matches(
2497 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2498
2499 EXPECT_TRUE(notMatches(
2500 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2501 EXPECT_TRUE(matches("class A { protected: int i; };",
2502 fieldDecl(isProtected(), hasName("i"))));
2503 EXPECT_TRUE(notMatches(
2504 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2505
2506 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2507 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2508 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2509 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2510}
2511
Dmitri Gribenko06963042012-08-18 00:29:27 +00002512TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper5901e472012-10-01 13:40:41 +00002513 // Fails in C++11 mode
2514 EXPECT_TRUE(matchesConditionally(
2515 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2516 "class X { void *operator new(std::size_t); };",
2517 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002518
2519 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002520 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002521
Daniel Jasper5901e472012-10-01 13:40:41 +00002522 // Fails in C++11 mode
2523 EXPECT_TRUE(matchesConditionally(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002524 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2525 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper5901e472012-10-01 13:40:41 +00002526 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002527}
2528
Manuel Klimek04616e42012-07-06 05:48:52 +00002529TEST(HasObjectExpression, DoesNotMatchMember) {
2530 EXPECT_TRUE(notMatches(
2531 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002532 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002533}
2534
2535TEST(HasObjectExpression, MatchesBaseOfVariable) {
2536 EXPECT_TRUE(matches(
2537 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002538 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002539 EXPECT_TRUE(matches(
2540 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002541 memberExpr(hasObjectExpression(
2542 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002543}
2544
2545TEST(HasObjectExpression,
2546 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2547 EXPECT_TRUE(matches(
2548 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002549 memberExpr(hasObjectExpression(
2550 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002551 EXPECT_TRUE(matches(
2552 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002553 memberExpr(hasObjectExpression(
2554 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002555}
2556
2557TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002558 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2559 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2560 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2561 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002562}
2563
2564TEST(Field, MatchesField) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002565 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002566}
2567
2568TEST(IsConstQualified, MatchesConstInt) {
2569 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002570 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002571}
2572
2573TEST(IsConstQualified, MatchesConstPointer) {
2574 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002575 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002576}
2577
2578TEST(IsConstQualified, MatchesThroughTypedef) {
2579 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002580 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002581 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002582 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002583}
2584
2585TEST(IsConstQualified, DoesNotMatchInappropriately) {
2586 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002587 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002588 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002589 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002590}
2591
Sam Panzer80c13772012-08-16 16:58:10 +00002592TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002593 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2594 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2595 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2596 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002597}
2598TEST(CastExpression, MatchesImplicitCasts) {
2599 // This test creates an implicit cast from int to char.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002600 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002601 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002602 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002603}
2604
2605TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002606 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2607 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2608 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2609 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002610}
2611
Manuel Klimek04616e42012-07-06 05:48:52 +00002612TEST(ReinterpretCast, MatchesSimpleCase) {
2613 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002614 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002615}
2616
2617TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002618 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002619 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002620 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002621 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002622 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002623 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2624 "B b;"
2625 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002626 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002627}
2628
2629TEST(FunctionalCast, MatchesSimpleCase) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002630 std::string foo_class = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002631 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002632 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002633}
2634
2635TEST(FunctionalCast, DoesNotMatchOtherCasts) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002636 std::string FooClass = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002637 EXPECT_TRUE(
2638 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002639 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002640 EXPECT_TRUE(
2641 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002642 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002643}
2644
2645TEST(DynamicCast, MatchesSimpleCase) {
2646 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2647 "B b;"
2648 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002649 dynamicCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002650}
2651
2652TEST(StaticCast, MatchesSimpleCase) {
2653 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002654 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002655}
2656
2657TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002658 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002659 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002660 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002661 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002662 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002663 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2664 "B b;"
2665 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002666 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002667}
2668
Daniel Jasper417f7762012-09-18 13:36:17 +00002669TEST(CStyleCast, MatchesSimpleCase) {
2670 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2671}
2672
2673TEST(CStyleCast, DoesNotMatchOtherCasts) {
2674 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2675 "char q, *r = const_cast<char*>(&q);"
2676 "void* s = reinterpret_cast<char*>(&s);"
2677 "struct B { virtual ~B() {} }; struct D : B {};"
2678 "B b;"
2679 "D* t = dynamic_cast<D*>(&b);",
2680 cStyleCastExpr()));
2681}
2682
Manuel Klimek04616e42012-07-06 05:48:52 +00002683TEST(HasDestinationType, MatchesSimpleCase) {
2684 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002685 staticCastExpr(hasDestinationType(
2686 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002687}
2688
Sam Panzer80c13772012-08-16 16:58:10 +00002689TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2690 // This test creates an implicit const cast.
2691 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002692 implicitCastExpr(
2693 hasImplicitDestinationType(isInteger()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002694 // This test creates an implicit array-to-pointer cast.
2695 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002696 implicitCastExpr(hasImplicitDestinationType(
2697 pointsTo(TypeMatcher(anything()))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002698}
2699
2700TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2701 // This test creates an implicit cast from int to char.
2702 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002703 implicitCastExpr(hasImplicitDestinationType(
2704 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002705 // This test creates an implicit array-to-pointer cast.
2706 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002707 implicitCastExpr(hasImplicitDestinationType(
2708 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002709}
2710
2711TEST(ImplicitCast, MatchesSimpleCase) {
2712 // This test creates an implicit const cast.
2713 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002714 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002715 // This test creates an implicit cast from int to char.
2716 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002717 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002718 // This test creates an implicit array-to-pointer cast.
2719 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002720 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002721}
2722
2723TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002724 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer80c13772012-08-16 16:58:10 +00002725 // are present, and that it ignores explicit and paren casts.
2726
2727 // These two test cases have no casts.
2728 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002729 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002730 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002731 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002732
2733 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002734 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002735 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002736 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002737
2738 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002739 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002740}
2741
2742TEST(IgnoringImpCasts, MatchesImpCasts) {
2743 // This test checks that ignoringImpCasts matches when implicit casts are
2744 // present and its inner matcher alone does not match.
2745 // Note that this test creates an implicit const cast.
2746 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002747 varDecl(hasInitializer(ignoringImpCasts(
2748 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002749 // This test creates an implict cast from int to char.
2750 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002751 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002752 integerLiteral(equals(0)))))));
2753}
2754
2755TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2756 // These tests verify that ignoringImpCasts does not match if the inner
2757 // matcher does not match.
2758 // Note that the first test creates an implicit const cast.
2759 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002760 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002761 unless(anything()))))));
2762 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002763 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002764 unless(anything()))))));
2765
2766 // These tests verify that ignoringImplictCasts does not look through explicit
2767 // casts or parentheses.
2768 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002769 varDecl(hasInitializer(ignoringImpCasts(
2770 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002771 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002772 varDecl(hasInitializer(ignoringImpCasts(
2773 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002774 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002775 varDecl(hasInitializer(ignoringImpCasts(
2776 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002777 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002778 varDecl(hasInitializer(ignoringImpCasts(
2779 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002780}
2781
2782TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2783 // This test verifies that expressions that do not have implicit casts
2784 // still match the inner matcher.
2785 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002786 varDecl(hasInitializer(ignoringImpCasts(
2787 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002788}
2789
2790TEST(IgnoringParenCasts, MatchesParenCasts) {
2791 // This test checks that ignoringParenCasts matches when parentheses and/or
2792 // casts are present and its inner matcher alone does not match.
2793 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002794 varDecl(hasInitializer(ignoringParenCasts(
2795 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002796 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002797 varDecl(hasInitializer(ignoringParenCasts(
2798 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002799
2800 // This test creates an implict cast from int to char in addition to the
2801 // parentheses.
2802 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002803 varDecl(hasInitializer(ignoringParenCasts(
2804 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002805
2806 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002807 varDecl(hasInitializer(ignoringParenCasts(
2808 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002809 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002810 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002811 integerLiteral(equals(0)))))));
2812}
2813
2814TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2815 // This test verifies that expressions that do not have any casts still match.
2816 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002817 varDecl(hasInitializer(ignoringParenCasts(
2818 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002819}
2820
2821TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2822 // These tests verify that ignoringImpCasts does not match if the inner
2823 // matcher does not match.
2824 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002825 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002826 unless(anything()))))));
2827
2828 // This test creates an implicit cast from int to char in addition to the
2829 // parentheses.
2830 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002831 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002832 unless(anything()))))));
2833
2834 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002835 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002836 unless(anything()))))));
2837}
2838
2839TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2840 // This test checks that ignoringParenAndImpCasts matches when
2841 // parentheses and/or implicit casts are present and its inner matcher alone
2842 // does not match.
2843 // Note that this test creates an implicit const cast.
2844 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002845 varDecl(hasInitializer(ignoringParenImpCasts(
2846 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002847 // This test creates an implicit cast from int to char.
2848 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002849 varDecl(hasInitializer(ignoringParenImpCasts(
2850 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002851}
2852
2853TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2854 // This test verifies that expressions that do not have parentheses or
2855 // implicit casts still match.
2856 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002857 varDecl(hasInitializer(ignoringParenImpCasts(
2858 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002859 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002860 varDecl(hasInitializer(ignoringParenImpCasts(
2861 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002862}
2863
2864TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2865 // These tests verify that ignoringParenImpCasts does not match if
2866 // the inner matcher does not match.
2867 // This test creates an implicit cast.
2868 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002869 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002870 unless(anything()))))));
2871 // These tests verify that ignoringParenAndImplictCasts does not look
2872 // through explicit casts.
2873 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002874 varDecl(hasInitializer(ignoringParenImpCasts(
2875 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002876 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002877 varDecl(hasInitializer(ignoringParenImpCasts(
2878 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002879 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002880 varDecl(hasInitializer(ignoringParenImpCasts(
2881 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002882}
2883
Manuel Klimeke9235692012-07-25 10:02:02 +00002884TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002885 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2886 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002887 implicitCastExpr(
2888 hasSourceExpression(constructExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002889}
2890
Manuel Klimeke9235692012-07-25 10:02:02 +00002891TEST(HasSourceExpression, MatchesExplicitCasts) {
2892 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002893 explicitCastExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002894 hasSourceExpression(hasDescendant(
Daniel Jasper848cbe12012-09-18 13:09:13 +00002895 expr(integerLiteral()))))));
Manuel Klimeke9235692012-07-25 10:02:02 +00002896}
2897
Manuel Klimek04616e42012-07-06 05:48:52 +00002898TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002899 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002900}
2901
2902TEST(Statement, MatchesCompoundStatments) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002903 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002904}
2905
2906TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002907 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002908}
2909
2910TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002911 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002912}
2913
Daniel Jasper1dad1832012-07-10 20:20:19 +00002914TEST(InitListExpression, MatchesInitListExpression) {
2915 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2916 initListExpr(hasType(asString("int [2]")))));
2917 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002918 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002919}
2920
2921TEST(UsingDeclaration, MatchesUsingDeclarations) {
2922 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2923 usingDecl()));
2924}
2925
2926TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2927 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2928 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2929}
2930
2931TEST(UsingDeclaration, MatchesSpecificTarget) {
2932 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2933 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002934 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002935 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2936 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002937 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002938}
2939
2940TEST(UsingDeclaration, ThroughUsingDeclaration) {
2941 EXPECT_TRUE(matches(
2942 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002943 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002944 EXPECT_TRUE(notMatches(
2945 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002946 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002947}
2948
Sam Panzerd624bfb2012-08-16 17:20:59 +00002949TEST(SingleDecl, IsSingleDecl) {
2950 StatementMatcher SingleDeclStmt =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002951 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002952 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2953 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2954 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2955 SingleDeclStmt));
2956}
2957
2958TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002959 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002960
2961 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002962 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002963 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002964 declStmt(containsDeclaration(0, MatchesInit),
2965 containsDeclaration(1, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002966 unsigned WrongIndex = 42;
2967 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002968 declStmt(containsDeclaration(WrongIndex,
Sam Panzerd624bfb2012-08-16 17:20:59 +00002969 MatchesInit))));
2970}
2971
2972TEST(DeclCount, DeclCountIsCorrect) {
2973 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002974 declStmt(declCountIs(2))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002975 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002976 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002977 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002978 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002979}
2980
Manuel Klimek04616e42012-07-06 05:48:52 +00002981TEST(While, MatchesWhileLoops) {
2982 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2983 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2984 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2985}
2986
2987TEST(Do, MatchesDoLoops) {
2988 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2989 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2990}
2991
2992TEST(Do, DoesNotMatchWhileLoops) {
2993 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2994}
2995
2996TEST(SwitchCase, MatchesCase) {
2997 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2998 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2999 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
3000 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
3001}
3002
Daniel Jasper87c3d362012-09-20 14:12:57 +00003003TEST(SwitchCase, MatchesSwitch) {
3004 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
3005 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
3006 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
3007 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
3008}
3009
Peter Collingbourne3154a102013-05-10 11:52:02 +00003010TEST(SwitchCase, MatchesEachCase) {
3011 EXPECT_TRUE(notMatches("void x() { switch(42); }",
3012 switchStmt(forEachSwitchCase(caseStmt()))));
3013 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
3014 switchStmt(forEachSwitchCase(caseStmt()))));
3015 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
3016 switchStmt(forEachSwitchCase(caseStmt()))));
3017 EXPECT_TRUE(notMatches(
3018 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3019 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3020 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3021 switchStmt(forEachSwitchCase(
3022 caseStmt(hasCaseConstant(integerLiteral()))))));
3023 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3024 switchStmt(forEachSwitchCase(
3025 caseStmt(hasCaseConstant(integerLiteral()))))));
3026 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3027 switchStmt(forEachSwitchCase(
3028 caseStmt(hasCaseConstant(integerLiteral()))))));
3029 EXPECT_TRUE(matchAndVerifyResultTrue(
3030 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3031 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3032 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3033}
3034
Manuel Klimekba46fc02013-07-19 11:50:54 +00003035TEST(ForEachConstructorInitializer, MatchesInitializers) {
3036 EXPECT_TRUE(matches(
3037 "struct X { X() : i(42), j(42) {} int i, j; };",
3038 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3039}
3040
Daniel Jasper87c3d362012-09-20 14:12:57 +00003041TEST(ExceptionHandling, SimpleCases) {
3042 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3043 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3044 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3045 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3046 throwExpr()));
3047 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3048 throwExpr()));
3049}
3050
Manuel Klimek04616e42012-07-06 05:48:52 +00003051TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3052 EXPECT_TRUE(notMatches(
3053 "void x() { if(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003054 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003055 EXPECT_TRUE(notMatches(
3056 "void x() { int x; if((x = 42)) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003057 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003058}
3059
3060TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3061 EXPECT_TRUE(matches(
3062 "void x() { if(int* a = 0) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003063 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003064}
3065
3066TEST(ForEach, BindsOneNode) {
3067 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003068 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003069 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003070}
3071
3072TEST(ForEach, BindsMultipleNodes) {
3073 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003074 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003075 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003076}
3077
3078TEST(ForEach, BindsRecursiveCombinations) {
3079 EXPECT_TRUE(matchAndVerifyResultTrue(
3080 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003081 recordDecl(hasName("C"),
3082 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003083 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003084}
3085
3086TEST(ForEachDescendant, BindsOneNode) {
3087 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003088 recordDecl(hasName("C"),
3089 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003090 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003091}
3092
Daniel Jasper94a56852012-11-16 18:39:22 +00003093TEST(ForEachDescendant, NestedForEachDescendant) {
3094 DeclarationMatcher m = recordDecl(
3095 isDefinition(), decl().bind("x"), hasName("C"));
3096 EXPECT_TRUE(matchAndVerifyResultTrue(
3097 "class A { class B { class C {}; }; };",
3098 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3099 new VerifyIdIsBoundTo<Decl>("x", "C")));
3100
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003101 // Check that a partial match of 'm' that binds 'x' in the
3102 // first part of anyOf(m, anything()) will not overwrite the
3103 // binding created by the earlier binding in the hasDescendant.
3104 EXPECT_TRUE(matchAndVerifyResultTrue(
3105 "class A { class B { class C {}; }; };",
3106 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3107 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper94a56852012-11-16 18:39:22 +00003108}
3109
Manuel Klimek04616e42012-07-06 05:48:52 +00003110TEST(ForEachDescendant, BindsMultipleNodes) {
3111 EXPECT_TRUE(matchAndVerifyResultTrue(
3112 "class C { class D { int x; int y; }; "
3113 " class E { class F { int y; int z; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003114 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003115 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003116}
3117
3118TEST(ForEachDescendant, BindsRecursiveCombinations) {
3119 EXPECT_TRUE(matchAndVerifyResultTrue(
3120 "class C { class D { "
3121 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003122 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3123 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003124 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003125}
3126
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003127TEST(ForEachDescendant, BindsCombinations) {
3128 EXPECT_TRUE(matchAndVerifyResultTrue(
3129 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3130 "(true) {} }",
3131 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3132 forEachDescendant(whileStmt().bind("while"))),
3133 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3134}
3135
3136TEST(Has, DoesNotDeleteBindings) {
3137 EXPECT_TRUE(matchAndVerifyResultTrue(
3138 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3139 new VerifyIdIsBoundTo<Decl>("x", 1)));
3140}
3141
3142TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3143 // Those matchers cover all the cases where an inner matcher is called
3144 // and there is not a 1:1 relationship between the match of the outer
3145 // matcher and the match of the inner matcher.
3146 // The pattern to look for is:
3147 // ... return InnerMatcher.matches(...); ...
3148 // In which case no special handling is needed.
3149 //
3150 // On the other hand, if there are multiple alternative matches
3151 // (for example forEach*) or matches might be discarded (for example has*)
3152 // the implementation must make sure that the discarded matches do not
3153 // affect the bindings.
3154 // When new such matchers are added, add a test here that:
3155 // - matches a simple node, and binds it as the first thing in the matcher:
3156 // recordDecl(decl().bind("x"), hasName("X")))
3157 // - uses the matcher under test afterwards in a way that not the first
3158 // alternative is matched; for anyOf, that means the first branch
3159 // would need to return false; for hasAncestor, it means that not
3160 // the direct parent matches the inner matcher.
3161
3162 EXPECT_TRUE(matchAndVerifyResultTrue(
3163 "class X { int y; };",
3164 recordDecl(
3165 recordDecl().bind("x"), hasName("::X"),
3166 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3167 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3168 EXPECT_TRUE(matchAndVerifyResultTrue(
3169 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3170 anyOf(unless(anything()), anything())),
3171 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3172 EXPECT_TRUE(matchAndVerifyResultTrue(
3173 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3174 classTemplateSpecializationDecl(
3175 decl().bind("x"),
3176 hasAnyTemplateArgument(refersToType(asString("int")))),
3177 new VerifyIdIsBoundTo<Decl>("x", 1)));
3178 EXPECT_TRUE(matchAndVerifyResultTrue(
3179 "class X { void f(); void g(); };",
3180 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3181 new VerifyIdIsBoundTo<Decl>("x", 1)));
3182 EXPECT_TRUE(matchAndVerifyResultTrue(
3183 "class X { X() : a(1), b(2) {} double a; int b; };",
3184 recordDecl(decl().bind("x"),
3185 has(constructorDecl(
3186 hasAnyConstructorInitializer(forField(hasName("b")))))),
3187 new VerifyIdIsBoundTo<Decl>("x", 1)));
3188 EXPECT_TRUE(matchAndVerifyResultTrue(
3189 "void x(int, int) { x(0, 42); }",
3190 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3191 new VerifyIdIsBoundTo<Expr>("x", 1)));
3192 EXPECT_TRUE(matchAndVerifyResultTrue(
3193 "void x(int, int y) {}",
3194 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3195 new VerifyIdIsBoundTo<Decl>("x", 1)));
3196 EXPECT_TRUE(matchAndVerifyResultTrue(
3197 "void x() { return; if (true) {} }",
3198 functionDecl(decl().bind("x"),
3199 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3200 new VerifyIdIsBoundTo<Decl>("x", 1)));
3201 EXPECT_TRUE(matchAndVerifyResultTrue(
3202 "namespace X { void b(int); void b(); }"
3203 "using X::b;",
3204 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3205 functionDecl(parameterCountIs(1))))),
3206 new VerifyIdIsBoundTo<Decl>("x", 1)));
3207 EXPECT_TRUE(matchAndVerifyResultTrue(
3208 "class A{}; class B{}; class C : B, A {};",
3209 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3210 new VerifyIdIsBoundTo<Decl>("x", 1)));
3211 EXPECT_TRUE(matchAndVerifyResultTrue(
3212 "class A{}; typedef A B; typedef A C; typedef A D;"
3213 "class E : A {};",
3214 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3215 new VerifyIdIsBoundTo<Decl>("x", 1)));
3216 EXPECT_TRUE(matchAndVerifyResultTrue(
3217 "class A { class B { void f() {} }; };",
3218 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3219 new VerifyIdIsBoundTo<Decl>("x", 1)));
3220 EXPECT_TRUE(matchAndVerifyResultTrue(
3221 "template <typename T> struct A { struct B {"
3222 " void f() { if(true) {} }"
3223 "}; };"
3224 "void t() { A<int>::B b; b.f(); }",
3225 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3226 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3227 EXPECT_TRUE(matchAndVerifyResultTrue(
3228 "class A {};",
3229 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3230 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimekba46fc02013-07-19 11:50:54 +00003231 EXPECT_TRUE(matchAndVerifyResultTrue(
3232 "class A { A() : s(), i(42) {} const char *s; int i; };",
3233 constructorDecl(hasName("::A::A"), decl().bind("x"),
3234 forEachConstructorInitializer(forField(hasName("i")))),
3235 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003236}
3237
Daniel Jasper33806cd2012-11-11 22:14:55 +00003238TEST(ForEachDescendant, BindsCorrectNodes) {
3239 EXPECT_TRUE(matchAndVerifyResultTrue(
3240 "class C { void f(); int i; };",
3241 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3242 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3243 EXPECT_TRUE(matchAndVerifyResultTrue(
3244 "class C { void f() {} int i; };",
3245 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3246 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3247}
3248
Manuel Klimekabf43712013-02-04 10:59:20 +00003249TEST(FindAll, BindsNodeOnMatch) {
3250 EXPECT_TRUE(matchAndVerifyResultTrue(
3251 "class A {};",
3252 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3253 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3254}
3255
3256TEST(FindAll, BindsDescendantNodeOnMatch) {
3257 EXPECT_TRUE(matchAndVerifyResultTrue(
3258 "class A { int a; int b; };",
3259 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3260 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3261}
3262
3263TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3264 EXPECT_TRUE(matchAndVerifyResultTrue(
3265 "class A { int a; int b; };",
3266 recordDecl(hasName("::A"),
3267 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3268 fieldDecl().bind("v"))))),
3269 new VerifyIdIsBoundTo<Decl>("v", 3)));
3270
3271 EXPECT_TRUE(matchAndVerifyResultTrue(
3272 "class A { class B {}; class C {}; };",
3273 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3274 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3275}
3276
Manuel Klimek88b95872013-02-04 09:42:38 +00003277TEST(EachOf, TriggersForEachMatch) {
3278 EXPECT_TRUE(matchAndVerifyResultTrue(
3279 "class A { int a; int b; };",
3280 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3281 has(fieldDecl(hasName("b")).bind("v")))),
3282 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3283}
3284
3285TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3286 EXPECT_TRUE(matchAndVerifyResultTrue(
3287 "class A { int a; int c; };",
3288 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3289 has(fieldDecl(hasName("b")).bind("v")))),
3290 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3291 EXPECT_TRUE(matchAndVerifyResultTrue(
3292 "class A { int c; int b; };",
3293 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3294 has(fieldDecl(hasName("b")).bind("v")))),
3295 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3296 EXPECT_TRUE(notMatches(
3297 "class A { int c; int d; };",
3298 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3299 has(fieldDecl(hasName("b")).bind("v"))))));
3300}
Manuel Klimek04616e42012-07-06 05:48:52 +00003301
3302TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3303 // Make sure that we can both match the class by name (::X) and by the type
3304 // the template was instantiated with (via a field).
3305
3306 EXPECT_TRUE(matches(
3307 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003308 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003309
3310 EXPECT_TRUE(matches(
3311 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003312 recordDecl(isTemplateInstantiation(), hasDescendant(
3313 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003314}
3315
3316TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3317 EXPECT_TRUE(matches(
3318 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003319 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek04616e42012-07-06 05:48:52 +00003320 isTemplateInstantiation())));
3321}
3322
3323TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3324 EXPECT_TRUE(matches(
3325 "template <typename T> class X { T t; }; class A {};"
3326 "template class X<A>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003327 recordDecl(isTemplateInstantiation(), hasDescendant(
3328 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003329}
3330
3331TEST(IsTemplateInstantiation,
3332 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3333 EXPECT_TRUE(matches(
3334 "template <typename T> class X {};"
3335 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003336 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003337}
3338
3339TEST(IsTemplateInstantiation,
3340 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3341 EXPECT_TRUE(matches(
3342 "class A {};"
3343 "class X {"
3344 " template <typename U> class Y { U u; };"
3345 " Y<A> y;"
3346 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003347 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003348}
3349
3350TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3351 // FIXME: Figure out whether this makes sense. It doesn't affect the
3352 // normal use case as long as the uppermost instantiation always is marked
3353 // as template instantiation, but it might be confusing as a predicate.
3354 EXPECT_TRUE(matches(
3355 "class A {};"
3356 "template <typename T> class X {"
3357 " template <typename U> class Y { U u; };"
3358 " Y<T> y;"
3359 "}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003360 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003361}
3362
3363TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3364 EXPECT_TRUE(notMatches(
3365 "template <typename T> class X {}; class A {};"
3366 "template <> class X<A> {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003367 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003368}
3369
3370TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3371 EXPECT_TRUE(notMatches(
3372 "class A {}; class Y { A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003373 recordDecl(isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003374}
3375
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003376TEST(IsExplicitTemplateSpecialization,
3377 DoesNotMatchPrimaryTemplate) {
3378 EXPECT_TRUE(notMatches(
3379 "template <typename T> class X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003380 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003381 EXPECT_TRUE(notMatches(
3382 "template <typename T> void f(T t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003383 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003384}
3385
3386TEST(IsExplicitTemplateSpecialization,
3387 DoesNotMatchExplicitTemplateInstantiations) {
3388 EXPECT_TRUE(notMatches(
3389 "template <typename T> class X {};"
3390 "template class X<int>; extern template class X<long>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003391 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003392 EXPECT_TRUE(notMatches(
3393 "template <typename T> void f(T t) {}"
3394 "template void f(int t); extern template void f(long t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003395 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003396}
3397
3398TEST(IsExplicitTemplateSpecialization,
3399 DoesNotMatchImplicitTemplateInstantiations) {
3400 EXPECT_TRUE(notMatches(
3401 "template <typename T> class X {}; X<int> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003402 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003403 EXPECT_TRUE(notMatches(
3404 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003405 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003406}
3407
3408TEST(IsExplicitTemplateSpecialization,
3409 MatchesExplicitTemplateSpecializations) {
3410 EXPECT_TRUE(matches(
3411 "template <typename T> class X {};"
3412 "template<> class X<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003413 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003414 EXPECT_TRUE(matches(
3415 "template <typename T> void f(T t) {}"
3416 "template<> void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003417 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003418}
3419
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003420TEST(HasAncenstor, MatchesDeclarationAncestors) {
3421 EXPECT_TRUE(matches(
3422 "class A { class B { class C {}; }; };",
3423 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3424}
3425
3426TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3427 EXPECT_TRUE(notMatches(
3428 "class A { class B { class C {}; }; };",
3429 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3430}
3431
3432TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3433 EXPECT_TRUE(matches(
3434 "class A { class B { void f() { C c; } class C {}; }; };",
3435 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3436 hasAncestor(recordDecl(hasName("A"))))))));
3437}
3438
3439TEST(HasAncenstor, MatchesStatementAncestors) {
3440 EXPECT_TRUE(matches(
3441 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003442 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003443}
3444
3445TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3446 EXPECT_TRUE(matches(
3447 "void f() { if (true) { int x = 42; } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003448 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003449}
3450
3451TEST(HasAncestor, BindsRecursiveCombinations) {
3452 EXPECT_TRUE(matchAndVerifyResultTrue(
3453 "class C { class D { class E { class F { int y; }; }; }; };",
3454 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003455 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003456}
3457
3458TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3459 EXPECT_TRUE(matchAndVerifyResultTrue(
3460 "class C { class D { class E { class F { int y; }; }; }; };",
3461 fieldDecl(hasAncestor(
3462 decl(
3463 hasDescendant(recordDecl(isDefinition(),
3464 hasAncestor(recordDecl())))
3465 ).bind("d")
3466 )),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003467 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003468}
3469
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003470TEST(HasAncestor, MatchesClosestAncestor) {
3471 EXPECT_TRUE(matchAndVerifyResultTrue(
3472 "template <typename T> struct C {"
3473 " void f(int) {"
3474 " struct I { void g(T) { int x; } } i; i.g(42);"
3475 " }"
3476 "};"
3477 "template struct C<int>;",
3478 varDecl(hasName("x"),
3479 hasAncestor(functionDecl(hasParameter(
3480 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3481 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3482}
3483
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003484TEST(HasAncestor, MatchesInTemplateInstantiations) {
3485 EXPECT_TRUE(matches(
3486 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3487 "A<int>::B::C a;",
3488 fieldDecl(hasType(asString("int")),
3489 hasAncestor(recordDecl(hasName("A"))))));
3490}
3491
3492TEST(HasAncestor, MatchesInImplicitCode) {
3493 EXPECT_TRUE(matches(
3494 "struct X {}; struct A { A() {} X x; };",
3495 constructorDecl(
3496 hasAnyConstructorInitializer(withInitializer(expr(
3497 hasAncestor(recordDecl(hasName("A")))))))));
3498}
3499
Daniel Jasper632aea92012-10-22 16:26:51 +00003500TEST(HasParent, MatchesOnlyParent) {
3501 EXPECT_TRUE(matches(
3502 "void f() { if (true) { int x = 42; } }",
3503 compoundStmt(hasParent(ifStmt()))));
3504 EXPECT_TRUE(notMatches(
3505 "void f() { for (;;) { int x = 42; } }",
3506 compoundStmt(hasParent(ifStmt()))));
3507 EXPECT_TRUE(notMatches(
3508 "void f() { if (true) for (;;) { int x = 42; } }",
3509 compoundStmt(hasParent(ifStmt()))));
3510}
3511
Manuel Klimekc844a462012-12-06 14:42:48 +00003512TEST(HasAncestor, MatchesAllAncestors) {
3513 EXPECT_TRUE(matches(
3514 "template <typename T> struct C { static void f() { 42; } };"
3515 "void t() { C<int>::f(); }",
3516 integerLiteral(
3517 equals(42),
3518 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3519 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3520}
3521
3522TEST(HasParent, MatchesAllParents) {
3523 EXPECT_TRUE(matches(
3524 "template <typename T> struct C { static void f() { 42; } };"
3525 "void t() { C<int>::f(); }",
3526 integerLiteral(
3527 equals(42),
3528 hasParent(compoundStmt(hasParent(functionDecl(
3529 hasParent(recordDecl(isTemplateInstantiation())))))))));
3530 EXPECT_TRUE(matches(
3531 "template <typename T> struct C { static void f() { 42; } };"
3532 "void t() { C<int>::f(); }",
3533 integerLiteral(
3534 equals(42),
3535 hasParent(compoundStmt(hasParent(functionDecl(
3536 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3537 EXPECT_TRUE(matches(
3538 "template <typename T> struct C { static void f() { 42; } };"
3539 "void t() { C<int>::f(); }",
3540 integerLiteral(equals(42),
3541 hasParent(compoundStmt(allOf(
3542 hasParent(functionDecl(
3543 hasParent(recordDecl(isTemplateInstantiation())))),
3544 hasParent(functionDecl(hasParent(recordDecl(
3545 unless(isTemplateInstantiation())))))))))));
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003546 EXPECT_TRUE(
3547 notMatches("template <typename T> struct C { static void f() {} };"
3548 "void t() { C<int>::f(); }",
3549 compoundStmt(hasParent(recordDecl()))));
Manuel Klimekc844a462012-12-06 14:42:48 +00003550}
3551
Daniel Jasper516b02e2012-10-17 08:52:59 +00003552TEST(TypeMatching, MatchesTypes) {
3553 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3554}
3555
3556TEST(TypeMatching, MatchesArrayTypes) {
3557 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3558 EXPECT_TRUE(matches("int a[42];", arrayType()));
3559 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3560
3561 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3562 arrayType(hasElementType(builtinType()))));
3563
3564 EXPECT_TRUE(matches(
3565 "int const a[] = { 2, 3 };",
3566 qualType(arrayType(hasElementType(builtinType())))));
3567 EXPECT_TRUE(matches(
3568 "int const a[] = { 2, 3 };",
3569 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3570 EXPECT_TRUE(matches(
3571 "typedef const int T; T x[] = { 1, 2 };",
3572 qualType(isConstQualified(), arrayType())));
3573
3574 EXPECT_TRUE(notMatches(
3575 "int a[] = { 2, 3 };",
3576 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3577 EXPECT_TRUE(notMatches(
3578 "int a[] = { 2, 3 };",
3579 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3580 EXPECT_TRUE(notMatches(
3581 "int const a[] = { 2, 3 };",
3582 qualType(arrayType(hasElementType(builtinType())),
3583 unless(isConstQualified()))));
3584
3585 EXPECT_TRUE(matches("int a[2];",
3586 constantArrayType(hasElementType(builtinType()))));
3587 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3588}
3589
3590TEST(TypeMatching, MatchesComplexTypes) {
3591 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3592 EXPECT_TRUE(matches(
3593 "_Complex float f;",
3594 complexType(hasElementType(builtinType()))));
3595 EXPECT_TRUE(notMatches(
3596 "_Complex float f;",
3597 complexType(hasElementType(isInteger()))));
3598}
3599
3600TEST(TypeMatching, MatchesConstantArrayTypes) {
3601 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3602 EXPECT_TRUE(notMatches(
3603 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3604 constantArrayType(hasElementType(builtinType()))));
3605
3606 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3607 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3608 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3609}
3610
3611TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3612 EXPECT_TRUE(matches(
3613 "template <typename T, int Size> class array { T data[Size]; };",
3614 dependentSizedArrayType()));
3615 EXPECT_TRUE(notMatches(
3616 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3617 dependentSizedArrayType()));
3618}
3619
3620TEST(TypeMatching, MatchesIncompleteArrayType) {
3621 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3622 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3623
3624 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3625 incompleteArrayType()));
3626}
3627
3628TEST(TypeMatching, MatchesVariableArrayType) {
3629 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3630 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3631
3632 EXPECT_TRUE(matches(
3633 "void f(int b) { int a[b]; }",
3634 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3635 varDecl(hasName("b")))))))));
3636}
3637
3638TEST(TypeMatching, MatchesAtomicTypes) {
3639 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3640
3641 EXPECT_TRUE(matches("_Atomic(int) i;",
3642 atomicType(hasValueType(isInteger()))));
3643 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3644 atomicType(hasValueType(isInteger()))));
3645}
3646
3647TEST(TypeMatching, MatchesAutoTypes) {
3648 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3649 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3650 autoType()));
3651
Richard Smith061f1e22013-04-30 21:23:01 +00003652 // FIXME: Matching against the type-as-written can't work here, because the
3653 // type as written was not deduced.
3654 //EXPECT_TRUE(matches("auto a = 1;",
3655 // autoType(hasDeducedType(isInteger()))));
3656 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3657 // autoType(hasDeducedType(isInteger()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003658}
3659
Daniel Jasperd29d5fa2012-10-29 10:14:44 +00003660TEST(TypeMatching, MatchesFunctionTypes) {
3661 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3662 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3663}
3664
Edwin Vaneec074802013-04-01 18:33:34 +00003665TEST(TypeMatching, MatchesParenType) {
3666 EXPECT_TRUE(
3667 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3668 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3669
3670 EXPECT_TRUE(matches(
3671 "int (*ptr_to_func)(int);",
3672 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3673 EXPECT_TRUE(notMatches(
3674 "int (*ptr_to_array)[4];",
3675 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3676}
3677
Daniel Jasper516b02e2012-10-17 08:52:59 +00003678TEST(TypeMatching, PointerTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003679 // FIXME: Reactive when these tests can be more specific (not matching
3680 // implicit code on certain platforms), likely when we have hasDescendant for
3681 // Types/TypeLocs.
3682 //EXPECT_TRUE(matchAndVerifyResultTrue(
3683 // "int* a;",
3684 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3685 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3686 //EXPECT_TRUE(matchAndVerifyResultTrue(
3687 // "int* a;",
3688 // pointerTypeLoc().bind("loc"),
3689 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003690 EXPECT_TRUE(matches(
3691 "int** a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003692 loc(pointerType(pointee(qualType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003693 EXPECT_TRUE(matches(
3694 "int** a;",
3695 loc(pointerType(pointee(pointerType())))));
3696 EXPECT_TRUE(matches(
3697 "int* b; int* * const a = &b;",
3698 loc(qualType(isConstQualified(), pointerType()))));
3699
3700 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper7943eb52012-10-17 13:35:36 +00003701 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3702 hasType(blockPointerType()))));
3703 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3704 hasType(memberPointerType()))));
3705 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3706 hasType(pointerType()))));
3707 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3708 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003709 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3710 hasType(lValueReferenceType()))));
3711 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3712 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003713
Daniel Jasper7943eb52012-10-17 13:35:36 +00003714 Fragment = "int *ptr;";
3715 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3716 hasType(blockPointerType()))));
3717 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3718 hasType(memberPointerType()))));
3719 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3720 hasType(pointerType()))));
3721 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3722 hasType(referenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003723
Daniel Jasper7943eb52012-10-17 13:35:36 +00003724 Fragment = "int a; int &ref = a;";
3725 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3726 hasType(blockPointerType()))));
3727 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3728 hasType(memberPointerType()))));
3729 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3730 hasType(pointerType()))));
3731 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3732 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003733 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3734 hasType(lValueReferenceType()))));
3735 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3736 hasType(rValueReferenceType()))));
3737
3738 Fragment = "int &&ref = 2;";
3739 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3740 hasType(blockPointerType()))));
3741 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3742 hasType(memberPointerType()))));
3743 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3744 hasType(pointerType()))));
3745 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3746 hasType(referenceType()))));
3747 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3748 hasType(lValueReferenceType()))));
3749 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3750 hasType(rValueReferenceType()))));
3751}
3752
3753TEST(TypeMatching, AutoRefTypes) {
3754 std::string Fragment = "auto a = 1;"
3755 "auto b = a;"
3756 "auto &c = a;"
3757 "auto &&d = c;"
3758 "auto &&e = 2;";
3759 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3760 hasType(referenceType()))));
3761 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3762 hasType(referenceType()))));
3763 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3764 hasType(referenceType()))));
3765 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3766 hasType(lValueReferenceType()))));
3767 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3768 hasType(rValueReferenceType()))));
3769 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3770 hasType(referenceType()))));
3771 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3772 hasType(lValueReferenceType()))));
3773 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3774 hasType(rValueReferenceType()))));
3775 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3776 hasType(referenceType()))));
3777 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3778 hasType(lValueReferenceType()))));
3779 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3780 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003781}
3782
3783TEST(TypeMatching, PointeeTypes) {
3784 EXPECT_TRUE(matches("int b; int &a = b;",
3785 referenceType(pointee(builtinType()))));
3786 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3787
3788 EXPECT_TRUE(matches("int *a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003789 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003790
3791 EXPECT_TRUE(matches(
3792 "int const *A;",
3793 pointerType(pointee(isConstQualified(), builtinType()))));
3794 EXPECT_TRUE(notMatches(
3795 "int *A;",
3796 pointerType(pointee(isConstQualified(), builtinType()))));
3797}
3798
3799TEST(TypeMatching, MatchesPointersToConstTypes) {
3800 EXPECT_TRUE(matches("int b; int * const a = &b;",
3801 loc(pointerType())));
3802 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003803 loc(pointerType())));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003804 EXPECT_TRUE(matches(
3805 "int b; const int * a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003806 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003807 EXPECT_TRUE(matches(
3808 "int b; const int * a = &b;",
3809 pointerType(pointee(builtinType()))));
3810}
3811
3812TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003813 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3814 hasType(typedefType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003815}
3816
Edwin Vanef901b712013-02-25 14:49:29 +00003817TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vaneb6eae142013-02-25 20:43:32 +00003818 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vanef901b712013-02-25 14:49:29 +00003819 templateSpecializationType()));
3820}
3821
Edwin Vaneb6eae142013-02-25 20:43:32 +00003822TEST(TypeMatching, MatchesRecordType) {
3823 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek59b0af62013-02-27 11:56:58 +00003824 EXPECT_TRUE(matches("struct S{}; S s;",
3825 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3826 EXPECT_TRUE(notMatches("int i;",
3827 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003828}
3829
3830TEST(TypeMatching, MatchesElaboratedType) {
3831 EXPECT_TRUE(matches(
3832 "namespace N {"
3833 " namespace M {"
3834 " class D {};"
3835 " }"
3836 "}"
3837 "N::M::D d;", elaboratedType()));
3838 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3839 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3840}
3841
3842TEST(ElaboratedTypeNarrowing, hasQualifier) {
3843 EXPECT_TRUE(matches(
3844 "namespace N {"
3845 " namespace M {"
3846 " class D {};"
3847 " }"
3848 "}"
3849 "N::M::D d;",
3850 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3851 EXPECT_TRUE(notMatches(
3852 "namespace M {"
3853 " class D {};"
3854 "}"
3855 "M::D d;",
3856 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vane6972f6d2013-03-04 17:51:00 +00003857 EXPECT_TRUE(notMatches(
3858 "struct D {"
3859 "} d;",
3860 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003861}
3862
3863TEST(ElaboratedTypeNarrowing, namesType) {
3864 EXPECT_TRUE(matches(
3865 "namespace N {"
3866 " namespace M {"
3867 " class D {};"
3868 " }"
3869 "}"
3870 "N::M::D d;",
3871 elaboratedType(elaboratedType(namesType(recordType(
3872 hasDeclaration(namedDecl(hasName("D")))))))));
3873 EXPECT_TRUE(notMatches(
3874 "namespace M {"
3875 " class D {};"
3876 "}"
3877 "M::D d;",
3878 elaboratedType(elaboratedType(namesType(typedefType())))));
3879}
3880
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003881TEST(NNS, MatchesNestedNameSpecifiers) {
3882 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3883 nestedNameSpecifier()));
3884 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3885 nestedNameSpecifier()));
3886 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3887 nestedNameSpecifier()));
3888
3889 EXPECT_TRUE(matches(
3890 "struct A { static void f() {} }; void g() { A::f(); }",
3891 nestedNameSpecifier()));
3892 EXPECT_TRUE(notMatches(
3893 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3894 nestedNameSpecifier()));
3895}
3896
Daniel Jasper87c3d362012-09-20 14:12:57 +00003897TEST(NullStatement, SimpleCases) {
3898 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3899 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3900}
3901
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003902TEST(NNS, MatchesTypes) {
3903 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3904 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3905 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3906 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3907 Matcher));
3908 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3909}
3910
3911TEST(NNS, MatchesNamespaceDecls) {
3912 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3913 specifiesNamespace(hasName("ns")));
3914 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3915 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3916 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3917}
3918
3919TEST(NNS, BindsNestedNameSpecifiers) {
3920 EXPECT_TRUE(matchAndVerifyResultTrue(
3921 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3922 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3923 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3924}
3925
3926TEST(NNS, BindsNestedNameSpecifierLocs) {
3927 EXPECT_TRUE(matchAndVerifyResultTrue(
3928 "namespace ns { struct B {}; } ns::B b;",
3929 loc(nestedNameSpecifier()).bind("loc"),
3930 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3931}
3932
3933TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3934 EXPECT_TRUE(matches(
3935 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3936 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3937 EXPECT_TRUE(matches(
3938 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasper516b02e2012-10-17 08:52:59 +00003939 nestedNameSpecifierLoc(hasPrefix(
3940 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003941}
3942
Daniel Jasper6fc34332012-10-30 15:42:00 +00003943TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3944 std::string Fragment =
3945 "namespace a { struct A { struct B { struct C {}; }; }; };"
3946 "void f() { a::A::B::C c; }";
3947 EXPECT_TRUE(matches(
3948 Fragment,
3949 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3950 hasDescendant(nestedNameSpecifier(
3951 specifiesNamespace(hasName("a")))))));
3952 EXPECT_TRUE(notMatches(
3953 Fragment,
3954 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3955 has(nestedNameSpecifier(
3956 specifiesNamespace(hasName("a")))))));
3957 EXPECT_TRUE(matches(
3958 Fragment,
3959 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3960 has(nestedNameSpecifier(
3961 specifiesNamespace(hasName("a")))))));
3962
3963 // Not really useful because a NestedNameSpecifier can af at most one child,
3964 // but to complete the interface.
3965 EXPECT_TRUE(matchAndVerifyResultTrue(
3966 Fragment,
3967 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3968 forEach(nestedNameSpecifier().bind("x"))),
3969 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3970}
3971
3972TEST(NNS, NestedNameSpecifiersAsDescendants) {
3973 std::string Fragment =
3974 "namespace a { struct A { struct B { struct C {}; }; }; };"
3975 "void f() { a::A::B::C c; }";
3976 EXPECT_TRUE(matches(
3977 Fragment,
3978 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3979 asString("struct a::A")))))));
3980 EXPECT_TRUE(matchAndVerifyResultTrue(
3981 Fragment,
3982 functionDecl(hasName("f"),
3983 forEachDescendant(nestedNameSpecifier().bind("x"))),
3984 // Nested names: a, a::A and a::A::B.
3985 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3986}
3987
3988TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3989 std::string Fragment =
3990 "namespace a { struct A { struct B { struct C {}; }; }; };"
3991 "void f() { a::A::B::C c; }";
3992 EXPECT_TRUE(matches(
3993 Fragment,
3994 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3995 hasDescendant(loc(nestedNameSpecifier(
3996 specifiesNamespace(hasName("a"))))))));
3997 EXPECT_TRUE(notMatches(
3998 Fragment,
3999 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4000 has(loc(nestedNameSpecifier(
4001 specifiesNamespace(hasName("a"))))))));
4002 EXPECT_TRUE(matches(
4003 Fragment,
4004 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
4005 has(loc(nestedNameSpecifier(
4006 specifiesNamespace(hasName("a"))))))));
4007
4008 EXPECT_TRUE(matchAndVerifyResultTrue(
4009 Fragment,
4010 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4011 forEach(nestedNameSpecifierLoc().bind("x"))),
4012 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
4013}
4014
4015TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
4016 std::string Fragment =
4017 "namespace a { struct A { struct B { struct C {}; }; }; };"
4018 "void f() { a::A::B::C c; }";
4019 EXPECT_TRUE(matches(
4020 Fragment,
4021 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4022 asString("struct a::A"))))))));
4023 EXPECT_TRUE(matchAndVerifyResultTrue(
4024 Fragment,
4025 functionDecl(hasName("f"),
4026 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4027 // Nested names: a, a::A and a::A::B.
4028 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4029}
4030
Manuel Klimek191c0932013-02-01 13:41:35 +00004031template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimekc2687452012-10-24 14:47:44 +00004032public:
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004033 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4034 StringRef InnerId)
4035 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jaspere9aa6872012-10-29 10:48:25 +00004036 }
4037
Manuel Klimek191c0932013-02-01 13:41:35 +00004038 virtual bool run(const BoundNodes *Nodes) { return false; }
4039
Manuel Klimekc2687452012-10-24 14:47:44 +00004040 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4041 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004042 return selectFirst<const T>(InnerId,
4043 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimekc2687452012-10-24 14:47:44 +00004044 }
4045private:
4046 std::string Id;
4047 internal::Matcher<T> InnerMatcher;
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004048 std::string InnerId;
Manuel Klimekc2687452012-10-24 14:47:44 +00004049};
4050
4051TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004052 EXPECT_TRUE(matchAndVerifyResultTrue(
4053 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4054 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004055 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4056 "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004057 EXPECT_TRUE(matchAndVerifyResultFalse(
4058 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4059 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004060 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4061 "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004062}
4063
4064TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004065 EXPECT_TRUE(matchAndVerifyResultTrue(
4066 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004067 new VerifyMatchOnNode<clang::Stmt>(
4068 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004069 EXPECT_TRUE(matchAndVerifyResultFalse(
4070 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004071 new VerifyMatchOnNode<clang::Stmt>(
4072 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004073}
4074
4075TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4076 EXPECT_TRUE(matchAndVerifyResultTrue(
4077 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4078 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004079 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004080 EXPECT_TRUE(matchAndVerifyResultFalse(
4081 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4082 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004083 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004084}
4085
Manuel Klimekbee08572013-02-07 12:42:10 +00004086template <typename T>
4087class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4088public:
4089 virtual bool run(const BoundNodes *Nodes) { return false; }
4090
4091 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4092 const T *Node = Nodes->getNodeAs<T>("");
4093 return verify(*Nodes, *Context, Node);
4094 }
4095
4096 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4097 return selectFirst<const T>(
4098 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4099 *Node, Context)) != NULL;
4100 }
4101 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4102 return selectFirst<const T>(
4103 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4104 *Node, Context)) != NULL;
4105 }
4106};
4107
4108TEST(IsEqualTo, MatchesNodesByIdentity) {
4109 EXPECT_TRUE(matchAndVerifyResultTrue(
4110 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4111 new VerifyAncestorHasChildIsEqual<Decl>()));
4112 EXPECT_TRUE(
4113 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4114 new VerifyAncestorHasChildIsEqual<Stmt>()));
4115}
4116
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004117class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4118public:
4119 VerifyStartOfTranslationUnit() : Called(false) {}
4120 virtual void run(const MatchFinder::MatchResult &Result) {
4121 EXPECT_TRUE(Called);
4122 }
4123 virtual void onStartOfTranslationUnit() {
4124 Called = true;
4125 }
4126 bool Called;
4127};
4128
4129TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4130 MatchFinder Finder;
4131 VerifyStartOfTranslationUnit VerifyCallback;
4132 Finder.addMatcher(decl(), &VerifyCallback);
4133 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4134 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4135 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004136
4137 VerifyCallback.Called = false;
4138 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4139 ASSERT_TRUE(AST.get());
4140 Finder.matchAST(AST->getASTContext());
4141 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004142}
4143
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004144class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4145public:
4146 VerifyEndOfTranslationUnit() : Called(false) {}
4147 virtual void run(const MatchFinder::MatchResult &Result) {
4148 EXPECT_FALSE(Called);
4149 }
4150 virtual void onEndOfTranslationUnit() {
4151 Called = true;
4152 }
4153 bool Called;
4154};
4155
4156TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4157 MatchFinder Finder;
4158 VerifyEndOfTranslationUnit VerifyCallback;
4159 Finder.addMatcher(decl(), &VerifyCallback);
4160 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4161 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4162 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004163
4164 VerifyCallback.Called = false;
4165 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4166 ASSERT_TRUE(AST.get());
4167 Finder.matchAST(AST->getASTContext());
4168 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004169}
4170
Manuel Klimekbbb75852013-06-20 14:06:32 +00004171TEST(EqualsBoundNodeMatcher, QualType) {
4172 EXPECT_TRUE(matches(
4173 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4174 hasInitializer(ignoringParenImpCasts(
4175 hasType(qualType(equalsBoundNode("type"))))))));
4176 EXPECT_TRUE(notMatches("int i = 1.f;",
4177 varDecl(hasType(qualType().bind("type")),
4178 hasInitializer(ignoringParenImpCasts(hasType(
4179 qualType(equalsBoundNode("type"))))))));
4180}
4181
4182TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4183 EXPECT_TRUE(notMatches(
4184 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4185 hasInitializer(ignoringParenImpCasts(
4186 hasType(qualType(equalsBoundNode("type"))))))));
4187}
4188
4189TEST(EqualsBoundNodeMatcher, Stmt) {
4190 EXPECT_TRUE(
4191 matches("void f() { if(true) {} }",
4192 stmt(allOf(ifStmt().bind("if"),
4193 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4194
4195 EXPECT_TRUE(notMatches(
4196 "void f() { if(true) { if (true) {} } }",
4197 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4198}
4199
4200TEST(EqualsBoundNodeMatcher, Decl) {
4201 EXPECT_TRUE(matches(
4202 "class X { class Y {}; };",
4203 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4204 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4205
4206 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4207 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4208 has(decl(equalsBoundNode("record")))))));
4209}
4210
4211TEST(EqualsBoundNodeMatcher, Type) {
4212 EXPECT_TRUE(matches(
4213 "class X { int a; int b; };",
4214 recordDecl(
4215 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4216 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4217
4218 EXPECT_TRUE(notMatches(
4219 "class X { int a; double b; };",
4220 recordDecl(
4221 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4222 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4223}
4224
4225TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4226
4227 EXPECT_TRUE(matchAndVerifyResultTrue(
4228 "int f() {"
4229 " if (1) {"
4230 " int i = 9;"
4231 " }"
4232 " int j = 10;"
4233 " {"
4234 " float k = 9.0;"
4235 " }"
4236 " return 0;"
4237 "}",
4238 // Look for variable declarations within functions whose type is the same
4239 // as the function return type.
4240 functionDecl(returns(qualType().bind("type")),
4241 forEachDescendant(varDecl(hasType(
4242 qualType(equalsBoundNode("type")))).bind("decl"))),
4243 // Only i and j should match, not k.
4244 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4245}
4246
4247TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4248 EXPECT_TRUE(matchAndVerifyResultTrue(
4249 "void f() {"
4250 " int x;"
4251 " double d;"
4252 " x = d + x - d + x;"
4253 "}",
4254 functionDecl(
4255 hasName("f"), forEachDescendant(varDecl().bind("d")),
4256 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4257 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4258}
4259
Manuel Klimek04616e42012-07-06 05:48:52 +00004260} // end namespace ast_matchers
4261} // end namespace clang