blob: 129f90f2dc120d05d917aeb553c9c519800076b8 [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[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000354 "struct T { };"
355 "int f(int, T*, int, int);"
356 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000357 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000358 callExpr(allOf(callee(functionDecl(hasName("f"))),
359 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000360 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000361 callExpr(allOf(callee(functionDecl(hasName("f"))),
362 hasArgument(0, declRefExpr(to(varDecl()))),
363 hasArgument(1, hasType(pointsTo(
364 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000365 EXPECT_TRUE(matches(Program,
366 callExpr(allOf(callee(functionDecl(hasName("f"))),
367 hasArgument(0, declRefExpr(to(varDecl()))),
368 hasArgument(1, hasType(pointsTo(
369 recordDecl(hasName("T"))))),
370 hasArgument(2, integerLiteral(equals(3)))))));
371 EXPECT_TRUE(matches(Program,
372 callExpr(allOf(callee(functionDecl(hasName("f"))),
373 hasArgument(0, declRefExpr(to(varDecl()))),
374 hasArgument(1, hasType(pointsTo(
375 recordDecl(hasName("T"))))),
376 hasArgument(2, integerLiteral(equals(3))),
377 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000378}
379
Manuel Klimek4da21662012-07-06 05:48:52 +0000380TEST(DeclarationMatcher, MatchAnyOf) {
381 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000382 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000383 EXPECT_TRUE(
384 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
385 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
386 EXPECT_TRUE(
387 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
388 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
389
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000390 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000391 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000392 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
393 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
394
Manuel Klimek4da21662012-07-06 05:48:52 +0000395 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000396 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
397 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000398 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
399 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
400 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
401 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
402 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
403 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
404}
405
406TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000407 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000408 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
409 EXPECT_TRUE(matches("class X {};", HasClassX));
410
411 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000412 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000413 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
414 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
415 EXPECT_TRUE(
416 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
417}
418
419TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
420 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000421 recordDecl(
422 has(recordDecl(
423 has(recordDecl(hasName("X"))),
424 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000425 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000426 has(recordDecl(
427 has(recordDecl(hasName("A"))),
428 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000429 hasName("C"))),
430 hasName("F"));
431
432 EXPECT_TRUE(matches(
433 "class F {"
434 " class Z {"
435 " class X {};"
436 " class Y {};"
437 " };"
438 " class C {"
439 " class A {};"
440 " class B {};"
441 " };"
442 "};", Recursive));
443
444 EXPECT_TRUE(matches(
445 "class F {"
446 " class Z {"
447 " class A {};"
448 " class X {};"
449 " class Y {};"
450 " };"
451 " class C {"
452 " class X {};"
453 " class A {};"
454 " class B {};"
455 " };"
456 "};", Recursive));
457
458 EXPECT_TRUE(matches(
459 "class O1 {"
460 " class O2 {"
461 " class F {"
462 " class Z {"
463 " class A {};"
464 " class X {};"
465 " class Y {};"
466 " };"
467 " class C {"
468 " class X {};"
469 " class A {};"
470 " class B {};"
471 " };"
472 " };"
473 " };"
474 "};", Recursive));
475}
476
477TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
478 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000479 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000480 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000481 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000482 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000483 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000484 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000485 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000486 hasName("Y"))),
487 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000488 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000489 anyOf(
490 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000491 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000492 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000493 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000494 hasName("B")))))),
495 hasName("F")));
496
497 EXPECT_TRUE(matches("class F {};", Recursive));
498 EXPECT_TRUE(matches("class Z {};", Recursive));
499 EXPECT_TRUE(matches("class C {};", Recursive));
500 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
501 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
502 EXPECT_TRUE(
503 matches("class O1 { class O2 {"
504 " class M { class N { class B {}; }; }; "
505 "}; };", Recursive));
506}
507
508TEST(DeclarationMatcher, MatchNot) {
509 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000510 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000511 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000512 unless(hasName("X")));
513 EXPECT_TRUE(notMatches("", NotClassX));
514 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
515 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
516 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
517 EXPECT_TRUE(
518 notMatches("class Y {}; class Z {}; class X : public Y {};",
519 NotClassX));
520
521 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000522 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000523 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000524 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000525 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000526 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000527 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
528 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
529 ClassXHasNotClassY));
530}
531
532TEST(DeclarationMatcher, HasDescendant) {
533 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000534 recordDecl(
535 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000536 hasName("Z"));
537 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
538 EXPECT_TRUE(
539 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
540 EXPECT_TRUE(
541 matches("class Z { class A { class Y { class X {}; }; }; };",
542 ZDescendantClassX));
543 EXPECT_TRUE(
544 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
545 ZDescendantClassX));
546 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
547
548 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000549 recordDecl(
550 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000551 hasName("X"))),
552 hasName("Z"));
553 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
554 ZDescendantClassXHasClassY));
555 EXPECT_TRUE(
556 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
557 ZDescendantClassXHasClassY));
558 EXPECT_TRUE(notMatches(
559 "class Z {"
560 " class A {"
561 " class B {"
562 " class X {"
563 " class C {"
564 " class Y {};"
565 " };"
566 " };"
567 " }; "
568 " };"
569 "};", ZDescendantClassXHasClassY));
570
571 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000572 recordDecl(
573 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
574 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000575 hasName("Z"));
576 EXPECT_TRUE(
577 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
578 ZDescendantClassXDescendantClassY));
579 EXPECT_TRUE(matches(
580 "class Z {"
581 " class A {"
582 " class X {"
583 " class B {"
584 " class Y {};"
585 " };"
586 " class Y {};"
587 " };"
588 " };"
589 "};", ZDescendantClassXDescendantClassY));
590}
591
Daniel Jaspera267cf62012-10-29 10:14:44 +0000592// Implements a run method that returns whether BoundNodes contains a
593// Decl bound to Id that can be dynamically cast to T.
594// Optionally checks that the check succeeded a specific number of times.
595template <typename T>
596class VerifyIdIsBoundTo : public BoundNodesCallback {
597public:
598 // Create an object that checks that a node of type \c T was bound to \c Id.
599 // Does not check for a certain number of matches.
600 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
601 : Id(Id), ExpectedCount(-1), Count(0) {}
602
603 // Create an object that checks that a node of type \c T was bound to \c Id.
604 // Checks that there were exactly \c ExpectedCount matches.
605 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
606 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
607
608 // Create an object that checks that a node of type \c T was bound to \c Id.
609 // Checks that there was exactly one match with the name \c ExpectedName.
610 // Note that \c T must be a NamedDecl for this to work.
611 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
612 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
613
614 ~VerifyIdIsBoundTo() {
615 if (ExpectedCount != -1)
616 EXPECT_EQ(ExpectedCount, Count);
617 if (!ExpectedName.empty())
618 EXPECT_EQ(ExpectedName, Name);
619 }
620
621 virtual bool run(const BoundNodes *Nodes) {
622 if (Nodes->getNodeAs<T>(Id)) {
623 ++Count;
624 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
625 Name = Named->getNameAsString();
626 } else if (const NestedNameSpecifier *NNS =
627 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
628 llvm::raw_string_ostream OS(Name);
629 NNS->print(OS, PrintingPolicy(LangOptions()));
630 }
631 return true;
632 }
633 return false;
634 }
635
Daniel Jasper452abbc2012-10-29 10:48:25 +0000636 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
637 return run(Nodes);
638 }
639
Daniel Jaspera267cf62012-10-29 10:14:44 +0000640private:
641 const std::string Id;
642 const int ExpectedCount;
643 int Count;
644 const std::string ExpectedName;
645 std::string Name;
646};
647
648TEST(HasDescendant, MatchesDescendantTypes) {
649 EXPECT_TRUE(matches("void f() { int i = 3; }",
650 decl(hasDescendant(loc(builtinType())))));
651 EXPECT_TRUE(matches("void f() { int i = 3; }",
652 stmt(hasDescendant(builtinType()))));
653
654 EXPECT_TRUE(matches("void f() { int i = 3; }",
655 stmt(hasDescendant(loc(builtinType())))));
656 EXPECT_TRUE(matches("void f() { int i = 3; }",
657 stmt(hasDescendant(qualType(builtinType())))));
658
659 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
660 stmt(hasDescendant(isInteger()))));
661
662 EXPECT_TRUE(matchAndVerifyResultTrue(
663 "void f() { int a; float c; int d; int e; }",
664 functionDecl(forEachDescendant(
665 varDecl(hasDescendant(isInteger())).bind("x"))),
666 new VerifyIdIsBoundTo<Decl>("x", 3)));
667}
668
669TEST(HasDescendant, MatchesDescendantsOfTypes) {
670 EXPECT_TRUE(matches("void f() { int*** i; }",
671 qualType(hasDescendant(builtinType()))));
672 EXPECT_TRUE(matches("void f() { int*** i; }",
673 qualType(hasDescendant(
674 pointerType(pointee(builtinType()))))));
675 EXPECT_TRUE(matches("void f() { int*** i; }",
676 typeLoc(hasDescendant(builtinTypeLoc()))));
677
678 EXPECT_TRUE(matchAndVerifyResultTrue(
679 "void f() { int*** i; }",
680 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
681 new VerifyIdIsBoundTo<Type>("x", 2)));
682}
683
684TEST(Has, MatchesChildrenOfTypes) {
685 EXPECT_TRUE(matches("int i;",
686 varDecl(hasName("i"), has(isInteger()))));
687 EXPECT_TRUE(notMatches("int** i;",
688 varDecl(hasName("i"), has(isInteger()))));
689 EXPECT_TRUE(matchAndVerifyResultTrue(
690 "int (*f)(float, int);",
691 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
692 new VerifyIdIsBoundTo<QualType>("x", 2)));
693}
694
695TEST(Has, MatchesChildTypes) {
696 EXPECT_TRUE(matches(
697 "int* i;",
698 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
699 EXPECT_TRUE(notMatches(
700 "int* i;",
701 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
702}
703
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000704TEST(Enum, DoesNotMatchClasses) {
705 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
706}
707
708TEST(Enum, MatchesEnums) {
709 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
710}
711
712TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000713 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000714 EXPECT_TRUE(matches("enum X{ A };", Matcher));
715 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
716 EXPECT_TRUE(notMatches("enum X {};", Matcher));
717}
718
Manuel Klimek4da21662012-07-06 05:48:52 +0000719TEST(StatementMatcher, Has) {
720 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000721 expr(hasType(pointsTo(recordDecl(hasName("X")))),
722 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000723
724 EXPECT_TRUE(matches(
725 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
726 EXPECT_TRUE(notMatches(
727 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
728}
729
730TEST(StatementMatcher, HasDescendant) {
731 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000732 expr(hasType(pointsTo(recordDecl(hasName("X")))),
733 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000734
735 EXPECT_TRUE(matches(
736 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
737 HasDescendantVariableI));
738 EXPECT_TRUE(notMatches(
739 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
740 HasDescendantVariableI));
741}
742
743TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000744 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000745
746 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
747 EXPECT_TRUE(notMatches("class A {};", TypeA));
748
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000749 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000750
751 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
752 TypeDerivedFromA));
753 EXPECT_TRUE(notMatches("class A {};", TypeA));
754
755 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000757
758 EXPECT_TRUE(
759 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
760}
761
Manuel Klimek4da21662012-07-06 05:48:52 +0000762TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000763 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000764
765 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000766 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000767
768 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000769 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000770
771 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000772 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000773
774 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
775 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000776 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000777
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 StatementMatcher MethodX =
779 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000780
781 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
782 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000783 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000784}
785
786TEST(Matcher, BindTheSameNameInAlternatives) {
787 StatementMatcher matcher = anyOf(
788 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000790 hasRHS(integerLiteral(equals(0)))),
791 binaryOperator(hasOperatorName("+"),
792 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000793 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000794
795 EXPECT_TRUE(matchAndVerifyResultTrue(
796 // The first branch of the matcher binds x to 0 but then fails.
797 // The second branch binds x to f() and succeeds.
798 "int f() { return 0 + f(); }",
799 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000800 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000801}
802
Manuel Klimek66341c52012-08-30 19:41:06 +0000803TEST(Matcher, BindsIDForMemoizedResults) {
804 // Using the same matcher in two match expressions will make memoization
805 // kick in.
806 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
807 EXPECT_TRUE(matchAndVerifyResultTrue(
808 "class A { class B { class X {}; }; };",
809 DeclarationMatcher(anyOf(
810 recordDecl(hasName("A"), hasDescendant(ClassX)),
811 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000812 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000813}
814
Daniel Jasper189f2e42012-12-03 15:43:25 +0000815TEST(HasDeclaration, HasDeclarationOfEnumType) {
816 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
817 expr(hasType(pointsTo(
818 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
819}
820
Manuel Klimek4da21662012-07-06 05:48:52 +0000821TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000822 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000823 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000824 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000825 EXPECT_TRUE(
826 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000827 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000828 EXPECT_TRUE(
829 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000830 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000831}
832
833TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000834 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000835 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000836 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000837 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000838 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000839 EXPECT_TRUE(
840 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000841 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000842}
843
844TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000845 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000846 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000847 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000848 EXPECT_TRUE(
849 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000850 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000851}
852
853TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000854 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000855 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000856 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000857 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000858 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000859}
860
861TEST(Matcher, Call) {
862 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000863 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000864 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000865
866 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
867 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
868
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000869 StatementMatcher MethodOnY =
870 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000871
872 EXPECT_TRUE(
873 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
874 MethodOnY));
875 EXPECT_TRUE(
876 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
877 MethodOnY));
878 EXPECT_TRUE(
879 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
880 MethodOnY));
881 EXPECT_TRUE(
882 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
883 MethodOnY));
884 EXPECT_TRUE(
885 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
886 MethodOnY));
887
888 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000889 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000890
891 EXPECT_TRUE(
892 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
893 MethodOnYPointer));
894 EXPECT_TRUE(
895 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
896 MethodOnYPointer));
897 EXPECT_TRUE(
898 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
899 MethodOnYPointer));
900 EXPECT_TRUE(
901 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
902 MethodOnYPointer));
903 EXPECT_TRUE(
904 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
905 MethodOnYPointer));
906}
907
Daniel Jasper31f7c082012-10-01 13:40:41 +0000908TEST(Matcher, Lambda) {
909 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
910 lambdaExpr()));
911}
912
913TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000914 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
915 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000916 forRangeStmt()));
917 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
918 forRangeStmt()));
919}
920
921TEST(Matcher, UserDefinedLiteral) {
922 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
923 " return i + 1;"
924 "}"
925 "char c = 'a'_inc;",
926 userDefinedLiteral()));
927}
928
Daniel Jasperb54b7642012-09-20 14:12:57 +0000929TEST(Matcher, FlowControl) {
930 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
931 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
932 continueStmt()));
933 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
934 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
935 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
936}
937
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000938TEST(HasType, MatchesAsString) {
939 EXPECT_TRUE(
940 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000941 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000942 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000943 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000944 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000945 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000946 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000947 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000948}
949
Manuel Klimek4da21662012-07-06 05:48:52 +0000950TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000951 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000952 // Unary operator
953 EXPECT_TRUE(matches("class Y { }; "
954 "bool operator!(Y x) { return false; }; "
955 "Y y; bool c = !y;", OpCall));
956 // No match -- special operators like "new", "delete"
957 // FIXME: operator new takes size_t, for which we need stddef.h, for which
958 // we need to figure out include paths in the test.
959 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
960 // "class Y { }; "
961 // "void *operator new(size_t size) { return 0; } "
962 // "Y *y = new Y;", OpCall));
963 EXPECT_TRUE(notMatches("class Y { }; "
964 "void operator delete(void *p) { } "
965 "void a() {Y *y = new Y; delete y;}", OpCall));
966 // Binary operator
967 EXPECT_TRUE(matches("class Y { }; "
968 "bool operator&&(Y x, Y y) { return true; }; "
969 "Y a; Y b; bool c = a && b;",
970 OpCall));
971 // No match -- normal operator, not an overloaded one.
972 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
973 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
974}
975
976TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
977 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000978 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000979 EXPECT_TRUE(matches("class Y { }; "
980 "bool operator&&(Y x, Y y) { return true; }; "
981 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
982 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000983 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000984 EXPECT_TRUE(notMatches("class Y { }; "
985 "bool operator&&(Y x, Y y) { return true; }; "
986 "Y a; Y b; bool c = a && b;",
987 OpCallLessLess));
988}
989
Daniel Jasper278057f2012-11-15 03:29:05 +0000990TEST(Matcher, NestedOverloadedOperatorCalls) {
991 EXPECT_TRUE(matchAndVerifyResultTrue(
992 "class Y { }; "
993 "Y& operator&&(Y& x, Y& y) { return x; }; "
994 "Y a; Y b; Y c; Y d = a && b && c;",
995 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
996 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
997 EXPECT_TRUE(matches(
998 "class Y { }; "
999 "Y& operator&&(Y& x, Y& y) { return x; }; "
1000 "Y a; Y b; Y c; Y d = a && b && c;",
1001 operatorCallExpr(hasParent(operatorCallExpr()))));
1002 EXPECT_TRUE(matches(
1003 "class Y { }; "
1004 "Y& operator&&(Y& x, Y& y) { return x; }; "
1005 "Y a; Y b; Y c; Y d = a && b && c;",
1006 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1007}
1008
Manuel Klimek4da21662012-07-06 05:48:52 +00001009TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001010 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001011 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001012
1013 EXPECT_TRUE(
1014 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1015 MethodOnY));
1016 EXPECT_TRUE(
1017 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1018 MethodOnY));
1019 EXPECT_TRUE(
1020 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1021 MethodOnY));
1022 EXPECT_TRUE(
1023 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1024 MethodOnY));
1025 EXPECT_TRUE(
1026 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1027 MethodOnY));
1028
1029 EXPECT_TRUE(matches(
1030 "class Y {"
1031 " public: virtual void x();"
1032 "};"
1033 "class X : public Y {"
1034 " public: virtual void x();"
1035 "};"
1036 "void z() { X *x; x->Y::x(); }", MethodOnY));
1037}
1038
1039TEST(Matcher, VariableUsage) {
1040 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001041 declRefExpr(to(
1042 varDecl(hasInitializer(
1043 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001044
1045 EXPECT_TRUE(matches(
1046 "class Y {"
1047 " public:"
1048 " bool x() const;"
1049 "};"
1050 "void z(const Y &y) {"
1051 " bool b = y.x();"
1052 " if (b) {}"
1053 "}", Reference));
1054
1055 EXPECT_TRUE(notMatches(
1056 "class Y {"
1057 " public:"
1058 " bool x() const;"
1059 "};"
1060 "void z(const Y &y) {"
1061 " bool b = y.x();"
1062 "}", Reference));
1063}
1064
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001065TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001066 EXPECT_TRUE(matches(
1067 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001068 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001069}
1070
Manuel Klimek4da21662012-07-06 05:48:52 +00001071TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001072 StatementMatcher CallOnVariableY =
1073 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001074
1075 EXPECT_TRUE(matches(
1076 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1077 EXPECT_TRUE(matches(
1078 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1079 EXPECT_TRUE(matches(
1080 "class Y { public: void x(); };"
1081 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1082 EXPECT_TRUE(matches(
1083 "class Y { public: void x(); };"
1084 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1085 EXPECT_TRUE(notMatches(
1086 "class Y { public: void x(); };"
1087 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1088 CallOnVariableY));
1089}
1090
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001091TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1092 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1093 unaryExprOrTypeTraitExpr()));
1094 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1095 alignOfExpr(anything())));
1096 // FIXME: Uncomment once alignof is enabled.
1097 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1098 // unaryExprOrTypeTraitExpr()));
1099 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1100 // sizeOfExpr()));
1101}
1102
1103TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1104 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1105 hasArgumentOfType(asString("int")))));
1106 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1107 hasArgumentOfType(asString("float")))));
1108 EXPECT_TRUE(matches(
1109 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001110 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001111 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001112 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001113}
1114
Manuel Klimek4da21662012-07-06 05:48:52 +00001115TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001116 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001117}
1118
1119TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001120 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001121}
1122
1123TEST(MemberExpression, MatchesVariable) {
1124 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001125 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001126 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001127 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001128 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001129 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001130}
1131
1132TEST(MemberExpression, MatchesStaticVariable) {
1133 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001134 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001135 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001136 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001137 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001138 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001139}
1140
Daniel Jasper6a124492012-07-12 08:50:38 +00001141TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001142 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1143 EXPECT_TRUE(matches(
1144 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1145 callExpr(hasArgument(0, declRefExpr(
1146 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001147}
1148
1149TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001150 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001151 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001152 callExpr(hasArgument(0, declRefExpr(
1153 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001154}
1155
Manuel Klimek4da21662012-07-06 05:48:52 +00001156TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1157 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001158 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001159 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001160 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001161 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001162 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001163}
1164
1165TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1166 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001167 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001168 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001169 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001170 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001171 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001172}
1173
1174TEST(IsArrow, MatchesMemberCallsViaArrow) {
1175 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001176 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001177 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001178 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001179 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001180 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001181}
1182
1183TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001184 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001185
1186 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1187 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1188}
1189
1190TEST(Callee, MatchesMemberExpressions) {
1191 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001192 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001193 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001194 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001195}
1196
1197TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001198 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001199
1200 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1201 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1202
Manuel Klimeke265c872012-07-10 14:21:30 +00001203#if !defined(_MSC_VER)
1204 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001205 // Dependent contexts, but a non-dependent call.
1206 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1207 CallFunctionF));
1208 EXPECT_TRUE(
1209 matches("void f(); template <int N> struct S { void g() { f(); } };",
1210 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001211#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001212
1213 // Depedent calls don't match.
1214 EXPECT_TRUE(
1215 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1216 CallFunctionF));
1217 EXPECT_TRUE(
1218 notMatches("void f(int);"
1219 "template <typename T> struct S { void g(T t) { f(t); } };",
1220 CallFunctionF));
1221}
1222
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001223TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1224 EXPECT_TRUE(
1225 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001226 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001227}
1228
1229TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1230 EXPECT_TRUE(
1231 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001232 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001233}
1234
1235TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1236 EXPECT_TRUE(
1237 notMatches("void g(); template <typename T> void f(T t) {}"
1238 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001239 functionTemplateDecl(hasName("f"),
1240 hasDescendant(declRefExpr(to(
1241 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001242}
1243
Manuel Klimek4da21662012-07-06 05:48:52 +00001244TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001245 StatementMatcher CallArgumentY = callExpr(
1246 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001247
1248 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1249 EXPECT_TRUE(
1250 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1251 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1252
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001253 StatementMatcher WrongIndex = callExpr(
1254 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001255 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1256}
1257
1258TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001259 StatementMatcher CallArgumentY = callExpr(
1260 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001261 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1262 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1263 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1264}
1265
1266TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001267 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001268
1269 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1270 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1271 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1272}
1273
Daniel Jasper36e29d62012-12-04 11:54:27 +00001274TEST(Matcher, ParameterCount) {
1275 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1276 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1277 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1278 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1279 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1280}
1281
Manuel Klimek4da21662012-07-06 05:48:52 +00001282TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001283 DeclarationMatcher ReferenceClassX = varDecl(
1284 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001285 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1286 ReferenceClassX));
1287 EXPECT_TRUE(
1288 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1289 EXPECT_TRUE(
1290 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1291 EXPECT_TRUE(
1292 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1293}
1294
1295TEST(HasParameter, CallsInnerMatcher) {
1296 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001297 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001298 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001299 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001300}
1301
1302TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1303 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001304 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001305}
1306
1307TEST(HasType, MatchesParameterVariableTypesStrictly) {
1308 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001309 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001310 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001311 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001312 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001313 methodDecl(hasParameter(0,
1314 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001315 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001316 methodDecl(hasParameter(0,
1317 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001318}
1319
1320TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1321 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001322 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001323 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001324 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001325}
1326
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001327TEST(Returns, MatchesReturnTypes) {
1328 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001329 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001330 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001331 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001332 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001333 functionDecl(returns(hasDeclaration(
1334 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001335}
1336
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001337TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001338 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1339 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1340 functionDecl(isExternC())));
1341 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001342}
1343
Manuel Klimek4da21662012-07-06 05:48:52 +00001344TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1345 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001346 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001347}
1348
1349TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1350 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001351 methodDecl(hasAnyParameter(hasType(pointsTo(
1352 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001353}
1354
1355TEST(HasName, MatchesParameterVariableDeclartions) {
1356 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001357 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001358 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001359 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001360}
1361
Daniel Jasper371f9392012-08-01 08:40:24 +00001362TEST(Matcher, MatchesClassTemplateSpecialization) {
1363 EXPECT_TRUE(matches("template<typename T> struct A {};"
1364 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001365 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001366 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001367 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001368 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001369 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001370}
1371
1372TEST(Matcher, MatchesTypeTemplateArgument) {
1373 EXPECT_TRUE(matches(
1374 "template<typename T> struct B {};"
1375 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001376 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001377 asString("int"))))));
1378}
1379
1380TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1381 EXPECT_TRUE(matches(
1382 "struct B { int next; };"
1383 "template<int(B::*next_ptr)> struct A {};"
1384 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001385 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1386 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001387
1388 EXPECT_TRUE(notMatches(
1389 "template <typename T> struct A {};"
1390 "A<int> a;",
1391 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1392 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001393}
1394
1395TEST(Matcher, MatchesSpecificArgument) {
1396 EXPECT_TRUE(matches(
1397 "template<typename T, typename U> class A {};"
1398 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001399 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001400 1, refersToType(asString("int"))))));
1401 EXPECT_TRUE(notMatches(
1402 "template<typename T, typename U> class A {};"
1403 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001404 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001405 1, refersToType(asString("int"))))));
1406}
1407
Manuel Klimek4da21662012-07-06 05:48:52 +00001408TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001409 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001410
1411 EXPECT_TRUE(
1412 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1413 EXPECT_TRUE(
1414 matches("class X { public: X(); }; void x() { X x = X(); }",
1415 Constructor));
1416 EXPECT_TRUE(
1417 matches("class X { public: X(int); }; void x() { X x = 0; }",
1418 Constructor));
1419 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1420}
1421
1422TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001423 StatementMatcher Constructor = constructExpr(
1424 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001425
1426 EXPECT_TRUE(
1427 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1428 Constructor));
1429 EXPECT_TRUE(
1430 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1431 Constructor));
1432 EXPECT_TRUE(
1433 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1434 Constructor));
1435 EXPECT_TRUE(
1436 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1437 Constructor));
1438
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001439 StatementMatcher WrongIndex = constructExpr(
1440 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001441 EXPECT_TRUE(
1442 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1443 WrongIndex));
1444}
1445
1446TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001447 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001448
1449 EXPECT_TRUE(
1450 matches("class X { public: X(int); }; void x() { X x(0); }",
1451 Constructor1Arg));
1452 EXPECT_TRUE(
1453 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1454 Constructor1Arg));
1455 EXPECT_TRUE(
1456 matches("class X { public: X(int); }; void x() { X x = 0; }",
1457 Constructor1Arg));
1458 EXPECT_TRUE(
1459 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1460 Constructor1Arg));
1461}
1462
Manuel Klimek70b9db92012-10-23 10:40:50 +00001463TEST(Matcher,ThisExpr) {
1464 EXPECT_TRUE(
1465 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1466 EXPECT_TRUE(
1467 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1468}
1469
Manuel Klimek4da21662012-07-06 05:48:52 +00001470TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001471 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001472
1473 std::string ClassString = "class string { public: string(); ~string(); }; ";
1474
1475 EXPECT_TRUE(
1476 matches(ClassString +
1477 "string GetStringByValue();"
1478 "void FunctionTakesString(string s);"
1479 "void run() { FunctionTakesString(GetStringByValue()); }",
1480 TempExpression));
1481
1482 EXPECT_TRUE(
1483 notMatches(ClassString +
1484 "string* GetStringPointer(); "
1485 "void FunctionTakesStringPtr(string* s);"
1486 "void run() {"
1487 " string* s = GetStringPointer();"
1488 " FunctionTakesStringPtr(GetStringPointer());"
1489 " FunctionTakesStringPtr(s);"
1490 "}",
1491 TempExpression));
1492
1493 EXPECT_TRUE(
1494 notMatches("class no_dtor {};"
1495 "no_dtor GetObjByValue();"
1496 "void ConsumeObj(no_dtor param);"
1497 "void run() { ConsumeObj(GetObjByValue()); }",
1498 TempExpression));
1499}
1500
Sam Panzere16acd32012-08-24 22:04:44 +00001501TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1502 std::string ClassString =
1503 "class string { public: string(); int length(); }; ";
1504
1505 EXPECT_TRUE(
1506 matches(ClassString +
1507 "string GetStringByValue();"
1508 "void FunctionTakesString(string s);"
1509 "void run() { FunctionTakesString(GetStringByValue()); }",
1510 materializeTemporaryExpr()));
1511
1512 EXPECT_TRUE(
1513 notMatches(ClassString +
1514 "string* GetStringPointer(); "
1515 "void FunctionTakesStringPtr(string* s);"
1516 "void run() {"
1517 " string* s = GetStringPointer();"
1518 " FunctionTakesStringPtr(GetStringPointer());"
1519 " FunctionTakesStringPtr(s);"
1520 "}",
1521 materializeTemporaryExpr()));
1522
1523 EXPECT_TRUE(
1524 notMatches(ClassString +
1525 "string GetStringByValue();"
1526 "void run() { int k = GetStringByValue().length(); }",
1527 materializeTemporaryExpr()));
1528
1529 EXPECT_TRUE(
1530 notMatches(ClassString +
1531 "string GetStringByValue();"
1532 "void run() { GetStringByValue(); }",
1533 materializeTemporaryExpr()));
1534}
1535
Manuel Klimek4da21662012-07-06 05:48:52 +00001536TEST(ConstructorDeclaration, SimpleCase) {
1537 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001538 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001539 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001540 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001541}
1542
1543TEST(ConstructorDeclaration, IsImplicit) {
1544 // This one doesn't match because the constructor is not added by the
1545 // compiler (it is not needed).
1546 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001547 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001548 // The compiler added the implicit default constructor.
1549 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001550 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001551 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001552 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001553}
1554
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001555TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1556 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001557 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001558}
1559
1560TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001561 EXPECT_TRUE(notMatches("class Foo {};",
1562 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001563}
1564
Manuel Klimek4da21662012-07-06 05:48:52 +00001565TEST(HasAnyConstructorInitializer, SimpleCase) {
1566 EXPECT_TRUE(notMatches(
1567 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001568 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001569 EXPECT_TRUE(matches(
1570 "class Foo {"
1571 " Foo() : foo_() { }"
1572 " int foo_;"
1573 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001574 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001575}
1576
1577TEST(HasAnyConstructorInitializer, ForField) {
1578 static const char Code[] =
1579 "class Baz { };"
1580 "class Foo {"
1581 " Foo() : foo_() { }"
1582 " Baz foo_;"
1583 " Baz bar_;"
1584 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001585 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1586 forField(hasType(recordDecl(hasName("Baz"))))))));
1587 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001588 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001589 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1590 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001591}
1592
1593TEST(HasAnyConstructorInitializer, WithInitializer) {
1594 static const char Code[] =
1595 "class Foo {"
1596 " Foo() : foo_(0) { }"
1597 " int foo_;"
1598 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001599 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001600 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001601 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001602 withInitializer(integerLiteral(equals(1)))))));
1603}
1604
1605TEST(HasAnyConstructorInitializer, IsWritten) {
1606 static const char Code[] =
1607 "struct Bar { Bar(){} };"
1608 "class Foo {"
1609 " Foo() : foo_() { }"
1610 " Bar foo_;"
1611 " Bar bar_;"
1612 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001613 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001614 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001615 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001616 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001617 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001618 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1619}
1620
1621TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001622 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001623
1624 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1625 EXPECT_TRUE(
1626 matches("class X { public: X(); }; void x() { new X(); }", New));
1627 EXPECT_TRUE(
1628 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1629 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1630}
1631
1632TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001633 StatementMatcher New = constructExpr(
1634 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001635
1636 EXPECT_TRUE(
1637 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1638 New));
1639 EXPECT_TRUE(
1640 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1641 New));
1642 EXPECT_TRUE(
1643 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1644 New));
1645
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001646 StatementMatcher WrongIndex = constructExpr(
1647 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001648 EXPECT_TRUE(
1649 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1650 WrongIndex));
1651}
1652
1653TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001654 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001655
1656 EXPECT_TRUE(
1657 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1658 EXPECT_TRUE(
1659 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1660 New));
1661}
1662
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001663TEST(Matcher, DeleteExpression) {
1664 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001665 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001666}
1667
Manuel Klimek4da21662012-07-06 05:48:52 +00001668TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001669 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001670
1671 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1672 EXPECT_TRUE(
1673 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1674 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1675}
1676
1677TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001678 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001679 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1680 // wide string
1681 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1682 // with escaped characters
1683 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1684 // no matching -- though the data type is the same, there is no string literal
1685 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1686}
1687
1688TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001689 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001690 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1691 // wide character
1692 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1693 // wide character, Hex encoded, NOT MATCHED!
1694 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1695 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1696}
1697
1698TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001699 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001700 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1701 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1702 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1703 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1704
1705 // Non-matching cases (character literals, float and double)
1706 EXPECT_TRUE(notMatches("int i = L'a';",
1707 HasIntLiteral)); // this is actually a character
1708 // literal cast to int
1709 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1710 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1711 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1712}
1713
Daniel Jasper31f7c082012-10-01 13:40:41 +00001714TEST(Matcher, NullPtrLiteral) {
1715 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1716}
1717
Daniel Jasperb54b7642012-09-20 14:12:57 +00001718TEST(Matcher, AsmStatement) {
1719 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1720}
1721
Manuel Klimek4da21662012-07-06 05:48:52 +00001722TEST(Matcher, Conditions) {
1723 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1724
1725 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1726 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1727 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1728 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1729 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1730}
1731
1732TEST(MatchBinaryOperator, HasOperatorName) {
1733 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1734
1735 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1736 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1737}
1738
1739TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1740 StatementMatcher OperatorTrueFalse =
1741 binaryOperator(hasLHS(boolLiteral(equals(true))),
1742 hasRHS(boolLiteral(equals(false))));
1743
1744 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1745 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1746 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1747}
1748
1749TEST(MatchBinaryOperator, HasEitherOperand) {
1750 StatementMatcher HasOperand =
1751 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1752
1753 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1754 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1755 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1756}
1757
1758TEST(Matcher, BinaryOperatorTypes) {
1759 // Integration test that verifies the AST provides all binary operators in
1760 // a way we expect.
1761 // FIXME: Operator ','
1762 EXPECT_TRUE(
1763 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1764 EXPECT_TRUE(
1765 matches("bool b; bool c = (b = true);",
1766 binaryOperator(hasOperatorName("="))));
1767 EXPECT_TRUE(
1768 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1769 EXPECT_TRUE(
1770 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1771 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1772 EXPECT_TRUE(
1773 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1774 EXPECT_TRUE(
1775 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1776 EXPECT_TRUE(
1777 matches("int i = 1; int j = (i <<= 2);",
1778 binaryOperator(hasOperatorName("<<="))));
1779 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1780 EXPECT_TRUE(
1781 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1782 EXPECT_TRUE(
1783 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1784 EXPECT_TRUE(
1785 matches("int i = 1; int j = (i >>= 2);",
1786 binaryOperator(hasOperatorName(">>="))));
1787 EXPECT_TRUE(
1788 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1789 EXPECT_TRUE(
1790 matches("int i = 42; int j = (i ^= 42);",
1791 binaryOperator(hasOperatorName("^="))));
1792 EXPECT_TRUE(
1793 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1794 EXPECT_TRUE(
1795 matches("int i = 42; int j = (i %= 42);",
1796 binaryOperator(hasOperatorName("%="))));
1797 EXPECT_TRUE(
1798 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1799 EXPECT_TRUE(
1800 matches("bool b = true && false;",
1801 binaryOperator(hasOperatorName("&&"))));
1802 EXPECT_TRUE(
1803 matches("bool b = true; bool c = (b &= false);",
1804 binaryOperator(hasOperatorName("&="))));
1805 EXPECT_TRUE(
1806 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1807 EXPECT_TRUE(
1808 matches("bool b = true || false;",
1809 binaryOperator(hasOperatorName("||"))));
1810 EXPECT_TRUE(
1811 matches("bool b = true; bool c = (b |= false);",
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("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1820 EXPECT_TRUE(
1821 matches("int i = 42; int j = (i /= 23);",
1822 binaryOperator(hasOperatorName("/="))));
1823 EXPECT_TRUE(
1824 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1825 EXPECT_TRUE(
1826 matches("int i = 42; int j = (i += 23);",
1827 binaryOperator(hasOperatorName("+="))));
1828 EXPECT_TRUE(
1829 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1830 EXPECT_TRUE(
1831 matches("int i = 42; int j = (i -= 23);",
1832 binaryOperator(hasOperatorName("-="))));
1833 EXPECT_TRUE(
1834 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1835 binaryOperator(hasOperatorName("->*"))));
1836 EXPECT_TRUE(
1837 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1838 binaryOperator(hasOperatorName(".*"))));
1839
1840 // Member expressions as operators are not supported in matches.
1841 EXPECT_TRUE(
1842 notMatches("struct A { void x(A *a) { a->x(this); } };",
1843 binaryOperator(hasOperatorName("->"))));
1844
1845 // Initializer assignments are not represented as operator equals.
1846 EXPECT_TRUE(
1847 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1848
1849 // Array indexing is not represented as operator.
1850 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1851
1852 // Overloaded operators do not match at all.
1853 EXPECT_TRUE(notMatches(
1854 "struct A { bool operator&&(const A &a) const { return false; } };"
1855 "void x() { A a, b; a && b; }",
1856 binaryOperator()));
1857}
1858
1859TEST(MatchUnaryOperator, HasOperatorName) {
1860 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1861
1862 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1863 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1864}
1865
1866TEST(MatchUnaryOperator, HasUnaryOperand) {
1867 StatementMatcher OperatorOnFalse =
1868 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1869
1870 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1871 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1872}
1873
1874TEST(Matcher, UnaryOperatorTypes) {
1875 // Integration test that verifies the AST provides all unary operators in
1876 // a way we expect.
1877 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1878 EXPECT_TRUE(
1879 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1880 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1881 EXPECT_TRUE(
1882 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1883 EXPECT_TRUE(
1884 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1885 EXPECT_TRUE(
1886 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1887 EXPECT_TRUE(
1888 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1889 EXPECT_TRUE(
1890 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1891 EXPECT_TRUE(
1892 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1893 EXPECT_TRUE(
1894 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1895
1896 // We don't match conversion operators.
1897 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1898
1899 // Function calls are not represented as operator.
1900 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1901
1902 // Overloaded operators do not match at all.
1903 // FIXME: We probably want to add that.
1904 EXPECT_TRUE(notMatches(
1905 "struct A { bool operator!() const { return false; } };"
1906 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1907}
1908
1909TEST(Matcher, ConditionalOperator) {
1910 StatementMatcher Conditional = conditionalOperator(
1911 hasCondition(boolLiteral(equals(true))),
1912 hasTrueExpression(boolLiteral(equals(false))));
1913
1914 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1915 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1916 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1917
1918 StatementMatcher ConditionalFalse = conditionalOperator(
1919 hasFalseExpression(boolLiteral(equals(false))));
1920
1921 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1922 EXPECT_TRUE(
1923 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1924}
1925
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001926TEST(ArraySubscriptMatchers, ArraySubscripts) {
1927 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1928 arraySubscriptExpr()));
1929 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1930 arraySubscriptExpr()));
1931}
1932
1933TEST(ArraySubscriptMatchers, ArrayIndex) {
1934 EXPECT_TRUE(matches(
1935 "int i[2]; void f() { i[1] = 1; }",
1936 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1937 EXPECT_TRUE(matches(
1938 "int i[2]; void f() { 1[i] = 1; }",
1939 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1940 EXPECT_TRUE(notMatches(
1941 "int i[2]; void f() { i[1] = 1; }",
1942 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1943}
1944
1945TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1946 EXPECT_TRUE(matches(
1947 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001948 arraySubscriptExpr(hasBase(implicitCastExpr(
1949 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001950}
1951
Manuel Klimek4da21662012-07-06 05:48:52 +00001952TEST(Matcher, HasNameSupportsNamespaces) {
1953 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001954 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001955 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001956 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001957 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001958 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001959 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001960 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001961 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001962 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001963 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001964 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001965 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001966 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001967 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001968 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001969 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001970 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001971 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001972 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001973 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001974 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001975 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001976 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001977}
1978
1979TEST(Matcher, HasNameSupportsOuterClasses) {
1980 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001981 matches("class A { class B { class C; }; };",
1982 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001983 EXPECT_TRUE(
1984 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001985 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001986 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001987 matches("class A { class B { class C; }; };",
1988 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001989 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001990 matches("class A { class B { class C; }; };",
1991 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001992 EXPECT_TRUE(
1993 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001994 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001995 EXPECT_TRUE(
1996 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001997 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001998 EXPECT_TRUE(
1999 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002000 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002001 EXPECT_TRUE(
2002 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002003 recordDecl(hasName("::C"))));
2004 EXPECT_TRUE(
2005 notMatches("class A { class B { class C; }; };",
2006 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002007 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002008 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002009 EXPECT_TRUE(
2010 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002011 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002012}
2013
2014TEST(Matcher, IsDefinition) {
2015 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002016 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002017 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2018 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2019
2020 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002021 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002022 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2023 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2024
2025 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002026 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002027 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2028 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2029}
2030
2031TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002032 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002033 ofClass(hasName("X")))));
2034
2035 EXPECT_TRUE(
2036 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2037 EXPECT_TRUE(
2038 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2039 Constructor));
2040 EXPECT_TRUE(
2041 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2042 Constructor));
2043}
2044
2045TEST(Matcher, VisitsTemplateInstantiations) {
2046 EXPECT_TRUE(matches(
2047 "class A { public: void x(); };"
2048 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002049 "void f() { B<A> b; b.y(); }",
2050 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002051
2052 EXPECT_TRUE(matches(
2053 "class A { public: void x(); };"
2054 "class C {"
2055 " public:"
2056 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2057 "};"
2058 "void f() {"
2059 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002060 "}",
2061 recordDecl(hasName("C"),
2062 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002063}
2064
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002065TEST(Matcher, HandlesNullQualTypes) {
2066 // FIXME: Add a Type matcher so we can replace uses of this
2067 // variable with Type(True())
2068 const TypeMatcher AnyType = anything();
2069
2070 // We don't really care whether this matcher succeeds; we're testing that
2071 // it completes without crashing.
2072 EXPECT_TRUE(matches(
2073 "struct A { };"
2074 "template <typename T>"
2075 "void f(T t) {"
2076 " T local_t(t /* this becomes a null QualType in the AST */);"
2077 "}"
2078 "void g() {"
2079 " f(0);"
2080 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002081 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002082 anyOf(
2083 TypeMatcher(hasDeclaration(anything())),
2084 pointsTo(AnyType),
2085 references(AnyType)
2086 // Other QualType matchers should go here.
2087 ))))));
2088}
2089
Manuel Klimek4da21662012-07-06 05:48:52 +00002090// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002091AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002092 // Make sure all special variables are used: node, match_finder,
2093 // bound_nodes_builder, and the parameter named 'AMatcher'.
2094 return AMatcher.matches(Node, Finder, Builder);
2095}
2096
2097TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002098 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002099
2100 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002101 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002102
2103 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002104 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002105
2106 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002107 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002108}
2109
2110AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002111 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2112 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2113 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002114 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002115 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002116 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002117 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2118 ASTMatchFinder::BK_First);
2119}
2120
2121TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002122 DeclarationMatcher HasClassB =
2123 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124
2125 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002126 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002127
2128 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002129 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130
2131 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002132 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002133
2134 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002135 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002136
2137 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2138}
2139
2140TEST(For, FindsForLoops) {
2141 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2142 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002143 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2144 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002145 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002146}
2147
Daniel Jasper6a124492012-07-12 08:50:38 +00002148TEST(For, ForLoopInternals) {
2149 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2150 forStmt(hasCondition(anything()))));
2151 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2152 forStmt(hasLoopInit(anything()))));
2153}
2154
2155TEST(For, NegativeForLoopInternals) {
2156 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002157 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002158 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2159 forStmt(hasLoopInit(anything()))));
2160}
2161
Manuel Klimek4da21662012-07-06 05:48:52 +00002162TEST(For, ReportsNoFalsePositives) {
2163 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2164 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2165}
2166
2167TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2169 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2170 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002171}
2172
2173TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2174 // It's not a compound statement just because there's "{}" in the source
2175 // text. This is an AST search, not grep.
2176 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002177 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002178 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002179 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002180}
2181
Daniel Jasper6a124492012-07-12 08:50:38 +00002182TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002183 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002184 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002185 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002186 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002187 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002189 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002190 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002191}
2192
2193TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2194 // The simplest case: every compound statement is in a function
2195 // definition, and the function body itself must be a compound
2196 // statement.
2197 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002198 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002199}
2200
2201TEST(HasAnySubstatement, IsNotRecursive) {
2202 // It's really "has any immediate substatement".
2203 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002205}
2206
2207TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2208 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002209 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002210}
2211
2212TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2213 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002214 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002215}
2216
2217TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2218 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002221 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002222}
2223
2224TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2225 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002226 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002227 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002228 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002229 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002230 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002231}
2232
2233TEST(StatementCountIs, WorksWithMultipleStatements) {
2234 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002235 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002236}
2237
2238TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2239 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002240 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002241 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002242 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002243 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002244 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002245 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002246 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002247}
2248
2249TEST(Member, WorksInSimplestCase) {
2250 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002251 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002252}
2253
2254TEST(Member, DoesNotMatchTheBaseExpression) {
2255 // Don't pick out the wrong part of the member expression, this should
2256 // be checking the member (name) only.
2257 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002258 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002259}
2260
2261TEST(Member, MatchesInMemberFunctionCall) {
2262 EXPECT_TRUE(matches("void f() {"
2263 " struct { void first() {}; } s;"
2264 " s.first();"
2265 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002266 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002267}
2268
Daniel Jasperc711af22012-10-23 15:46:39 +00002269TEST(Member, MatchesMember) {
2270 EXPECT_TRUE(matches(
2271 "struct A { int i; }; void f() { A a; a.i = 2; }",
2272 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2273 EXPECT_TRUE(notMatches(
2274 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2275 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2276}
2277
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002278TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002279 // Fails in C++11 mode
2280 EXPECT_TRUE(matchesConditionally(
2281 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2282 "class X { void *operator new(std::size_t); };",
2283 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002284
2285 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002286 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002287
Daniel Jasper31f7c082012-10-01 13:40:41 +00002288 // Fails in C++11 mode
2289 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002290 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2291 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002292 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002293}
2294
Manuel Klimek4da21662012-07-06 05:48:52 +00002295TEST(HasObjectExpression, DoesNotMatchMember) {
2296 EXPECT_TRUE(notMatches(
2297 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002298 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002299}
2300
2301TEST(HasObjectExpression, MatchesBaseOfVariable) {
2302 EXPECT_TRUE(matches(
2303 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002304 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002305 EXPECT_TRUE(matches(
2306 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002307 memberExpr(hasObjectExpression(
2308 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002309}
2310
2311TEST(HasObjectExpression,
2312 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2313 EXPECT_TRUE(matches(
2314 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002315 memberExpr(hasObjectExpression(
2316 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002317 EXPECT_TRUE(matches(
2318 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002319 memberExpr(hasObjectExpression(
2320 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002321}
2322
2323TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002324 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2325 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2326 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2327 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002328}
2329
2330TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002331 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002332}
2333
2334TEST(IsConstQualified, MatchesConstInt) {
2335 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002336 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002337}
2338
2339TEST(IsConstQualified, MatchesConstPointer) {
2340 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002341 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002342}
2343
2344TEST(IsConstQualified, MatchesThroughTypedef) {
2345 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002346 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002347 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002348 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002349}
2350
2351TEST(IsConstQualified, DoesNotMatchInappropriately) {
2352 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002353 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002354 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002355 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002356}
2357
Sam Panzer089e5b32012-08-16 16:58:10 +00002358TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002359 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2360 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2361 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2362 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002363}
2364TEST(CastExpression, MatchesImplicitCasts) {
2365 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002366 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002367 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002368 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002369}
2370
2371TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002372 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2373 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2374 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2375 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002376}
2377
Manuel Klimek4da21662012-07-06 05:48:52 +00002378TEST(ReinterpretCast, MatchesSimpleCase) {
2379 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002380 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002381}
2382
2383TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002384 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002385 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002386 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002387 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002388 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002389 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2390 "B b;"
2391 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002392 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002393}
2394
2395TEST(FunctionalCast, MatchesSimpleCase) {
2396 std::string foo_class = "class Foo { public: Foo(char*); };";
2397 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002398 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002399}
2400
2401TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2402 std::string FooClass = "class Foo { public: Foo(char*); };";
2403 EXPECT_TRUE(
2404 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002405 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002406 EXPECT_TRUE(
2407 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002408 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002409}
2410
2411TEST(DynamicCast, MatchesSimpleCase) {
2412 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2413 "B b;"
2414 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002415 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002416}
2417
2418TEST(StaticCast, MatchesSimpleCase) {
2419 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002420 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002421}
2422
2423TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002424 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002425 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002426 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002427 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002428 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002429 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2430 "B b;"
2431 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002432 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002433}
2434
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002435TEST(CStyleCast, MatchesSimpleCase) {
2436 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2437}
2438
2439TEST(CStyleCast, DoesNotMatchOtherCasts) {
2440 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2441 "char q, *r = const_cast<char*>(&q);"
2442 "void* s = reinterpret_cast<char*>(&s);"
2443 "struct B { virtual ~B() {} }; struct D : B {};"
2444 "B b;"
2445 "D* t = dynamic_cast<D*>(&b);",
2446 cStyleCastExpr()));
2447}
2448
Manuel Klimek4da21662012-07-06 05:48:52 +00002449TEST(HasDestinationType, MatchesSimpleCase) {
2450 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002451 staticCastExpr(hasDestinationType(
2452 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002453}
2454
Sam Panzer089e5b32012-08-16 16:58:10 +00002455TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2456 // This test creates an implicit const cast.
2457 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002458 implicitCastExpr(
2459 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002460 // This test creates an implicit array-to-pointer cast.
2461 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002462 implicitCastExpr(hasImplicitDestinationType(
2463 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002464}
2465
2466TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2467 // This test creates an implicit cast from int to char.
2468 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002469 implicitCastExpr(hasImplicitDestinationType(
2470 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002471 // This test creates an implicit array-to-pointer cast.
2472 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002473 implicitCastExpr(hasImplicitDestinationType(
2474 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002475}
2476
2477TEST(ImplicitCast, MatchesSimpleCase) {
2478 // This test creates an implicit const cast.
2479 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002481 // This test creates an implicit cast from int to char.
2482 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002483 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002484 // This test creates an implicit array-to-pointer cast.
2485 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002486 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002487}
2488
2489TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002490 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002491 // are present, and that it ignores explicit and paren casts.
2492
2493 // These two test cases have no casts.
2494 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002495 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002496 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002498
2499 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002500 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002501 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002502 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002503
2504 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002505 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002506}
2507
2508TEST(IgnoringImpCasts, MatchesImpCasts) {
2509 // This test checks that ignoringImpCasts matches when implicit casts are
2510 // present and its inner matcher alone does not match.
2511 // Note that this test creates an implicit const cast.
2512 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002513 varDecl(hasInitializer(ignoringImpCasts(
2514 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002515 // This test creates an implict cast from int to char.
2516 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002517 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002518 integerLiteral(equals(0)))))));
2519}
2520
2521TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2522 // These tests verify that ignoringImpCasts does not match if the inner
2523 // matcher does not match.
2524 // Note that the first test creates an implicit const cast.
2525 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002526 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002527 unless(anything()))))));
2528 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002529 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002530 unless(anything()))))));
2531
2532 // These tests verify that ignoringImplictCasts does not look through explicit
2533 // casts or parentheses.
2534 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002535 varDecl(hasInitializer(ignoringImpCasts(
2536 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002537 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002538 varDecl(hasInitializer(ignoringImpCasts(
2539 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002540 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002541 varDecl(hasInitializer(ignoringImpCasts(
2542 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002543 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002544 varDecl(hasInitializer(ignoringImpCasts(
2545 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002546}
2547
2548TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2549 // This test verifies that expressions that do not have implicit casts
2550 // still match the inner matcher.
2551 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002552 varDecl(hasInitializer(ignoringImpCasts(
2553 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002554}
2555
2556TEST(IgnoringParenCasts, MatchesParenCasts) {
2557 // This test checks that ignoringParenCasts matches when parentheses and/or
2558 // casts are present and its inner matcher alone does not match.
2559 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002560 varDecl(hasInitializer(ignoringParenCasts(
2561 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002562 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002563 varDecl(hasInitializer(ignoringParenCasts(
2564 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002565
2566 // This test creates an implict cast from int to char in addition to the
2567 // parentheses.
2568 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002569 varDecl(hasInitializer(ignoringParenCasts(
2570 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002571
2572 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002573 varDecl(hasInitializer(ignoringParenCasts(
2574 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002575 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002576 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002577 integerLiteral(equals(0)))))));
2578}
2579
2580TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2581 // This test verifies that expressions that do not have any casts still match.
2582 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002583 varDecl(hasInitializer(ignoringParenCasts(
2584 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002585}
2586
2587TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2588 // These tests verify that ignoringImpCasts does not match if the inner
2589 // matcher does not match.
2590 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002591 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002592 unless(anything()))))));
2593
2594 // This test creates an implicit cast from int to char in addition to the
2595 // parentheses.
2596 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002597 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002598 unless(anything()))))));
2599
2600 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002601 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002602 unless(anything()))))));
2603}
2604
2605TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2606 // This test checks that ignoringParenAndImpCasts matches when
2607 // parentheses and/or implicit casts are present and its inner matcher alone
2608 // does not match.
2609 // Note that this test creates an implicit const cast.
2610 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002611 varDecl(hasInitializer(ignoringParenImpCasts(
2612 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002613 // This test creates an implicit cast from int to char.
2614 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002615 varDecl(hasInitializer(ignoringParenImpCasts(
2616 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002617}
2618
2619TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2620 // This test verifies that expressions that do not have parentheses or
2621 // implicit casts still match.
2622 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002623 varDecl(hasInitializer(ignoringParenImpCasts(
2624 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002625 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002626 varDecl(hasInitializer(ignoringParenImpCasts(
2627 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002628}
2629
2630TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2631 // These tests verify that ignoringParenImpCasts does not match if
2632 // the inner matcher does not match.
2633 // This test creates an implicit cast.
2634 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002635 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002636 unless(anything()))))));
2637 // These tests verify that ignoringParenAndImplictCasts does not look
2638 // through explicit casts.
2639 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002640 varDecl(hasInitializer(ignoringParenImpCasts(
2641 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002642 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002643 varDecl(hasInitializer(ignoringParenImpCasts(
2644 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002645 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002646 varDecl(hasInitializer(ignoringParenImpCasts(
2647 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002648}
2649
Manuel Klimek715c9562012-07-25 10:02:02 +00002650TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002651 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2652 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002653 implicitCastExpr(
2654 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002655}
2656
Manuel Klimek715c9562012-07-25 10:02:02 +00002657TEST(HasSourceExpression, MatchesExplicitCasts) {
2658 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002659 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002660 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002661 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002662}
2663
Manuel Klimek4da21662012-07-06 05:48:52 +00002664TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002665 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002666}
2667
2668TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002669 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002670}
2671
2672TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002673 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002674}
2675
2676TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002677 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002678}
2679
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002680TEST(InitListExpression, MatchesInitListExpression) {
2681 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2682 initListExpr(hasType(asString("int [2]")))));
2683 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002684 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002685}
2686
2687TEST(UsingDeclaration, MatchesUsingDeclarations) {
2688 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2689 usingDecl()));
2690}
2691
2692TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2693 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2694 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2695}
2696
2697TEST(UsingDeclaration, MatchesSpecificTarget) {
2698 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2699 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002700 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002701 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2702 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002703 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002704}
2705
2706TEST(UsingDeclaration, ThroughUsingDeclaration) {
2707 EXPECT_TRUE(matches(
2708 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002709 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002710 EXPECT_TRUE(notMatches(
2711 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002712 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002713}
2714
Sam Panzer425f41b2012-08-16 17:20:59 +00002715TEST(SingleDecl, IsSingleDecl) {
2716 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002718 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2719 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2720 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2721 SingleDeclStmt));
2722}
2723
2724TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002725 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002726
2727 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002728 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002729 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002730 declStmt(containsDeclaration(0, MatchesInit),
2731 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002732 unsigned WrongIndex = 42;
2733 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002734 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002735 MatchesInit))));
2736}
2737
2738TEST(DeclCount, DeclCountIsCorrect) {
2739 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002740 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002741 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002742 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002743 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002744 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002745}
2746
Manuel Klimek4da21662012-07-06 05:48:52 +00002747TEST(While, MatchesWhileLoops) {
2748 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2749 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2750 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2751}
2752
2753TEST(Do, MatchesDoLoops) {
2754 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2755 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2756}
2757
2758TEST(Do, DoesNotMatchWhileLoops) {
2759 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2760}
2761
2762TEST(SwitchCase, MatchesCase) {
2763 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2764 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2765 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2766 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2767}
2768
Daniel Jasperb54b7642012-09-20 14:12:57 +00002769TEST(SwitchCase, MatchesSwitch) {
2770 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2771 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2772 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2773 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2774}
2775
2776TEST(ExceptionHandling, SimpleCases) {
2777 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2778 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2779 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2780 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2781 throwExpr()));
2782 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2783 throwExpr()));
2784}
2785
Manuel Klimek4da21662012-07-06 05:48:52 +00002786TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2787 EXPECT_TRUE(notMatches(
2788 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002789 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002790 EXPECT_TRUE(notMatches(
2791 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002792 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002793}
2794
2795TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2796 EXPECT_TRUE(matches(
2797 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002798 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002799}
2800
2801TEST(ForEach, BindsOneNode) {
2802 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002803 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002804 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002805}
2806
2807TEST(ForEach, BindsMultipleNodes) {
2808 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002809 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002810 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002811}
2812
2813TEST(ForEach, BindsRecursiveCombinations) {
2814 EXPECT_TRUE(matchAndVerifyResultTrue(
2815 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002816 recordDecl(hasName("C"),
2817 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002818 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002819}
2820
2821TEST(ForEachDescendant, BindsOneNode) {
2822 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002823 recordDecl(hasName("C"),
2824 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002825 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002826}
2827
Daniel Jasper5f684e92012-11-16 18:39:22 +00002828TEST(ForEachDescendant, NestedForEachDescendant) {
2829 DeclarationMatcher m = recordDecl(
2830 isDefinition(), decl().bind("x"), hasName("C"));
2831 EXPECT_TRUE(matchAndVerifyResultTrue(
2832 "class A { class B { class C {}; }; };",
2833 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2834 new VerifyIdIsBoundTo<Decl>("x", "C")));
2835
2836 // FIXME: This is not really a useful matcher, but the result is still
2837 // surprising (currently binds "A").
2838 //EXPECT_TRUE(matchAndVerifyResultTrue(
2839 // "class A { class B { class C {}; }; };",
2840 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2841 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2842}
2843
Manuel Klimek4da21662012-07-06 05:48:52 +00002844TEST(ForEachDescendant, BindsMultipleNodes) {
2845 EXPECT_TRUE(matchAndVerifyResultTrue(
2846 "class C { class D { int x; int y; }; "
2847 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002848 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002849 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002850}
2851
2852TEST(ForEachDescendant, BindsRecursiveCombinations) {
2853 EXPECT_TRUE(matchAndVerifyResultTrue(
2854 "class C { class D { "
2855 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002856 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2857 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002858 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002859}
2860
Daniel Jasper11c98772012-11-11 22:14:55 +00002861TEST(ForEachDescendant, BindsCorrectNodes) {
2862 EXPECT_TRUE(matchAndVerifyResultTrue(
2863 "class C { void f(); int i; };",
2864 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2865 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2866 EXPECT_TRUE(matchAndVerifyResultTrue(
2867 "class C { void f() {} int i; };",
2868 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2869 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2870}
2871
Manuel Klimek152ea0e2013-02-04 10:59:20 +00002872TEST(FindAll, BindsNodeOnMatch) {
2873 EXPECT_TRUE(matchAndVerifyResultTrue(
2874 "class A {};",
2875 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
2876 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
2877}
2878
2879TEST(FindAll, BindsDescendantNodeOnMatch) {
2880 EXPECT_TRUE(matchAndVerifyResultTrue(
2881 "class A { int a; int b; };",
2882 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
2883 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2884}
2885
2886TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
2887 EXPECT_TRUE(matchAndVerifyResultTrue(
2888 "class A { int a; int b; };",
2889 recordDecl(hasName("::A"),
2890 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
2891 fieldDecl().bind("v"))))),
2892 new VerifyIdIsBoundTo<Decl>("v", 3)));
2893
2894 EXPECT_TRUE(matchAndVerifyResultTrue(
2895 "class A { class B {}; class C {}; };",
2896 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
2897 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
2898}
2899
Manuel Klimek73876732013-02-04 09:42:38 +00002900TEST(EachOf, TriggersForEachMatch) {
2901 EXPECT_TRUE(matchAndVerifyResultTrue(
2902 "class A { int a; int b; };",
2903 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2904 has(fieldDecl(hasName("b")).bind("v")))),
2905 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2906}
2907
2908TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
2909 EXPECT_TRUE(matchAndVerifyResultTrue(
2910 "class A { int a; int c; };",
2911 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2912 has(fieldDecl(hasName("b")).bind("v")))),
2913 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
2914 EXPECT_TRUE(matchAndVerifyResultTrue(
2915 "class A { int c; int b; };",
2916 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2917 has(fieldDecl(hasName("b")).bind("v")))),
2918 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
2919 EXPECT_TRUE(notMatches(
2920 "class A { int c; int d; };",
2921 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2922 has(fieldDecl(hasName("b")).bind("v"))))));
2923}
Manuel Klimek4da21662012-07-06 05:48:52 +00002924
2925TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2926 // Make sure that we can both match the class by name (::X) and by the type
2927 // the template was instantiated with (via a field).
2928
2929 EXPECT_TRUE(matches(
2930 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002931 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002932
2933 EXPECT_TRUE(matches(
2934 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002935 recordDecl(isTemplateInstantiation(), hasDescendant(
2936 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002937}
2938
2939TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2940 EXPECT_TRUE(matches(
2941 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002942 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002943 isTemplateInstantiation())));
2944}
2945
2946TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
2947 EXPECT_TRUE(matches(
2948 "template <typename T> class X { T t; }; class A {};"
2949 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002950 recordDecl(isTemplateInstantiation(), hasDescendant(
2951 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002952}
2953
2954TEST(IsTemplateInstantiation,
2955 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
2956 EXPECT_TRUE(matches(
2957 "template <typename T> class X {};"
2958 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002959 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002960}
2961
2962TEST(IsTemplateInstantiation,
2963 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
2964 EXPECT_TRUE(matches(
2965 "class A {};"
2966 "class X {"
2967 " template <typename U> class Y { U u; };"
2968 " Y<A> y;"
2969 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002970 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002971}
2972
2973TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
2974 // FIXME: Figure out whether this makes sense. It doesn't affect the
2975 // normal use case as long as the uppermost instantiation always is marked
2976 // as template instantiation, but it might be confusing as a predicate.
2977 EXPECT_TRUE(matches(
2978 "class A {};"
2979 "template <typename T> class X {"
2980 " template <typename U> class Y { U u; };"
2981 " Y<T> y;"
2982 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002983 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002984}
2985
2986TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
2987 EXPECT_TRUE(notMatches(
2988 "template <typename T> class X {}; class A {};"
2989 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002990 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002991}
2992
2993TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
2994 EXPECT_TRUE(notMatches(
2995 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002996 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002997}
2998
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00002999TEST(IsExplicitTemplateSpecialization,
3000 DoesNotMatchPrimaryTemplate) {
3001 EXPECT_TRUE(notMatches(
3002 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003003 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003004 EXPECT_TRUE(notMatches(
3005 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003006 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003007}
3008
3009TEST(IsExplicitTemplateSpecialization,
3010 DoesNotMatchExplicitTemplateInstantiations) {
3011 EXPECT_TRUE(notMatches(
3012 "template <typename T> class X {};"
3013 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003014 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003015 EXPECT_TRUE(notMatches(
3016 "template <typename T> void f(T t) {}"
3017 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003018 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003019}
3020
3021TEST(IsExplicitTemplateSpecialization,
3022 DoesNotMatchImplicitTemplateInstantiations) {
3023 EXPECT_TRUE(notMatches(
3024 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003025 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003026 EXPECT_TRUE(notMatches(
3027 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003028 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003029}
3030
3031TEST(IsExplicitTemplateSpecialization,
3032 MatchesExplicitTemplateSpecializations) {
3033 EXPECT_TRUE(matches(
3034 "template <typename T> class X {};"
3035 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003036 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003037 EXPECT_TRUE(matches(
3038 "template <typename T> void f(T t) {}"
3039 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003040 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003041}
3042
Manuel Klimek579b1202012-09-07 09:26:10 +00003043TEST(HasAncenstor, MatchesDeclarationAncestors) {
3044 EXPECT_TRUE(matches(
3045 "class A { class B { class C {}; }; };",
3046 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3047}
3048
3049TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3050 EXPECT_TRUE(notMatches(
3051 "class A { class B { class C {}; }; };",
3052 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3053}
3054
3055TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3056 EXPECT_TRUE(matches(
3057 "class A { class B { void f() { C c; } class C {}; }; };",
3058 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3059 hasAncestor(recordDecl(hasName("A"))))))));
3060}
3061
3062TEST(HasAncenstor, MatchesStatementAncestors) {
3063 EXPECT_TRUE(matches(
3064 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003065 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003066}
3067
3068TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3069 EXPECT_TRUE(matches(
3070 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003071 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003072}
3073
3074TEST(HasAncestor, BindsRecursiveCombinations) {
3075 EXPECT_TRUE(matchAndVerifyResultTrue(
3076 "class C { class D { class E { class F { int y; }; }; }; };",
3077 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003078 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003079}
3080
3081TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3082 EXPECT_TRUE(matchAndVerifyResultTrue(
3083 "class C { class D { class E { class F { int y; }; }; }; };",
3084 fieldDecl(hasAncestor(
3085 decl(
3086 hasDescendant(recordDecl(isDefinition(),
3087 hasAncestor(recordDecl())))
3088 ).bind("d")
3089 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003090 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003091}
3092
3093TEST(HasAncestor, MatchesInTemplateInstantiations) {
3094 EXPECT_TRUE(matches(
3095 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3096 "A<int>::B::C a;",
3097 fieldDecl(hasType(asString("int")),
3098 hasAncestor(recordDecl(hasName("A"))))));
3099}
3100
3101TEST(HasAncestor, MatchesInImplicitCode) {
3102 EXPECT_TRUE(matches(
3103 "struct X {}; struct A { A() {} X x; };",
3104 constructorDecl(
3105 hasAnyConstructorInitializer(withInitializer(expr(
3106 hasAncestor(recordDecl(hasName("A")))))))));
3107}
3108
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003109TEST(HasParent, MatchesOnlyParent) {
3110 EXPECT_TRUE(matches(
3111 "void f() { if (true) { int x = 42; } }",
3112 compoundStmt(hasParent(ifStmt()))));
3113 EXPECT_TRUE(notMatches(
3114 "void f() { for (;;) { int x = 42; } }",
3115 compoundStmt(hasParent(ifStmt()))));
3116 EXPECT_TRUE(notMatches(
3117 "void f() { if (true) for (;;) { int x = 42; } }",
3118 compoundStmt(hasParent(ifStmt()))));
3119}
3120
Manuel Klimek30ace372012-12-06 14:42:48 +00003121TEST(HasAncestor, MatchesAllAncestors) {
3122 EXPECT_TRUE(matches(
3123 "template <typename T> struct C { static void f() { 42; } };"
3124 "void t() { C<int>::f(); }",
3125 integerLiteral(
3126 equals(42),
3127 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3128 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3129}
3130
3131TEST(HasParent, MatchesAllParents) {
3132 EXPECT_TRUE(matches(
3133 "template <typename T> struct C { static void f() { 42; } };"
3134 "void t() { C<int>::f(); }",
3135 integerLiteral(
3136 equals(42),
3137 hasParent(compoundStmt(hasParent(functionDecl(
3138 hasParent(recordDecl(isTemplateInstantiation())))))))));
3139 EXPECT_TRUE(matches(
3140 "template <typename T> struct C { static void f() { 42; } };"
3141 "void t() { C<int>::f(); }",
3142 integerLiteral(
3143 equals(42),
3144 hasParent(compoundStmt(hasParent(functionDecl(
3145 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3146 EXPECT_TRUE(matches(
3147 "template <typename T> struct C { static void f() { 42; } };"
3148 "void t() { C<int>::f(); }",
3149 integerLiteral(equals(42),
3150 hasParent(compoundStmt(allOf(
3151 hasParent(functionDecl(
3152 hasParent(recordDecl(isTemplateInstantiation())))),
3153 hasParent(functionDecl(hasParent(recordDecl(
3154 unless(isTemplateInstantiation())))))))))));
3155}
3156
Daniel Jasperce620072012-10-17 08:52:59 +00003157TEST(TypeMatching, MatchesTypes) {
3158 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3159}
3160
3161TEST(TypeMatching, MatchesArrayTypes) {
3162 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3163 EXPECT_TRUE(matches("int a[42];", arrayType()));
3164 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3165
3166 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3167 arrayType(hasElementType(builtinType()))));
3168
3169 EXPECT_TRUE(matches(
3170 "int const a[] = { 2, 3 };",
3171 qualType(arrayType(hasElementType(builtinType())))));
3172 EXPECT_TRUE(matches(
3173 "int const a[] = { 2, 3 };",
3174 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3175 EXPECT_TRUE(matches(
3176 "typedef const int T; T x[] = { 1, 2 };",
3177 qualType(isConstQualified(), arrayType())));
3178
3179 EXPECT_TRUE(notMatches(
3180 "int a[] = { 2, 3 };",
3181 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3182 EXPECT_TRUE(notMatches(
3183 "int a[] = { 2, 3 };",
3184 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3185 EXPECT_TRUE(notMatches(
3186 "int const a[] = { 2, 3 };",
3187 qualType(arrayType(hasElementType(builtinType())),
3188 unless(isConstQualified()))));
3189
3190 EXPECT_TRUE(matches("int a[2];",
3191 constantArrayType(hasElementType(builtinType()))));
3192 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3193}
3194
3195TEST(TypeMatching, MatchesComplexTypes) {
3196 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3197 EXPECT_TRUE(matches(
3198 "_Complex float f;",
3199 complexType(hasElementType(builtinType()))));
3200 EXPECT_TRUE(notMatches(
3201 "_Complex float f;",
3202 complexType(hasElementType(isInteger()))));
3203}
3204
3205TEST(TypeMatching, MatchesConstantArrayTypes) {
3206 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3207 EXPECT_TRUE(notMatches(
3208 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3209 constantArrayType(hasElementType(builtinType()))));
3210
3211 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3212 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3213 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3214}
3215
3216TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3217 EXPECT_TRUE(matches(
3218 "template <typename T, int Size> class array { T data[Size]; };",
3219 dependentSizedArrayType()));
3220 EXPECT_TRUE(notMatches(
3221 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3222 dependentSizedArrayType()));
3223}
3224
3225TEST(TypeMatching, MatchesIncompleteArrayType) {
3226 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3227 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3228
3229 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3230 incompleteArrayType()));
3231}
3232
3233TEST(TypeMatching, MatchesVariableArrayType) {
3234 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3235 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3236
3237 EXPECT_TRUE(matches(
3238 "void f(int b) { int a[b]; }",
3239 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3240 varDecl(hasName("b")))))))));
3241}
3242
3243TEST(TypeMatching, MatchesAtomicTypes) {
3244 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3245
3246 EXPECT_TRUE(matches("_Atomic(int) i;",
3247 atomicType(hasValueType(isInteger()))));
3248 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3249 atomicType(hasValueType(isInteger()))));
3250}
3251
3252TEST(TypeMatching, MatchesAutoTypes) {
3253 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3254 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3255 autoType()));
3256
3257 EXPECT_TRUE(matches("auto a = 1;",
3258 autoType(hasDeducedType(isInteger()))));
3259 EXPECT_TRUE(notMatches("auto b = 2.0;",
3260 autoType(hasDeducedType(isInteger()))));
3261}
3262
Daniel Jaspera267cf62012-10-29 10:14:44 +00003263TEST(TypeMatching, MatchesFunctionTypes) {
3264 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3265 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3266}
3267
Daniel Jasperce620072012-10-17 08:52:59 +00003268TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003269 // FIXME: Reactive when these tests can be more specific (not matching
3270 // implicit code on certain platforms), likely when we have hasDescendant for
3271 // Types/TypeLocs.
3272 //EXPECT_TRUE(matchAndVerifyResultTrue(
3273 // "int* a;",
3274 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3275 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3276 //EXPECT_TRUE(matchAndVerifyResultTrue(
3277 // "int* a;",
3278 // pointerTypeLoc().bind("loc"),
3279 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003280 EXPECT_TRUE(matches(
3281 "int** a;",
3282 pointerTypeLoc(pointeeLoc(loc(qualType())))));
3283 EXPECT_TRUE(matches(
3284 "int** a;",
3285 loc(pointerType(pointee(pointerType())))));
3286 EXPECT_TRUE(matches(
3287 "int* b; int* * const a = &b;",
3288 loc(qualType(isConstQualified(), pointerType()))));
3289
3290 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003291 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3292 hasType(blockPointerType()))));
3293 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3294 hasType(memberPointerType()))));
3295 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3296 hasType(pointerType()))));
3297 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3298 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003299
Daniel Jasper1802daf2012-10-17 13:35:36 +00003300 Fragment = "int *ptr;";
3301 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3302 hasType(blockPointerType()))));
3303 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3304 hasType(memberPointerType()))));
3305 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3306 hasType(pointerType()))));
3307 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3308 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003309
Daniel Jasper1802daf2012-10-17 13:35:36 +00003310 Fragment = "int a; int &ref = a;";
3311 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3312 hasType(blockPointerType()))));
3313 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3314 hasType(memberPointerType()))));
3315 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3316 hasType(pointerType()))));
3317 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3318 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003319}
3320
3321TEST(TypeMatching, PointeeTypes) {
3322 EXPECT_TRUE(matches("int b; int &a = b;",
3323 referenceType(pointee(builtinType()))));
3324 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3325
3326 EXPECT_TRUE(matches("int *a;",
3327 pointerTypeLoc(pointeeLoc(loc(builtinType())))));
3328
3329 EXPECT_TRUE(matches(
3330 "int const *A;",
3331 pointerType(pointee(isConstQualified(), builtinType()))));
3332 EXPECT_TRUE(notMatches(
3333 "int *A;",
3334 pointerType(pointee(isConstQualified(), builtinType()))));
3335}
3336
3337TEST(TypeMatching, MatchesPointersToConstTypes) {
3338 EXPECT_TRUE(matches("int b; int * const a = &b;",
3339 loc(pointerType())));
3340 EXPECT_TRUE(matches("int b; int * const a = &b;",
3341 pointerTypeLoc()));
3342 EXPECT_TRUE(matches(
3343 "int b; const int * a = &b;",
3344 pointerTypeLoc(pointeeLoc(builtinTypeLoc()))));
3345 EXPECT_TRUE(matches(
3346 "int b; const int * a = &b;",
3347 pointerType(pointee(builtinType()))));
3348}
3349
3350TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003351 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3352 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003353
Daniel Jasper1802daf2012-10-17 13:35:36 +00003354 EXPECT_TRUE(matches("typedef int X; X a;",
3355 varDecl(hasName("a"),
3356 hasType(typedefType(hasDecl(decl()))))));
Daniel Jasperce620072012-10-17 08:52:59 +00003357}
3358
Daniel Jaspera7564432012-09-13 13:11:25 +00003359TEST(NNS, MatchesNestedNameSpecifiers) {
3360 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3361 nestedNameSpecifier()));
3362 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3363 nestedNameSpecifier()));
3364 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3365 nestedNameSpecifier()));
3366
3367 EXPECT_TRUE(matches(
3368 "struct A { static void f() {} }; void g() { A::f(); }",
3369 nestedNameSpecifier()));
3370 EXPECT_TRUE(notMatches(
3371 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3372 nestedNameSpecifier()));
3373}
3374
Daniel Jasperb54b7642012-09-20 14:12:57 +00003375TEST(NullStatement, SimpleCases) {
3376 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3377 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3378}
3379
Daniel Jaspera7564432012-09-13 13:11:25 +00003380TEST(NNS, MatchesTypes) {
3381 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3382 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3383 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3384 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3385 Matcher));
3386 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3387}
3388
3389TEST(NNS, MatchesNamespaceDecls) {
3390 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3391 specifiesNamespace(hasName("ns")));
3392 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3393 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3394 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3395}
3396
3397TEST(NNS, BindsNestedNameSpecifiers) {
3398 EXPECT_TRUE(matchAndVerifyResultTrue(
3399 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3400 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3401 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3402}
3403
3404TEST(NNS, BindsNestedNameSpecifierLocs) {
3405 EXPECT_TRUE(matchAndVerifyResultTrue(
3406 "namespace ns { struct B {}; } ns::B b;",
3407 loc(nestedNameSpecifier()).bind("loc"),
3408 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3409}
3410
3411TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3412 EXPECT_TRUE(matches(
3413 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3414 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3415 EXPECT_TRUE(matches(
3416 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003417 nestedNameSpecifierLoc(hasPrefix(
3418 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003419}
3420
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003421TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3422 std::string Fragment =
3423 "namespace a { struct A { struct B { struct C {}; }; }; };"
3424 "void f() { a::A::B::C c; }";
3425 EXPECT_TRUE(matches(
3426 Fragment,
3427 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3428 hasDescendant(nestedNameSpecifier(
3429 specifiesNamespace(hasName("a")))))));
3430 EXPECT_TRUE(notMatches(
3431 Fragment,
3432 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3433 has(nestedNameSpecifier(
3434 specifiesNamespace(hasName("a")))))));
3435 EXPECT_TRUE(matches(
3436 Fragment,
3437 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3438 has(nestedNameSpecifier(
3439 specifiesNamespace(hasName("a")))))));
3440
3441 // Not really useful because a NestedNameSpecifier can af at most one child,
3442 // but to complete the interface.
3443 EXPECT_TRUE(matchAndVerifyResultTrue(
3444 Fragment,
3445 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3446 forEach(nestedNameSpecifier().bind("x"))),
3447 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3448}
3449
3450TEST(NNS, NestedNameSpecifiersAsDescendants) {
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(nestedNameSpecifier(specifiesType(
3457 asString("struct a::A")))))));
3458 EXPECT_TRUE(matchAndVerifyResultTrue(
3459 Fragment,
3460 functionDecl(hasName("f"),
3461 forEachDescendant(nestedNameSpecifier().bind("x"))),
3462 // Nested names: a, a::A and a::A::B.
3463 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3464}
3465
3466TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3467 std::string Fragment =
3468 "namespace a { struct A { struct B { struct C {}; }; }; };"
3469 "void f() { a::A::B::C c; }";
3470 EXPECT_TRUE(matches(
3471 Fragment,
3472 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3473 hasDescendant(loc(nestedNameSpecifier(
3474 specifiesNamespace(hasName("a"))))))));
3475 EXPECT_TRUE(notMatches(
3476 Fragment,
3477 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3478 has(loc(nestedNameSpecifier(
3479 specifiesNamespace(hasName("a"))))))));
3480 EXPECT_TRUE(matches(
3481 Fragment,
3482 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3483 has(loc(nestedNameSpecifier(
3484 specifiesNamespace(hasName("a"))))))));
3485
3486 EXPECT_TRUE(matchAndVerifyResultTrue(
3487 Fragment,
3488 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3489 forEach(nestedNameSpecifierLoc().bind("x"))),
3490 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3491}
3492
3493TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3494 std::string Fragment =
3495 "namespace a { struct A { struct B { struct C {}; }; }; };"
3496 "void f() { a::A::B::C c; }";
3497 EXPECT_TRUE(matches(
3498 Fragment,
3499 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3500 asString("struct a::A"))))))));
3501 EXPECT_TRUE(matchAndVerifyResultTrue(
3502 Fragment,
3503 functionDecl(hasName("f"),
3504 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3505 // Nested names: a, a::A and a::A::B.
3506 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3507}
3508
Manuel Klimek60969f52013-02-01 13:41:35 +00003509template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003510public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003511 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3512 StringRef InnerId)
3513 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003514 }
3515
Manuel Klimek60969f52013-02-01 13:41:35 +00003516 virtual bool run(const BoundNodes *Nodes) { return false; }
3517
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003518 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3519 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003520 return selectFirst<const T>(InnerId,
3521 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003522 }
3523private:
3524 std::string Id;
3525 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003526 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003527};
3528
3529TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003530 EXPECT_TRUE(matchAndVerifyResultTrue(
3531 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3532 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003533 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
3534 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003535 EXPECT_TRUE(matchAndVerifyResultFalse(
3536 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3537 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003538 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
3539 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003540}
3541
3542TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003543 EXPECT_TRUE(matchAndVerifyResultTrue(
3544 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003545 new VerifyMatchOnNode<clang::Stmt>(
3546 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003547 EXPECT_TRUE(matchAndVerifyResultFalse(
3548 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003549 new VerifyMatchOnNode<clang::Stmt>(
3550 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003551}
3552
3553TEST(MatchFinder, CanMatchSingleNodesRecursively) {
3554 EXPECT_TRUE(matchAndVerifyResultTrue(
3555 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3556 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003557 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003558 EXPECT_TRUE(matchAndVerifyResultFalse(
3559 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3560 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003561 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003562}
3563
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00003564template <typename T>
3565class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
3566public:
3567 virtual bool run(const BoundNodes *Nodes) { return false; }
3568
3569 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3570 const T *Node = Nodes->getNodeAs<T>("");
3571 return verify(*Nodes, *Context, Node);
3572 }
3573
3574 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
3575 return selectFirst<const T>(
3576 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
3577 *Node, Context)) != NULL;
3578 }
3579 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
3580 return selectFirst<const T>(
3581 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
3582 *Node, Context)) != NULL;
3583 }
3584};
3585
3586TEST(IsEqualTo, MatchesNodesByIdentity) {
3587 EXPECT_TRUE(matchAndVerifyResultTrue(
3588 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
3589 new VerifyAncestorHasChildIsEqual<Decl>()));
3590 EXPECT_TRUE(
3591 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
3592 new VerifyAncestorHasChildIsEqual<Stmt>()));
3593}
3594
Manuel Klimeke5793282012-11-02 01:31:03 +00003595class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3596public:
3597 VerifyStartOfTranslationUnit() : Called(false) {}
3598 virtual void run(const MatchFinder::MatchResult &Result) {
3599 EXPECT_TRUE(Called);
3600 }
3601 virtual void onStartOfTranslationUnit() {
3602 Called = true;
3603 }
3604 bool Called;
3605};
3606
3607TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3608 MatchFinder Finder;
3609 VerifyStartOfTranslationUnit VerifyCallback;
3610 Finder.addMatcher(decl(), &VerifyCallback);
3611 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3612 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3613 EXPECT_TRUE(VerifyCallback.Called);
3614}
3615
Manuel Klimek4da21662012-07-06 05:48:52 +00003616} // end namespace ast_matchers
3617} // end namespace clang