blob: dc8b15fde156d4268a4832036cf7f622f2d21467 [file] [log] [blame]
Manuel Klimek4da21662012-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 Kramer8b9ed712012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000014#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace ast_matchers {
19
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000020#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000021TEST(HasNameDeathTest, DiesOnEmptyName) {
22 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000023 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000024 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
25 }, "");
26}
27
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000028TEST(HasNameDeathTest, DiesOnEmptyPattern) {
29 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000030 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000031 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
32 }, "");
33}
34
Manuel Klimek4da21662012-07-06 05:48:52 +000035TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
36 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000037 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000038 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
39 }, "");
40}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000041#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000042
Manuel Klimek715c9562012-07-25 10:02:02 +000043TEST(Decl, MatchesDeclarations) {
44 EXPECT_TRUE(notMatches("", decl(usingDecl())));
45 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
46 decl(usingDecl())));
47}
48
Manuel Klimek4da21662012-07-06 05:48:52 +000049TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000050 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000051 EXPECT_TRUE(matches("typedef int X;", NamedX));
52 EXPECT_TRUE(matches("int X;", NamedX));
53 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
54 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
55 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
56 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000057 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000058
59 EXPECT_TRUE(notMatches("#define X 1", NamedX));
60}
61
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000062TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000063 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000064 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
65 EXPECT_TRUE(matches("int Xb;", NamedX));
66 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
67 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
68 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
69 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
70 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
71
72 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
73
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000074 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000075 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
76 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
77
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000078 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000079 EXPECT_TRUE(matches("int abc;", Abc));
80 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
81 EXPECT_TRUE(notMatches("int cab;", Abc));
82 EXPECT_TRUE(matches("int cabc;", Abc));
Manuel Klimekec33b6f2012-12-10 07:08:53 +000083
84 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
85 EXPECT_TRUE(matches("int k;", StartsWithK));
86 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
87 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
88 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
89 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000090}
91
Manuel Klimek4da21662012-07-06 05:48:52 +000092TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000094#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000095 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000096#else
97 // Matches class type_info.
98 EXPECT_TRUE(matches("", ClassMatcher));
99#endif
Manuel Klimek4da21662012-07-06 05:48:52 +0000100
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102 EXPECT_TRUE(matches("class X;", ClassX));
103 EXPECT_TRUE(matches("class X {};", ClassX));
104 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
105 EXPECT_TRUE(notMatches("", ClassX));
106}
107
108TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000109 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000112 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
113 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000114 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
115 EXPECT_TRUE(notMatches("", IsDerivedFromX));
116
Daniel Jasper63d88722012-09-12 21:14:15 +0000117 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000118
119 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
120 EXPECT_TRUE(matches("class X {};", IsAX));
121 EXPECT_TRUE(matches("class X;", IsAX));
122 EXPECT_TRUE(notMatches("class Y;", IsAX));
123 EXPECT_TRUE(notMatches("", IsAX));
124
Manuel Klimek4da21662012-07-06 05:48:52 +0000125 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000126 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000127 EXPECT_TRUE(
128 matches("class X {}; class Y : public X {}; class Z : public Y {};",
129 ZIsDerivedFromX));
130 EXPECT_TRUE(
131 matches("class X {};"
132 "template<class T> class Y : public X {};"
133 "class Z : public Y<int> {};", ZIsDerivedFromX));
134 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 matches("template<class T> class X {}; "
138 "template<class T> class Z : public X<T> {};",
139 ZIsDerivedFromX));
140 EXPECT_TRUE(
141 matches("template<class T, class U=T> class X {}; "
142 "template<class T> class Z : public X<T> {};",
143 ZIsDerivedFromX));
144 EXPECT_TRUE(
145 notMatches("template<class X> class A { class Z : public X {}; };",
146 ZIsDerivedFromX));
147 EXPECT_TRUE(
148 matches("template<class X> class A { public: class Z : public X {}; }; "
149 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template <class T> class X {}; "
152 "template<class Y> class A { class Z : public X<Y> {}; };",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<template<class T> class X> class A { "
156 " class Z : public X<int> {}; };", ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<template<class T> class X> class A { "
159 " public: class Z : public X<int> {}; }; "
160 "template<class T> class X {}; void y() { A<X>::Z z; }",
161 ZIsDerivedFromX));
162 EXPECT_TRUE(
163 notMatches("template<class X> class A { class Z : public X::D {}; };",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class X> class A { public: "
167 " class Z : public X::D {}; }; "
168 "class Y { public: class X {}; typedef X D; }; "
169 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
170 EXPECT_TRUE(
171 matches("class X {}; typedef X Y; class Z : public Y {};",
172 ZIsDerivedFromX));
173 EXPECT_TRUE(
174 matches("template<class T> class Y { typedef typename T::U X; "
175 " class Z : public X {}; };", ZIsDerivedFromX));
176 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
177 ZIsDerivedFromX));
178 EXPECT_TRUE(
179 notMatches("template<class T> class X {}; "
180 "template<class T> class A { class Z : public X<T>::D {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("template<class T> class X { public: typedef X<T> D; }; "
184 "template<class T> class A { public: "
185 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
186 ZIsDerivedFromX));
187 EXPECT_TRUE(
188 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 matches("class X {}; class Y : public X {}; "
195 "typedef Y V; typedef V W; class Z : public W {};",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("template<class T, class U> class X {}; "
199 "template<class T> class A { class Z : public X<T, int> {}; };",
200 ZIsDerivedFromX));
201 EXPECT_TRUE(
202 notMatches("template<class X> class D { typedef X A; typedef A B; "
203 " typedef B C; class Z : public C {}; };",
204 ZIsDerivedFromX));
205 EXPECT_TRUE(
206 matches("class X {}; typedef X A; typedef A B; "
207 "class Z : public B {};", ZIsDerivedFromX));
208 EXPECT_TRUE(
209 matches("class X {}; typedef X A; typedef A B; typedef B C; "
210 "class Z : public C {};", ZIsDerivedFromX));
211 EXPECT_TRUE(
212 matches("class U {}; typedef U X; typedef X V; "
213 "class Z : public V {};", ZIsDerivedFromX));
214 EXPECT_TRUE(
215 matches("class Base {}; typedef Base X; "
216 "class Z : public Base {};", ZIsDerivedFromX));
217 EXPECT_TRUE(
218 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
219 "class Z : public Base {};", ZIsDerivedFromX));
220 EXPECT_TRUE(
221 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
222 "class Z : public Base {};", ZIsDerivedFromX));
223 EXPECT_TRUE(
224 matches("class A {}; typedef A X; typedef A Y; "
225 "class Z : public Y {};", ZIsDerivedFromX));
226 EXPECT_TRUE(
227 notMatches("template <typename T> class Z;"
228 "template <> class Z<void> {};"
229 "template <typename T> class Z : public Z<void> {};",
230 IsDerivedFromX));
231 EXPECT_TRUE(
232 matches("template <typename T> class X;"
233 "template <> class X<void> {};"
234 "template <typename T> class X : public X<void> {};",
235 IsDerivedFromX));
236 EXPECT_TRUE(matches(
237 "class X {};"
238 "template <typename T> class Z;"
239 "template <> class Z<void> {};"
240 "template <typename T> class Z : public Z<void>, public X {};",
241 ZIsDerivedFromX));
Manuel Klimek987c2f52012-12-04 13:40:29 +0000242 EXPECT_TRUE(
243 notMatches("template<int> struct X;"
244 "template<int i> struct X : public X<i-1> {};",
245 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
246 EXPECT_TRUE(matches(
247 "struct A {};"
248 "template<int> struct X;"
249 "template<int i> struct X : public X<i-1> {};"
250 "template<> struct X<0> : public A {};"
251 "struct B : public X<42> {};",
252 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000253
254 // FIXME: Once we have better matchers for template type matching,
255 // get rid of the Variable(...) matching and match the right template
256 // declarations directly.
257 const char *RecursiveTemplateOneParameter =
258 "class Base1 {}; class Base2 {};"
259 "template <typename T> class Z;"
260 "template <> class Z<void> : public Base1 {};"
261 "template <> class Z<int> : public Base2 {};"
262 "template <> class Z<float> : public Z<void> {};"
263 "template <> class Z<double> : public Z<int> {};"
264 "template <typename T> class Z : public Z<float>, public Z<double> {};"
265 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
266 EXPECT_TRUE(matches(
267 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000268 varDecl(hasName("z_float"),
269 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000270 EXPECT_TRUE(notMatches(
271 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000272 varDecl(hasName("z_float"),
273 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000274 EXPECT_TRUE(matches(
275 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000276 varDecl(hasName("z_char"),
277 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
278 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000279
280 const char *RecursiveTemplateTwoParameters =
281 "class Base1 {}; class Base2 {};"
282 "template <typename T1, typename T2> class Z;"
283 "template <typename T> class Z<void, T> : public Base1 {};"
284 "template <typename T> class Z<int, T> : public Base2 {};"
285 "template <typename T> class Z<float, T> : public Z<void, T> {};"
286 "template <typename T> class Z<double, T> : public Z<int, T> {};"
287 "template <typename T1, typename T2> class Z : "
288 " public Z<float, T2>, public Z<double, T2> {};"
289 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
290 " Z<char, void> z_char; }";
291 EXPECT_TRUE(matches(
292 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000293 varDecl(hasName("z_float"),
294 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295 EXPECT_TRUE(notMatches(
296 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000297 varDecl(hasName("z_float"),
298 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000299 EXPECT_TRUE(matches(
300 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000301 varDecl(hasName("z_char"),
302 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
303 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000304 EXPECT_TRUE(matches(
305 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000306 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000307 EXPECT_TRUE(notMatches(
308 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000309 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000310
311 EXPECT_TRUE(matches(
312 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000313 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000314}
315
Daniel Jasper08f0c532012-09-18 14:17:42 +0000316TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
317 EXPECT_TRUE(matches(
318 "template <typename T> struct A {"
319 " template <typename T2> struct F {};"
320 "};"
321 "template <typename T> struct B : A<T>::template F<T> {};"
322 "B<int> b;",
323 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
324}
325
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000326TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000327 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000328 EXPECT_TRUE(notMatches("class X;", ClassX));
329 EXPECT_TRUE(notMatches("class X {};", ClassX));
330}
331
332TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000333 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000334 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
335 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
336}
337
338TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
339 EXPECT_TRUE(notMatches("template<typename T> class X { };"
340 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000341 classTemplateDecl(hasName("X"),
342 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000343}
344
345TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
346 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
347 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000348 classTemplateDecl(hasName("X"),
349 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000350}
351
Daniel Jasper6a124492012-07-12 08:50:38 +0000352TEST(AllOf, AllOverloadsWork) {
353 const char Program[] =
354 "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }";
355 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000356 callExpr(allOf(callee(functionDecl(hasName("f"))),
357 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000358 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000359 callExpr(allOf(callee(functionDecl(hasName("f"))),
360 hasArgument(0, declRefExpr(to(varDecl()))),
361 hasArgument(1, hasType(pointsTo(
362 recordDecl(hasName("T")))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000363}
364
Manuel Klimek4da21662012-07-06 05:48:52 +0000365TEST(DeclarationMatcher, MatchAnyOf) {
366 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000367 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000368 EXPECT_TRUE(
369 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
370 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
371 EXPECT_TRUE(
372 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
373 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
374
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000375 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000376 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000377 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
378 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
379
Manuel Klimek4da21662012-07-06 05:48:52 +0000380 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000381 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
382 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000383 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
384 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
385 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
386 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
387 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
388 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
389}
390
391TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000392 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000393 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
394 EXPECT_TRUE(matches("class X {};", HasClassX));
395
396 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000397 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000398 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
399 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
400 EXPECT_TRUE(
401 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
402}
403
404TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
405 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000406 recordDecl(
407 has(recordDecl(
408 has(recordDecl(hasName("X"))),
409 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000410 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000411 has(recordDecl(
412 has(recordDecl(hasName("A"))),
413 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000414 hasName("C"))),
415 hasName("F"));
416
417 EXPECT_TRUE(matches(
418 "class F {"
419 " class Z {"
420 " class X {};"
421 " class Y {};"
422 " };"
423 " class C {"
424 " class A {};"
425 " class B {};"
426 " };"
427 "};", Recursive));
428
429 EXPECT_TRUE(matches(
430 "class F {"
431 " class Z {"
432 " class A {};"
433 " class X {};"
434 " class Y {};"
435 " };"
436 " class C {"
437 " class X {};"
438 " class A {};"
439 " class B {};"
440 " };"
441 "};", Recursive));
442
443 EXPECT_TRUE(matches(
444 "class O1 {"
445 " class O2 {"
446 " class F {"
447 " class Z {"
448 " class A {};"
449 " class X {};"
450 " class Y {};"
451 " };"
452 " class C {"
453 " class X {};"
454 " class A {};"
455 " class B {};"
456 " };"
457 " };"
458 " };"
459 "};", Recursive));
460}
461
462TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
463 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000464 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000465 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000466 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000467 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000468 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000469 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000470 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000471 hasName("Y"))),
472 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000473 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000474 anyOf(
475 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000476 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000477 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000478 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000479 hasName("B")))))),
480 hasName("F")));
481
482 EXPECT_TRUE(matches("class F {};", Recursive));
483 EXPECT_TRUE(matches("class Z {};", Recursive));
484 EXPECT_TRUE(matches("class C {};", Recursive));
485 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
486 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
487 EXPECT_TRUE(
488 matches("class O1 { class O2 {"
489 " class M { class N { class B {}; }; }; "
490 "}; };", Recursive));
491}
492
493TEST(DeclarationMatcher, MatchNot) {
494 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000495 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000496 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000497 unless(hasName("X")));
498 EXPECT_TRUE(notMatches("", NotClassX));
499 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
500 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
501 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
502 EXPECT_TRUE(
503 notMatches("class Y {}; class Z {}; class X : public Y {};",
504 NotClassX));
505
506 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000507 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000508 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000509 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000510 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000511 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000512 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
513 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
514 ClassXHasNotClassY));
515}
516
517TEST(DeclarationMatcher, HasDescendant) {
518 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000519 recordDecl(
520 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000521 hasName("Z"));
522 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
523 EXPECT_TRUE(
524 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
525 EXPECT_TRUE(
526 matches("class Z { class A { class Y { class X {}; }; }; };",
527 ZDescendantClassX));
528 EXPECT_TRUE(
529 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
530 ZDescendantClassX));
531 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
532
533 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000534 recordDecl(
535 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000536 hasName("X"))),
537 hasName("Z"));
538 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
539 ZDescendantClassXHasClassY));
540 EXPECT_TRUE(
541 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
542 ZDescendantClassXHasClassY));
543 EXPECT_TRUE(notMatches(
544 "class Z {"
545 " class A {"
546 " class B {"
547 " class X {"
548 " class C {"
549 " class Y {};"
550 " };"
551 " };"
552 " }; "
553 " };"
554 "};", ZDescendantClassXHasClassY));
555
556 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000557 recordDecl(
558 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
559 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000560 hasName("Z"));
561 EXPECT_TRUE(
562 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
563 ZDescendantClassXDescendantClassY));
564 EXPECT_TRUE(matches(
565 "class Z {"
566 " class A {"
567 " class X {"
568 " class B {"
569 " class Y {};"
570 " };"
571 " class Y {};"
572 " };"
573 " };"
574 "};", ZDescendantClassXDescendantClassY));
575}
576
Daniel Jaspera267cf62012-10-29 10:14:44 +0000577// Implements a run method that returns whether BoundNodes contains a
578// Decl bound to Id that can be dynamically cast to T.
579// Optionally checks that the check succeeded a specific number of times.
580template <typename T>
581class VerifyIdIsBoundTo : public BoundNodesCallback {
582public:
583 // Create an object that checks that a node of type \c T was bound to \c Id.
584 // Does not check for a certain number of matches.
585 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
586 : Id(Id), ExpectedCount(-1), Count(0) {}
587
588 // Create an object that checks that a node of type \c T was bound to \c Id.
589 // Checks that there were exactly \c ExpectedCount matches.
590 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
591 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
592
593 // Create an object that checks that a node of type \c T was bound to \c Id.
594 // Checks that there was exactly one match with the name \c ExpectedName.
595 // Note that \c T must be a NamedDecl for this to work.
596 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
597 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
598
599 ~VerifyIdIsBoundTo() {
600 if (ExpectedCount != -1)
601 EXPECT_EQ(ExpectedCount, Count);
602 if (!ExpectedName.empty())
603 EXPECT_EQ(ExpectedName, Name);
604 }
605
606 virtual bool run(const BoundNodes *Nodes) {
607 if (Nodes->getNodeAs<T>(Id)) {
608 ++Count;
609 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
610 Name = Named->getNameAsString();
611 } else if (const NestedNameSpecifier *NNS =
612 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
613 llvm::raw_string_ostream OS(Name);
614 NNS->print(OS, PrintingPolicy(LangOptions()));
615 }
616 return true;
617 }
618 return false;
619 }
620
Daniel Jasper452abbc2012-10-29 10:48:25 +0000621 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
622 return run(Nodes);
623 }
624
Daniel Jaspera267cf62012-10-29 10:14:44 +0000625private:
626 const std::string Id;
627 const int ExpectedCount;
628 int Count;
629 const std::string ExpectedName;
630 std::string Name;
631};
632
633TEST(HasDescendant, MatchesDescendantTypes) {
634 EXPECT_TRUE(matches("void f() { int i = 3; }",
635 decl(hasDescendant(loc(builtinType())))));
636 EXPECT_TRUE(matches("void f() { int i = 3; }",
637 stmt(hasDescendant(builtinType()))));
638
639 EXPECT_TRUE(matches("void f() { int i = 3; }",
640 stmt(hasDescendant(loc(builtinType())))));
641 EXPECT_TRUE(matches("void f() { int i = 3; }",
642 stmt(hasDescendant(qualType(builtinType())))));
643
644 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
645 stmt(hasDescendant(isInteger()))));
646
647 EXPECT_TRUE(matchAndVerifyResultTrue(
648 "void f() { int a; float c; int d; int e; }",
649 functionDecl(forEachDescendant(
650 varDecl(hasDescendant(isInteger())).bind("x"))),
651 new VerifyIdIsBoundTo<Decl>("x", 3)));
652}
653
654TEST(HasDescendant, MatchesDescendantsOfTypes) {
655 EXPECT_TRUE(matches("void f() { int*** i; }",
656 qualType(hasDescendant(builtinType()))));
657 EXPECT_TRUE(matches("void f() { int*** i; }",
658 qualType(hasDescendant(
659 pointerType(pointee(builtinType()))))));
660 EXPECT_TRUE(matches("void f() { int*** i; }",
661 typeLoc(hasDescendant(builtinTypeLoc()))));
662
663 EXPECT_TRUE(matchAndVerifyResultTrue(
664 "void f() { int*** i; }",
665 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
666 new VerifyIdIsBoundTo<Type>("x", 2)));
667}
668
669TEST(Has, MatchesChildrenOfTypes) {
670 EXPECT_TRUE(matches("int i;",
671 varDecl(hasName("i"), has(isInteger()))));
672 EXPECT_TRUE(notMatches("int** i;",
673 varDecl(hasName("i"), has(isInteger()))));
674 EXPECT_TRUE(matchAndVerifyResultTrue(
675 "int (*f)(float, int);",
676 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
677 new VerifyIdIsBoundTo<QualType>("x", 2)));
678}
679
680TEST(Has, MatchesChildTypes) {
681 EXPECT_TRUE(matches(
682 "int* i;",
683 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
684 EXPECT_TRUE(notMatches(
685 "int* i;",
686 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
687}
688
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000689TEST(Enum, DoesNotMatchClasses) {
690 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
691}
692
693TEST(Enum, MatchesEnums) {
694 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
695}
696
697TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000698 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000699 EXPECT_TRUE(matches("enum X{ A };", Matcher));
700 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
701 EXPECT_TRUE(notMatches("enum X {};", Matcher));
702}
703
Manuel Klimek4da21662012-07-06 05:48:52 +0000704TEST(StatementMatcher, Has) {
705 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000706 expr(hasType(pointsTo(recordDecl(hasName("X")))),
707 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000708
709 EXPECT_TRUE(matches(
710 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
711 EXPECT_TRUE(notMatches(
712 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
713}
714
715TEST(StatementMatcher, HasDescendant) {
716 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000717 expr(hasType(pointsTo(recordDecl(hasName("X")))),
718 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000719
720 EXPECT_TRUE(matches(
721 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
722 HasDescendantVariableI));
723 EXPECT_TRUE(notMatches(
724 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
725 HasDescendantVariableI));
726}
727
728TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000729 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000730
731 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
732 EXPECT_TRUE(notMatches("class A {};", TypeA));
733
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000734 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000735
736 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
737 TypeDerivedFromA));
738 EXPECT_TRUE(notMatches("class A {};", TypeA));
739
740 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000741 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000742
743 EXPECT_TRUE(
744 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
745}
746
Manuel Klimek4da21662012-07-06 05:48:52 +0000747TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000748 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000749
750 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000751 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000752
753 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000754 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000755
756 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000757 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000758
759 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
760 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000761 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000762
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000763 StatementMatcher MethodX =
764 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000765
766 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
767 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000768 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000769}
770
771TEST(Matcher, BindTheSameNameInAlternatives) {
772 StatementMatcher matcher = anyOf(
773 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000774 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000775 hasRHS(integerLiteral(equals(0)))),
776 binaryOperator(hasOperatorName("+"),
777 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000779
780 EXPECT_TRUE(matchAndVerifyResultTrue(
781 // The first branch of the matcher binds x to 0 but then fails.
782 // The second branch binds x to f() and succeeds.
783 "int f() { return 0 + f(); }",
784 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000785 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000786}
787
Manuel Klimek66341c52012-08-30 19:41:06 +0000788TEST(Matcher, BindsIDForMemoizedResults) {
789 // Using the same matcher in two match expressions will make memoization
790 // kick in.
791 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
792 EXPECT_TRUE(matchAndVerifyResultTrue(
793 "class A { class B { class X {}; }; };",
794 DeclarationMatcher(anyOf(
795 recordDecl(hasName("A"), hasDescendant(ClassX)),
796 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000797 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000798}
799
Daniel Jasper189f2e42012-12-03 15:43:25 +0000800TEST(HasDeclaration, HasDeclarationOfEnumType) {
801 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
802 expr(hasType(pointsTo(
803 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
804}
805
Manuel Klimek4da21662012-07-06 05:48:52 +0000806TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000807 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000808 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000809 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000810 EXPECT_TRUE(
811 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000813 EXPECT_TRUE(
814 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000815 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000816}
817
818TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000819 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000820 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000821 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000822 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000823 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000824 EXPECT_TRUE(
825 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000826 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000827}
828
829TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000830 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000831 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000832 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000833 EXPECT_TRUE(
834 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000835 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000836}
837
838TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000839 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000840 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000841 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000842 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000843 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000844}
845
846TEST(Matcher, Call) {
847 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000848 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000849 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000850
851 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
852 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
853
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000854 StatementMatcher MethodOnY =
855 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000856
857 EXPECT_TRUE(
858 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
859 MethodOnY));
860 EXPECT_TRUE(
861 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
862 MethodOnY));
863 EXPECT_TRUE(
864 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
865 MethodOnY));
866 EXPECT_TRUE(
867 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
868 MethodOnY));
869 EXPECT_TRUE(
870 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
871 MethodOnY));
872
873 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000874 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000875
876 EXPECT_TRUE(
877 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
878 MethodOnYPointer));
879 EXPECT_TRUE(
880 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
881 MethodOnYPointer));
882 EXPECT_TRUE(
883 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
884 MethodOnYPointer));
885 EXPECT_TRUE(
886 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
887 MethodOnYPointer));
888 EXPECT_TRUE(
889 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
890 MethodOnYPointer));
891}
892
Daniel Jasper31f7c082012-10-01 13:40:41 +0000893TEST(Matcher, Lambda) {
894 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
895 lambdaExpr()));
896}
897
898TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000899 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
900 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000901 forRangeStmt()));
902 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
903 forRangeStmt()));
904}
905
906TEST(Matcher, UserDefinedLiteral) {
907 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
908 " return i + 1;"
909 "}"
910 "char c = 'a'_inc;",
911 userDefinedLiteral()));
912}
913
Daniel Jasperb54b7642012-09-20 14:12:57 +0000914TEST(Matcher, FlowControl) {
915 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
916 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
917 continueStmt()));
918 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
919 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
920 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
921}
922
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000923TEST(HasType, MatchesAsString) {
924 EXPECT_TRUE(
925 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000926 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000927 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000928 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000929 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000930 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000931 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000932 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000933}
934
Manuel Klimek4da21662012-07-06 05:48:52 +0000935TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000936 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000937 // Unary operator
938 EXPECT_TRUE(matches("class Y { }; "
939 "bool operator!(Y x) { return false; }; "
940 "Y y; bool c = !y;", OpCall));
941 // No match -- special operators like "new", "delete"
942 // FIXME: operator new takes size_t, for which we need stddef.h, for which
943 // we need to figure out include paths in the test.
944 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
945 // "class Y { }; "
946 // "void *operator new(size_t size) { return 0; } "
947 // "Y *y = new Y;", OpCall));
948 EXPECT_TRUE(notMatches("class Y { }; "
949 "void operator delete(void *p) { } "
950 "void a() {Y *y = new Y; delete y;}", OpCall));
951 // Binary operator
952 EXPECT_TRUE(matches("class Y { }; "
953 "bool operator&&(Y x, Y y) { return true; }; "
954 "Y a; Y b; bool c = a && b;",
955 OpCall));
956 // No match -- normal operator, not an overloaded one.
957 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
958 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
959}
960
961TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
962 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000963 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000964 EXPECT_TRUE(matches("class Y { }; "
965 "bool operator&&(Y x, Y y) { return true; }; "
966 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
967 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000968 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000969 EXPECT_TRUE(notMatches("class Y { }; "
970 "bool operator&&(Y x, Y y) { return true; }; "
971 "Y a; Y b; bool c = a && b;",
972 OpCallLessLess));
973}
974
Daniel Jasper278057f2012-11-15 03:29:05 +0000975TEST(Matcher, NestedOverloadedOperatorCalls) {
976 EXPECT_TRUE(matchAndVerifyResultTrue(
977 "class Y { }; "
978 "Y& operator&&(Y& x, Y& y) { return x; }; "
979 "Y a; Y b; Y c; Y d = a && b && c;",
980 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
981 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
982 EXPECT_TRUE(matches(
983 "class Y { }; "
984 "Y& operator&&(Y& x, Y& y) { return x; }; "
985 "Y a; Y b; Y c; Y d = a && b && c;",
986 operatorCallExpr(hasParent(operatorCallExpr()))));
987 EXPECT_TRUE(matches(
988 "class Y { }; "
989 "Y& operator&&(Y& x, Y& y) { return x; }; "
990 "Y a; Y b; Y c; Y d = a && b && c;",
991 operatorCallExpr(hasDescendant(operatorCallExpr()))));
992}
993
Manuel Klimek4da21662012-07-06 05:48:52 +0000994TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +0000995 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000996 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000997
998 EXPECT_TRUE(
999 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1000 MethodOnY));
1001 EXPECT_TRUE(
1002 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1003 MethodOnY));
1004 EXPECT_TRUE(
1005 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1006 MethodOnY));
1007 EXPECT_TRUE(
1008 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1009 MethodOnY));
1010 EXPECT_TRUE(
1011 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1012 MethodOnY));
1013
1014 EXPECT_TRUE(matches(
1015 "class Y {"
1016 " public: virtual void x();"
1017 "};"
1018 "class X : public Y {"
1019 " public: virtual void x();"
1020 "};"
1021 "void z() { X *x; x->Y::x(); }", MethodOnY));
1022}
1023
1024TEST(Matcher, VariableUsage) {
1025 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001026 declRefExpr(to(
1027 varDecl(hasInitializer(
1028 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001029
1030 EXPECT_TRUE(matches(
1031 "class Y {"
1032 " public:"
1033 " bool x() const;"
1034 "};"
1035 "void z(const Y &y) {"
1036 " bool b = y.x();"
1037 " if (b) {}"
1038 "}", Reference));
1039
1040 EXPECT_TRUE(notMatches(
1041 "class Y {"
1042 " public:"
1043 " bool x() const;"
1044 "};"
1045 "void z(const Y &y) {"
1046 " bool b = y.x();"
1047 "}", Reference));
1048}
1049
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001050TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001051 EXPECT_TRUE(matches(
1052 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001053 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001054}
1055
Manuel Klimek4da21662012-07-06 05:48:52 +00001056TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001057 StatementMatcher CallOnVariableY =
1058 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001059
1060 EXPECT_TRUE(matches(
1061 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1062 EXPECT_TRUE(matches(
1063 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1064 EXPECT_TRUE(matches(
1065 "class Y { public: void x(); };"
1066 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1067 EXPECT_TRUE(matches(
1068 "class Y { public: void x(); };"
1069 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1070 EXPECT_TRUE(notMatches(
1071 "class Y { public: void x(); };"
1072 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1073 CallOnVariableY));
1074}
1075
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001076TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1077 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1078 unaryExprOrTypeTraitExpr()));
1079 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1080 alignOfExpr(anything())));
1081 // FIXME: Uncomment once alignof is enabled.
1082 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1083 // unaryExprOrTypeTraitExpr()));
1084 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1085 // sizeOfExpr()));
1086}
1087
1088TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1089 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1090 hasArgumentOfType(asString("int")))));
1091 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1092 hasArgumentOfType(asString("float")))));
1093 EXPECT_TRUE(matches(
1094 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001095 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001096 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001097 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001098}
1099
Manuel Klimek4da21662012-07-06 05:48:52 +00001100TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001101 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001102}
1103
1104TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001105 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001106}
1107
1108TEST(MemberExpression, MatchesVariable) {
1109 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001110 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001111 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001112 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001113 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001114 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001115}
1116
1117TEST(MemberExpression, MatchesStaticVariable) {
1118 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001119 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001120 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001121 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001122 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001123 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001124}
1125
Daniel Jasper6a124492012-07-12 08:50:38 +00001126TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001127 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1128 EXPECT_TRUE(matches(
1129 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1130 callExpr(hasArgument(0, declRefExpr(
1131 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001132}
1133
1134TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001135 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001136 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001137 callExpr(hasArgument(0, declRefExpr(
1138 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001139}
1140
Manuel Klimek4da21662012-07-06 05:48:52 +00001141TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1142 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001143 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001144 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001145 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001146 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001147 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001148}
1149
1150TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1151 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001152 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001153 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001154 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001155 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001156 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001157}
1158
1159TEST(IsArrow, MatchesMemberCallsViaArrow) {
1160 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001161 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001162 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001163 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001164 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001165 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001166}
1167
1168TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001169 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001170
1171 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1172 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1173}
1174
1175TEST(Callee, MatchesMemberExpressions) {
1176 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001177 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001178 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001179 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001180}
1181
1182TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001183 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001184
1185 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1186 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1187
Manuel Klimeke265c872012-07-10 14:21:30 +00001188#if !defined(_MSC_VER)
1189 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001190 // Dependent contexts, but a non-dependent call.
1191 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1192 CallFunctionF));
1193 EXPECT_TRUE(
1194 matches("void f(); template <int N> struct S { void g() { f(); } };",
1195 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001196#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001197
1198 // Depedent calls don't match.
1199 EXPECT_TRUE(
1200 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1201 CallFunctionF));
1202 EXPECT_TRUE(
1203 notMatches("void f(int);"
1204 "template <typename T> struct S { void g(T t) { f(t); } };",
1205 CallFunctionF));
1206}
1207
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001208TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1209 EXPECT_TRUE(
1210 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001211 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001212}
1213
1214TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1215 EXPECT_TRUE(
1216 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001217 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001218}
1219
1220TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1221 EXPECT_TRUE(
1222 notMatches("void g(); template <typename T> void f(T t) {}"
1223 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001224 functionTemplateDecl(hasName("f"),
1225 hasDescendant(declRefExpr(to(
1226 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001227}
1228
Manuel Klimek4da21662012-07-06 05:48:52 +00001229TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001230 StatementMatcher CallArgumentY = callExpr(
1231 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001232
1233 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1234 EXPECT_TRUE(
1235 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1236 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1237
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001238 StatementMatcher WrongIndex = callExpr(
1239 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001240 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1241}
1242
1243TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001244 StatementMatcher CallArgumentY = callExpr(
1245 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001246 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1247 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1248 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1249}
1250
1251TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001252 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001253
1254 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1255 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1256 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1257}
1258
Daniel Jasper36e29d62012-12-04 11:54:27 +00001259TEST(Matcher, ParameterCount) {
1260 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1261 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1262 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1263 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1264 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1265}
1266
Manuel Klimek4da21662012-07-06 05:48:52 +00001267TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001268 DeclarationMatcher ReferenceClassX = varDecl(
1269 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001270 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1271 ReferenceClassX));
1272 EXPECT_TRUE(
1273 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1274 EXPECT_TRUE(
1275 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1276 EXPECT_TRUE(
1277 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1278}
1279
1280TEST(HasParameter, CallsInnerMatcher) {
1281 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001282 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001283 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001284 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001285}
1286
1287TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1288 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001289 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001290}
1291
1292TEST(HasType, MatchesParameterVariableTypesStrictly) {
1293 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001294 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001295 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001296 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001297 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001298 methodDecl(hasParameter(0,
1299 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001300 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001301 methodDecl(hasParameter(0,
1302 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001303}
1304
1305TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1306 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001307 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001308 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001309 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001310}
1311
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001312TEST(Returns, MatchesReturnTypes) {
1313 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001314 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001315 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001316 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001317 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001318 functionDecl(returns(hasDeclaration(
1319 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001320}
1321
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001322TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001323 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1324 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1325 functionDecl(isExternC())));
1326 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001327}
1328
Manuel Klimek4da21662012-07-06 05:48:52 +00001329TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1330 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001331 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001332}
1333
1334TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1335 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001336 methodDecl(hasAnyParameter(hasType(pointsTo(
1337 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001338}
1339
1340TEST(HasName, MatchesParameterVariableDeclartions) {
1341 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001342 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001343 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001344 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001345}
1346
Daniel Jasper371f9392012-08-01 08:40:24 +00001347TEST(Matcher, MatchesClassTemplateSpecialization) {
1348 EXPECT_TRUE(matches("template<typename T> struct A {};"
1349 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001350 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001351 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001352 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001353 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001354 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001355}
1356
1357TEST(Matcher, MatchesTypeTemplateArgument) {
1358 EXPECT_TRUE(matches(
1359 "template<typename T> struct B {};"
1360 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001361 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001362 asString("int"))))));
1363}
1364
1365TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1366 EXPECT_TRUE(matches(
1367 "struct B { int next; };"
1368 "template<int(B::*next_ptr)> struct A {};"
1369 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001370 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1371 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001372
1373 EXPECT_TRUE(notMatches(
1374 "template <typename T> struct A {};"
1375 "A<int> a;",
1376 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1377 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001378}
1379
1380TEST(Matcher, MatchesSpecificArgument) {
1381 EXPECT_TRUE(matches(
1382 "template<typename T, typename U> class A {};"
1383 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001384 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001385 1, refersToType(asString("int"))))));
1386 EXPECT_TRUE(notMatches(
1387 "template<typename T, typename U> class A {};"
1388 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001389 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001390 1, refersToType(asString("int"))))));
1391}
1392
Manuel Klimek4da21662012-07-06 05:48:52 +00001393TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001394 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001395
1396 EXPECT_TRUE(
1397 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1398 EXPECT_TRUE(
1399 matches("class X { public: X(); }; void x() { X x = X(); }",
1400 Constructor));
1401 EXPECT_TRUE(
1402 matches("class X { public: X(int); }; void x() { X x = 0; }",
1403 Constructor));
1404 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1405}
1406
1407TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001408 StatementMatcher Constructor = constructExpr(
1409 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001410
1411 EXPECT_TRUE(
1412 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1413 Constructor));
1414 EXPECT_TRUE(
1415 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1416 Constructor));
1417 EXPECT_TRUE(
1418 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1419 Constructor));
1420 EXPECT_TRUE(
1421 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1422 Constructor));
1423
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001424 StatementMatcher WrongIndex = constructExpr(
1425 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001426 EXPECT_TRUE(
1427 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1428 WrongIndex));
1429}
1430
1431TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001432 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001433
1434 EXPECT_TRUE(
1435 matches("class X { public: X(int); }; void x() { X x(0); }",
1436 Constructor1Arg));
1437 EXPECT_TRUE(
1438 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1439 Constructor1Arg));
1440 EXPECT_TRUE(
1441 matches("class X { public: X(int); }; void x() { X x = 0; }",
1442 Constructor1Arg));
1443 EXPECT_TRUE(
1444 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1445 Constructor1Arg));
1446}
1447
Manuel Klimek70b9db92012-10-23 10:40:50 +00001448TEST(Matcher,ThisExpr) {
1449 EXPECT_TRUE(
1450 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1451 EXPECT_TRUE(
1452 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1453}
1454
Manuel Klimek4da21662012-07-06 05:48:52 +00001455TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001456 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001457
1458 std::string ClassString = "class string { public: string(); ~string(); }; ";
1459
1460 EXPECT_TRUE(
1461 matches(ClassString +
1462 "string GetStringByValue();"
1463 "void FunctionTakesString(string s);"
1464 "void run() { FunctionTakesString(GetStringByValue()); }",
1465 TempExpression));
1466
1467 EXPECT_TRUE(
1468 notMatches(ClassString +
1469 "string* GetStringPointer(); "
1470 "void FunctionTakesStringPtr(string* s);"
1471 "void run() {"
1472 " string* s = GetStringPointer();"
1473 " FunctionTakesStringPtr(GetStringPointer());"
1474 " FunctionTakesStringPtr(s);"
1475 "}",
1476 TempExpression));
1477
1478 EXPECT_TRUE(
1479 notMatches("class no_dtor {};"
1480 "no_dtor GetObjByValue();"
1481 "void ConsumeObj(no_dtor param);"
1482 "void run() { ConsumeObj(GetObjByValue()); }",
1483 TempExpression));
1484}
1485
Sam Panzere16acd32012-08-24 22:04:44 +00001486TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1487 std::string ClassString =
1488 "class string { public: string(); int length(); }; ";
1489
1490 EXPECT_TRUE(
1491 matches(ClassString +
1492 "string GetStringByValue();"
1493 "void FunctionTakesString(string s);"
1494 "void run() { FunctionTakesString(GetStringByValue()); }",
1495 materializeTemporaryExpr()));
1496
1497 EXPECT_TRUE(
1498 notMatches(ClassString +
1499 "string* GetStringPointer(); "
1500 "void FunctionTakesStringPtr(string* s);"
1501 "void run() {"
1502 " string* s = GetStringPointer();"
1503 " FunctionTakesStringPtr(GetStringPointer());"
1504 " FunctionTakesStringPtr(s);"
1505 "}",
1506 materializeTemporaryExpr()));
1507
1508 EXPECT_TRUE(
1509 notMatches(ClassString +
1510 "string GetStringByValue();"
1511 "void run() { int k = GetStringByValue().length(); }",
1512 materializeTemporaryExpr()));
1513
1514 EXPECT_TRUE(
1515 notMatches(ClassString +
1516 "string GetStringByValue();"
1517 "void run() { GetStringByValue(); }",
1518 materializeTemporaryExpr()));
1519}
1520
Manuel Klimek4da21662012-07-06 05:48:52 +00001521TEST(ConstructorDeclaration, SimpleCase) {
1522 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001523 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001524 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001525 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001526}
1527
1528TEST(ConstructorDeclaration, IsImplicit) {
1529 // This one doesn't match because the constructor is not added by the
1530 // compiler (it is not needed).
1531 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001532 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001533 // The compiler added the implicit default constructor.
1534 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001535 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001536 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001537 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001538}
1539
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001540TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1541 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001542 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001543}
1544
1545TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001546 EXPECT_TRUE(notMatches("class Foo {};",
1547 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001548}
1549
Manuel Klimek4da21662012-07-06 05:48:52 +00001550TEST(HasAnyConstructorInitializer, SimpleCase) {
1551 EXPECT_TRUE(notMatches(
1552 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001553 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001554 EXPECT_TRUE(matches(
1555 "class Foo {"
1556 " Foo() : foo_() { }"
1557 " int foo_;"
1558 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001559 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001560}
1561
1562TEST(HasAnyConstructorInitializer, ForField) {
1563 static const char Code[] =
1564 "class Baz { };"
1565 "class Foo {"
1566 " Foo() : foo_() { }"
1567 " Baz foo_;"
1568 " Baz bar_;"
1569 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001570 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1571 forField(hasType(recordDecl(hasName("Baz"))))))));
1572 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001573 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001574 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1575 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001576}
1577
1578TEST(HasAnyConstructorInitializer, WithInitializer) {
1579 static const char Code[] =
1580 "class Foo {"
1581 " Foo() : foo_(0) { }"
1582 " int foo_;"
1583 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001584 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001585 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001586 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001587 withInitializer(integerLiteral(equals(1)))))));
1588}
1589
1590TEST(HasAnyConstructorInitializer, IsWritten) {
1591 static const char Code[] =
1592 "struct Bar { Bar(){} };"
1593 "class Foo {"
1594 " Foo() : foo_() { }"
1595 " Bar foo_;"
1596 " Bar bar_;"
1597 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001598 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001599 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001600 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001601 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001602 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001603 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1604}
1605
1606TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001607 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001608
1609 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1610 EXPECT_TRUE(
1611 matches("class X { public: X(); }; void x() { new X(); }", New));
1612 EXPECT_TRUE(
1613 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1614 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1615}
1616
1617TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001618 StatementMatcher New = constructExpr(
1619 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001620
1621 EXPECT_TRUE(
1622 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1623 New));
1624 EXPECT_TRUE(
1625 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1626 New));
1627 EXPECT_TRUE(
1628 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1629 New));
1630
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001631 StatementMatcher WrongIndex = constructExpr(
1632 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001633 EXPECT_TRUE(
1634 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1635 WrongIndex));
1636}
1637
1638TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001639 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001640
1641 EXPECT_TRUE(
1642 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1643 EXPECT_TRUE(
1644 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1645 New));
1646}
1647
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001648TEST(Matcher, DeleteExpression) {
1649 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001650 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001651}
1652
Manuel Klimek4da21662012-07-06 05:48:52 +00001653TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001654 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001655
1656 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1657 EXPECT_TRUE(
1658 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1659 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1660}
1661
1662TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001663 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001664 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1665 // wide string
1666 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1667 // with escaped characters
1668 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1669 // no matching -- though the data type is the same, there is no string literal
1670 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1671}
1672
1673TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001674 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001675 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1676 // wide character
1677 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1678 // wide character, Hex encoded, NOT MATCHED!
1679 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1680 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1681}
1682
1683TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001684 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001685 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1686 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1687 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1688 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1689
1690 // Non-matching cases (character literals, float and double)
1691 EXPECT_TRUE(notMatches("int i = L'a';",
1692 HasIntLiteral)); // this is actually a character
1693 // literal cast to int
1694 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1695 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1696 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1697}
1698
Daniel Jasper31f7c082012-10-01 13:40:41 +00001699TEST(Matcher, NullPtrLiteral) {
1700 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1701}
1702
Daniel Jasperb54b7642012-09-20 14:12:57 +00001703TEST(Matcher, AsmStatement) {
1704 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1705}
1706
Manuel Klimek4da21662012-07-06 05:48:52 +00001707TEST(Matcher, Conditions) {
1708 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1709
1710 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1711 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1712 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1713 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1714 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1715}
1716
1717TEST(MatchBinaryOperator, HasOperatorName) {
1718 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1719
1720 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1721 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1722}
1723
1724TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1725 StatementMatcher OperatorTrueFalse =
1726 binaryOperator(hasLHS(boolLiteral(equals(true))),
1727 hasRHS(boolLiteral(equals(false))));
1728
1729 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1730 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1731 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1732}
1733
1734TEST(MatchBinaryOperator, HasEitherOperand) {
1735 StatementMatcher HasOperand =
1736 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1737
1738 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1739 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1740 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1741}
1742
1743TEST(Matcher, BinaryOperatorTypes) {
1744 // Integration test that verifies the AST provides all binary operators in
1745 // a way we expect.
1746 // FIXME: Operator ','
1747 EXPECT_TRUE(
1748 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1749 EXPECT_TRUE(
1750 matches("bool b; bool c = (b = true);",
1751 binaryOperator(hasOperatorName("="))));
1752 EXPECT_TRUE(
1753 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1754 EXPECT_TRUE(
1755 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1756 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1757 EXPECT_TRUE(
1758 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1759 EXPECT_TRUE(
1760 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1761 EXPECT_TRUE(
1762 matches("int i = 1; int j = (i <<= 2);",
1763 binaryOperator(hasOperatorName("<<="))));
1764 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1765 EXPECT_TRUE(
1766 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1767 EXPECT_TRUE(
1768 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1769 EXPECT_TRUE(
1770 matches("int i = 1; int j = (i >>= 2);",
1771 binaryOperator(hasOperatorName(">>="))));
1772 EXPECT_TRUE(
1773 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1774 EXPECT_TRUE(
1775 matches("int i = 42; int j = (i ^= 42);",
1776 binaryOperator(hasOperatorName("^="))));
1777 EXPECT_TRUE(
1778 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1779 EXPECT_TRUE(
1780 matches("int i = 42; int j = (i %= 42);",
1781 binaryOperator(hasOperatorName("%="))));
1782 EXPECT_TRUE(
1783 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1784 EXPECT_TRUE(
1785 matches("bool b = true && false;",
1786 binaryOperator(hasOperatorName("&&"))));
1787 EXPECT_TRUE(
1788 matches("bool b = true; bool c = (b &= false);",
1789 binaryOperator(hasOperatorName("&="))));
1790 EXPECT_TRUE(
1791 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1792 EXPECT_TRUE(
1793 matches("bool b = true || false;",
1794 binaryOperator(hasOperatorName("||"))));
1795 EXPECT_TRUE(
1796 matches("bool b = true; bool c = (b |= false);",
1797 binaryOperator(hasOperatorName("|="))));
1798 EXPECT_TRUE(
1799 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1800 EXPECT_TRUE(
1801 matches("int i = 42; int j = (i *= 23);",
1802 binaryOperator(hasOperatorName("*="))));
1803 EXPECT_TRUE(
1804 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1805 EXPECT_TRUE(
1806 matches("int i = 42; int j = (i /= 23);",
1807 binaryOperator(hasOperatorName("/="))));
1808 EXPECT_TRUE(
1809 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1810 EXPECT_TRUE(
1811 matches("int i = 42; int j = (i += 23);",
1812 binaryOperator(hasOperatorName("+="))));
1813 EXPECT_TRUE(
1814 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1815 EXPECT_TRUE(
1816 matches("int i = 42; int j = (i -= 23);",
1817 binaryOperator(hasOperatorName("-="))));
1818 EXPECT_TRUE(
1819 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1820 binaryOperator(hasOperatorName("->*"))));
1821 EXPECT_TRUE(
1822 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1823 binaryOperator(hasOperatorName(".*"))));
1824
1825 // Member expressions as operators are not supported in matches.
1826 EXPECT_TRUE(
1827 notMatches("struct A { void x(A *a) { a->x(this); } };",
1828 binaryOperator(hasOperatorName("->"))));
1829
1830 // Initializer assignments are not represented as operator equals.
1831 EXPECT_TRUE(
1832 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1833
1834 // Array indexing is not represented as operator.
1835 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1836
1837 // Overloaded operators do not match at all.
1838 EXPECT_TRUE(notMatches(
1839 "struct A { bool operator&&(const A &a) const { return false; } };"
1840 "void x() { A a, b; a && b; }",
1841 binaryOperator()));
1842}
1843
1844TEST(MatchUnaryOperator, HasOperatorName) {
1845 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1846
1847 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1848 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1849}
1850
1851TEST(MatchUnaryOperator, HasUnaryOperand) {
1852 StatementMatcher OperatorOnFalse =
1853 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1854
1855 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1856 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1857}
1858
1859TEST(Matcher, UnaryOperatorTypes) {
1860 // Integration test that verifies the AST provides all unary operators in
1861 // a way we expect.
1862 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1863 EXPECT_TRUE(
1864 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1865 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1866 EXPECT_TRUE(
1867 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1868 EXPECT_TRUE(
1869 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1870 EXPECT_TRUE(
1871 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1872 EXPECT_TRUE(
1873 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1874 EXPECT_TRUE(
1875 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1876 EXPECT_TRUE(
1877 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1878 EXPECT_TRUE(
1879 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1880
1881 // We don't match conversion operators.
1882 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1883
1884 // Function calls are not represented as operator.
1885 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1886
1887 // Overloaded operators do not match at all.
1888 // FIXME: We probably want to add that.
1889 EXPECT_TRUE(notMatches(
1890 "struct A { bool operator!() const { return false; } };"
1891 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1892}
1893
1894TEST(Matcher, ConditionalOperator) {
1895 StatementMatcher Conditional = conditionalOperator(
1896 hasCondition(boolLiteral(equals(true))),
1897 hasTrueExpression(boolLiteral(equals(false))));
1898
1899 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1900 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1901 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1902
1903 StatementMatcher ConditionalFalse = conditionalOperator(
1904 hasFalseExpression(boolLiteral(equals(false))));
1905
1906 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1907 EXPECT_TRUE(
1908 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1909}
1910
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001911TEST(ArraySubscriptMatchers, ArraySubscripts) {
1912 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1913 arraySubscriptExpr()));
1914 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1915 arraySubscriptExpr()));
1916}
1917
1918TEST(ArraySubscriptMatchers, ArrayIndex) {
1919 EXPECT_TRUE(matches(
1920 "int i[2]; void f() { i[1] = 1; }",
1921 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1922 EXPECT_TRUE(matches(
1923 "int i[2]; void f() { 1[i] = 1; }",
1924 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1925 EXPECT_TRUE(notMatches(
1926 "int i[2]; void f() { i[1] = 1; }",
1927 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1928}
1929
1930TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1931 EXPECT_TRUE(matches(
1932 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001933 arraySubscriptExpr(hasBase(implicitCastExpr(
1934 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001935}
1936
Manuel Klimek4da21662012-07-06 05:48:52 +00001937TEST(Matcher, HasNameSupportsNamespaces) {
1938 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001939 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001940 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001941 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001942 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001943 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001944 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001945 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001946 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001947 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001948 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001949 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001950 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001951 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001952 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001953 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001954 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001955 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001956 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001957 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001958 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001959 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001960 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001961 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001962}
1963
1964TEST(Matcher, HasNameSupportsOuterClasses) {
1965 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001966 matches("class A { class B { class C; }; };",
1967 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001968 EXPECT_TRUE(
1969 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001970 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001971 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001972 matches("class A { class B { class C; }; };",
1973 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001974 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001975 matches("class A { class B { class C; }; };",
1976 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001977 EXPECT_TRUE(
1978 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001979 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001980 EXPECT_TRUE(
1981 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001982 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001983 EXPECT_TRUE(
1984 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001985 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001986 EXPECT_TRUE(
1987 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001988 recordDecl(hasName("::C"))));
1989 EXPECT_TRUE(
1990 notMatches("class A { class B { class C; }; };",
1991 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001992 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001993 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001994 EXPECT_TRUE(
1995 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001996 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001997}
1998
1999TEST(Matcher, IsDefinition) {
2000 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002001 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002002 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2003 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2004
2005 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002006 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002007 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2008 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2009
2010 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002011 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002012 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2013 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2014}
2015
2016TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002017 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002018 ofClass(hasName("X")))));
2019
2020 EXPECT_TRUE(
2021 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2022 EXPECT_TRUE(
2023 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2024 Constructor));
2025 EXPECT_TRUE(
2026 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2027 Constructor));
2028}
2029
2030TEST(Matcher, VisitsTemplateInstantiations) {
2031 EXPECT_TRUE(matches(
2032 "class A { public: void x(); };"
2033 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002034 "void f() { B<A> b; b.y(); }",
2035 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002036
2037 EXPECT_TRUE(matches(
2038 "class A { public: void x(); };"
2039 "class C {"
2040 " public:"
2041 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2042 "};"
2043 "void f() {"
2044 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002045 "}",
2046 recordDecl(hasName("C"),
2047 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002048}
2049
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002050TEST(Matcher, HandlesNullQualTypes) {
2051 // FIXME: Add a Type matcher so we can replace uses of this
2052 // variable with Type(True())
2053 const TypeMatcher AnyType = anything();
2054
2055 // We don't really care whether this matcher succeeds; we're testing that
2056 // it completes without crashing.
2057 EXPECT_TRUE(matches(
2058 "struct A { };"
2059 "template <typename T>"
2060 "void f(T t) {"
2061 " T local_t(t /* this becomes a null QualType in the AST */);"
2062 "}"
2063 "void g() {"
2064 " f(0);"
2065 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002066 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002067 anyOf(
2068 TypeMatcher(hasDeclaration(anything())),
2069 pointsTo(AnyType),
2070 references(AnyType)
2071 // Other QualType matchers should go here.
2072 ))))));
2073}
2074
Manuel Klimek4da21662012-07-06 05:48:52 +00002075// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002076AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002077 // Make sure all special variables are used: node, match_finder,
2078 // bound_nodes_builder, and the parameter named 'AMatcher'.
2079 return AMatcher.matches(Node, Finder, Builder);
2080}
2081
2082TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002083 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002084
2085 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002086 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002087
2088 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002089 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002090
2091 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002092 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002093}
2094
2095AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002096 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2097 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2098 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002099 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002100 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002101 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002102 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2103 ASTMatchFinder::BK_First);
2104}
2105
2106TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002107 DeclarationMatcher HasClassB =
2108 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002109
2110 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002111 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002112
2113 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002114 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002115
2116 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002117 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118
2119 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002120 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002121
2122 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2123}
2124
2125TEST(For, FindsForLoops) {
2126 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2127 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002128 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2129 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002130 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002131}
2132
Daniel Jasper6a124492012-07-12 08:50:38 +00002133TEST(For, ForLoopInternals) {
2134 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2135 forStmt(hasCondition(anything()))));
2136 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2137 forStmt(hasLoopInit(anything()))));
2138}
2139
2140TEST(For, NegativeForLoopInternals) {
2141 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002142 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002143 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2144 forStmt(hasLoopInit(anything()))));
2145}
2146
Manuel Klimek4da21662012-07-06 05:48:52 +00002147TEST(For, ReportsNoFalsePositives) {
2148 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2149 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2150}
2151
2152TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002153 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2154 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2155 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002156}
2157
2158TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2159 // It's not a compound statement just because there's "{}" in the source
2160 // text. This is an AST search, not grep.
2161 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002162 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002163 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002164 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002165}
2166
Daniel Jasper6a124492012-07-12 08:50:38 +00002167TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002168 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002169 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002170 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002172 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002173 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002174 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002175 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002176}
2177
2178TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2179 // The simplest case: every compound statement is in a function
2180 // definition, and the function body itself must be a compound
2181 // statement.
2182 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002183 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002184}
2185
2186TEST(HasAnySubstatement, IsNotRecursive) {
2187 // It's really "has any immediate substatement".
2188 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002189 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002190}
2191
2192TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2193 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002194 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195}
2196
2197TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2198 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002199 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002200}
2201
2202TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2203 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002205 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002206 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002207}
2208
2209TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2210 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002211 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002212 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002213 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002214 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002215 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002216}
2217
2218TEST(StatementCountIs, WorksWithMultipleStatements) {
2219 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002220 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002221}
2222
2223TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2224 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002225 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002226 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002227 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002228 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002229 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002230 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002231 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002232}
2233
2234TEST(Member, WorksInSimplestCase) {
2235 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002236 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002237}
2238
2239TEST(Member, DoesNotMatchTheBaseExpression) {
2240 // Don't pick out the wrong part of the member expression, this should
2241 // be checking the member (name) only.
2242 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002243 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002244}
2245
2246TEST(Member, MatchesInMemberFunctionCall) {
2247 EXPECT_TRUE(matches("void f() {"
2248 " struct { void first() {}; } s;"
2249 " s.first();"
2250 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002251 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002252}
2253
Daniel Jasperc711af22012-10-23 15:46:39 +00002254TEST(Member, MatchesMember) {
2255 EXPECT_TRUE(matches(
2256 "struct A { int i; }; void f() { A a; a.i = 2; }",
2257 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2258 EXPECT_TRUE(notMatches(
2259 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2260 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2261}
2262
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002263TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002264 // Fails in C++11 mode
2265 EXPECT_TRUE(matchesConditionally(
2266 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2267 "class X { void *operator new(std::size_t); };",
2268 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002269
2270 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002271 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002272
Daniel Jasper31f7c082012-10-01 13:40:41 +00002273 // Fails in C++11 mode
2274 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002275 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2276 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002277 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002278}
2279
Manuel Klimek4da21662012-07-06 05:48:52 +00002280TEST(HasObjectExpression, DoesNotMatchMember) {
2281 EXPECT_TRUE(notMatches(
2282 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002283 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002284}
2285
2286TEST(HasObjectExpression, MatchesBaseOfVariable) {
2287 EXPECT_TRUE(matches(
2288 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002289 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002290 EXPECT_TRUE(matches(
2291 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002292 memberExpr(hasObjectExpression(
2293 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002294}
2295
2296TEST(HasObjectExpression,
2297 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2298 EXPECT_TRUE(matches(
2299 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002300 memberExpr(hasObjectExpression(
2301 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002302 EXPECT_TRUE(matches(
2303 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002304 memberExpr(hasObjectExpression(
2305 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002306}
2307
2308TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002309 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2310 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2311 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2312 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002313}
2314
2315TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002316 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002317}
2318
2319TEST(IsConstQualified, MatchesConstInt) {
2320 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002321 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002322}
2323
2324TEST(IsConstQualified, MatchesConstPointer) {
2325 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002326 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002327}
2328
2329TEST(IsConstQualified, MatchesThroughTypedef) {
2330 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002331 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002332 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002333 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002334}
2335
2336TEST(IsConstQualified, DoesNotMatchInappropriately) {
2337 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002338 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002339 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002340 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002341}
2342
Sam Panzer089e5b32012-08-16 16:58:10 +00002343TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002344 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2345 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2346 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2347 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002348}
2349TEST(CastExpression, MatchesImplicitCasts) {
2350 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002351 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002352 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002353 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002354}
2355
2356TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002357 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2358 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2359 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2360 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002361}
2362
Manuel Klimek4da21662012-07-06 05:48:52 +00002363TEST(ReinterpretCast, MatchesSimpleCase) {
2364 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002365 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002366}
2367
2368TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002369 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002370 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002371 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002372 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002373 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002374 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2375 "B b;"
2376 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002377 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002378}
2379
2380TEST(FunctionalCast, MatchesSimpleCase) {
2381 std::string foo_class = "class Foo { public: Foo(char*); };";
2382 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002383 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002384}
2385
2386TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2387 std::string FooClass = "class Foo { public: Foo(char*); };";
2388 EXPECT_TRUE(
2389 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002390 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002391 EXPECT_TRUE(
2392 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002393 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002394}
2395
2396TEST(DynamicCast, MatchesSimpleCase) {
2397 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2398 "B b;"
2399 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002400 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002401}
2402
2403TEST(StaticCast, MatchesSimpleCase) {
2404 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002405 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002406}
2407
2408TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002409 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002410 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002411 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002412 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002413 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002414 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2415 "B b;"
2416 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002417 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002418}
2419
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002420TEST(CStyleCast, MatchesSimpleCase) {
2421 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2422}
2423
2424TEST(CStyleCast, DoesNotMatchOtherCasts) {
2425 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2426 "char q, *r = const_cast<char*>(&q);"
2427 "void* s = reinterpret_cast<char*>(&s);"
2428 "struct B { virtual ~B() {} }; struct D : B {};"
2429 "B b;"
2430 "D* t = dynamic_cast<D*>(&b);",
2431 cStyleCastExpr()));
2432}
2433
Manuel Klimek4da21662012-07-06 05:48:52 +00002434TEST(HasDestinationType, MatchesSimpleCase) {
2435 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002436 staticCastExpr(hasDestinationType(
2437 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002438}
2439
Sam Panzer089e5b32012-08-16 16:58:10 +00002440TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2441 // This test creates an implicit const cast.
2442 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002443 implicitCastExpr(
2444 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002445 // This test creates an implicit array-to-pointer cast.
2446 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002447 implicitCastExpr(hasImplicitDestinationType(
2448 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002449}
2450
2451TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2452 // This test creates an implicit cast from int to char.
2453 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002454 implicitCastExpr(hasImplicitDestinationType(
2455 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002456 // This test creates an implicit array-to-pointer cast.
2457 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002458 implicitCastExpr(hasImplicitDestinationType(
2459 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002460}
2461
2462TEST(ImplicitCast, MatchesSimpleCase) {
2463 // This test creates an implicit const cast.
2464 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002465 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002466 // This test creates an implicit cast from int to char.
2467 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002468 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002469 // This test creates an implicit array-to-pointer cast.
2470 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002471 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002472}
2473
2474TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002475 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002476 // are present, and that it ignores explicit and paren casts.
2477
2478 // These two test cases have no casts.
2479 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002481 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002482 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002483
2484 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002485 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002486 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002487 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002488
2489 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002490 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002491}
2492
2493TEST(IgnoringImpCasts, MatchesImpCasts) {
2494 // This test checks that ignoringImpCasts matches when implicit casts are
2495 // present and its inner matcher alone does not match.
2496 // Note that this test creates an implicit const cast.
2497 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002498 varDecl(hasInitializer(ignoringImpCasts(
2499 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002500 // This test creates an implict cast from int to char.
2501 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002502 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002503 integerLiteral(equals(0)))))));
2504}
2505
2506TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2507 // These tests verify that ignoringImpCasts does not match if the inner
2508 // matcher does not match.
2509 // Note that the first test creates an implicit const cast.
2510 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002511 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002512 unless(anything()))))));
2513 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002514 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002515 unless(anything()))))));
2516
2517 // These tests verify that ignoringImplictCasts does not look through explicit
2518 // casts or parentheses.
2519 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002520 varDecl(hasInitializer(ignoringImpCasts(
2521 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002522 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002523 varDecl(hasInitializer(ignoringImpCasts(
2524 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002525 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002526 varDecl(hasInitializer(ignoringImpCasts(
2527 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002528 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002529 varDecl(hasInitializer(ignoringImpCasts(
2530 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002531}
2532
2533TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2534 // This test verifies that expressions that do not have implicit casts
2535 // still match the inner matcher.
2536 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002537 varDecl(hasInitializer(ignoringImpCasts(
2538 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002539}
2540
2541TEST(IgnoringParenCasts, MatchesParenCasts) {
2542 // This test checks that ignoringParenCasts matches when parentheses and/or
2543 // casts are present and its inner matcher alone does not match.
2544 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002545 varDecl(hasInitializer(ignoringParenCasts(
2546 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002547 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002548 varDecl(hasInitializer(ignoringParenCasts(
2549 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002550
2551 // This test creates an implict cast from int to char in addition to the
2552 // parentheses.
2553 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002554 varDecl(hasInitializer(ignoringParenCasts(
2555 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002556
2557 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002558 varDecl(hasInitializer(ignoringParenCasts(
2559 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002560 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002561 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002562 integerLiteral(equals(0)))))));
2563}
2564
2565TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2566 // This test verifies that expressions that do not have any casts still match.
2567 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002568 varDecl(hasInitializer(ignoringParenCasts(
2569 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002570}
2571
2572TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2573 // These tests verify that ignoringImpCasts does not match if the inner
2574 // matcher does not match.
2575 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002576 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002577 unless(anything()))))));
2578
2579 // This test creates an implicit cast from int to char in addition to the
2580 // parentheses.
2581 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002582 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002583 unless(anything()))))));
2584
2585 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002586 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002587 unless(anything()))))));
2588}
2589
2590TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2591 // This test checks that ignoringParenAndImpCasts matches when
2592 // parentheses and/or implicit casts are present and its inner matcher alone
2593 // does not match.
2594 // Note that this test creates an implicit const cast.
2595 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002596 varDecl(hasInitializer(ignoringParenImpCasts(
2597 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002598 // This test creates an implicit cast from int to char.
2599 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002600 varDecl(hasInitializer(ignoringParenImpCasts(
2601 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002602}
2603
2604TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2605 // This test verifies that expressions that do not have parentheses or
2606 // implicit casts still match.
2607 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002608 varDecl(hasInitializer(ignoringParenImpCasts(
2609 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002610 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002611 varDecl(hasInitializer(ignoringParenImpCasts(
2612 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002613}
2614
2615TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2616 // These tests verify that ignoringParenImpCasts does not match if
2617 // the inner matcher does not match.
2618 // This test creates an implicit cast.
2619 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002620 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002621 unless(anything()))))));
2622 // These tests verify that ignoringParenAndImplictCasts does not look
2623 // through explicit casts.
2624 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002625 varDecl(hasInitializer(ignoringParenImpCasts(
2626 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002627 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002628 varDecl(hasInitializer(ignoringParenImpCasts(
2629 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002630 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002631 varDecl(hasInitializer(ignoringParenImpCasts(
2632 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002633}
2634
Manuel Klimek715c9562012-07-25 10:02:02 +00002635TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002636 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2637 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002638 implicitCastExpr(
2639 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002640}
2641
Manuel Klimek715c9562012-07-25 10:02:02 +00002642TEST(HasSourceExpression, MatchesExplicitCasts) {
2643 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002644 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002645 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002646 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002647}
2648
Manuel Klimek4da21662012-07-06 05:48:52 +00002649TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002650 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002651}
2652
2653TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002654 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002655}
2656
2657TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002658 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002659}
2660
2661TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002662 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002663}
2664
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002665TEST(InitListExpression, MatchesInitListExpression) {
2666 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2667 initListExpr(hasType(asString("int [2]")))));
2668 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002669 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002670}
2671
2672TEST(UsingDeclaration, MatchesUsingDeclarations) {
2673 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2674 usingDecl()));
2675}
2676
2677TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2678 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2679 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2680}
2681
2682TEST(UsingDeclaration, MatchesSpecificTarget) {
2683 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2684 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002685 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002686 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2687 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002688 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002689}
2690
2691TEST(UsingDeclaration, ThroughUsingDeclaration) {
2692 EXPECT_TRUE(matches(
2693 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002694 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002695 EXPECT_TRUE(notMatches(
2696 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002697 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002698}
2699
Sam Panzer425f41b2012-08-16 17:20:59 +00002700TEST(SingleDecl, IsSingleDecl) {
2701 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002702 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002703 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2704 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2705 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2706 SingleDeclStmt));
2707}
2708
2709TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002710 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002711
2712 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002713 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002714 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002715 declStmt(containsDeclaration(0, MatchesInit),
2716 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002717 unsigned WrongIndex = 42;
2718 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002719 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002720 MatchesInit))));
2721}
2722
2723TEST(DeclCount, DeclCountIsCorrect) {
2724 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002725 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002726 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002727 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002728 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002729 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002730}
2731
Manuel Klimek4da21662012-07-06 05:48:52 +00002732TEST(While, MatchesWhileLoops) {
2733 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2734 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2735 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2736}
2737
2738TEST(Do, MatchesDoLoops) {
2739 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2740 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2741}
2742
2743TEST(Do, DoesNotMatchWhileLoops) {
2744 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2745}
2746
2747TEST(SwitchCase, MatchesCase) {
2748 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2749 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2750 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2751 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2752}
2753
Daniel Jasperb54b7642012-09-20 14:12:57 +00002754TEST(SwitchCase, MatchesSwitch) {
2755 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2756 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2757 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2758 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2759}
2760
2761TEST(ExceptionHandling, SimpleCases) {
2762 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2763 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2764 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2765 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2766 throwExpr()));
2767 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2768 throwExpr()));
2769}
2770
Manuel Klimek4da21662012-07-06 05:48:52 +00002771TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2772 EXPECT_TRUE(notMatches(
2773 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002774 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002775 EXPECT_TRUE(notMatches(
2776 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002777 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002778}
2779
2780TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2781 EXPECT_TRUE(matches(
2782 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002783 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002784}
2785
2786TEST(ForEach, BindsOneNode) {
2787 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002788 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002789 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002790}
2791
2792TEST(ForEach, BindsMultipleNodes) {
2793 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002794 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002795 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002796}
2797
2798TEST(ForEach, BindsRecursiveCombinations) {
2799 EXPECT_TRUE(matchAndVerifyResultTrue(
2800 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002801 recordDecl(hasName("C"),
2802 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002803 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002804}
2805
2806TEST(ForEachDescendant, BindsOneNode) {
2807 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002808 recordDecl(hasName("C"),
2809 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002810 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002811}
2812
Daniel Jasper5f684e92012-11-16 18:39:22 +00002813TEST(ForEachDescendant, NestedForEachDescendant) {
2814 DeclarationMatcher m = recordDecl(
2815 isDefinition(), decl().bind("x"), hasName("C"));
2816 EXPECT_TRUE(matchAndVerifyResultTrue(
2817 "class A { class B { class C {}; }; };",
2818 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2819 new VerifyIdIsBoundTo<Decl>("x", "C")));
2820
2821 // FIXME: This is not really a useful matcher, but the result is still
2822 // surprising (currently binds "A").
2823 //EXPECT_TRUE(matchAndVerifyResultTrue(
2824 // "class A { class B { class C {}; }; };",
2825 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2826 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2827}
2828
Manuel Klimek4da21662012-07-06 05:48:52 +00002829TEST(ForEachDescendant, BindsMultipleNodes) {
2830 EXPECT_TRUE(matchAndVerifyResultTrue(
2831 "class C { class D { int x; int y; }; "
2832 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002833 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002834 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002835}
2836
2837TEST(ForEachDescendant, BindsRecursiveCombinations) {
2838 EXPECT_TRUE(matchAndVerifyResultTrue(
2839 "class C { class D { "
2840 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002841 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2842 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002843 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002844}
2845
Daniel Jasper11c98772012-11-11 22:14:55 +00002846TEST(ForEachDescendant, BindsCorrectNodes) {
2847 EXPECT_TRUE(matchAndVerifyResultTrue(
2848 "class C { void f(); int i; };",
2849 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2850 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2851 EXPECT_TRUE(matchAndVerifyResultTrue(
2852 "class C { void f() {} int i; };",
2853 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2854 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2855}
2856
Manuel Klimek73876732013-02-04 09:42:38 +00002857TEST(EachOf, TriggersForEachMatch) {
2858 EXPECT_TRUE(matchAndVerifyResultTrue(
2859 "class A { int a; int b; };",
2860 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2861 has(fieldDecl(hasName("b")).bind("v")))),
2862 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2863}
2864
2865TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
2866 EXPECT_TRUE(matchAndVerifyResultTrue(
2867 "class A { int a; int c; };",
2868 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2869 has(fieldDecl(hasName("b")).bind("v")))),
2870 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
2871 EXPECT_TRUE(matchAndVerifyResultTrue(
2872 "class A { int c; int b; };",
2873 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2874 has(fieldDecl(hasName("b")).bind("v")))),
2875 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
2876 EXPECT_TRUE(notMatches(
2877 "class A { int c; int d; };",
2878 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2879 has(fieldDecl(hasName("b")).bind("v"))))));
2880}
Manuel Klimek4da21662012-07-06 05:48:52 +00002881
2882TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2883 // Make sure that we can both match the class by name (::X) and by the type
2884 // the template was instantiated with (via a field).
2885
2886 EXPECT_TRUE(matches(
2887 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002888 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002889
2890 EXPECT_TRUE(matches(
2891 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002892 recordDecl(isTemplateInstantiation(), hasDescendant(
2893 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002894}
2895
2896TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2897 EXPECT_TRUE(matches(
2898 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002899 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002900 isTemplateInstantiation())));
2901}
2902
2903TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2904 EXPECT_TRUE(matches(
2905 "template <typename T> class X { T t; }; class A {};"
2906 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002907 recordDecl(isTemplateInstantiation(), hasDescendant(
2908 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002909}
2910
2911TEST(IsTemplateInstantiation,
2912 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2913 EXPECT_TRUE(matches(
2914 "template <typename T> class X {};"
2915 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002916 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002917}
2918
2919TEST(IsTemplateInstantiation,
2920 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2921 EXPECT_TRUE(matches(
2922 "class A {};"
2923 "class X {"
2924 " template <typename U> class Y { U u; };"
2925 " Y<A> y;"
2926 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002927 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002928}
2929
2930TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2931 // FIXME: Figure out whether this makes sense. It doesn't affect the
2932 // normal use case as long as the uppermost instantiation always is marked
2933 // as template instantiation, but it might be confusing as a predicate.
2934 EXPECT_TRUE(matches(
2935 "class A {};"
2936 "template <typename T> class X {"
2937 " template <typename U> class Y { U u; };"
2938 " Y<T> y;"
2939 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002940 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002941}
2942
2943TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2944 EXPECT_TRUE(notMatches(
2945 "template <typename T> class X {}; class A {};"
2946 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002947 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002948}
2949
2950TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2951 EXPECT_TRUE(notMatches(
2952 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002953 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002954}
2955
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002956TEST(IsExplicitTemplateSpecialization,
2957 DoesNotMatchPrimaryTemplate) {
2958 EXPECT_TRUE(notMatches(
2959 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002960 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002961 EXPECT_TRUE(notMatches(
2962 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002963 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002964}
2965
2966TEST(IsExplicitTemplateSpecialization,
2967 DoesNotMatchExplicitTemplateInstantiations) {
2968 EXPECT_TRUE(notMatches(
2969 "template <typename T> class X {};"
2970 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002971 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002972 EXPECT_TRUE(notMatches(
2973 "template <typename T> void f(T t) {}"
2974 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002975 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002976}
2977
2978TEST(IsExplicitTemplateSpecialization,
2979 DoesNotMatchImplicitTemplateInstantiations) {
2980 EXPECT_TRUE(notMatches(
2981 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002982 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002983 EXPECT_TRUE(notMatches(
2984 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002985 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002986}
2987
2988TEST(IsExplicitTemplateSpecialization,
2989 MatchesExplicitTemplateSpecializations) {
2990 EXPECT_TRUE(matches(
2991 "template <typename T> class X {};"
2992 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002993 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002994 EXPECT_TRUE(matches(
2995 "template <typename T> void f(T t) {}"
2996 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002997 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002998}
2999
Manuel Klimek579b1202012-09-07 09:26:10 +00003000TEST(HasAncenstor, MatchesDeclarationAncestors) {
3001 EXPECT_TRUE(matches(
3002 "class A { class B { class C {}; }; };",
3003 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3004}
3005
3006TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3007 EXPECT_TRUE(notMatches(
3008 "class A { class B { class C {}; }; };",
3009 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3010}
3011
3012TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3013 EXPECT_TRUE(matches(
3014 "class A { class B { void f() { C c; } class C {}; }; };",
3015 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3016 hasAncestor(recordDecl(hasName("A"))))))));
3017}
3018
3019TEST(HasAncenstor, MatchesStatementAncestors) {
3020 EXPECT_TRUE(matches(
3021 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003022 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003023}
3024
3025TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3026 EXPECT_TRUE(matches(
3027 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003028 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003029}
3030
3031TEST(HasAncestor, BindsRecursiveCombinations) {
3032 EXPECT_TRUE(matchAndVerifyResultTrue(
3033 "class C { class D { class E { class F { int y; }; }; }; };",
3034 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003035 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003036}
3037
3038TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3039 EXPECT_TRUE(matchAndVerifyResultTrue(
3040 "class C { class D { class E { class F { int y; }; }; }; };",
3041 fieldDecl(hasAncestor(
3042 decl(
3043 hasDescendant(recordDecl(isDefinition(),
3044 hasAncestor(recordDecl())))
3045 ).bind("d")
3046 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003047 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003048}
3049
3050TEST(HasAncestor, MatchesInTemplateInstantiations) {
3051 EXPECT_TRUE(matches(
3052 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3053 "A<int>::B::C a;",
3054 fieldDecl(hasType(asString("int")),
3055 hasAncestor(recordDecl(hasName("A"))))));
3056}
3057
3058TEST(HasAncestor, MatchesInImplicitCode) {
3059 EXPECT_TRUE(matches(
3060 "struct X {}; struct A { A() {} X x; };",
3061 constructorDecl(
3062 hasAnyConstructorInitializer(withInitializer(expr(
3063 hasAncestor(recordDecl(hasName("A")))))))));
3064}
3065
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003066TEST(HasParent, MatchesOnlyParent) {
3067 EXPECT_TRUE(matches(
3068 "void f() { if (true) { int x = 42; } }",
3069 compoundStmt(hasParent(ifStmt()))));
3070 EXPECT_TRUE(notMatches(
3071 "void f() { for (;;) { int x = 42; } }",
3072 compoundStmt(hasParent(ifStmt()))));
3073 EXPECT_TRUE(notMatches(
3074 "void f() { if (true) for (;;) { int x = 42; } }",
3075 compoundStmt(hasParent(ifStmt()))));
3076}
3077
Manuel Klimek30ace372012-12-06 14:42:48 +00003078TEST(HasAncestor, MatchesAllAncestors) {
3079 EXPECT_TRUE(matches(
3080 "template <typename T> struct C { static void f() { 42; } };"
3081 "void t() { C<int>::f(); }",
3082 integerLiteral(
3083 equals(42),
3084 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3085 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3086}
3087
3088TEST(HasParent, MatchesAllParents) {
3089 EXPECT_TRUE(matches(
3090 "template <typename T> struct C { static void f() { 42; } };"
3091 "void t() { C<int>::f(); }",
3092 integerLiteral(
3093 equals(42),
3094 hasParent(compoundStmt(hasParent(functionDecl(
3095 hasParent(recordDecl(isTemplateInstantiation())))))))));
3096 EXPECT_TRUE(matches(
3097 "template <typename T> struct C { static void f() { 42; } };"
3098 "void t() { C<int>::f(); }",
3099 integerLiteral(
3100 equals(42),
3101 hasParent(compoundStmt(hasParent(functionDecl(
3102 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3103 EXPECT_TRUE(matches(
3104 "template <typename T> struct C { static void f() { 42; } };"
3105 "void t() { C<int>::f(); }",
3106 integerLiteral(equals(42),
3107 hasParent(compoundStmt(allOf(
3108 hasParent(functionDecl(
3109 hasParent(recordDecl(isTemplateInstantiation())))),
3110 hasParent(functionDecl(hasParent(recordDecl(
3111 unless(isTemplateInstantiation())))))))))));
3112}
3113
Daniel Jasperce620072012-10-17 08:52:59 +00003114TEST(TypeMatching, MatchesTypes) {
3115 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3116}
3117
3118TEST(TypeMatching, MatchesArrayTypes) {
3119 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3120 EXPECT_TRUE(matches("int a[42];", arrayType()));
3121 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3122
3123 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3124 arrayType(hasElementType(builtinType()))));
3125
3126 EXPECT_TRUE(matches(
3127 "int const a[] = { 2, 3 };",
3128 qualType(arrayType(hasElementType(builtinType())))));
3129 EXPECT_TRUE(matches(
3130 "int const a[] = { 2, 3 };",
3131 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3132 EXPECT_TRUE(matches(
3133 "typedef const int T; T x[] = { 1, 2 };",
3134 qualType(isConstQualified(), arrayType())));
3135
3136 EXPECT_TRUE(notMatches(
3137 "int a[] = { 2, 3 };",
3138 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3139 EXPECT_TRUE(notMatches(
3140 "int a[] = { 2, 3 };",
3141 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3142 EXPECT_TRUE(notMatches(
3143 "int const a[] = { 2, 3 };",
3144 qualType(arrayType(hasElementType(builtinType())),
3145 unless(isConstQualified()))));
3146
3147 EXPECT_TRUE(matches("int a[2];",
3148 constantArrayType(hasElementType(builtinType()))));
3149 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3150}
3151
3152TEST(TypeMatching, MatchesComplexTypes) {
3153 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3154 EXPECT_TRUE(matches(
3155 "_Complex float f;",
3156 complexType(hasElementType(builtinType()))));
3157 EXPECT_TRUE(notMatches(
3158 "_Complex float f;",
3159 complexType(hasElementType(isInteger()))));
3160}
3161
3162TEST(TypeMatching, MatchesConstantArrayTypes) {
3163 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3164 EXPECT_TRUE(notMatches(
3165 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3166 constantArrayType(hasElementType(builtinType()))));
3167
3168 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3169 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3170 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3171}
3172
3173TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3174 EXPECT_TRUE(matches(
3175 "template <typename T, int Size> class array { T data[Size]; };",
3176 dependentSizedArrayType()));
3177 EXPECT_TRUE(notMatches(
3178 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3179 dependentSizedArrayType()));
3180}
3181
3182TEST(TypeMatching, MatchesIncompleteArrayType) {
3183 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3184 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3185
3186 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3187 incompleteArrayType()));
3188}
3189
3190TEST(TypeMatching, MatchesVariableArrayType) {
3191 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3192 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3193
3194 EXPECT_TRUE(matches(
3195 "void f(int b) { int a[b]; }",
3196 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3197 varDecl(hasName("b")))))))));
3198}
3199
3200TEST(TypeMatching, MatchesAtomicTypes) {
3201 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3202
3203 EXPECT_TRUE(matches("_Atomic(int) i;",
3204 atomicType(hasValueType(isInteger()))));
3205 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3206 atomicType(hasValueType(isInteger()))));
3207}
3208
3209TEST(TypeMatching, MatchesAutoTypes) {
3210 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3211 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3212 autoType()));
3213
3214 EXPECT_TRUE(matches("auto a = 1;",
3215 autoType(hasDeducedType(isInteger()))));
3216 EXPECT_TRUE(notMatches("auto b = 2.0;",
3217 autoType(hasDeducedType(isInteger()))));
3218}
3219
Daniel Jaspera267cf62012-10-29 10:14:44 +00003220TEST(TypeMatching, MatchesFunctionTypes) {
3221 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3222 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3223}
3224
Daniel Jasperce620072012-10-17 08:52:59 +00003225TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003226 // FIXME: Reactive when these tests can be more specific (not matching
3227 // implicit code on certain platforms), likely when we have hasDescendant for
3228 // Types/TypeLocs.
3229 //EXPECT_TRUE(matchAndVerifyResultTrue(
3230 // "int* a;",
3231 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3232 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3233 //EXPECT_TRUE(matchAndVerifyResultTrue(
3234 // "int* a;",
3235 // pointerTypeLoc().bind("loc"),
3236 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003237 EXPECT_TRUE(matches(
3238 "int** a;",
3239 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3240 EXPECT_TRUE(matches(
3241 "int** a;",
3242 loc(pointerType(pointee(pointerType())))));
3243 EXPECT_TRUE(matches(
3244 "int* b; int* * const a = &b;",
3245 loc(qualType(isConstQualified(), pointerType()))));
3246
3247 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003248 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3249 hasType(blockPointerType()))));
3250 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3251 hasType(memberPointerType()))));
3252 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3253 hasType(pointerType()))));
3254 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3255 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003256
Daniel Jasper1802daf2012-10-17 13:35:36 +00003257 Fragment = "int *ptr;";
3258 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3259 hasType(blockPointerType()))));
3260 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3261 hasType(memberPointerType()))));
3262 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3263 hasType(pointerType()))));
3264 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3265 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003266
Daniel Jasper1802daf2012-10-17 13:35:36 +00003267 Fragment = "int a; int &ref = a;";
3268 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3269 hasType(blockPointerType()))));
3270 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3271 hasType(memberPointerType()))));
3272 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3273 hasType(pointerType()))));
3274 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3275 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003276}
3277
3278TEST(TypeMatching, PointeeTypes) {
3279 EXPECT_TRUE(matches("int b; int &a = b;",
3280 referenceType(pointee(builtinType()))));
3281 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3282
3283 EXPECT_TRUE(matches("int *a;",
3284 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3285
3286 EXPECT_TRUE(matches(
3287 "int const *A;",
3288 pointerType(pointee(isConstQualified(), builtinType()))));
3289 EXPECT_TRUE(notMatches(
3290 "int *A;",
3291 pointerType(pointee(isConstQualified(), builtinType()))));
3292}
3293
3294TEST(TypeMatching, MatchesPointersToConstTypes) {
3295 EXPECT_TRUE(matches("int b; int * const a = &b;",
3296 loc(pointerType())));
3297 EXPECT_TRUE(matches("int b; int * const a = &b;",
3298 pointerTypeLoc()));
3299 EXPECT_TRUE(matches(
3300 "int b; const int * a = &b;",
3301 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3302 EXPECT_TRUE(matches(
3303 "int b; const int * a = &b;",
3304 pointerType(pointee(builtinType()))));
3305}
3306
3307TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003308 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3309 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003310
Daniel Jasper1802daf2012-10-17 13:35:36 +00003311 EXPECT_TRUE(matches("typedef int X; X a;",
3312 varDecl(hasName("a"),
3313 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003314}
3315
Daniel Jaspera7564432012-09-13 13:11:25 +00003316TEST(NNS, MatchesNestedNameSpecifiers) {
3317 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3318 nestedNameSpecifier()));
3319 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3320 nestedNameSpecifier()));
3321 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3322 nestedNameSpecifier()));
3323
3324 EXPECT_TRUE(matches(
3325 "struct A { static void f() {} }; void g() { A::f(); }",
3326 nestedNameSpecifier()));
3327 EXPECT_TRUE(notMatches(
3328 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3329 nestedNameSpecifier()));
3330}
3331
Daniel Jasperb54b7642012-09-20 14:12:57 +00003332TEST(NullStatement, SimpleCases) {
3333 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3334 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3335}
3336
Daniel Jaspera7564432012-09-13 13:11:25 +00003337TEST(NNS, MatchesTypes) {
3338 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3339 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3340 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3341 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3342 Matcher));
3343 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3344}
3345
3346TEST(NNS, MatchesNamespaceDecls) {
3347 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3348 specifiesNamespace(hasName("ns")));
3349 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3350 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3351 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3352}
3353
3354TEST(NNS, BindsNestedNameSpecifiers) {
3355 EXPECT_TRUE(matchAndVerifyResultTrue(
3356 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3357 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3358 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3359}
3360
3361TEST(NNS, BindsNestedNameSpecifierLocs) {
3362 EXPECT_TRUE(matchAndVerifyResultTrue(
3363 "namespace ns { struct B {}; } ns::B b;",
3364 loc(nestedNameSpecifier()).bind("loc"),
3365 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3366}
3367
3368TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3369 EXPECT_TRUE(matches(
3370 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3371 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3372 EXPECT_TRUE(matches(
3373 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003374 nestedNameSpecifierLoc(hasPrefix(
3375 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003376}
3377
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003378TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3379 std::string Fragment =
3380 "namespace a { struct A { struct B { struct C {}; }; }; };"
3381 "void f() { a::A::B::C c; }";
3382 EXPECT_TRUE(matches(
3383 Fragment,
3384 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3385 hasDescendant(nestedNameSpecifier(
3386 specifiesNamespace(hasName("a")))))));
3387 EXPECT_TRUE(notMatches(
3388 Fragment,
3389 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3390 has(nestedNameSpecifier(
3391 specifiesNamespace(hasName("a")))))));
3392 EXPECT_TRUE(matches(
3393 Fragment,
3394 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3395 has(nestedNameSpecifier(
3396 specifiesNamespace(hasName("a")))))));
3397
3398 // Not really useful because a NestedNameSpecifier can af at most one child,
3399 // but to complete the interface.
3400 EXPECT_TRUE(matchAndVerifyResultTrue(
3401 Fragment,
3402 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3403 forEach(nestedNameSpecifier().bind("x"))),
3404 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3405}
3406
3407TEST(NNS, NestedNameSpecifiersAsDescendants) {
3408 std::string Fragment =
3409 "namespace a { struct A { struct B { struct C {}; }; }; };"
3410 "void f() { a::A::B::C c; }";
3411 EXPECT_TRUE(matches(
3412 Fragment,
3413 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3414 asString("struct a::A")))))));
3415 EXPECT_TRUE(matchAndVerifyResultTrue(
3416 Fragment,
3417 functionDecl(hasName("f"),
3418 forEachDescendant(nestedNameSpecifier().bind("x"))),
3419 // Nested names: a, a::A and a::A::B.
3420 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3421}
3422
3423TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3424 std::string Fragment =
3425 "namespace a { struct A { struct B { struct C {}; }; }; };"
3426 "void f() { a::A::B::C c; }";
3427 EXPECT_TRUE(matches(
3428 Fragment,
3429 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3430 hasDescendant(loc(nestedNameSpecifier(
3431 specifiesNamespace(hasName("a"))))))));
3432 EXPECT_TRUE(notMatches(
3433 Fragment,
3434 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3435 has(loc(nestedNameSpecifier(
3436 specifiesNamespace(hasName("a"))))))));
3437 EXPECT_TRUE(matches(
3438 Fragment,
3439 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3440 has(loc(nestedNameSpecifier(
3441 specifiesNamespace(hasName("a"))))))));
3442
3443 EXPECT_TRUE(matchAndVerifyResultTrue(
3444 Fragment,
3445 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3446 forEach(nestedNameSpecifierLoc().bind("x"))),
3447 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3448}
3449
3450TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3451 std::string Fragment =
3452 "namespace a { struct A { struct B { struct C {}; }; }; };"
3453 "void f() { a::A::B::C c; }";
3454 EXPECT_TRUE(matches(
3455 Fragment,
3456 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3457 asString("struct a::A"))))))));
3458 EXPECT_TRUE(matchAndVerifyResultTrue(
3459 Fragment,
3460 functionDecl(hasName("f"),
3461 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3462 // Nested names: a, a::A and a::A::B.
3463 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3464}
3465
Manuel Klimek60969f52013-02-01 13:41:35 +00003466template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003467public:
Manuel Klimek60969f52013-02-01 13:41:35 +00003468 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher)
3469 : Id(Id), InnerMatcher(InnerMatcher) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003470 }
3471
Manuel Klimek60969f52013-02-01 13:41:35 +00003472 virtual bool run(const BoundNodes *Nodes) { return false; }
3473
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003474 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3475 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimek60969f52013-02-01 13:41:35 +00003476 SmallVector<BoundNodes, 1> Result = match(InnerMatcher, *Node, *Context);
3477 return !Result.empty();
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003478 }
3479private:
3480 std::string Id;
3481 internal::Matcher<T> InnerMatcher;
3482};
3483
3484TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003485 EXPECT_TRUE(matchAndVerifyResultTrue(
3486 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3487 new VerifyMatchOnNode<clang::Decl>(
3488 "X", decl(hasDescendant(recordDecl(hasName("X::Y")))))));
3489 EXPECT_TRUE(matchAndVerifyResultFalse(
3490 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3491 new VerifyMatchOnNode<clang::Decl>(
3492 "X", decl(hasDescendant(recordDecl(hasName("X::Z")))))));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003493}
3494
3495TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003496 EXPECT_TRUE(matchAndVerifyResultTrue(
3497 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
3498 new VerifyMatchOnNode<clang::Stmt>("if",
3499 stmt(hasDescendant(forStmt())))));
3500 EXPECT_TRUE(matchAndVerifyResultFalse(
3501 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
3502 new VerifyMatchOnNode<clang::Stmt>("if",
3503 stmt(hasDescendant(declStmt())))));
3504}
3505
3506TEST(MatchFinder, CanMatchSingleNodesRecursively) {
3507 EXPECT_TRUE(matchAndVerifyResultTrue(
3508 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3509 new VerifyMatchOnNode<clang::Decl>(
3510 "X", recordDecl(has(recordDecl(hasName("X::Y")))))));
3511 EXPECT_TRUE(matchAndVerifyResultFalse(
3512 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3513 new VerifyMatchOnNode<clang::Decl>(
3514 "X", recordDecl(has(recordDecl(hasName("X::Z")))))));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003515}
3516
Manuel Klimeke5793282012-11-02 01:31:03 +00003517class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3518public:
3519 VerifyStartOfTranslationUnit() : Called(false) {}
3520 virtual void run(const MatchFinder::MatchResult &Result) {
3521 EXPECT_TRUE(Called);
3522 }
3523 virtual void onStartOfTranslationUnit() {
3524 Called = true;
3525 }
3526 bool Called;
3527};
3528
3529TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3530 MatchFinder Finder;
3531 VerifyStartOfTranslationUnit VerifyCallback;
3532 Finder.addMatcher(decl(), &VerifyCallback);
3533 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3534 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3535 EXPECT_TRUE(VerifyCallback.Called);
3536}
3537
Manuel Klimek4da21662012-07-06 05:48:52 +00003538} // end namespace ast_matchers
3539} // end namespace clang