blob: cf77f2f4dd5736dfcd2591b8455aa1c271c7066d [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())))));
Peter Collingbourne564597f2014-02-20 19:18:03 +00001533
1534 EXPECT_TRUE(matches(
1535 "struct B { int next; };"
1536 "template<int(B::*next_ptr)> struct A {};"
1537 "A<&B::next> a;",
1538 templateSpecializationType(hasAnyTemplateArgument(isExpr(
1539 hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))));
1540
1541 EXPECT_TRUE(notMatches(
1542 "template <typename T> struct A {};"
1543 "A<int> a;",
1544 templateSpecializationType(hasAnyTemplateArgument(
1545 refersToDeclaration(decl())))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001546}
1547
1548TEST(Matcher, MatchesSpecificArgument) {
1549 EXPECT_TRUE(matches(
1550 "template<typename T, typename U> class A {};"
1551 "A<bool, int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001552 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001553 1, refersToType(asString("int"))))));
1554 EXPECT_TRUE(notMatches(
1555 "template<typename T, typename U> class A {};"
1556 "A<int, bool> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001557 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001558 1, refersToType(asString("int"))))));
Peter Collingbourne564597f2014-02-20 19:18:03 +00001559
1560 EXPECT_TRUE(matches(
1561 "template<typename T, typename U> class A {};"
1562 "A<bool, int> a;",
1563 templateSpecializationType(hasTemplateArgument(
1564 1, refersToType(asString("int"))))));
1565 EXPECT_TRUE(notMatches(
1566 "template<typename T, typename U> class A {};"
1567 "A<int, bool> a;",
1568 templateSpecializationType(hasTemplateArgument(
1569 1, refersToType(asString("int"))))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001570}
1571
Daniel Jasper639522c2013-02-25 12:02:08 +00001572TEST(Matcher, MatchesAccessSpecDecls) {
1573 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1574 EXPECT_TRUE(
1575 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1576 EXPECT_TRUE(
1577 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1578 EXPECT_TRUE(
1579 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1580
1581 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1582}
1583
Edwin Vane37ee1d72013-04-09 20:46:36 +00001584TEST(Matcher, MatchesVirtualMethod) {
1585 EXPECT_TRUE(matches("class X { virtual int f(); };",
1586 methodDecl(isVirtual(), hasName("::X::f"))));
1587 EXPECT_TRUE(notMatches("class X { int f(); };",
1588 methodDecl(isVirtual())));
1589}
1590
Edwin Vanefc4f7dc2013-05-09 17:00:17 +00001591TEST(Matcher, MatchesConstMethod) {
1592 EXPECT_TRUE(matches("struct A { void foo() const; };",
1593 methodDecl(isConst())));
1594 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1595 methodDecl(isConst())));
1596}
1597
Edwin Vane37ee1d72013-04-09 20:46:36 +00001598TEST(Matcher, MatchesOverridingMethod) {
1599 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1600 "class Y : public X { int f(); };",
1601 methodDecl(isOverride(), hasName("::Y::f"))));
1602 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1603 "class Y : public X { int f(); };",
1604 methodDecl(isOverride(), hasName("::X::f"))));
1605 EXPECT_TRUE(notMatches("class X { int f(); }; "
1606 "class Y : public X { int f(); };",
1607 methodDecl(isOverride())));
1608 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1609 methodDecl(isOverride())));
1610}
1611
Manuel Klimek04616e42012-07-06 05:48:52 +00001612TEST(Matcher, ConstructorCall) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001613 StatementMatcher Constructor = constructExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001614
1615 EXPECT_TRUE(
1616 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1617 EXPECT_TRUE(
1618 matches("class X { public: X(); }; void x() { X x = X(); }",
1619 Constructor));
1620 EXPECT_TRUE(
1621 matches("class X { public: X(int); }; void x() { X x = 0; }",
1622 Constructor));
1623 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1624}
1625
1626TEST(Matcher, ConstructorArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001627 StatementMatcher Constructor = constructExpr(
1628 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001629
1630 EXPECT_TRUE(
1631 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1632 Constructor));
1633 EXPECT_TRUE(
1634 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1635 Constructor));
1636 EXPECT_TRUE(
1637 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1638 Constructor));
1639 EXPECT_TRUE(
1640 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1641 Constructor));
1642
Daniel Jasper848cbe12012-09-18 13:09:13 +00001643 StatementMatcher WrongIndex = constructExpr(
1644 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001645 EXPECT_TRUE(
1646 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1647 WrongIndex));
1648}
1649
1650TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001651 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001652
1653 EXPECT_TRUE(
1654 matches("class X { public: X(int); }; void x() { X x(0); }",
1655 Constructor1Arg));
1656 EXPECT_TRUE(
1657 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1658 Constructor1Arg));
1659 EXPECT_TRUE(
1660 matches("class X { public: X(int); }; void x() { X x = 0; }",
1661 Constructor1Arg));
1662 EXPECT_TRUE(
1663 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1664 Constructor1Arg));
1665}
1666
Peter Collingbourne1fec3df2014-02-06 21:52:24 +00001667TEST(Matcher, ConstructorListInitialization) {
1668 StatementMatcher ConstructorListInit = constructExpr(isListInitialization());
1669
1670 EXPECT_TRUE(
1671 matches("class X { public: X(int); }; void x() { X x{0}; }",
1672 ConstructorListInit));
1673 EXPECT_FALSE(
1674 matches("class X { public: X(int); }; void x() { X x(0); }",
1675 ConstructorListInit));
1676}
1677
Manuel Klimek7fca93b2012-10-23 10:40:50 +00001678TEST(Matcher,ThisExpr) {
1679 EXPECT_TRUE(
1680 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1681 EXPECT_TRUE(
1682 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1683}
1684
Manuel Klimek04616e42012-07-06 05:48:52 +00001685TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001686 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001687
1688 std::string ClassString = "class string { public: string(); ~string(); }; ";
1689
1690 EXPECT_TRUE(
1691 matches(ClassString +
1692 "string GetStringByValue();"
1693 "void FunctionTakesString(string s);"
1694 "void run() { FunctionTakesString(GetStringByValue()); }",
1695 TempExpression));
1696
1697 EXPECT_TRUE(
1698 notMatches(ClassString +
1699 "string* GetStringPointer(); "
1700 "void FunctionTakesStringPtr(string* s);"
1701 "void run() {"
1702 " string* s = GetStringPointer();"
1703 " FunctionTakesStringPtr(GetStringPointer());"
1704 " FunctionTakesStringPtr(s);"
1705 "}",
1706 TempExpression));
1707
1708 EXPECT_TRUE(
1709 notMatches("class no_dtor {};"
1710 "no_dtor GetObjByValue();"
1711 "void ConsumeObj(no_dtor param);"
1712 "void run() { ConsumeObj(GetObjByValue()); }",
1713 TempExpression));
1714}
1715
Sam Panzer68a35af2012-08-24 22:04:44 +00001716TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1717 std::string ClassString =
1718 "class string { public: string(); int length(); }; ";
1719
1720 EXPECT_TRUE(
1721 matches(ClassString +
1722 "string GetStringByValue();"
1723 "void FunctionTakesString(string s);"
1724 "void run() { FunctionTakesString(GetStringByValue()); }",
1725 materializeTemporaryExpr()));
1726
1727 EXPECT_TRUE(
1728 notMatches(ClassString +
1729 "string* GetStringPointer(); "
1730 "void FunctionTakesStringPtr(string* s);"
1731 "void run() {"
1732 " string* s = GetStringPointer();"
1733 " FunctionTakesStringPtr(GetStringPointer());"
1734 " FunctionTakesStringPtr(s);"
1735 "}",
1736 materializeTemporaryExpr()));
1737
1738 EXPECT_TRUE(
1739 notMatches(ClassString +
1740 "string GetStringByValue();"
1741 "void run() { int k = GetStringByValue().length(); }",
1742 materializeTemporaryExpr()));
1743
1744 EXPECT_TRUE(
1745 notMatches(ClassString +
1746 "string GetStringByValue();"
1747 "void run() { GetStringByValue(); }",
1748 materializeTemporaryExpr()));
1749}
1750
Manuel Klimek04616e42012-07-06 05:48:52 +00001751TEST(ConstructorDeclaration, SimpleCase) {
1752 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001753 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001754 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001755 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001756}
1757
1758TEST(ConstructorDeclaration, IsImplicit) {
1759 // This one doesn't match because the constructor is not added by the
1760 // compiler (it is not needed).
1761 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001762 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001763 // The compiler added the implicit default constructor.
1764 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001765 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001766 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001767 constructorDecl(unless(isImplicit()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001768}
1769
Daniel Jasper1dad1832012-07-10 20:20:19 +00001770TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1771 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001772 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001773}
1774
1775TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001776 EXPECT_TRUE(notMatches("class Foo {};",
1777 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001778}
1779
Manuel Klimek04616e42012-07-06 05:48:52 +00001780TEST(HasAnyConstructorInitializer, SimpleCase) {
1781 EXPECT_TRUE(notMatches(
1782 "class Foo { Foo() { } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001783 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001784 EXPECT_TRUE(matches(
1785 "class Foo {"
1786 " Foo() : foo_() { }"
1787 " int foo_;"
1788 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001789 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001790}
1791
1792TEST(HasAnyConstructorInitializer, ForField) {
1793 static const char Code[] =
1794 "class Baz { };"
1795 "class Foo {"
1796 " Foo() : foo_() { }"
1797 " Baz foo_;"
1798 " Baz bar_;"
1799 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001800 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1801 forField(hasType(recordDecl(hasName("Baz"))))))));
1802 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001803 forField(hasName("foo_"))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001804 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1805 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001806}
1807
1808TEST(HasAnyConstructorInitializer, WithInitializer) {
1809 static const char Code[] =
1810 "class Foo {"
1811 " Foo() : foo_(0) { }"
1812 " int foo_;"
1813 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001814 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001815 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001816 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001817 withInitializer(integerLiteral(equals(1)))))));
1818}
1819
1820TEST(HasAnyConstructorInitializer, IsWritten) {
1821 static const char Code[] =
1822 "struct Bar { Bar(){} };"
1823 "class Foo {"
1824 " Foo() : foo_() { }"
1825 " Bar foo_;"
1826 " Bar bar_;"
1827 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001828 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001829 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001830 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001831 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001832 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001833 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1834}
1835
1836TEST(Matcher, NewExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001837 StatementMatcher New = newExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001838
1839 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1840 EXPECT_TRUE(
1841 matches("class X { public: X(); }; void x() { new X(); }", New));
1842 EXPECT_TRUE(
1843 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1844 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1845}
1846
1847TEST(Matcher, NewExpressionArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001848 StatementMatcher New = constructExpr(
1849 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001850
1851 EXPECT_TRUE(
1852 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1853 New));
1854 EXPECT_TRUE(
1855 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1856 New));
1857 EXPECT_TRUE(
1858 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1859 New));
1860
Daniel Jasper848cbe12012-09-18 13:09:13 +00001861 StatementMatcher WrongIndex = constructExpr(
1862 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001863 EXPECT_TRUE(
1864 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1865 WrongIndex));
1866}
1867
1868TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001869 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001870
1871 EXPECT_TRUE(
1872 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1873 EXPECT_TRUE(
1874 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1875 New));
1876}
1877
Daniel Jasper1dad1832012-07-10 20:20:19 +00001878TEST(Matcher, DeleteExpression) {
1879 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001880 deleteExpr()));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001881}
1882
Manuel Klimek04616e42012-07-06 05:48:52 +00001883TEST(Matcher, DefaultArgument) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001884 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001885
1886 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1887 EXPECT_TRUE(
1888 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1889 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1890}
1891
1892TEST(Matcher, StringLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001893 StatementMatcher Literal = stringLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001894 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1895 // wide string
1896 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1897 // with escaped characters
1898 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1899 // no matching -- though the data type is the same, there is no string literal
1900 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1901}
1902
1903TEST(Matcher, CharacterLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001904 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001905 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1906 // wide character
1907 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1908 // wide character, Hex encoded, NOT MATCHED!
1909 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1910 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1911}
1912
1913TEST(Matcher, IntegerLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001914 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001915 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1916 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1917 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1918 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1919
1920 // Non-matching cases (character literals, float and double)
1921 EXPECT_TRUE(notMatches("int i = L'a';",
1922 HasIntLiteral)); // this is actually a character
1923 // literal cast to int
1924 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1925 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1926 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1927}
1928
Daniel Jasper91f1c8c2013-07-26 18:52:58 +00001929TEST(Matcher, FloatLiterals) {
1930 StatementMatcher HasFloatLiteral = floatLiteral();
1931 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1932 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1933 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1934 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1935 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1936
1937 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1938}
1939
Daniel Jasper5901e472012-10-01 13:40:41 +00001940TEST(Matcher, NullPtrLiteral) {
1941 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1942}
1943
Daniel Jasper87c3d362012-09-20 14:12:57 +00001944TEST(Matcher, AsmStatement) {
1945 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1946}
1947
Manuel Klimek04616e42012-07-06 05:48:52 +00001948TEST(Matcher, Conditions) {
1949 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1950
1951 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1952 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1953 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1954 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1955 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1956}
1957
1958TEST(MatchBinaryOperator, HasOperatorName) {
1959 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1960
1961 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1962 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1963}
1964
1965TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1966 StatementMatcher OperatorTrueFalse =
1967 binaryOperator(hasLHS(boolLiteral(equals(true))),
1968 hasRHS(boolLiteral(equals(false))));
1969
1970 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1971 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1972 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1973}
1974
1975TEST(MatchBinaryOperator, HasEitherOperand) {
1976 StatementMatcher HasOperand =
1977 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1978
1979 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1980 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1981 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1982}
1983
1984TEST(Matcher, BinaryOperatorTypes) {
1985 // Integration test that verifies the AST provides all binary operators in
1986 // a way we expect.
1987 // FIXME: Operator ','
1988 EXPECT_TRUE(
1989 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1990 EXPECT_TRUE(
1991 matches("bool b; bool c = (b = true);",
1992 binaryOperator(hasOperatorName("="))));
1993 EXPECT_TRUE(
1994 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1995 EXPECT_TRUE(
1996 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1997 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1998 EXPECT_TRUE(
1999 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
2000 EXPECT_TRUE(
2001 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
2002 EXPECT_TRUE(
2003 matches("int i = 1; int j = (i <<= 2);",
2004 binaryOperator(hasOperatorName("<<="))));
2005 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
2006 EXPECT_TRUE(
2007 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
2008 EXPECT_TRUE(
2009 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
2010 EXPECT_TRUE(
2011 matches("int i = 1; int j = (i >>= 2);",
2012 binaryOperator(hasOperatorName(">>="))));
2013 EXPECT_TRUE(
2014 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
2015 EXPECT_TRUE(
2016 matches("int i = 42; int j = (i ^= 42);",
2017 binaryOperator(hasOperatorName("^="))));
2018 EXPECT_TRUE(
2019 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
2020 EXPECT_TRUE(
2021 matches("int i = 42; int j = (i %= 42);",
2022 binaryOperator(hasOperatorName("%="))));
2023 EXPECT_TRUE(
2024 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
2025 EXPECT_TRUE(
2026 matches("bool b = true && false;",
2027 binaryOperator(hasOperatorName("&&"))));
2028 EXPECT_TRUE(
2029 matches("bool b = true; bool c = (b &= false);",
2030 binaryOperator(hasOperatorName("&="))));
2031 EXPECT_TRUE(
2032 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
2033 EXPECT_TRUE(
2034 matches("bool b = true || false;",
2035 binaryOperator(hasOperatorName("||"))));
2036 EXPECT_TRUE(
2037 matches("bool b = true; bool c = (b |= false);",
2038 binaryOperator(hasOperatorName("|="))));
2039 EXPECT_TRUE(
2040 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2041 EXPECT_TRUE(
2042 matches("int i = 42; int j = (i *= 23);",
2043 binaryOperator(hasOperatorName("*="))));
2044 EXPECT_TRUE(
2045 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
2046 EXPECT_TRUE(
2047 matches("int i = 42; int j = (i /= 23);",
2048 binaryOperator(hasOperatorName("/="))));
2049 EXPECT_TRUE(
2050 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
2051 EXPECT_TRUE(
2052 matches("int i = 42; int j = (i += 23);",
2053 binaryOperator(hasOperatorName("+="))));
2054 EXPECT_TRUE(
2055 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
2056 EXPECT_TRUE(
2057 matches("int i = 42; int j = (i -= 23);",
2058 binaryOperator(hasOperatorName("-="))));
2059 EXPECT_TRUE(
2060 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2061 binaryOperator(hasOperatorName("->*"))));
2062 EXPECT_TRUE(
2063 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2064 binaryOperator(hasOperatorName(".*"))));
2065
2066 // Member expressions as operators are not supported in matches.
2067 EXPECT_TRUE(
2068 notMatches("struct A { void x(A *a) { a->x(this); } };",
2069 binaryOperator(hasOperatorName("->"))));
2070
2071 // Initializer assignments are not represented as operator equals.
2072 EXPECT_TRUE(
2073 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2074
2075 // Array indexing is not represented as operator.
2076 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2077
2078 // Overloaded operators do not match at all.
2079 EXPECT_TRUE(notMatches(
2080 "struct A { bool operator&&(const A &a) const { return false; } };"
2081 "void x() { A a, b; a && b; }",
2082 binaryOperator()));
2083}
2084
2085TEST(MatchUnaryOperator, HasOperatorName) {
2086 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2087
2088 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2089 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2090}
2091
2092TEST(MatchUnaryOperator, HasUnaryOperand) {
2093 StatementMatcher OperatorOnFalse =
2094 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2095
2096 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2097 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2098}
2099
2100TEST(Matcher, UnaryOperatorTypes) {
2101 // Integration test that verifies the AST provides all unary operators in
2102 // a way we expect.
2103 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2104 EXPECT_TRUE(
2105 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2106 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2107 EXPECT_TRUE(
2108 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2109 EXPECT_TRUE(
2110 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2111 EXPECT_TRUE(
2112 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2113 EXPECT_TRUE(
2114 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2115 EXPECT_TRUE(
2116 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2117 EXPECT_TRUE(
2118 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2119 EXPECT_TRUE(
2120 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2121
2122 // We don't match conversion operators.
2123 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2124
2125 // Function calls are not represented as operator.
2126 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2127
2128 // Overloaded operators do not match at all.
2129 // FIXME: We probably want to add that.
2130 EXPECT_TRUE(notMatches(
2131 "struct A { bool operator!() const { return false; } };"
2132 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2133}
2134
2135TEST(Matcher, ConditionalOperator) {
2136 StatementMatcher Conditional = conditionalOperator(
2137 hasCondition(boolLiteral(equals(true))),
2138 hasTrueExpression(boolLiteral(equals(false))));
2139
2140 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2141 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2142 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2143
2144 StatementMatcher ConditionalFalse = conditionalOperator(
2145 hasFalseExpression(boolLiteral(equals(false))));
2146
2147 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2148 EXPECT_TRUE(
2149 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2150}
2151
Daniel Jasper1dad1832012-07-10 20:20:19 +00002152TEST(ArraySubscriptMatchers, ArraySubscripts) {
2153 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2154 arraySubscriptExpr()));
2155 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2156 arraySubscriptExpr()));
2157}
2158
2159TEST(ArraySubscriptMatchers, ArrayIndex) {
2160 EXPECT_TRUE(matches(
2161 "int i[2]; void f() { i[1] = 1; }",
2162 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2163 EXPECT_TRUE(matches(
2164 "int i[2]; void f() { 1[i] = 1; }",
2165 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2166 EXPECT_TRUE(notMatches(
2167 "int i[2]; void f() { i[1] = 1; }",
2168 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2169}
2170
2171TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2172 EXPECT_TRUE(matches(
2173 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002174 arraySubscriptExpr(hasBase(implicitCastExpr(
2175 hasSourceExpression(declRefExpr()))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002176}
2177
Manuel Klimek04616e42012-07-06 05:48:52 +00002178TEST(Matcher, HasNameSupportsNamespaces) {
2179 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002180 recordDecl(hasName("a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002181 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002182 recordDecl(hasName("::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002183 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002184 recordDecl(hasName("b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002185 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002186 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002187 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002188 recordDecl(hasName("c::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002189 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002190 recordDecl(hasName("a::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002191 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002192 recordDecl(hasName("a::b::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002193 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002194 recordDecl(hasName("::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002195 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002196 recordDecl(hasName("::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002197 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002198 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002199 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002200 recordDecl(hasName("a+b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002201 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002202 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002203}
2204
2205TEST(Matcher, HasNameSupportsOuterClasses) {
2206 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002207 matches("class A { class B { class C; }; };",
2208 recordDecl(hasName("A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002209 EXPECT_TRUE(
2210 matches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002211 recordDecl(hasName("::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002212 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002213 matches("class A { class B { class C; }; };",
2214 recordDecl(hasName("B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002215 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002216 matches("class A { class B { class C; }; };",
2217 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002218 EXPECT_TRUE(
2219 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002220 recordDecl(hasName("c::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002221 EXPECT_TRUE(
2222 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002223 recordDecl(hasName("A::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002224 EXPECT_TRUE(
2225 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002226 recordDecl(hasName("A::B::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002227 EXPECT_TRUE(
2228 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002229 recordDecl(hasName("::C"))));
2230 EXPECT_TRUE(
2231 notMatches("class A { class B { class C; }; };",
2232 recordDecl(hasName("::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002233 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002234 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002235 EXPECT_TRUE(
2236 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002237 recordDecl(hasName("A+B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002238}
2239
2240TEST(Matcher, IsDefinition) {
2241 DeclarationMatcher DefinitionOfClassA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002242 recordDecl(hasName("A"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002243 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2244 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2245
2246 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002247 varDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002248 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2249 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2250
2251 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002252 methodDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002253 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2254 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2255}
2256
2257TEST(Matcher, OfClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002258 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +00002259 ofClass(hasName("X")))));
2260
2261 EXPECT_TRUE(
2262 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2263 EXPECT_TRUE(
2264 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2265 Constructor));
2266 EXPECT_TRUE(
2267 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2268 Constructor));
2269}
2270
2271TEST(Matcher, VisitsTemplateInstantiations) {
2272 EXPECT_TRUE(matches(
2273 "class A { public: void x(); };"
2274 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002275 "void f() { B<A> b; b.y(); }",
2276 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002277
2278 EXPECT_TRUE(matches(
2279 "class A { public: void x(); };"
2280 "class C {"
2281 " public:"
2282 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2283 "};"
2284 "void f() {"
2285 " C::B<A> b; b.y();"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002286 "}",
2287 recordDecl(hasName("C"),
2288 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002289}
2290
Daniel Jasper1dad1832012-07-10 20:20:19 +00002291TEST(Matcher, HandlesNullQualTypes) {
2292 // FIXME: Add a Type matcher so we can replace uses of this
2293 // variable with Type(True())
2294 const TypeMatcher AnyType = anything();
2295
2296 // We don't really care whether this matcher succeeds; we're testing that
2297 // it completes without crashing.
2298 EXPECT_TRUE(matches(
2299 "struct A { };"
2300 "template <typename T>"
2301 "void f(T t) {"
2302 " T local_t(t /* this becomes a null QualType in the AST */);"
2303 "}"
2304 "void g() {"
2305 " f(0);"
2306 "}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002307 expr(hasType(TypeMatcher(
Daniel Jasper1dad1832012-07-10 20:20:19 +00002308 anyOf(
2309 TypeMatcher(hasDeclaration(anything())),
2310 pointsTo(AnyType),
2311 references(AnyType)
2312 // Other QualType matchers should go here.
2313 ))))));
2314}
2315
Manuel Klimek04616e42012-07-06 05:48:52 +00002316// For testing AST_MATCHER_P().
Daniel Jasper1dad1832012-07-10 20:20:19 +00002317AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002318 // Make sure all special variables are used: node, match_finder,
2319 // bound_nodes_builder, and the parameter named 'AMatcher'.
2320 return AMatcher.matches(Node, Finder, Builder);
2321}
2322
2323TEST(AstMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002324 DeclarationMatcher HasClassB = just(has(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
2336AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +00002337 polymorphicHas,
2338 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2339 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002340 return Finder->matchesChildOf(
Manuel Klimekeb958de2012-09-05 12:12:07 +00002341 Node, AMatcher, Builder,
Manuel Klimek04616e42012-07-06 05:48:52 +00002342 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2343 ASTMatchFinder::BK_First);
2344}
2345
2346TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002347 DeclarationMatcher HasClassB =
2348 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek04616e42012-07-06 05:48:52 +00002349
2350 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002351 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002352
2353 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002354 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002355
2356 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002357 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002358
2359 StatementMatcher StatementHasClassB =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002360 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002361
2362 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2363}
2364
2365TEST(For, FindsForLoops) {
2366 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2367 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper6f595392012-10-01 15:05:34 +00002368 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2369 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00002370 forStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002371}
2372
Daniel Jasper4e566c42012-07-12 08:50:38 +00002373TEST(For, ForLoopInternals) {
2374 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2375 forStmt(hasCondition(anything()))));
2376 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2377 forStmt(hasLoopInit(anything()))));
2378}
2379
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002380TEST(For, ForRangeLoopInternals) {
2381 EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2382 forRangeStmt(hasLoopVariable(anything()))));
2383}
2384
Daniel Jasper4e566c42012-07-12 08:50:38 +00002385TEST(For, NegativeForLoopInternals) {
2386 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002387 forStmt(hasCondition(expr()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002388 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2389 forStmt(hasLoopInit(anything()))));
2390}
2391
Manuel Klimek04616e42012-07-06 05:48:52 +00002392TEST(For, ReportsNoFalsePositives) {
2393 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2394 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2395}
2396
2397TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002398 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2399 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2400 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002401}
2402
2403TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2404 // It's not a compound statement just because there's "{}" in the source
2405 // text. This is an AST search, not grep.
2406 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002407 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002408 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002409 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002410}
2411
Daniel Jasper4e566c42012-07-12 08:50:38 +00002412TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002413 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002414 forStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002415 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002416 forStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002417 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002418 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002419 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002420 doStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002421}
2422
2423TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2424 // The simplest case: every compound statement is in a function
2425 // definition, and the function body itself must be a compound
2426 // statement.
2427 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002428 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002429}
2430
2431TEST(HasAnySubstatement, IsNotRecursive) {
2432 // It's really "has any immediate substatement".
2433 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002434 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002435}
2436
2437TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2438 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002439 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002440}
2441
2442TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2443 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002444 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002445}
2446
2447TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2448 EXPECT_TRUE(matches("void f() { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002449 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002450 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002451 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002452}
2453
2454TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2455 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002456 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002457 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002458 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002459 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002460 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002461}
2462
2463TEST(StatementCountIs, WorksWithMultipleStatements) {
2464 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002465 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002466}
2467
2468TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2469 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002470 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002471 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002472 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002473 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002474 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002475 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002476 compoundStmt(statementCountIs(4))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002477}
2478
2479TEST(Member, WorksInSimplestCase) {
2480 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002481 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002482}
2483
2484TEST(Member, DoesNotMatchTheBaseExpression) {
2485 // Don't pick out the wrong part of the member expression, this should
2486 // be checking the member (name) only.
2487 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002488 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002489}
2490
2491TEST(Member, MatchesInMemberFunctionCall) {
2492 EXPECT_TRUE(matches("void f() {"
2493 " struct { void first() {}; } s;"
2494 " s.first();"
2495 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002496 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002497}
2498
Daniel Jasperb0c7b612012-10-23 15:46:39 +00002499TEST(Member, MatchesMember) {
2500 EXPECT_TRUE(matches(
2501 "struct A { int i; }; void f() { A a; a.i = 2; }",
2502 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2503 EXPECT_TRUE(notMatches(
2504 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2505 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2506}
2507
Daniel Jasper639522c2013-02-25 12:02:08 +00002508TEST(Member, UnderstandsAccess) {
2509 EXPECT_TRUE(matches(
2510 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2511 EXPECT_TRUE(notMatches(
2512 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2513 EXPECT_TRUE(notMatches(
2514 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2515
2516 EXPECT_TRUE(notMatches(
2517 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2518 EXPECT_TRUE(notMatches(
2519 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2520 EXPECT_TRUE(matches(
2521 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2522
2523 EXPECT_TRUE(notMatches(
2524 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2525 EXPECT_TRUE(matches("class A { protected: int i; };",
2526 fieldDecl(isProtected(), hasName("i"))));
2527 EXPECT_TRUE(notMatches(
2528 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2529
2530 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2531 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2532 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2533 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2534}
2535
Dmitri Gribenko06963042012-08-18 00:29:27 +00002536TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper5901e472012-10-01 13:40:41 +00002537 // Fails in C++11 mode
2538 EXPECT_TRUE(matchesConditionally(
2539 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2540 "class X { void *operator new(std::size_t); };",
2541 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002542
2543 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002544 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002545
Daniel Jasper5901e472012-10-01 13:40:41 +00002546 // Fails in C++11 mode
2547 EXPECT_TRUE(matchesConditionally(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002548 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2549 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper5901e472012-10-01 13:40:41 +00002550 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002551}
2552
Manuel Klimek04616e42012-07-06 05:48:52 +00002553TEST(HasObjectExpression, DoesNotMatchMember) {
2554 EXPECT_TRUE(notMatches(
2555 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002556 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002557}
2558
2559TEST(HasObjectExpression, MatchesBaseOfVariable) {
2560 EXPECT_TRUE(matches(
2561 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002562 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002563 EXPECT_TRUE(matches(
2564 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002565 memberExpr(hasObjectExpression(
2566 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002567}
2568
2569TEST(HasObjectExpression,
2570 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2571 EXPECT_TRUE(matches(
2572 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002573 memberExpr(hasObjectExpression(
2574 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002575 EXPECT_TRUE(matches(
2576 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002577 memberExpr(hasObjectExpression(
2578 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002579}
2580
2581TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002582 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2583 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2584 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2585 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002586}
2587
2588TEST(Field, MatchesField) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002589 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002590}
2591
2592TEST(IsConstQualified, MatchesConstInt) {
2593 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002594 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002595}
2596
2597TEST(IsConstQualified, MatchesConstPointer) {
2598 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002599 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002600}
2601
2602TEST(IsConstQualified, MatchesThroughTypedef) {
2603 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002604 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002605 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002606 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002607}
2608
2609TEST(IsConstQualified, DoesNotMatchInappropriately) {
2610 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002611 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002612 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002613 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002614}
2615
Sam Panzer80c13772012-08-16 16:58:10 +00002616TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002617 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2618 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2619 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2620 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002621}
2622TEST(CastExpression, MatchesImplicitCasts) {
2623 // This test creates an implicit cast from int to char.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002624 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002625 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002626 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002627}
2628
2629TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002630 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2631 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2632 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2633 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002634}
2635
Manuel Klimek04616e42012-07-06 05:48:52 +00002636TEST(ReinterpretCast, MatchesSimpleCase) {
2637 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002638 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002639}
2640
2641TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002642 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002643 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002644 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002645 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002646 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002647 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2648 "B b;"
2649 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002650 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002651}
2652
2653TEST(FunctionalCast, MatchesSimpleCase) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002654 std::string foo_class = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002655 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002656 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002657}
2658
2659TEST(FunctionalCast, DoesNotMatchOtherCasts) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002660 std::string FooClass = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002661 EXPECT_TRUE(
2662 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002663 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002664 EXPECT_TRUE(
2665 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002666 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002667}
2668
2669TEST(DynamicCast, MatchesSimpleCase) {
2670 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2671 "B b;"
2672 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002673 dynamicCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002674}
2675
2676TEST(StaticCast, MatchesSimpleCase) {
2677 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002678 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002679}
2680
2681TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002682 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002683 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002684 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002685 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002686 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002687 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2688 "B b;"
2689 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002690 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002691}
2692
Daniel Jasper417f7762012-09-18 13:36:17 +00002693TEST(CStyleCast, MatchesSimpleCase) {
2694 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2695}
2696
2697TEST(CStyleCast, DoesNotMatchOtherCasts) {
2698 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2699 "char q, *r = const_cast<char*>(&q);"
2700 "void* s = reinterpret_cast<char*>(&s);"
2701 "struct B { virtual ~B() {} }; struct D : B {};"
2702 "B b;"
2703 "D* t = dynamic_cast<D*>(&b);",
2704 cStyleCastExpr()));
2705}
2706
Manuel Klimek04616e42012-07-06 05:48:52 +00002707TEST(HasDestinationType, MatchesSimpleCase) {
2708 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002709 staticCastExpr(hasDestinationType(
2710 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002711}
2712
Sam Panzer80c13772012-08-16 16:58:10 +00002713TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2714 // This test creates an implicit const cast.
2715 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002716 implicitCastExpr(
2717 hasImplicitDestinationType(isInteger()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002718 // This test creates an implicit array-to-pointer cast.
2719 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002720 implicitCastExpr(hasImplicitDestinationType(
2721 pointsTo(TypeMatcher(anything()))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002722}
2723
2724TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2725 // This test creates an implicit cast from int to char.
2726 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002727 implicitCastExpr(hasImplicitDestinationType(
2728 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002729 // This test creates an implicit array-to-pointer cast.
2730 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002731 implicitCastExpr(hasImplicitDestinationType(
2732 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002733}
2734
2735TEST(ImplicitCast, MatchesSimpleCase) {
2736 // This test creates an implicit const cast.
2737 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002738 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002739 // This test creates an implicit cast from int to char.
2740 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002741 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002742 // This test creates an implicit array-to-pointer cast.
2743 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002744 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002745}
2746
2747TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002748 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer80c13772012-08-16 16:58:10 +00002749 // are present, and that it ignores explicit and paren casts.
2750
2751 // These two test cases have no casts.
2752 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002753 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002754 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002755 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002756
2757 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002758 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002759 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002760 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002761
2762 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002763 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002764}
2765
2766TEST(IgnoringImpCasts, MatchesImpCasts) {
2767 // This test checks that ignoringImpCasts matches when implicit casts are
2768 // present and its inner matcher alone does not match.
2769 // Note that this test creates an implicit const cast.
2770 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002771 varDecl(hasInitializer(ignoringImpCasts(
2772 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002773 // This test creates an implict cast from int to char.
2774 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002775 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002776 integerLiteral(equals(0)))))));
2777}
2778
2779TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2780 // These tests verify that ignoringImpCasts does not match if the inner
2781 // matcher does not match.
2782 // Note that the first test creates an implicit const cast.
2783 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002784 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002785 unless(anything()))))));
2786 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002787 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002788 unless(anything()))))));
2789
2790 // These tests verify that ignoringImplictCasts does not look through explicit
2791 // casts or parentheses.
2792 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002793 varDecl(hasInitializer(ignoringImpCasts(
2794 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002795 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002796 varDecl(hasInitializer(ignoringImpCasts(
2797 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002798 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002799 varDecl(hasInitializer(ignoringImpCasts(
2800 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002801 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002802 varDecl(hasInitializer(ignoringImpCasts(
2803 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002804}
2805
2806TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2807 // This test verifies that expressions that do not have implicit casts
2808 // still match the inner matcher.
2809 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002810 varDecl(hasInitializer(ignoringImpCasts(
2811 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002812}
2813
2814TEST(IgnoringParenCasts, MatchesParenCasts) {
2815 // This test checks that ignoringParenCasts matches when parentheses and/or
2816 // casts are present and its inner matcher alone does not match.
2817 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002818 varDecl(hasInitializer(ignoringParenCasts(
2819 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002820 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002821 varDecl(hasInitializer(ignoringParenCasts(
2822 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002823
2824 // This test creates an implict cast from int to char in addition to the
2825 // parentheses.
2826 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002827 varDecl(hasInitializer(ignoringParenCasts(
2828 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002829
2830 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002831 varDecl(hasInitializer(ignoringParenCasts(
2832 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002833 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002834 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002835 integerLiteral(equals(0)))))));
2836}
2837
2838TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2839 // This test verifies that expressions that do not have any casts still match.
2840 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002841 varDecl(hasInitializer(ignoringParenCasts(
2842 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002843}
2844
2845TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2846 // These tests verify that ignoringImpCasts does not match if the inner
2847 // matcher does not match.
2848 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002849 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002850 unless(anything()))))));
2851
2852 // This test creates an implicit cast from int to char in addition to the
2853 // parentheses.
2854 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002855 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002856 unless(anything()))))));
2857
2858 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002859 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002860 unless(anything()))))));
2861}
2862
2863TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2864 // This test checks that ignoringParenAndImpCasts matches when
2865 // parentheses and/or implicit casts are present and its inner matcher alone
2866 // does not match.
2867 // Note that this test creates an implicit const cast.
2868 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002869 varDecl(hasInitializer(ignoringParenImpCasts(
2870 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002871 // This test creates an implicit cast from int to char.
2872 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002873 varDecl(hasInitializer(ignoringParenImpCasts(
2874 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002875}
2876
2877TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2878 // This test verifies that expressions that do not have parentheses or
2879 // implicit casts still match.
2880 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002881 varDecl(hasInitializer(ignoringParenImpCasts(
2882 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002883 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002884 varDecl(hasInitializer(ignoringParenImpCasts(
2885 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002886}
2887
2888TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2889 // These tests verify that ignoringParenImpCasts does not match if
2890 // the inner matcher does not match.
2891 // This test creates an implicit cast.
2892 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002893 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002894 unless(anything()))))));
2895 // These tests verify that ignoringParenAndImplictCasts does not look
2896 // through explicit casts.
2897 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002898 varDecl(hasInitializer(ignoringParenImpCasts(
2899 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002900 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002901 varDecl(hasInitializer(ignoringParenImpCasts(
2902 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002903 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002904 varDecl(hasInitializer(ignoringParenImpCasts(
2905 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002906}
2907
Manuel Klimeke9235692012-07-25 10:02:02 +00002908TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002909 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2910 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002911 implicitCastExpr(
2912 hasSourceExpression(constructExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002913}
2914
Manuel Klimeke9235692012-07-25 10:02:02 +00002915TEST(HasSourceExpression, MatchesExplicitCasts) {
2916 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002917 explicitCastExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002918 hasSourceExpression(hasDescendant(
Daniel Jasper848cbe12012-09-18 13:09:13 +00002919 expr(integerLiteral()))))));
Manuel Klimeke9235692012-07-25 10:02:02 +00002920}
2921
Manuel Klimek04616e42012-07-06 05:48:52 +00002922TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002923 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002924}
2925
2926TEST(Statement, MatchesCompoundStatments) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002927 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002928}
2929
2930TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002931 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002932}
2933
2934TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002935 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002936}
2937
Daniel Jasper1dad1832012-07-10 20:20:19 +00002938TEST(InitListExpression, MatchesInitListExpression) {
2939 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2940 initListExpr(hasType(asString("int [2]")))));
2941 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002942 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002943}
2944
2945TEST(UsingDeclaration, MatchesUsingDeclarations) {
2946 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2947 usingDecl()));
2948}
2949
2950TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2951 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2952 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2953}
2954
2955TEST(UsingDeclaration, MatchesSpecificTarget) {
2956 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2957 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002958 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002959 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2960 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002961 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002962}
2963
2964TEST(UsingDeclaration, ThroughUsingDeclaration) {
2965 EXPECT_TRUE(matches(
2966 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002967 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002968 EXPECT_TRUE(notMatches(
2969 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002970 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002971}
2972
Sam Panzerd624bfb2012-08-16 17:20:59 +00002973TEST(SingleDecl, IsSingleDecl) {
2974 StatementMatcher SingleDeclStmt =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002975 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002976 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2977 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2978 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2979 SingleDeclStmt));
2980}
2981
2982TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002983 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002984
2985 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002986 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002987 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002988 declStmt(containsDeclaration(0, MatchesInit),
2989 containsDeclaration(1, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002990 unsigned WrongIndex = 42;
2991 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002992 declStmt(containsDeclaration(WrongIndex,
Sam Panzerd624bfb2012-08-16 17:20:59 +00002993 MatchesInit))));
2994}
2995
2996TEST(DeclCount, DeclCountIsCorrect) {
2997 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002998 declStmt(declCountIs(2))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002999 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003000 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003001 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003002 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003003}
3004
Manuel Klimek04616e42012-07-06 05:48:52 +00003005TEST(While, MatchesWhileLoops) {
3006 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
3007 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
3008 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
3009}
3010
3011TEST(Do, MatchesDoLoops) {
3012 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
3013 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
3014}
3015
3016TEST(Do, DoesNotMatchWhileLoops) {
3017 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
3018}
3019
3020TEST(SwitchCase, MatchesCase) {
3021 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
3022 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
3023 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
3024 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
3025}
3026
Daniel Jasper87c3d362012-09-20 14:12:57 +00003027TEST(SwitchCase, MatchesSwitch) {
3028 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
3029 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
3030 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
3031 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
3032}
3033
Peter Collingbourne3154a102013-05-10 11:52:02 +00003034TEST(SwitchCase, MatchesEachCase) {
3035 EXPECT_TRUE(notMatches("void x() { switch(42); }",
3036 switchStmt(forEachSwitchCase(caseStmt()))));
3037 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
3038 switchStmt(forEachSwitchCase(caseStmt()))));
3039 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
3040 switchStmt(forEachSwitchCase(caseStmt()))));
3041 EXPECT_TRUE(notMatches(
3042 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3043 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3044 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3045 switchStmt(forEachSwitchCase(
3046 caseStmt(hasCaseConstant(integerLiteral()))))));
3047 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3048 switchStmt(forEachSwitchCase(
3049 caseStmt(hasCaseConstant(integerLiteral()))))));
3050 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3051 switchStmt(forEachSwitchCase(
3052 caseStmt(hasCaseConstant(integerLiteral()))))));
3053 EXPECT_TRUE(matchAndVerifyResultTrue(
3054 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3055 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3056 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3057}
3058
Manuel Klimekba46fc02013-07-19 11:50:54 +00003059TEST(ForEachConstructorInitializer, MatchesInitializers) {
3060 EXPECT_TRUE(matches(
3061 "struct X { X() : i(42), j(42) {} int i, j; };",
3062 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3063}
3064
Daniel Jasper87c3d362012-09-20 14:12:57 +00003065TEST(ExceptionHandling, SimpleCases) {
3066 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3067 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3068 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3069 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3070 throwExpr()));
3071 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3072 throwExpr()));
3073}
3074
Manuel Klimek04616e42012-07-06 05:48:52 +00003075TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3076 EXPECT_TRUE(notMatches(
3077 "void x() { if(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003078 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003079 EXPECT_TRUE(notMatches(
3080 "void x() { int x; if((x = 42)) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003081 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003082}
3083
3084TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3085 EXPECT_TRUE(matches(
3086 "void x() { if(int* a = 0) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003087 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003088}
3089
3090TEST(ForEach, BindsOneNode) {
3091 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003092 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003093 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003094}
3095
3096TEST(ForEach, BindsMultipleNodes) {
3097 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003098 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003099 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003100}
3101
3102TEST(ForEach, BindsRecursiveCombinations) {
3103 EXPECT_TRUE(matchAndVerifyResultTrue(
3104 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003105 recordDecl(hasName("C"),
3106 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003107 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003108}
3109
3110TEST(ForEachDescendant, BindsOneNode) {
3111 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003112 recordDecl(hasName("C"),
3113 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003114 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003115}
3116
Daniel Jasper94a56852012-11-16 18:39:22 +00003117TEST(ForEachDescendant, NestedForEachDescendant) {
3118 DeclarationMatcher m = recordDecl(
3119 isDefinition(), decl().bind("x"), hasName("C"));
3120 EXPECT_TRUE(matchAndVerifyResultTrue(
3121 "class A { class B { class C {}; }; };",
3122 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3123 new VerifyIdIsBoundTo<Decl>("x", "C")));
3124
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003125 // Check that a partial match of 'm' that binds 'x' in the
3126 // first part of anyOf(m, anything()) will not overwrite the
3127 // binding created by the earlier binding in the hasDescendant.
3128 EXPECT_TRUE(matchAndVerifyResultTrue(
3129 "class A { class B { class C {}; }; };",
3130 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3131 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper94a56852012-11-16 18:39:22 +00003132}
3133
Manuel Klimek04616e42012-07-06 05:48:52 +00003134TEST(ForEachDescendant, BindsMultipleNodes) {
3135 EXPECT_TRUE(matchAndVerifyResultTrue(
3136 "class C { class D { int x; int y; }; "
3137 " class E { class F { int y; int z; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003138 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003139 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003140}
3141
3142TEST(ForEachDescendant, BindsRecursiveCombinations) {
3143 EXPECT_TRUE(matchAndVerifyResultTrue(
3144 "class C { class D { "
3145 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003146 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3147 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003148 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003149}
3150
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003151TEST(ForEachDescendant, BindsCombinations) {
3152 EXPECT_TRUE(matchAndVerifyResultTrue(
3153 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3154 "(true) {} }",
3155 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3156 forEachDescendant(whileStmt().bind("while"))),
3157 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3158}
3159
3160TEST(Has, DoesNotDeleteBindings) {
3161 EXPECT_TRUE(matchAndVerifyResultTrue(
3162 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3163 new VerifyIdIsBoundTo<Decl>("x", 1)));
3164}
3165
3166TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3167 // Those matchers cover all the cases where an inner matcher is called
3168 // and there is not a 1:1 relationship between the match of the outer
3169 // matcher and the match of the inner matcher.
3170 // The pattern to look for is:
3171 // ... return InnerMatcher.matches(...); ...
3172 // In which case no special handling is needed.
3173 //
3174 // On the other hand, if there are multiple alternative matches
3175 // (for example forEach*) or matches might be discarded (for example has*)
3176 // the implementation must make sure that the discarded matches do not
3177 // affect the bindings.
3178 // When new such matchers are added, add a test here that:
3179 // - matches a simple node, and binds it as the first thing in the matcher:
3180 // recordDecl(decl().bind("x"), hasName("X")))
3181 // - uses the matcher under test afterwards in a way that not the first
3182 // alternative is matched; for anyOf, that means the first branch
3183 // would need to return false; for hasAncestor, it means that not
3184 // the direct parent matches the inner matcher.
3185
3186 EXPECT_TRUE(matchAndVerifyResultTrue(
3187 "class X { int y; };",
3188 recordDecl(
3189 recordDecl().bind("x"), hasName("::X"),
3190 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3191 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3192 EXPECT_TRUE(matchAndVerifyResultTrue(
3193 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3194 anyOf(unless(anything()), anything())),
3195 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3196 EXPECT_TRUE(matchAndVerifyResultTrue(
3197 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3198 classTemplateSpecializationDecl(
3199 decl().bind("x"),
3200 hasAnyTemplateArgument(refersToType(asString("int")))),
3201 new VerifyIdIsBoundTo<Decl>("x", 1)));
3202 EXPECT_TRUE(matchAndVerifyResultTrue(
3203 "class X { void f(); void g(); };",
3204 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3205 new VerifyIdIsBoundTo<Decl>("x", 1)));
3206 EXPECT_TRUE(matchAndVerifyResultTrue(
3207 "class X { X() : a(1), b(2) {} double a; int b; };",
3208 recordDecl(decl().bind("x"),
3209 has(constructorDecl(
3210 hasAnyConstructorInitializer(forField(hasName("b")))))),
3211 new VerifyIdIsBoundTo<Decl>("x", 1)));
3212 EXPECT_TRUE(matchAndVerifyResultTrue(
3213 "void x(int, int) { x(0, 42); }",
3214 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3215 new VerifyIdIsBoundTo<Expr>("x", 1)));
3216 EXPECT_TRUE(matchAndVerifyResultTrue(
3217 "void x(int, int y) {}",
3218 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3219 new VerifyIdIsBoundTo<Decl>("x", 1)));
3220 EXPECT_TRUE(matchAndVerifyResultTrue(
3221 "void x() { return; if (true) {} }",
3222 functionDecl(decl().bind("x"),
3223 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3224 new VerifyIdIsBoundTo<Decl>("x", 1)));
3225 EXPECT_TRUE(matchAndVerifyResultTrue(
3226 "namespace X { void b(int); void b(); }"
3227 "using X::b;",
3228 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3229 functionDecl(parameterCountIs(1))))),
3230 new VerifyIdIsBoundTo<Decl>("x", 1)));
3231 EXPECT_TRUE(matchAndVerifyResultTrue(
3232 "class A{}; class B{}; class C : B, A {};",
3233 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3234 new VerifyIdIsBoundTo<Decl>("x", 1)));
3235 EXPECT_TRUE(matchAndVerifyResultTrue(
3236 "class A{}; typedef A B; typedef A C; typedef A D;"
3237 "class E : A {};",
3238 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3239 new VerifyIdIsBoundTo<Decl>("x", 1)));
3240 EXPECT_TRUE(matchAndVerifyResultTrue(
3241 "class A { class B { void f() {} }; };",
3242 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3243 new VerifyIdIsBoundTo<Decl>("x", 1)));
3244 EXPECT_TRUE(matchAndVerifyResultTrue(
3245 "template <typename T> struct A { struct B {"
3246 " void f() { if(true) {} }"
3247 "}; };"
3248 "void t() { A<int>::B b; b.f(); }",
3249 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3250 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3251 EXPECT_TRUE(matchAndVerifyResultTrue(
3252 "class A {};",
3253 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3254 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimekba46fc02013-07-19 11:50:54 +00003255 EXPECT_TRUE(matchAndVerifyResultTrue(
3256 "class A { A() : s(), i(42) {} const char *s; int i; };",
3257 constructorDecl(hasName("::A::A"), decl().bind("x"),
3258 forEachConstructorInitializer(forField(hasName("i")))),
3259 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003260}
3261
Daniel Jasper33806cd2012-11-11 22:14:55 +00003262TEST(ForEachDescendant, BindsCorrectNodes) {
3263 EXPECT_TRUE(matchAndVerifyResultTrue(
3264 "class C { void f(); int i; };",
3265 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3266 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3267 EXPECT_TRUE(matchAndVerifyResultTrue(
3268 "class C { void f() {} int i; };",
3269 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3270 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3271}
3272
Manuel Klimekabf43712013-02-04 10:59:20 +00003273TEST(FindAll, BindsNodeOnMatch) {
3274 EXPECT_TRUE(matchAndVerifyResultTrue(
3275 "class A {};",
3276 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3277 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3278}
3279
3280TEST(FindAll, BindsDescendantNodeOnMatch) {
3281 EXPECT_TRUE(matchAndVerifyResultTrue(
3282 "class A { int a; int b; };",
3283 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3284 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3285}
3286
3287TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3288 EXPECT_TRUE(matchAndVerifyResultTrue(
3289 "class A { int a; int b; };",
3290 recordDecl(hasName("::A"),
3291 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3292 fieldDecl().bind("v"))))),
3293 new VerifyIdIsBoundTo<Decl>("v", 3)));
3294
3295 EXPECT_TRUE(matchAndVerifyResultTrue(
3296 "class A { class B {}; class C {}; };",
3297 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3298 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3299}
3300
Manuel Klimek88b95872013-02-04 09:42:38 +00003301TEST(EachOf, TriggersForEachMatch) {
3302 EXPECT_TRUE(matchAndVerifyResultTrue(
3303 "class A { int a; int b; };",
3304 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3305 has(fieldDecl(hasName("b")).bind("v")))),
3306 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3307}
3308
3309TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3310 EXPECT_TRUE(matchAndVerifyResultTrue(
3311 "class A { int a; int c; };",
3312 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3313 has(fieldDecl(hasName("b")).bind("v")))),
3314 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3315 EXPECT_TRUE(matchAndVerifyResultTrue(
3316 "class A { int c; int b; };",
3317 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3318 has(fieldDecl(hasName("b")).bind("v")))),
3319 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3320 EXPECT_TRUE(notMatches(
3321 "class A { int c; int d; };",
3322 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3323 has(fieldDecl(hasName("b")).bind("v"))))));
3324}
Manuel Klimek04616e42012-07-06 05:48:52 +00003325
3326TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3327 // Make sure that we can both match the class by name (::X) and by the type
3328 // the template was instantiated with (via a field).
3329
3330 EXPECT_TRUE(matches(
3331 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003332 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003333
3334 EXPECT_TRUE(matches(
3335 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003336 recordDecl(isTemplateInstantiation(), hasDescendant(
3337 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003338}
3339
3340TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3341 EXPECT_TRUE(matches(
3342 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003343 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek04616e42012-07-06 05:48:52 +00003344 isTemplateInstantiation())));
3345}
3346
3347TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3348 EXPECT_TRUE(matches(
3349 "template <typename T> class X { T t; }; class A {};"
3350 "template class X<A>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003351 recordDecl(isTemplateInstantiation(), hasDescendant(
3352 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003353}
3354
3355TEST(IsTemplateInstantiation,
3356 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3357 EXPECT_TRUE(matches(
3358 "template <typename T> class X {};"
3359 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003360 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003361}
3362
3363TEST(IsTemplateInstantiation,
3364 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3365 EXPECT_TRUE(matches(
3366 "class A {};"
3367 "class X {"
3368 " template <typename U> class Y { U u; };"
3369 " Y<A> y;"
3370 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003371 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003372}
3373
3374TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3375 // FIXME: Figure out whether this makes sense. It doesn't affect the
3376 // normal use case as long as the uppermost instantiation always is marked
3377 // as template instantiation, but it might be confusing as a predicate.
3378 EXPECT_TRUE(matches(
3379 "class A {};"
3380 "template <typename T> class X {"
3381 " template <typename U> class Y { U u; };"
3382 " Y<T> y;"
3383 "}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003384 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003385}
3386
3387TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3388 EXPECT_TRUE(notMatches(
3389 "template <typename T> class X {}; class A {};"
3390 "template <> class X<A> {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003391 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003392}
3393
3394TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3395 EXPECT_TRUE(notMatches(
3396 "class A {}; class Y { A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003397 recordDecl(isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003398}
3399
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003400TEST(IsExplicitTemplateSpecialization,
3401 DoesNotMatchPrimaryTemplate) {
3402 EXPECT_TRUE(notMatches(
3403 "template <typename T> class X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003404 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003405 EXPECT_TRUE(notMatches(
3406 "template <typename T> void f(T t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003407 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003408}
3409
3410TEST(IsExplicitTemplateSpecialization,
3411 DoesNotMatchExplicitTemplateInstantiations) {
3412 EXPECT_TRUE(notMatches(
3413 "template <typename T> class X {};"
3414 "template class X<int>; extern template class X<long>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003415 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003416 EXPECT_TRUE(notMatches(
3417 "template <typename T> void f(T t) {}"
3418 "template void f(int t); extern template void f(long t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003419 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003420}
3421
3422TEST(IsExplicitTemplateSpecialization,
3423 DoesNotMatchImplicitTemplateInstantiations) {
3424 EXPECT_TRUE(notMatches(
3425 "template <typename T> class X {}; X<int> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003426 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003427 EXPECT_TRUE(notMatches(
3428 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003429 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003430}
3431
3432TEST(IsExplicitTemplateSpecialization,
3433 MatchesExplicitTemplateSpecializations) {
3434 EXPECT_TRUE(matches(
3435 "template <typename T> class X {};"
3436 "template<> class X<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003437 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003438 EXPECT_TRUE(matches(
3439 "template <typename T> void f(T t) {}"
3440 "template<> void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003441 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003442}
3443
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003444TEST(HasAncenstor, MatchesDeclarationAncestors) {
3445 EXPECT_TRUE(matches(
3446 "class A { class B { class C {}; }; };",
3447 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3448}
3449
3450TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3451 EXPECT_TRUE(notMatches(
3452 "class A { class B { class C {}; }; };",
3453 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3454}
3455
3456TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3457 EXPECT_TRUE(matches(
3458 "class A { class B { void f() { C c; } class C {}; }; };",
3459 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3460 hasAncestor(recordDecl(hasName("A"))))))));
3461}
3462
3463TEST(HasAncenstor, MatchesStatementAncestors) {
3464 EXPECT_TRUE(matches(
3465 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003466 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003467}
3468
3469TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3470 EXPECT_TRUE(matches(
3471 "void f() { if (true) { int x = 42; } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003472 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003473}
3474
3475TEST(HasAncestor, BindsRecursiveCombinations) {
3476 EXPECT_TRUE(matchAndVerifyResultTrue(
3477 "class C { class D { class E { class F { int y; }; }; }; };",
3478 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003479 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003480}
3481
3482TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3483 EXPECT_TRUE(matchAndVerifyResultTrue(
3484 "class C { class D { class E { class F { int y; }; }; }; };",
3485 fieldDecl(hasAncestor(
3486 decl(
3487 hasDescendant(recordDecl(isDefinition(),
3488 hasAncestor(recordDecl())))
3489 ).bind("d")
3490 )),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003491 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003492}
3493
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003494TEST(HasAncestor, MatchesClosestAncestor) {
3495 EXPECT_TRUE(matchAndVerifyResultTrue(
3496 "template <typename T> struct C {"
3497 " void f(int) {"
3498 " struct I { void g(T) { int x; } } i; i.g(42);"
3499 " }"
3500 "};"
3501 "template struct C<int>;",
3502 varDecl(hasName("x"),
3503 hasAncestor(functionDecl(hasParameter(
3504 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3505 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3506}
3507
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003508TEST(HasAncestor, MatchesInTemplateInstantiations) {
3509 EXPECT_TRUE(matches(
3510 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3511 "A<int>::B::C a;",
3512 fieldDecl(hasType(asString("int")),
3513 hasAncestor(recordDecl(hasName("A"))))));
3514}
3515
3516TEST(HasAncestor, MatchesInImplicitCode) {
3517 EXPECT_TRUE(matches(
3518 "struct X {}; struct A { A() {} X x; };",
3519 constructorDecl(
3520 hasAnyConstructorInitializer(withInitializer(expr(
3521 hasAncestor(recordDecl(hasName("A")))))))));
3522}
3523
Daniel Jasper632aea92012-10-22 16:26:51 +00003524TEST(HasParent, MatchesOnlyParent) {
3525 EXPECT_TRUE(matches(
3526 "void f() { if (true) { int x = 42; } }",
3527 compoundStmt(hasParent(ifStmt()))));
3528 EXPECT_TRUE(notMatches(
3529 "void f() { for (;;) { int x = 42; } }",
3530 compoundStmt(hasParent(ifStmt()))));
3531 EXPECT_TRUE(notMatches(
3532 "void f() { if (true) for (;;) { int x = 42; } }",
3533 compoundStmt(hasParent(ifStmt()))));
3534}
3535
Manuel Klimekc844a462012-12-06 14:42:48 +00003536TEST(HasAncestor, MatchesAllAncestors) {
3537 EXPECT_TRUE(matches(
3538 "template <typename T> struct C { static void f() { 42; } };"
3539 "void t() { C<int>::f(); }",
3540 integerLiteral(
3541 equals(42),
3542 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3543 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3544}
3545
3546TEST(HasParent, MatchesAllParents) {
3547 EXPECT_TRUE(matches(
3548 "template <typename T> struct C { static void f() { 42; } };"
3549 "void t() { C<int>::f(); }",
3550 integerLiteral(
3551 equals(42),
3552 hasParent(compoundStmt(hasParent(functionDecl(
3553 hasParent(recordDecl(isTemplateInstantiation())))))))));
3554 EXPECT_TRUE(matches(
3555 "template <typename T> struct C { static void f() { 42; } };"
3556 "void t() { C<int>::f(); }",
3557 integerLiteral(
3558 equals(42),
3559 hasParent(compoundStmt(hasParent(functionDecl(
3560 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3561 EXPECT_TRUE(matches(
3562 "template <typename T> struct C { static void f() { 42; } };"
3563 "void t() { C<int>::f(); }",
3564 integerLiteral(equals(42),
3565 hasParent(compoundStmt(allOf(
3566 hasParent(functionDecl(
3567 hasParent(recordDecl(isTemplateInstantiation())))),
3568 hasParent(functionDecl(hasParent(recordDecl(
3569 unless(isTemplateInstantiation())))))))))));
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003570 EXPECT_TRUE(
3571 notMatches("template <typename T> struct C { static void f() {} };"
3572 "void t() { C<int>::f(); }",
3573 compoundStmt(hasParent(recordDecl()))));
Manuel Klimekc844a462012-12-06 14:42:48 +00003574}
3575
Daniel Jasper516b02e2012-10-17 08:52:59 +00003576TEST(TypeMatching, MatchesTypes) {
3577 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3578}
3579
3580TEST(TypeMatching, MatchesArrayTypes) {
3581 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3582 EXPECT_TRUE(matches("int a[42];", arrayType()));
3583 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3584
3585 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3586 arrayType(hasElementType(builtinType()))));
3587
3588 EXPECT_TRUE(matches(
3589 "int const a[] = { 2, 3 };",
3590 qualType(arrayType(hasElementType(builtinType())))));
3591 EXPECT_TRUE(matches(
3592 "int const a[] = { 2, 3 };",
3593 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3594 EXPECT_TRUE(matches(
3595 "typedef const int T; T x[] = { 1, 2 };",
3596 qualType(isConstQualified(), arrayType())));
3597
3598 EXPECT_TRUE(notMatches(
3599 "int a[] = { 2, 3 };",
3600 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3601 EXPECT_TRUE(notMatches(
3602 "int a[] = { 2, 3 };",
3603 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3604 EXPECT_TRUE(notMatches(
3605 "int const a[] = { 2, 3 };",
3606 qualType(arrayType(hasElementType(builtinType())),
3607 unless(isConstQualified()))));
3608
3609 EXPECT_TRUE(matches("int a[2];",
3610 constantArrayType(hasElementType(builtinType()))));
3611 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3612}
3613
3614TEST(TypeMatching, MatchesComplexTypes) {
3615 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3616 EXPECT_TRUE(matches(
3617 "_Complex float f;",
3618 complexType(hasElementType(builtinType()))));
3619 EXPECT_TRUE(notMatches(
3620 "_Complex float f;",
3621 complexType(hasElementType(isInteger()))));
3622}
3623
3624TEST(TypeMatching, MatchesConstantArrayTypes) {
3625 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3626 EXPECT_TRUE(notMatches(
3627 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3628 constantArrayType(hasElementType(builtinType()))));
3629
3630 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3631 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3632 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3633}
3634
3635TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3636 EXPECT_TRUE(matches(
3637 "template <typename T, int Size> class array { T data[Size]; };",
3638 dependentSizedArrayType()));
3639 EXPECT_TRUE(notMatches(
3640 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3641 dependentSizedArrayType()));
3642}
3643
3644TEST(TypeMatching, MatchesIncompleteArrayType) {
3645 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3646 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3647
3648 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3649 incompleteArrayType()));
3650}
3651
3652TEST(TypeMatching, MatchesVariableArrayType) {
3653 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3654 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3655
3656 EXPECT_TRUE(matches(
3657 "void f(int b) { int a[b]; }",
3658 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3659 varDecl(hasName("b")))))))));
3660}
3661
3662TEST(TypeMatching, MatchesAtomicTypes) {
3663 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3664
3665 EXPECT_TRUE(matches("_Atomic(int) i;",
3666 atomicType(hasValueType(isInteger()))));
3667 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3668 atomicType(hasValueType(isInteger()))));
3669}
3670
3671TEST(TypeMatching, MatchesAutoTypes) {
3672 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3673 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3674 autoType()));
3675
Richard Smith061f1e22013-04-30 21:23:01 +00003676 // FIXME: Matching against the type-as-written can't work here, because the
3677 // type as written was not deduced.
3678 //EXPECT_TRUE(matches("auto a = 1;",
3679 // autoType(hasDeducedType(isInteger()))));
3680 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3681 // autoType(hasDeducedType(isInteger()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003682}
3683
Daniel Jasperd29d5fa2012-10-29 10:14:44 +00003684TEST(TypeMatching, MatchesFunctionTypes) {
3685 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3686 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3687}
3688
Edwin Vaneec074802013-04-01 18:33:34 +00003689TEST(TypeMatching, MatchesParenType) {
3690 EXPECT_TRUE(
3691 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3692 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3693
3694 EXPECT_TRUE(matches(
3695 "int (*ptr_to_func)(int);",
3696 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3697 EXPECT_TRUE(notMatches(
3698 "int (*ptr_to_array)[4];",
3699 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3700}
3701
Daniel Jasper516b02e2012-10-17 08:52:59 +00003702TEST(TypeMatching, PointerTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003703 // FIXME: Reactive when these tests can be more specific (not matching
3704 // implicit code on certain platforms), likely when we have hasDescendant for
3705 // Types/TypeLocs.
3706 //EXPECT_TRUE(matchAndVerifyResultTrue(
3707 // "int* a;",
3708 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3709 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3710 //EXPECT_TRUE(matchAndVerifyResultTrue(
3711 // "int* a;",
3712 // pointerTypeLoc().bind("loc"),
3713 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003714 EXPECT_TRUE(matches(
3715 "int** a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003716 loc(pointerType(pointee(qualType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003717 EXPECT_TRUE(matches(
3718 "int** a;",
3719 loc(pointerType(pointee(pointerType())))));
3720 EXPECT_TRUE(matches(
3721 "int* b; int* * const a = &b;",
3722 loc(qualType(isConstQualified(), pointerType()))));
3723
3724 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper7943eb52012-10-17 13:35:36 +00003725 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3726 hasType(blockPointerType()))));
3727 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3728 hasType(memberPointerType()))));
3729 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3730 hasType(pointerType()))));
3731 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3732 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003733 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3734 hasType(lValueReferenceType()))));
3735 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3736 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003737
Daniel Jasper7943eb52012-10-17 13:35:36 +00003738 Fragment = "int *ptr;";
3739 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3740 hasType(blockPointerType()))));
3741 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3742 hasType(memberPointerType()))));
3743 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3744 hasType(pointerType()))));
3745 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3746 hasType(referenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003747
Daniel Jasper7943eb52012-10-17 13:35:36 +00003748 Fragment = "int a; int &ref = a;";
3749 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3750 hasType(blockPointerType()))));
3751 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3752 hasType(memberPointerType()))));
3753 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3754 hasType(pointerType()))));
3755 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3756 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003757 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3758 hasType(lValueReferenceType()))));
3759 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3760 hasType(rValueReferenceType()))));
3761
3762 Fragment = "int &&ref = 2;";
3763 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3764 hasType(blockPointerType()))));
3765 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3766 hasType(memberPointerType()))));
3767 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3768 hasType(pointerType()))));
3769 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3770 hasType(referenceType()))));
3771 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3772 hasType(lValueReferenceType()))));
3773 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3774 hasType(rValueReferenceType()))));
3775}
3776
3777TEST(TypeMatching, AutoRefTypes) {
3778 std::string Fragment = "auto a = 1;"
3779 "auto b = a;"
3780 "auto &c = a;"
3781 "auto &&d = c;"
3782 "auto &&e = 2;";
3783 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3784 hasType(referenceType()))));
3785 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3786 hasType(referenceType()))));
3787 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3788 hasType(referenceType()))));
3789 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3790 hasType(lValueReferenceType()))));
3791 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3792 hasType(rValueReferenceType()))));
3793 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3794 hasType(referenceType()))));
3795 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3796 hasType(lValueReferenceType()))));
3797 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3798 hasType(rValueReferenceType()))));
3799 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3800 hasType(referenceType()))));
3801 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3802 hasType(lValueReferenceType()))));
3803 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3804 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003805}
3806
3807TEST(TypeMatching, PointeeTypes) {
3808 EXPECT_TRUE(matches("int b; int &a = b;",
3809 referenceType(pointee(builtinType()))));
3810 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3811
3812 EXPECT_TRUE(matches("int *a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003813 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003814
3815 EXPECT_TRUE(matches(
3816 "int const *A;",
3817 pointerType(pointee(isConstQualified(), builtinType()))));
3818 EXPECT_TRUE(notMatches(
3819 "int *A;",
3820 pointerType(pointee(isConstQualified(), builtinType()))));
3821}
3822
3823TEST(TypeMatching, MatchesPointersToConstTypes) {
3824 EXPECT_TRUE(matches("int b; int * const a = &b;",
3825 loc(pointerType())));
3826 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003827 loc(pointerType())));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003828 EXPECT_TRUE(matches(
3829 "int b; const int * a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003830 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003831 EXPECT_TRUE(matches(
3832 "int b; const int * a = &b;",
3833 pointerType(pointee(builtinType()))));
3834}
3835
3836TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003837 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3838 hasType(typedefType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003839}
3840
Edwin Vanef901b712013-02-25 14:49:29 +00003841TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vaneb6eae142013-02-25 20:43:32 +00003842 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vanef901b712013-02-25 14:49:29 +00003843 templateSpecializationType()));
3844}
3845
Edwin Vaneb6eae142013-02-25 20:43:32 +00003846TEST(TypeMatching, MatchesRecordType) {
3847 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek59b0af62013-02-27 11:56:58 +00003848 EXPECT_TRUE(matches("struct S{}; S s;",
3849 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3850 EXPECT_TRUE(notMatches("int i;",
3851 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003852}
3853
3854TEST(TypeMatching, MatchesElaboratedType) {
3855 EXPECT_TRUE(matches(
3856 "namespace N {"
3857 " namespace M {"
3858 " class D {};"
3859 " }"
3860 "}"
3861 "N::M::D d;", elaboratedType()));
3862 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3863 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3864}
3865
3866TEST(ElaboratedTypeNarrowing, hasQualifier) {
3867 EXPECT_TRUE(matches(
3868 "namespace N {"
3869 " namespace M {"
3870 " class D {};"
3871 " }"
3872 "}"
3873 "N::M::D d;",
3874 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3875 EXPECT_TRUE(notMatches(
3876 "namespace M {"
3877 " class D {};"
3878 "}"
3879 "M::D d;",
3880 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vane6972f6d2013-03-04 17:51:00 +00003881 EXPECT_TRUE(notMatches(
3882 "struct D {"
3883 "} d;",
3884 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003885}
3886
3887TEST(ElaboratedTypeNarrowing, namesType) {
3888 EXPECT_TRUE(matches(
3889 "namespace N {"
3890 " namespace M {"
3891 " class D {};"
3892 " }"
3893 "}"
3894 "N::M::D d;",
3895 elaboratedType(elaboratedType(namesType(recordType(
3896 hasDeclaration(namedDecl(hasName("D")))))))));
3897 EXPECT_TRUE(notMatches(
3898 "namespace M {"
3899 " class D {};"
3900 "}"
3901 "M::D d;",
3902 elaboratedType(elaboratedType(namesType(typedefType())))));
3903}
3904
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003905TEST(NNS, MatchesNestedNameSpecifiers) {
3906 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3907 nestedNameSpecifier()));
3908 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3909 nestedNameSpecifier()));
3910 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3911 nestedNameSpecifier()));
3912
3913 EXPECT_TRUE(matches(
3914 "struct A { static void f() {} }; void g() { A::f(); }",
3915 nestedNameSpecifier()));
3916 EXPECT_TRUE(notMatches(
3917 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3918 nestedNameSpecifier()));
3919}
3920
Daniel Jasper87c3d362012-09-20 14:12:57 +00003921TEST(NullStatement, SimpleCases) {
3922 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3923 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3924}
3925
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003926TEST(NNS, MatchesTypes) {
3927 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3928 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3929 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3930 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3931 Matcher));
3932 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3933}
3934
3935TEST(NNS, MatchesNamespaceDecls) {
3936 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3937 specifiesNamespace(hasName("ns")));
3938 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3939 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3940 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3941}
3942
3943TEST(NNS, BindsNestedNameSpecifiers) {
3944 EXPECT_TRUE(matchAndVerifyResultTrue(
3945 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3946 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3947 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3948}
3949
3950TEST(NNS, BindsNestedNameSpecifierLocs) {
3951 EXPECT_TRUE(matchAndVerifyResultTrue(
3952 "namespace ns { struct B {}; } ns::B b;",
3953 loc(nestedNameSpecifier()).bind("loc"),
3954 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3955}
3956
3957TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3958 EXPECT_TRUE(matches(
3959 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3960 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3961 EXPECT_TRUE(matches(
3962 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasper516b02e2012-10-17 08:52:59 +00003963 nestedNameSpecifierLoc(hasPrefix(
3964 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003965}
3966
Daniel Jasper6fc34332012-10-30 15:42:00 +00003967TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3968 std::string Fragment =
3969 "namespace a { struct A { struct B { struct C {}; }; }; };"
3970 "void f() { a::A::B::C c; }";
3971 EXPECT_TRUE(matches(
3972 Fragment,
3973 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3974 hasDescendant(nestedNameSpecifier(
3975 specifiesNamespace(hasName("a")))))));
3976 EXPECT_TRUE(notMatches(
3977 Fragment,
3978 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3979 has(nestedNameSpecifier(
3980 specifiesNamespace(hasName("a")))))));
3981 EXPECT_TRUE(matches(
3982 Fragment,
3983 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3984 has(nestedNameSpecifier(
3985 specifiesNamespace(hasName("a")))))));
3986
3987 // Not really useful because a NestedNameSpecifier can af at most one child,
3988 // but to complete the interface.
3989 EXPECT_TRUE(matchAndVerifyResultTrue(
3990 Fragment,
3991 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3992 forEach(nestedNameSpecifier().bind("x"))),
3993 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3994}
3995
3996TEST(NNS, NestedNameSpecifiersAsDescendants) {
3997 std::string Fragment =
3998 "namespace a { struct A { struct B { struct C {}; }; }; };"
3999 "void f() { a::A::B::C c; }";
4000 EXPECT_TRUE(matches(
4001 Fragment,
4002 decl(hasDescendant(nestedNameSpecifier(specifiesType(
4003 asString("struct a::A")))))));
4004 EXPECT_TRUE(matchAndVerifyResultTrue(
4005 Fragment,
4006 functionDecl(hasName("f"),
4007 forEachDescendant(nestedNameSpecifier().bind("x"))),
4008 // Nested names: a, a::A and a::A::B.
4009 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
4010}
4011
4012TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
4013 std::string Fragment =
4014 "namespace a { struct A { struct B { struct C {}; }; }; };"
4015 "void f() { a::A::B::C c; }";
4016 EXPECT_TRUE(matches(
4017 Fragment,
4018 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4019 hasDescendant(loc(nestedNameSpecifier(
4020 specifiesNamespace(hasName("a"))))))));
4021 EXPECT_TRUE(notMatches(
4022 Fragment,
4023 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4024 has(loc(nestedNameSpecifier(
4025 specifiesNamespace(hasName("a"))))))));
4026 EXPECT_TRUE(matches(
4027 Fragment,
4028 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
4029 has(loc(nestedNameSpecifier(
4030 specifiesNamespace(hasName("a"))))))));
4031
4032 EXPECT_TRUE(matchAndVerifyResultTrue(
4033 Fragment,
4034 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4035 forEach(nestedNameSpecifierLoc().bind("x"))),
4036 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
4037}
4038
4039TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
4040 std::string Fragment =
4041 "namespace a { struct A { struct B { struct C {}; }; }; };"
4042 "void f() { a::A::B::C c; }";
4043 EXPECT_TRUE(matches(
4044 Fragment,
4045 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4046 asString("struct a::A"))))))));
4047 EXPECT_TRUE(matchAndVerifyResultTrue(
4048 Fragment,
4049 functionDecl(hasName("f"),
4050 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4051 // Nested names: a, a::A and a::A::B.
4052 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4053}
4054
Manuel Klimek191c0932013-02-01 13:41:35 +00004055template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimekc2687452012-10-24 14:47:44 +00004056public:
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004057 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4058 StringRef InnerId)
4059 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jaspere9aa6872012-10-29 10:48:25 +00004060 }
4061
Manuel Klimek191c0932013-02-01 13:41:35 +00004062 virtual bool run(const BoundNodes *Nodes) { return false; }
4063
Manuel Klimekc2687452012-10-24 14:47:44 +00004064 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4065 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004066 return selectFirst<const T>(InnerId,
4067 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimekc2687452012-10-24 14:47:44 +00004068 }
4069private:
4070 std::string Id;
4071 internal::Matcher<T> InnerMatcher;
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004072 std::string InnerId;
Manuel Klimekc2687452012-10-24 14:47:44 +00004073};
4074
4075TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004076 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", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4080 "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004081 EXPECT_TRUE(matchAndVerifyResultFalse(
4082 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4083 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004084 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4085 "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004086}
4087
4088TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004089 EXPECT_TRUE(matchAndVerifyResultTrue(
4090 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004091 new VerifyMatchOnNode<clang::Stmt>(
4092 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004093 EXPECT_TRUE(matchAndVerifyResultFalse(
4094 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004095 new VerifyMatchOnNode<clang::Stmt>(
4096 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004097}
4098
4099TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4100 EXPECT_TRUE(matchAndVerifyResultTrue(
4101 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4102 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004103 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004104 EXPECT_TRUE(matchAndVerifyResultFalse(
4105 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4106 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004107 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004108}
4109
Manuel Klimekbee08572013-02-07 12:42:10 +00004110template <typename T>
4111class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4112public:
4113 virtual bool run(const BoundNodes *Nodes) { return false; }
4114
4115 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4116 const T *Node = Nodes->getNodeAs<T>("");
4117 return verify(*Nodes, *Context, Node);
4118 }
4119
4120 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4121 return selectFirst<const T>(
4122 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4123 *Node, Context)) != NULL;
4124 }
4125 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4126 return selectFirst<const T>(
4127 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4128 *Node, Context)) != NULL;
4129 }
4130};
4131
4132TEST(IsEqualTo, MatchesNodesByIdentity) {
4133 EXPECT_TRUE(matchAndVerifyResultTrue(
4134 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4135 new VerifyAncestorHasChildIsEqual<Decl>()));
4136 EXPECT_TRUE(
4137 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4138 new VerifyAncestorHasChildIsEqual<Stmt>()));
4139}
4140
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004141class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4142public:
4143 VerifyStartOfTranslationUnit() : Called(false) {}
4144 virtual void run(const MatchFinder::MatchResult &Result) {
4145 EXPECT_TRUE(Called);
4146 }
4147 virtual void onStartOfTranslationUnit() {
4148 Called = true;
4149 }
4150 bool Called;
4151};
4152
4153TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4154 MatchFinder Finder;
4155 VerifyStartOfTranslationUnit VerifyCallback;
4156 Finder.addMatcher(decl(), &VerifyCallback);
4157 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4158 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4159 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004160
4161 VerifyCallback.Called = false;
4162 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4163 ASSERT_TRUE(AST.get());
4164 Finder.matchAST(AST->getASTContext());
4165 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004166}
4167
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004168class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4169public:
4170 VerifyEndOfTranslationUnit() : Called(false) {}
4171 virtual void run(const MatchFinder::MatchResult &Result) {
4172 EXPECT_FALSE(Called);
4173 }
4174 virtual void onEndOfTranslationUnit() {
4175 Called = true;
4176 }
4177 bool Called;
4178};
4179
4180TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4181 MatchFinder Finder;
4182 VerifyEndOfTranslationUnit VerifyCallback;
4183 Finder.addMatcher(decl(), &VerifyCallback);
4184 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4185 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4186 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004187
4188 VerifyCallback.Called = false;
4189 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4190 ASSERT_TRUE(AST.get());
4191 Finder.matchAST(AST->getASTContext());
4192 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004193}
4194
Manuel Klimekbbb75852013-06-20 14:06:32 +00004195TEST(EqualsBoundNodeMatcher, QualType) {
4196 EXPECT_TRUE(matches(
4197 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4198 hasInitializer(ignoringParenImpCasts(
4199 hasType(qualType(equalsBoundNode("type"))))))));
4200 EXPECT_TRUE(notMatches("int i = 1.f;",
4201 varDecl(hasType(qualType().bind("type")),
4202 hasInitializer(ignoringParenImpCasts(hasType(
4203 qualType(equalsBoundNode("type"))))))));
4204}
4205
4206TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4207 EXPECT_TRUE(notMatches(
4208 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4209 hasInitializer(ignoringParenImpCasts(
4210 hasType(qualType(equalsBoundNode("type"))))))));
4211}
4212
4213TEST(EqualsBoundNodeMatcher, Stmt) {
4214 EXPECT_TRUE(
4215 matches("void f() { if(true) {} }",
4216 stmt(allOf(ifStmt().bind("if"),
4217 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4218
4219 EXPECT_TRUE(notMatches(
4220 "void f() { if(true) { if (true) {} } }",
4221 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4222}
4223
4224TEST(EqualsBoundNodeMatcher, Decl) {
4225 EXPECT_TRUE(matches(
4226 "class X { class Y {}; };",
4227 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4228 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4229
4230 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4231 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4232 has(decl(equalsBoundNode("record")))))));
4233}
4234
4235TEST(EqualsBoundNodeMatcher, Type) {
4236 EXPECT_TRUE(matches(
4237 "class X { int a; int b; };",
4238 recordDecl(
4239 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4240 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4241
4242 EXPECT_TRUE(notMatches(
4243 "class X { int a; double b; };",
4244 recordDecl(
4245 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4246 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4247}
4248
4249TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4250
4251 EXPECT_TRUE(matchAndVerifyResultTrue(
4252 "int f() {"
4253 " if (1) {"
4254 " int i = 9;"
4255 " }"
4256 " int j = 10;"
4257 " {"
4258 " float k = 9.0;"
4259 " }"
4260 " return 0;"
4261 "}",
4262 // Look for variable declarations within functions whose type is the same
4263 // as the function return type.
4264 functionDecl(returns(qualType().bind("type")),
4265 forEachDescendant(varDecl(hasType(
4266 qualType(equalsBoundNode("type")))).bind("decl"))),
4267 // Only i and j should match, not k.
4268 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4269}
4270
4271TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4272 EXPECT_TRUE(matchAndVerifyResultTrue(
4273 "void f() {"
4274 " int x;"
4275 " double d;"
4276 " x = d + x - d + x;"
4277 "}",
4278 functionDecl(
4279 hasName("f"), forEachDescendant(varDecl().bind("d")),
4280 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4281 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4282}
4283
Manuel Klimek04616e42012-07-06 05:48:52 +00004284} // end namespace ast_matchers
4285} // end namespace clang