blob: 565c356235fc6e58d6ef191e2e4e4a8dbecd693b [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
Edwin Vane742d9e72013-02-25 20:43:32 +0000326TEST(DeclarationMatcher, hasDeclContext) {
327 EXPECT_TRUE(matches(
328 "namespace N {"
329 " namespace M {"
330 " class D {};"
331 " }"
332 "}",
333 recordDecl(hasDeclContext(namedDecl(hasName("M"))))));
334 EXPECT_TRUE(notMatches(
335 "namespace N {"
336 " namespace M {"
337 " class D {};"
338 " }"
339 "}",
340 recordDecl(hasDeclContext(namedDecl(hasName("N"))))));
341}
342
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000343TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000344 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000345 EXPECT_TRUE(notMatches("class X;", ClassX));
346 EXPECT_TRUE(notMatches("class X {};", ClassX));
347}
348
349TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000350 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000351 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
352 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
353}
354
355TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
356 EXPECT_TRUE(notMatches("template<typename T> class X { };"
357 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000358 classTemplateDecl(hasName("X"),
359 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000360}
361
362TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
363 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
364 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000365 classTemplateDecl(hasName("X"),
366 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000367}
368
Daniel Jasper6a124492012-07-12 08:50:38 +0000369TEST(AllOf, AllOverloadsWork) {
370 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000371 "struct T { };"
372 "int f(int, T*, int, int);"
373 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000374 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000375 callExpr(allOf(callee(functionDecl(hasName("f"))),
376 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000377 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000378 callExpr(allOf(callee(functionDecl(hasName("f"))),
379 hasArgument(0, declRefExpr(to(varDecl()))),
380 hasArgument(1, hasType(pointsTo(
381 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000382 EXPECT_TRUE(matches(Program,
383 callExpr(allOf(callee(functionDecl(hasName("f"))),
384 hasArgument(0, declRefExpr(to(varDecl()))),
385 hasArgument(1, hasType(pointsTo(
386 recordDecl(hasName("T"))))),
387 hasArgument(2, integerLiteral(equals(3)))))));
388 EXPECT_TRUE(matches(Program,
389 callExpr(allOf(callee(functionDecl(hasName("f"))),
390 hasArgument(0, declRefExpr(to(varDecl()))),
391 hasArgument(1, hasType(pointsTo(
392 recordDecl(hasName("T"))))),
393 hasArgument(2, integerLiteral(equals(3))),
394 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000395}
396
Manuel Klimek4da21662012-07-06 05:48:52 +0000397TEST(DeclarationMatcher, MatchAnyOf) {
398 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000399 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000400 EXPECT_TRUE(
401 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
402 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
403 EXPECT_TRUE(
404 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
405 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
406
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000407 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000408 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000409 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
410 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
411
Manuel Klimek4da21662012-07-06 05:48:52 +0000412 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000413 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
414 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000415 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
416 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
417 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
418 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
419 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
420 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
421}
422
423TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000424 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000425 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
426 EXPECT_TRUE(matches("class X {};", HasClassX));
427
428 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000429 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000430 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
431 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
432 EXPECT_TRUE(
433 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
434}
435
436TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
437 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000438 recordDecl(
439 has(recordDecl(
440 has(recordDecl(hasName("X"))),
441 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000442 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000443 has(recordDecl(
444 has(recordDecl(hasName("A"))),
445 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000446 hasName("C"))),
447 hasName("F"));
448
449 EXPECT_TRUE(matches(
450 "class F {"
451 " class Z {"
452 " class X {};"
453 " class Y {};"
454 " };"
455 " class C {"
456 " class A {};"
457 " class B {};"
458 " };"
459 "};", Recursive));
460
461 EXPECT_TRUE(matches(
462 "class F {"
463 " class Z {"
464 " class A {};"
465 " class X {};"
466 " class Y {};"
467 " };"
468 " class C {"
469 " class X {};"
470 " class A {};"
471 " class B {};"
472 " };"
473 "};", Recursive));
474
475 EXPECT_TRUE(matches(
476 "class O1 {"
477 " class O2 {"
478 " class F {"
479 " class Z {"
480 " class A {};"
481 " class X {};"
482 " class Y {};"
483 " };"
484 " class C {"
485 " class X {};"
486 " class A {};"
487 " class B {};"
488 " };"
489 " };"
490 " };"
491 "};", Recursive));
492}
493
494TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
495 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000496 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000497 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000498 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000499 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000500 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000501 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000502 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000503 hasName("Y"))),
504 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000505 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000506 anyOf(
507 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000508 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000509 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000510 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000511 hasName("B")))))),
512 hasName("F")));
513
514 EXPECT_TRUE(matches("class F {};", Recursive));
515 EXPECT_TRUE(matches("class Z {};", Recursive));
516 EXPECT_TRUE(matches("class C {};", Recursive));
517 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
518 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
519 EXPECT_TRUE(
520 matches("class O1 { class O2 {"
521 " class M { class N { class B {}; }; }; "
522 "}; };", Recursive));
523}
524
525TEST(DeclarationMatcher, MatchNot) {
526 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000527 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000528 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000529 unless(hasName("X")));
530 EXPECT_TRUE(notMatches("", NotClassX));
531 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
532 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
533 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
534 EXPECT_TRUE(
535 notMatches("class Y {}; class Z {}; class X : public Y {};",
536 NotClassX));
537
538 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000539 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000540 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000541 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000542 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000543 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000544 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
545 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
546 ClassXHasNotClassY));
547}
548
549TEST(DeclarationMatcher, HasDescendant) {
550 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000551 recordDecl(
552 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000553 hasName("Z"));
554 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
555 EXPECT_TRUE(
556 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
557 EXPECT_TRUE(
558 matches("class Z { class A { class Y { class X {}; }; }; };",
559 ZDescendantClassX));
560 EXPECT_TRUE(
561 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
562 ZDescendantClassX));
563 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
564
565 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000566 recordDecl(
567 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000568 hasName("X"))),
569 hasName("Z"));
570 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
571 ZDescendantClassXHasClassY));
572 EXPECT_TRUE(
573 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
574 ZDescendantClassXHasClassY));
575 EXPECT_TRUE(notMatches(
576 "class Z {"
577 " class A {"
578 " class B {"
579 " class X {"
580 " class C {"
581 " class Y {};"
582 " };"
583 " };"
584 " }; "
585 " };"
586 "};", ZDescendantClassXHasClassY));
587
588 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000589 recordDecl(
590 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
591 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000592 hasName("Z"));
593 EXPECT_TRUE(
594 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
595 ZDescendantClassXDescendantClassY));
596 EXPECT_TRUE(matches(
597 "class Z {"
598 " class A {"
599 " class X {"
600 " class B {"
601 " class Y {};"
602 " };"
603 " class Y {};"
604 " };"
605 " };"
606 "};", ZDescendantClassXDescendantClassY));
607}
608
Daniel Jaspera267cf62012-10-29 10:14:44 +0000609// Implements a run method that returns whether BoundNodes contains a
610// Decl bound to Id that can be dynamically cast to T.
611// Optionally checks that the check succeeded a specific number of times.
612template <typename T>
613class VerifyIdIsBoundTo : public BoundNodesCallback {
614public:
615 // Create an object that checks that a node of type \c T was bound to \c Id.
616 // Does not check for a certain number of matches.
617 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
618 : Id(Id), ExpectedCount(-1), Count(0) {}
619
620 // Create an object that checks that a node of type \c T was bound to \c Id.
621 // Checks that there were exactly \c ExpectedCount matches.
622 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
623 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
624
625 // Create an object that checks that a node of type \c T was bound to \c Id.
626 // Checks that there was exactly one match with the name \c ExpectedName.
627 // Note that \c T must be a NamedDecl for this to work.
628 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
629 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
630
631 ~VerifyIdIsBoundTo() {
632 if (ExpectedCount != -1)
633 EXPECT_EQ(ExpectedCount, Count);
634 if (!ExpectedName.empty())
635 EXPECT_EQ(ExpectedName, Name);
636 }
637
638 virtual bool run(const BoundNodes *Nodes) {
639 if (Nodes->getNodeAs<T>(Id)) {
640 ++Count;
641 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
642 Name = Named->getNameAsString();
643 } else if (const NestedNameSpecifier *NNS =
644 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
645 llvm::raw_string_ostream OS(Name);
646 NNS->print(OS, PrintingPolicy(LangOptions()));
647 }
648 return true;
649 }
650 return false;
651 }
652
Daniel Jasper452abbc2012-10-29 10:48:25 +0000653 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
654 return run(Nodes);
655 }
656
Daniel Jaspera267cf62012-10-29 10:14:44 +0000657private:
658 const std::string Id;
659 const int ExpectedCount;
660 int Count;
661 const std::string ExpectedName;
662 std::string Name;
663};
664
665TEST(HasDescendant, MatchesDescendantTypes) {
666 EXPECT_TRUE(matches("void f() { int i = 3; }",
667 decl(hasDescendant(loc(builtinType())))));
668 EXPECT_TRUE(matches("void f() { int i = 3; }",
669 stmt(hasDescendant(builtinType()))));
670
671 EXPECT_TRUE(matches("void f() { int i = 3; }",
672 stmt(hasDescendant(loc(builtinType())))));
673 EXPECT_TRUE(matches("void f() { int i = 3; }",
674 stmt(hasDescendant(qualType(builtinType())))));
675
676 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
677 stmt(hasDescendant(isInteger()))));
678
679 EXPECT_TRUE(matchAndVerifyResultTrue(
680 "void f() { int a; float c; int d; int e; }",
681 functionDecl(forEachDescendant(
682 varDecl(hasDescendant(isInteger())).bind("x"))),
683 new VerifyIdIsBoundTo<Decl>("x", 3)));
684}
685
686TEST(HasDescendant, MatchesDescendantsOfTypes) {
687 EXPECT_TRUE(matches("void f() { int*** i; }",
688 qualType(hasDescendant(builtinType()))));
689 EXPECT_TRUE(matches("void f() { int*** i; }",
690 qualType(hasDescendant(
691 pointerType(pointee(builtinType()))))));
692 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000693 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000694
695 EXPECT_TRUE(matchAndVerifyResultTrue(
696 "void f() { int*** i; }",
697 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
698 new VerifyIdIsBoundTo<Type>("x", 2)));
699}
700
701TEST(Has, MatchesChildrenOfTypes) {
702 EXPECT_TRUE(matches("int i;",
703 varDecl(hasName("i"), has(isInteger()))));
704 EXPECT_TRUE(notMatches("int** i;",
705 varDecl(hasName("i"), has(isInteger()))));
706 EXPECT_TRUE(matchAndVerifyResultTrue(
707 "int (*f)(float, int);",
708 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
709 new VerifyIdIsBoundTo<QualType>("x", 2)));
710}
711
712TEST(Has, MatchesChildTypes) {
713 EXPECT_TRUE(matches(
714 "int* i;",
715 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
716 EXPECT_TRUE(notMatches(
717 "int* i;",
718 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
719}
720
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000721TEST(Enum, DoesNotMatchClasses) {
722 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
723}
724
725TEST(Enum, MatchesEnums) {
726 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
727}
728
729TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000730 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000731 EXPECT_TRUE(matches("enum X{ A };", Matcher));
732 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
733 EXPECT_TRUE(notMatches("enum X {};", Matcher));
734}
735
Manuel Klimek4da21662012-07-06 05:48:52 +0000736TEST(StatementMatcher, Has) {
737 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000738 expr(hasType(pointsTo(recordDecl(hasName("X")))),
739 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000740
741 EXPECT_TRUE(matches(
742 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
743 EXPECT_TRUE(notMatches(
744 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
745}
746
747TEST(StatementMatcher, HasDescendant) {
748 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000749 expr(hasType(pointsTo(recordDecl(hasName("X")))),
750 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000751
752 EXPECT_TRUE(matches(
753 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
754 HasDescendantVariableI));
755 EXPECT_TRUE(notMatches(
756 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
757 HasDescendantVariableI));
758}
759
760TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000761 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000762
763 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
764 EXPECT_TRUE(notMatches("class A {};", TypeA));
765
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000766 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000767
768 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
769 TypeDerivedFromA));
770 EXPECT_TRUE(notMatches("class A {};", TypeA));
771
772 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000773 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000774
775 EXPECT_TRUE(
776 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
777}
778
Manuel Klimek4da21662012-07-06 05:48:52 +0000779TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000780 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000781
782 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000783 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000784
785 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000786 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000787
788 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000790
791 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
792 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000793 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000794
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000795 StatementMatcher MethodX =
796 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000797
798 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
799 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000800 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000801}
802
803TEST(Matcher, BindTheSameNameInAlternatives) {
804 StatementMatcher matcher = anyOf(
805 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000806 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000807 hasRHS(integerLiteral(equals(0)))),
808 binaryOperator(hasOperatorName("+"),
809 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000810 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000811
812 EXPECT_TRUE(matchAndVerifyResultTrue(
813 // The first branch of the matcher binds x to 0 but then fails.
814 // The second branch binds x to f() and succeeds.
815 "int f() { return 0 + f(); }",
816 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000817 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000818}
819
Manuel Klimek66341c52012-08-30 19:41:06 +0000820TEST(Matcher, BindsIDForMemoizedResults) {
821 // Using the same matcher in two match expressions will make memoization
822 // kick in.
823 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
824 EXPECT_TRUE(matchAndVerifyResultTrue(
825 "class A { class B { class X {}; }; };",
826 DeclarationMatcher(anyOf(
827 recordDecl(hasName("A"), hasDescendant(ClassX)),
828 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000829 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000830}
831
Daniel Jasper189f2e42012-12-03 15:43:25 +0000832TEST(HasDeclaration, HasDeclarationOfEnumType) {
833 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
834 expr(hasType(pointsTo(
835 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
836}
837
Edwin Vaneb45083d2013-02-25 14:32:42 +0000838TEST(HasDeclaration, HasGetDeclTraitTest) {
839 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
840 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
841 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
842}
843
Edwin Vane52380602013-02-19 17:14:34 +0000844TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
845 EXPECT_TRUE(matches("typedef int X; X a;",
846 varDecl(hasName("a"),
847 hasType(typedefType(hasDeclaration(decl()))))));
848
849 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
850}
851
Edwin Vane3abf7782013-02-25 14:49:29 +0000852TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
853 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
854 varDecl(hasType(templateSpecializationType(
855 hasDeclaration(namedDecl(hasName("A"))))))));
856}
857
Manuel Klimek4da21662012-07-06 05:48:52 +0000858TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000859 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000860 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000861 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000862 EXPECT_TRUE(
863 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000864 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000865 EXPECT_TRUE(
866 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000867 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000868}
869
870TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000871 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000872 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000873 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000874 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000875 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000876 EXPECT_TRUE(
877 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000878 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000879}
880
881TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000882 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000883 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000884 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000885 EXPECT_TRUE(
886 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000887 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000888}
889
890TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000891 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000892 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000893 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000894 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000895 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000896}
897
898TEST(Matcher, Call) {
899 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000900 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000901 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000902
903 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
904 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
905
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000906 StatementMatcher MethodOnY =
907 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000908
909 EXPECT_TRUE(
910 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
911 MethodOnY));
912 EXPECT_TRUE(
913 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
914 MethodOnY));
915 EXPECT_TRUE(
916 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
917 MethodOnY));
918 EXPECT_TRUE(
919 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
920 MethodOnY));
921 EXPECT_TRUE(
922 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
923 MethodOnY));
924
925 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000926 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000927
928 EXPECT_TRUE(
929 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
930 MethodOnYPointer));
931 EXPECT_TRUE(
932 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
933 MethodOnYPointer));
934 EXPECT_TRUE(
935 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
936 MethodOnYPointer));
937 EXPECT_TRUE(
938 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
939 MethodOnYPointer));
940 EXPECT_TRUE(
941 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
942 MethodOnYPointer));
943}
944
Daniel Jasper31f7c082012-10-01 13:40:41 +0000945TEST(Matcher, Lambda) {
946 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
947 lambdaExpr()));
948}
949
950TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000951 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
952 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000953 forRangeStmt()));
954 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
955 forRangeStmt()));
956}
957
958TEST(Matcher, UserDefinedLiteral) {
959 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
960 " return i + 1;"
961 "}"
962 "char c = 'a'_inc;",
963 userDefinedLiteral()));
964}
965
Daniel Jasperb54b7642012-09-20 14:12:57 +0000966TEST(Matcher, FlowControl) {
967 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
968 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
969 continueStmt()));
970 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
971 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
972 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
973}
974
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000975TEST(HasType, MatchesAsString) {
976 EXPECT_TRUE(
977 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000978 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000979 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000980 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000981 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000982 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000983 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000984 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000985}
986
Manuel Klimek4da21662012-07-06 05:48:52 +0000987TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000988 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000989 // Unary operator
990 EXPECT_TRUE(matches("class Y { }; "
991 "bool operator!(Y x) { return false; }; "
992 "Y y; bool c = !y;", OpCall));
993 // No match -- special operators like "new", "delete"
994 // FIXME: operator new takes size_t, for which we need stddef.h, for which
995 // we need to figure out include paths in the test.
996 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
997 // "class Y { }; "
998 // "void *operator new(size_t size) { return 0; } "
999 // "Y *y = new Y;", OpCall));
1000 EXPECT_TRUE(notMatches("class Y { }; "
1001 "void operator delete(void *p) { } "
1002 "void a() {Y *y = new Y; delete y;}", OpCall));
1003 // Binary operator
1004 EXPECT_TRUE(matches("class Y { }; "
1005 "bool operator&&(Y x, Y y) { return true; }; "
1006 "Y a; Y b; bool c = a && b;",
1007 OpCall));
1008 // No match -- normal operator, not an overloaded one.
1009 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1010 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1011}
1012
1013TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1014 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001015 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001016 EXPECT_TRUE(matches("class Y { }; "
1017 "bool operator&&(Y x, Y y) { return true; }; "
1018 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1019 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001020 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001021 EXPECT_TRUE(notMatches("class Y { }; "
1022 "bool operator&&(Y x, Y y) { return true; }; "
1023 "Y a; Y b; bool c = a && b;",
1024 OpCallLessLess));
1025}
1026
Daniel Jasper278057f2012-11-15 03:29:05 +00001027TEST(Matcher, NestedOverloadedOperatorCalls) {
1028 EXPECT_TRUE(matchAndVerifyResultTrue(
1029 "class Y { }; "
1030 "Y& operator&&(Y& x, Y& y) { return x; }; "
1031 "Y a; Y b; Y c; Y d = a && b && c;",
1032 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1033 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1034 EXPECT_TRUE(matches(
1035 "class Y { }; "
1036 "Y& operator&&(Y& x, Y& y) { return x; }; "
1037 "Y a; Y b; Y c; Y d = a && b && c;",
1038 operatorCallExpr(hasParent(operatorCallExpr()))));
1039 EXPECT_TRUE(matches(
1040 "class Y { }; "
1041 "Y& operator&&(Y& x, Y& y) { return x; }; "
1042 "Y a; Y b; Y c; Y d = a && b && c;",
1043 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1044}
1045
Manuel Klimek4da21662012-07-06 05:48:52 +00001046TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001047 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001048 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001049
1050 EXPECT_TRUE(
1051 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1052 MethodOnY));
1053 EXPECT_TRUE(
1054 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1055 MethodOnY));
1056 EXPECT_TRUE(
1057 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1058 MethodOnY));
1059 EXPECT_TRUE(
1060 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1061 MethodOnY));
1062 EXPECT_TRUE(
1063 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1064 MethodOnY));
1065
1066 EXPECT_TRUE(matches(
1067 "class Y {"
1068 " public: virtual void x();"
1069 "};"
1070 "class X : public Y {"
1071 " public: virtual void x();"
1072 "};"
1073 "void z() { X *x; x->Y::x(); }", MethodOnY));
1074}
1075
1076TEST(Matcher, VariableUsage) {
1077 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001078 declRefExpr(to(
1079 varDecl(hasInitializer(
1080 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001081
1082 EXPECT_TRUE(matches(
1083 "class Y {"
1084 " public:"
1085 " bool x() const;"
1086 "};"
1087 "void z(const Y &y) {"
1088 " bool b = y.x();"
1089 " if (b) {}"
1090 "}", Reference));
1091
1092 EXPECT_TRUE(notMatches(
1093 "class Y {"
1094 " public:"
1095 " bool x() const;"
1096 "};"
1097 "void z(const Y &y) {"
1098 " bool b = y.x();"
1099 "}", Reference));
1100}
1101
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001102TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001103 EXPECT_TRUE(matches(
1104 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001105 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001106}
1107
Manuel Klimek4da21662012-07-06 05:48:52 +00001108TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001109 StatementMatcher CallOnVariableY =
1110 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001111
1112 EXPECT_TRUE(matches(
1113 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1114 EXPECT_TRUE(matches(
1115 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1116 EXPECT_TRUE(matches(
1117 "class Y { public: void x(); };"
1118 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1119 EXPECT_TRUE(matches(
1120 "class Y { public: void x(); };"
1121 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1122 EXPECT_TRUE(notMatches(
1123 "class Y { public: void x(); };"
1124 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1125 CallOnVariableY));
1126}
1127
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001128TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1129 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1130 unaryExprOrTypeTraitExpr()));
1131 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1132 alignOfExpr(anything())));
1133 // FIXME: Uncomment once alignof is enabled.
1134 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1135 // unaryExprOrTypeTraitExpr()));
1136 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1137 // sizeOfExpr()));
1138}
1139
1140TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1141 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1142 hasArgumentOfType(asString("int")))));
1143 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1144 hasArgumentOfType(asString("float")))));
1145 EXPECT_TRUE(matches(
1146 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001147 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001148 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001149 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001150}
1151
Manuel Klimek4da21662012-07-06 05:48:52 +00001152TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001153 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001154}
1155
1156TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001157 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001158}
1159
1160TEST(MemberExpression, MatchesVariable) {
1161 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001162 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001163 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001164 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001165 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001166 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001167}
1168
1169TEST(MemberExpression, MatchesStaticVariable) {
1170 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001171 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001172 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001173 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001174 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001175 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001176}
1177
Daniel Jasper6a124492012-07-12 08:50:38 +00001178TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001179 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1180 EXPECT_TRUE(matches(
1181 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1182 callExpr(hasArgument(0, declRefExpr(
1183 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001184}
1185
1186TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001187 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001188 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001189 callExpr(hasArgument(0, declRefExpr(
1190 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001191}
1192
Manuel Klimek4da21662012-07-06 05:48:52 +00001193TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1194 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001195 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001196 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001197 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001198 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001199 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001200}
1201
1202TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1203 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001204 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001205 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001206 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001207 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001208 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001209}
1210
1211TEST(IsArrow, MatchesMemberCallsViaArrow) {
1212 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001213 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001214 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001215 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001216 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001217 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001218}
1219
1220TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001221 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001222
1223 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1224 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1225}
1226
1227TEST(Callee, MatchesMemberExpressions) {
1228 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001229 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001230 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001231 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001232}
1233
1234TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001235 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001236
1237 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1238 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1239
Manuel Klimeke265c872012-07-10 14:21:30 +00001240#if !defined(_MSC_VER)
1241 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001242 // Dependent contexts, but a non-dependent call.
1243 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1244 CallFunctionF));
1245 EXPECT_TRUE(
1246 matches("void f(); template <int N> struct S { void g() { f(); } };",
1247 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001248#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001249
1250 // Depedent calls don't match.
1251 EXPECT_TRUE(
1252 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1253 CallFunctionF));
1254 EXPECT_TRUE(
1255 notMatches("void f(int);"
1256 "template <typename T> struct S { void g(T t) { f(t); } };",
1257 CallFunctionF));
1258}
1259
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001260TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1261 EXPECT_TRUE(
1262 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001263 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001264}
1265
1266TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1267 EXPECT_TRUE(
1268 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001269 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001270}
1271
1272TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1273 EXPECT_TRUE(
1274 notMatches("void g(); template <typename T> void f(T t) {}"
1275 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001276 functionTemplateDecl(hasName("f"),
1277 hasDescendant(declRefExpr(to(
1278 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001279}
1280
Manuel Klimek4da21662012-07-06 05:48:52 +00001281TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001282 StatementMatcher CallArgumentY = callExpr(
1283 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001284
1285 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1286 EXPECT_TRUE(
1287 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1288 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1289
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001290 StatementMatcher WrongIndex = callExpr(
1291 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001292 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1293}
1294
1295TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001296 StatementMatcher CallArgumentY = callExpr(
1297 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001298 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1299 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1300 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1301}
1302
1303TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001304 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001305
1306 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1307 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1308 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1309}
1310
Daniel Jasper36e29d62012-12-04 11:54:27 +00001311TEST(Matcher, ParameterCount) {
1312 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1313 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1314 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1315 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1316 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1317}
1318
Manuel Klimek4da21662012-07-06 05:48:52 +00001319TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001320 DeclarationMatcher ReferenceClassX = varDecl(
1321 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001322 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1323 ReferenceClassX));
1324 EXPECT_TRUE(
1325 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1326 EXPECT_TRUE(
1327 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1328 EXPECT_TRUE(
1329 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1330}
1331
1332TEST(HasParameter, CallsInnerMatcher) {
1333 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001334 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001335 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001336 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001337}
1338
1339TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1340 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001341 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001342}
1343
1344TEST(HasType, MatchesParameterVariableTypesStrictly) {
1345 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001346 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001347 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001348 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001349 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001350 methodDecl(hasParameter(0,
1351 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001352 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001353 methodDecl(hasParameter(0,
1354 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001355}
1356
1357TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1358 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001359 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001360 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001361 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001362}
1363
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001364TEST(Returns, MatchesReturnTypes) {
1365 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001366 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001367 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001368 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001369 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001370 functionDecl(returns(hasDeclaration(
1371 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001372}
1373
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001374TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001375 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1376 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1377 functionDecl(isExternC())));
1378 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001379}
1380
Manuel Klimek4da21662012-07-06 05:48:52 +00001381TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1382 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001383 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001384}
1385
1386TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1387 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001388 methodDecl(hasAnyParameter(hasType(pointsTo(
1389 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001390}
1391
1392TEST(HasName, MatchesParameterVariableDeclartions) {
1393 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001394 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001395 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001396 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001397}
1398
Daniel Jasper371f9392012-08-01 08:40:24 +00001399TEST(Matcher, MatchesClassTemplateSpecialization) {
1400 EXPECT_TRUE(matches("template<typename T> struct A {};"
1401 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001402 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001403 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001404 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001405 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001406 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001407}
1408
1409TEST(Matcher, MatchesTypeTemplateArgument) {
1410 EXPECT_TRUE(matches(
1411 "template<typename T> struct B {};"
1412 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001414 asString("int"))))));
1415}
1416
1417TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1418 EXPECT_TRUE(matches(
1419 "struct B { int next; };"
1420 "template<int(B::*next_ptr)> struct A {};"
1421 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001422 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1423 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001424
1425 EXPECT_TRUE(notMatches(
1426 "template <typename T> struct A {};"
1427 "A<int> a;",
1428 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1429 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001430}
1431
1432TEST(Matcher, MatchesSpecificArgument) {
1433 EXPECT_TRUE(matches(
1434 "template<typename T, typename U> class A {};"
1435 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001436 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001437 1, refersToType(asString("int"))))));
1438 EXPECT_TRUE(notMatches(
1439 "template<typename T, typename U> class A {};"
1440 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001441 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001442 1, refersToType(asString("int"))))));
1443}
1444
Daniel Jasperf3197e92013-02-25 12:02:08 +00001445TEST(Matcher, MatchesAccessSpecDecls) {
1446 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1447 EXPECT_TRUE(
1448 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1449 EXPECT_TRUE(
1450 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1451 EXPECT_TRUE(
1452 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1453
1454 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1455}
1456
Manuel Klimek4da21662012-07-06 05:48:52 +00001457TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001458 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001459
1460 EXPECT_TRUE(
1461 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1462 EXPECT_TRUE(
1463 matches("class X { public: X(); }; void x() { X x = X(); }",
1464 Constructor));
1465 EXPECT_TRUE(
1466 matches("class X { public: X(int); }; void x() { X x = 0; }",
1467 Constructor));
1468 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1469}
1470
1471TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001472 StatementMatcher Constructor = constructExpr(
1473 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001474
1475 EXPECT_TRUE(
1476 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1477 Constructor));
1478 EXPECT_TRUE(
1479 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1480 Constructor));
1481 EXPECT_TRUE(
1482 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1483 Constructor));
1484 EXPECT_TRUE(
1485 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1486 Constructor));
1487
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001488 StatementMatcher WrongIndex = constructExpr(
1489 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001490 EXPECT_TRUE(
1491 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1492 WrongIndex));
1493}
1494
1495TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001496 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001497
1498 EXPECT_TRUE(
1499 matches("class X { public: X(int); }; void x() { X x(0); }",
1500 Constructor1Arg));
1501 EXPECT_TRUE(
1502 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1503 Constructor1Arg));
1504 EXPECT_TRUE(
1505 matches("class X { public: X(int); }; void x() { X x = 0; }",
1506 Constructor1Arg));
1507 EXPECT_TRUE(
1508 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1509 Constructor1Arg));
1510}
1511
Manuel Klimek70b9db92012-10-23 10:40:50 +00001512TEST(Matcher,ThisExpr) {
1513 EXPECT_TRUE(
1514 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1515 EXPECT_TRUE(
1516 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1517}
1518
Manuel Klimek4da21662012-07-06 05:48:52 +00001519TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001520 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001521
1522 std::string ClassString = "class string { public: string(); ~string(); }; ";
1523
1524 EXPECT_TRUE(
1525 matches(ClassString +
1526 "string GetStringByValue();"
1527 "void FunctionTakesString(string s);"
1528 "void run() { FunctionTakesString(GetStringByValue()); }",
1529 TempExpression));
1530
1531 EXPECT_TRUE(
1532 notMatches(ClassString +
1533 "string* GetStringPointer(); "
1534 "void FunctionTakesStringPtr(string* s);"
1535 "void run() {"
1536 " string* s = GetStringPointer();"
1537 " FunctionTakesStringPtr(GetStringPointer());"
1538 " FunctionTakesStringPtr(s);"
1539 "}",
1540 TempExpression));
1541
1542 EXPECT_TRUE(
1543 notMatches("class no_dtor {};"
1544 "no_dtor GetObjByValue();"
1545 "void ConsumeObj(no_dtor param);"
1546 "void run() { ConsumeObj(GetObjByValue()); }",
1547 TempExpression));
1548}
1549
Sam Panzere16acd32012-08-24 22:04:44 +00001550TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1551 std::string ClassString =
1552 "class string { public: string(); int length(); }; ";
1553
1554 EXPECT_TRUE(
1555 matches(ClassString +
1556 "string GetStringByValue();"
1557 "void FunctionTakesString(string s);"
1558 "void run() { FunctionTakesString(GetStringByValue()); }",
1559 materializeTemporaryExpr()));
1560
1561 EXPECT_TRUE(
1562 notMatches(ClassString +
1563 "string* GetStringPointer(); "
1564 "void FunctionTakesStringPtr(string* s);"
1565 "void run() {"
1566 " string* s = GetStringPointer();"
1567 " FunctionTakesStringPtr(GetStringPointer());"
1568 " FunctionTakesStringPtr(s);"
1569 "}",
1570 materializeTemporaryExpr()));
1571
1572 EXPECT_TRUE(
1573 notMatches(ClassString +
1574 "string GetStringByValue();"
1575 "void run() { int k = GetStringByValue().length(); }",
1576 materializeTemporaryExpr()));
1577
1578 EXPECT_TRUE(
1579 notMatches(ClassString +
1580 "string GetStringByValue();"
1581 "void run() { GetStringByValue(); }",
1582 materializeTemporaryExpr()));
1583}
1584
Manuel Klimek4da21662012-07-06 05:48:52 +00001585TEST(ConstructorDeclaration, SimpleCase) {
1586 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001587 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001588 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001589 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001590}
1591
1592TEST(ConstructorDeclaration, IsImplicit) {
1593 // This one doesn't match because the constructor is not added by the
1594 // compiler (it is not needed).
1595 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001596 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001597 // The compiler added the implicit default constructor.
1598 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001599 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001600 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001601 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001602}
1603
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001604TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1605 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001606 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001607}
1608
1609TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001610 EXPECT_TRUE(notMatches("class Foo {};",
1611 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001612}
1613
Manuel Klimek4da21662012-07-06 05:48:52 +00001614TEST(HasAnyConstructorInitializer, SimpleCase) {
1615 EXPECT_TRUE(notMatches(
1616 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001617 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001618 EXPECT_TRUE(matches(
1619 "class Foo {"
1620 " Foo() : foo_() { }"
1621 " int foo_;"
1622 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001623 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001624}
1625
1626TEST(HasAnyConstructorInitializer, ForField) {
1627 static const char Code[] =
1628 "class Baz { };"
1629 "class Foo {"
1630 " Foo() : foo_() { }"
1631 " Baz foo_;"
1632 " Baz bar_;"
1633 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001634 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1635 forField(hasType(recordDecl(hasName("Baz"))))))));
1636 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001637 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001638 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1639 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001640}
1641
1642TEST(HasAnyConstructorInitializer, WithInitializer) {
1643 static const char Code[] =
1644 "class Foo {"
1645 " Foo() : foo_(0) { }"
1646 " int foo_;"
1647 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001648 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001649 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001650 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001651 withInitializer(integerLiteral(equals(1)))))));
1652}
1653
1654TEST(HasAnyConstructorInitializer, IsWritten) {
1655 static const char Code[] =
1656 "struct Bar { Bar(){} };"
1657 "class Foo {"
1658 " Foo() : foo_() { }"
1659 " Bar foo_;"
1660 " Bar bar_;"
1661 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001662 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001663 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001664 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001665 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001666 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001667 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1668}
1669
1670TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001671 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001672
1673 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1674 EXPECT_TRUE(
1675 matches("class X { public: X(); }; void x() { new X(); }", New));
1676 EXPECT_TRUE(
1677 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1678 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1679}
1680
1681TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001682 StatementMatcher New = constructExpr(
1683 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001684
1685 EXPECT_TRUE(
1686 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1687 New));
1688 EXPECT_TRUE(
1689 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1690 New));
1691 EXPECT_TRUE(
1692 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1693 New));
1694
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001695 StatementMatcher WrongIndex = constructExpr(
1696 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001697 EXPECT_TRUE(
1698 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1699 WrongIndex));
1700}
1701
1702TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001703 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001704
1705 EXPECT_TRUE(
1706 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1707 EXPECT_TRUE(
1708 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1709 New));
1710}
1711
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001712TEST(Matcher, DeleteExpression) {
1713 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001714 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001715}
1716
Manuel Klimek4da21662012-07-06 05:48:52 +00001717TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001718 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001719
1720 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1721 EXPECT_TRUE(
1722 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1723 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1724}
1725
1726TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001727 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001728 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1729 // wide string
1730 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1731 // with escaped characters
1732 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1733 // no matching -- though the data type is the same, there is no string literal
1734 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1735}
1736
1737TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001738 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001739 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1740 // wide character
1741 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1742 // wide character, Hex encoded, NOT MATCHED!
1743 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1744 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1745}
1746
1747TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001748 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001749 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1750 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1751 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1752 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1753
1754 // Non-matching cases (character literals, float and double)
1755 EXPECT_TRUE(notMatches("int i = L'a';",
1756 HasIntLiteral)); // this is actually a character
1757 // literal cast to int
1758 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1759 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1760 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1761}
1762
Daniel Jasper31f7c082012-10-01 13:40:41 +00001763TEST(Matcher, NullPtrLiteral) {
1764 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1765}
1766
Daniel Jasperb54b7642012-09-20 14:12:57 +00001767TEST(Matcher, AsmStatement) {
1768 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1769}
1770
Manuel Klimek4da21662012-07-06 05:48:52 +00001771TEST(Matcher, Conditions) {
1772 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1773
1774 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1775 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1776 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1777 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1778 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1779}
1780
1781TEST(MatchBinaryOperator, HasOperatorName) {
1782 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1783
1784 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1785 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1786}
1787
1788TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1789 StatementMatcher OperatorTrueFalse =
1790 binaryOperator(hasLHS(boolLiteral(equals(true))),
1791 hasRHS(boolLiteral(equals(false))));
1792
1793 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1794 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1795 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1796}
1797
1798TEST(MatchBinaryOperator, HasEitherOperand) {
1799 StatementMatcher HasOperand =
1800 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1801
1802 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1803 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1804 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1805}
1806
1807TEST(Matcher, BinaryOperatorTypes) {
1808 // Integration test that verifies the AST provides all binary operators in
1809 // a way we expect.
1810 // FIXME: Operator ','
1811 EXPECT_TRUE(
1812 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1813 EXPECT_TRUE(
1814 matches("bool b; bool c = (b = true);",
1815 binaryOperator(hasOperatorName("="))));
1816 EXPECT_TRUE(
1817 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1818 EXPECT_TRUE(
1819 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1820 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1821 EXPECT_TRUE(
1822 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1823 EXPECT_TRUE(
1824 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1825 EXPECT_TRUE(
1826 matches("int i = 1; int j = (i <<= 2);",
1827 binaryOperator(hasOperatorName("<<="))));
1828 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1829 EXPECT_TRUE(
1830 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1831 EXPECT_TRUE(
1832 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1833 EXPECT_TRUE(
1834 matches("int i = 1; int j = (i >>= 2);",
1835 binaryOperator(hasOperatorName(">>="))));
1836 EXPECT_TRUE(
1837 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1838 EXPECT_TRUE(
1839 matches("int i = 42; int j = (i ^= 42);",
1840 binaryOperator(hasOperatorName("^="))));
1841 EXPECT_TRUE(
1842 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1843 EXPECT_TRUE(
1844 matches("int i = 42; int j = (i %= 42);",
1845 binaryOperator(hasOperatorName("%="))));
1846 EXPECT_TRUE(
1847 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1848 EXPECT_TRUE(
1849 matches("bool b = true && false;",
1850 binaryOperator(hasOperatorName("&&"))));
1851 EXPECT_TRUE(
1852 matches("bool b = true; bool c = (b &= false);",
1853 binaryOperator(hasOperatorName("&="))));
1854 EXPECT_TRUE(
1855 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1856 EXPECT_TRUE(
1857 matches("bool b = true || false;",
1858 binaryOperator(hasOperatorName("||"))));
1859 EXPECT_TRUE(
1860 matches("bool b = true; bool c = (b |= false);",
1861 binaryOperator(hasOperatorName("|="))));
1862 EXPECT_TRUE(
1863 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1864 EXPECT_TRUE(
1865 matches("int i = 42; int j = (i *= 23);",
1866 binaryOperator(hasOperatorName("*="))));
1867 EXPECT_TRUE(
1868 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1869 EXPECT_TRUE(
1870 matches("int i = 42; int j = (i /= 23);",
1871 binaryOperator(hasOperatorName("/="))));
1872 EXPECT_TRUE(
1873 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1874 EXPECT_TRUE(
1875 matches("int i = 42; int j = (i += 23);",
1876 binaryOperator(hasOperatorName("+="))));
1877 EXPECT_TRUE(
1878 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1879 EXPECT_TRUE(
1880 matches("int i = 42; int j = (i -= 23);",
1881 binaryOperator(hasOperatorName("-="))));
1882 EXPECT_TRUE(
1883 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1884 binaryOperator(hasOperatorName("->*"))));
1885 EXPECT_TRUE(
1886 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1887 binaryOperator(hasOperatorName(".*"))));
1888
1889 // Member expressions as operators are not supported in matches.
1890 EXPECT_TRUE(
1891 notMatches("struct A { void x(A *a) { a->x(this); } };",
1892 binaryOperator(hasOperatorName("->"))));
1893
1894 // Initializer assignments are not represented as operator equals.
1895 EXPECT_TRUE(
1896 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1897
1898 // Array indexing is not represented as operator.
1899 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1900
1901 // Overloaded operators do not match at all.
1902 EXPECT_TRUE(notMatches(
1903 "struct A { bool operator&&(const A &a) const { return false; } };"
1904 "void x() { A a, b; a && b; }",
1905 binaryOperator()));
1906}
1907
1908TEST(MatchUnaryOperator, HasOperatorName) {
1909 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1910
1911 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1912 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1913}
1914
1915TEST(MatchUnaryOperator, HasUnaryOperand) {
1916 StatementMatcher OperatorOnFalse =
1917 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1918
1919 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1920 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1921}
1922
1923TEST(Matcher, UnaryOperatorTypes) {
1924 // Integration test that verifies the AST provides all unary operators in
1925 // a way we expect.
1926 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1927 EXPECT_TRUE(
1928 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1929 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1930 EXPECT_TRUE(
1931 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1932 EXPECT_TRUE(
1933 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1934 EXPECT_TRUE(
1935 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1936 EXPECT_TRUE(
1937 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1938 EXPECT_TRUE(
1939 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1940 EXPECT_TRUE(
1941 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1942 EXPECT_TRUE(
1943 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1944
1945 // We don't match conversion operators.
1946 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1947
1948 // Function calls are not represented as operator.
1949 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1950
1951 // Overloaded operators do not match at all.
1952 // FIXME: We probably want to add that.
1953 EXPECT_TRUE(notMatches(
1954 "struct A { bool operator!() const { return false; } };"
1955 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1956}
1957
1958TEST(Matcher, ConditionalOperator) {
1959 StatementMatcher Conditional = conditionalOperator(
1960 hasCondition(boolLiteral(equals(true))),
1961 hasTrueExpression(boolLiteral(equals(false))));
1962
1963 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1964 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1965 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1966
1967 StatementMatcher ConditionalFalse = conditionalOperator(
1968 hasFalseExpression(boolLiteral(equals(false))));
1969
1970 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1971 EXPECT_TRUE(
1972 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1973}
1974
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001975TEST(ArraySubscriptMatchers, ArraySubscripts) {
1976 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1977 arraySubscriptExpr()));
1978 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1979 arraySubscriptExpr()));
1980}
1981
1982TEST(ArraySubscriptMatchers, ArrayIndex) {
1983 EXPECT_TRUE(matches(
1984 "int i[2]; void f() { i[1] = 1; }",
1985 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1986 EXPECT_TRUE(matches(
1987 "int i[2]; void f() { 1[i] = 1; }",
1988 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1989 EXPECT_TRUE(notMatches(
1990 "int i[2]; void f() { i[1] = 1; }",
1991 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1992}
1993
1994TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1995 EXPECT_TRUE(matches(
1996 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001997 arraySubscriptExpr(hasBase(implicitCastExpr(
1998 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001999}
2000
Manuel Klimek4da21662012-07-06 05:48:52 +00002001TEST(Matcher, HasNameSupportsNamespaces) {
2002 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002003 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002004 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002005 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002006 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002007 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002008 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002009 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002010 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002011 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002012 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002013 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002014 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002015 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002016 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002017 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002018 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002019 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002020 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002021 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002022 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002023 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002024 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002025 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002026}
2027
2028TEST(Matcher, HasNameSupportsOuterClasses) {
2029 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002030 matches("class A { class B { class C; }; };",
2031 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002032 EXPECT_TRUE(
2033 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002034 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002035 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002036 matches("class A { class B { class C; }; };",
2037 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002038 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002039 matches("class A { class B { class C; }; };",
2040 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002041 EXPECT_TRUE(
2042 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002043 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002044 EXPECT_TRUE(
2045 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002046 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002047 EXPECT_TRUE(
2048 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002049 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002050 EXPECT_TRUE(
2051 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002052 recordDecl(hasName("::C"))));
2053 EXPECT_TRUE(
2054 notMatches("class A { class B { class C; }; };",
2055 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002056 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002057 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002058 EXPECT_TRUE(
2059 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002060 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002061}
2062
2063TEST(Matcher, IsDefinition) {
2064 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002065 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002066 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2067 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2068
2069 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002070 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002071 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2072 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2073
2074 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002075 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002076 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2077 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2078}
2079
2080TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002081 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002082 ofClass(hasName("X")))));
2083
2084 EXPECT_TRUE(
2085 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2086 EXPECT_TRUE(
2087 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2088 Constructor));
2089 EXPECT_TRUE(
2090 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2091 Constructor));
2092}
2093
2094TEST(Matcher, VisitsTemplateInstantiations) {
2095 EXPECT_TRUE(matches(
2096 "class A { public: void x(); };"
2097 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002098 "void f() { B<A> b; b.y(); }",
2099 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002100
2101 EXPECT_TRUE(matches(
2102 "class A { public: void x(); };"
2103 "class C {"
2104 " public:"
2105 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2106 "};"
2107 "void f() {"
2108 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002109 "}",
2110 recordDecl(hasName("C"),
2111 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002112}
2113
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002114TEST(Matcher, HandlesNullQualTypes) {
2115 // FIXME: Add a Type matcher so we can replace uses of this
2116 // variable with Type(True())
2117 const TypeMatcher AnyType = anything();
2118
2119 // We don't really care whether this matcher succeeds; we're testing that
2120 // it completes without crashing.
2121 EXPECT_TRUE(matches(
2122 "struct A { };"
2123 "template <typename T>"
2124 "void f(T t) {"
2125 " T local_t(t /* this becomes a null QualType in the AST */);"
2126 "}"
2127 "void g() {"
2128 " f(0);"
2129 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002130 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002131 anyOf(
2132 TypeMatcher(hasDeclaration(anything())),
2133 pointsTo(AnyType),
2134 references(AnyType)
2135 // Other QualType matchers should go here.
2136 ))))));
2137}
2138
Manuel Klimek4da21662012-07-06 05:48:52 +00002139// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002140AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002141 // Make sure all special variables are used: node, match_finder,
2142 // bound_nodes_builder, and the parameter named 'AMatcher'.
2143 return AMatcher.matches(Node, Finder, Builder);
2144}
2145
2146TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002147 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002148
2149 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002150 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002151
2152 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002153 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002154
2155 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002156 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002157}
2158
2159AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002160 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2161 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2162 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002163 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002164 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002165 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002166 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2167 ASTMatchFinder::BK_First);
2168}
2169
2170TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002171 DeclarationMatcher HasClassB =
2172 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002173
2174 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002175 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002176
2177 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002178 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002179
2180 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002181 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002182
2183 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002184 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002185
2186 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2187}
2188
2189TEST(For, FindsForLoops) {
2190 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2191 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002192 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2193 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002194 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195}
2196
Daniel Jasper6a124492012-07-12 08:50:38 +00002197TEST(For, ForLoopInternals) {
2198 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2199 forStmt(hasCondition(anything()))));
2200 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2201 forStmt(hasLoopInit(anything()))));
2202}
2203
2204TEST(For, NegativeForLoopInternals) {
2205 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002206 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002207 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2208 forStmt(hasLoopInit(anything()))));
2209}
2210
Manuel Klimek4da21662012-07-06 05:48:52 +00002211TEST(For, ReportsNoFalsePositives) {
2212 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2213 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2214}
2215
2216TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002217 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2218 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2219 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220}
2221
2222TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2223 // It's not a compound statement just because there's "{}" in the source
2224 // text. This is an AST search, not grep.
2225 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002226 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002227 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002228 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002229}
2230
Daniel Jasper6a124492012-07-12 08:50:38 +00002231TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002232 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002233 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002234 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002235 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002236 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002237 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002238 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002239 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002240}
2241
2242TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2243 // The simplest case: every compound statement is in a function
2244 // definition, and the function body itself must be a compound
2245 // statement.
2246 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002247 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002248}
2249
2250TEST(HasAnySubstatement, IsNotRecursive) {
2251 // It's really "has any immediate substatement".
2252 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002253 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002254}
2255
2256TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2257 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002258 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002259}
2260
2261TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2262 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002263 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002264}
2265
2266TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2267 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002268 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002269 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002270 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002271}
2272
2273TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2274 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002275 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002276 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002277 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002278 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002279 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002280}
2281
2282TEST(StatementCountIs, WorksWithMultipleStatements) {
2283 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002284 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002285}
2286
2287TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2288 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002289 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002290 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002291 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002292 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002293 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002294 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002295 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002296}
2297
2298TEST(Member, WorksInSimplestCase) {
2299 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002300 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002301}
2302
2303TEST(Member, DoesNotMatchTheBaseExpression) {
2304 // Don't pick out the wrong part of the member expression, this should
2305 // be checking the member (name) only.
2306 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002307 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002308}
2309
2310TEST(Member, MatchesInMemberFunctionCall) {
2311 EXPECT_TRUE(matches("void f() {"
2312 " struct { void first() {}; } s;"
2313 " s.first();"
2314 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002315 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002316}
2317
Daniel Jasperc711af22012-10-23 15:46:39 +00002318TEST(Member, MatchesMember) {
2319 EXPECT_TRUE(matches(
2320 "struct A { int i; }; void f() { A a; a.i = 2; }",
2321 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2322 EXPECT_TRUE(notMatches(
2323 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2324 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2325}
2326
Daniel Jasperf3197e92013-02-25 12:02:08 +00002327TEST(Member, UnderstandsAccess) {
2328 EXPECT_TRUE(matches(
2329 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2330 EXPECT_TRUE(notMatches(
2331 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2332 EXPECT_TRUE(notMatches(
2333 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2334
2335 EXPECT_TRUE(notMatches(
2336 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2337 EXPECT_TRUE(notMatches(
2338 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2339 EXPECT_TRUE(matches(
2340 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2341
2342 EXPECT_TRUE(notMatches(
2343 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2344 EXPECT_TRUE(matches("class A { protected: int i; };",
2345 fieldDecl(isProtected(), hasName("i"))));
2346 EXPECT_TRUE(notMatches(
2347 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2348
2349 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2350 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2351 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2352 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2353}
2354
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002355TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002356 // Fails in C++11 mode
2357 EXPECT_TRUE(matchesConditionally(
2358 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2359 "class X { void *operator new(std::size_t); };",
2360 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002361
2362 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002363 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002364
Daniel Jasper31f7c082012-10-01 13:40:41 +00002365 // Fails in C++11 mode
2366 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002367 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2368 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002369 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002370}
2371
Manuel Klimek4da21662012-07-06 05:48:52 +00002372TEST(HasObjectExpression, DoesNotMatchMember) {
2373 EXPECT_TRUE(notMatches(
2374 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002375 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002376}
2377
2378TEST(HasObjectExpression, MatchesBaseOfVariable) {
2379 EXPECT_TRUE(matches(
2380 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002381 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002382 EXPECT_TRUE(matches(
2383 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002384 memberExpr(hasObjectExpression(
2385 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002386}
2387
2388TEST(HasObjectExpression,
2389 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2390 EXPECT_TRUE(matches(
2391 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002392 memberExpr(hasObjectExpression(
2393 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002394 EXPECT_TRUE(matches(
2395 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002396 memberExpr(hasObjectExpression(
2397 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002398}
2399
2400TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002401 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2402 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2403 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2404 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002405}
2406
2407TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002408 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002409}
2410
2411TEST(IsConstQualified, MatchesConstInt) {
2412 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002413 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002414}
2415
2416TEST(IsConstQualified, MatchesConstPointer) {
2417 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002418 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002419}
2420
2421TEST(IsConstQualified, MatchesThroughTypedef) {
2422 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002423 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002424 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002425 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002426}
2427
2428TEST(IsConstQualified, DoesNotMatchInappropriately) {
2429 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002430 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002431 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002432 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002433}
2434
Sam Panzer089e5b32012-08-16 16:58:10 +00002435TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002436 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2437 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2438 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2439 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002440}
2441TEST(CastExpression, MatchesImplicitCasts) {
2442 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002443 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002444 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002445 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002446}
2447
2448TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002449 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2450 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2451 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2452 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002453}
2454
Manuel Klimek4da21662012-07-06 05:48:52 +00002455TEST(ReinterpretCast, MatchesSimpleCase) {
2456 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002457 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002458}
2459
2460TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002461 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002462 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002463 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002464 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002465 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002466 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2467 "B b;"
2468 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002469 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002470}
2471
2472TEST(FunctionalCast, MatchesSimpleCase) {
2473 std::string foo_class = "class Foo { public: Foo(char*); };";
2474 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002475 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002476}
2477
2478TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2479 std::string FooClass = "class Foo { public: Foo(char*); };";
2480 EXPECT_TRUE(
2481 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002482 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002483 EXPECT_TRUE(
2484 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002485 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002486}
2487
2488TEST(DynamicCast, MatchesSimpleCase) {
2489 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2490 "B b;"
2491 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002492 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002493}
2494
2495TEST(StaticCast, MatchesSimpleCase) {
2496 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002497 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002498}
2499
2500TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002501 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002502 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002503 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002504 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002505 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002506 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2507 "B b;"
2508 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002509 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002510}
2511
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002512TEST(CStyleCast, MatchesSimpleCase) {
2513 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2514}
2515
2516TEST(CStyleCast, DoesNotMatchOtherCasts) {
2517 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2518 "char q, *r = const_cast<char*>(&q);"
2519 "void* s = reinterpret_cast<char*>(&s);"
2520 "struct B { virtual ~B() {} }; struct D : B {};"
2521 "B b;"
2522 "D* t = dynamic_cast<D*>(&b);",
2523 cStyleCastExpr()));
2524}
2525
Manuel Klimek4da21662012-07-06 05:48:52 +00002526TEST(HasDestinationType, MatchesSimpleCase) {
2527 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002528 staticCastExpr(hasDestinationType(
2529 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002530}
2531
Sam Panzer089e5b32012-08-16 16:58:10 +00002532TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2533 // This test creates an implicit const cast.
2534 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002535 implicitCastExpr(
2536 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002537 // This test creates an implicit array-to-pointer cast.
2538 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002539 implicitCastExpr(hasImplicitDestinationType(
2540 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002541}
2542
2543TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2544 // This test creates an implicit cast from int to char.
2545 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002546 implicitCastExpr(hasImplicitDestinationType(
2547 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002548 // This test creates an implicit array-to-pointer cast.
2549 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002550 implicitCastExpr(hasImplicitDestinationType(
2551 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002552}
2553
2554TEST(ImplicitCast, MatchesSimpleCase) {
2555 // This test creates an implicit const cast.
2556 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002557 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002558 // This test creates an implicit cast from int to char.
2559 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002560 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002561 // This test creates an implicit array-to-pointer cast.
2562 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002563 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002564}
2565
2566TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002567 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002568 // are present, and that it ignores explicit and paren casts.
2569
2570 // These two test cases have no casts.
2571 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002572 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002573 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002574 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002575
2576 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002577 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002578 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002579 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002580
2581 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002582 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002583}
2584
2585TEST(IgnoringImpCasts, MatchesImpCasts) {
2586 // This test checks that ignoringImpCasts matches when implicit casts are
2587 // present and its inner matcher alone does not match.
2588 // Note that this test creates an implicit const cast.
2589 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002590 varDecl(hasInitializer(ignoringImpCasts(
2591 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002592 // This test creates an implict cast from int to char.
2593 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002594 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002595 integerLiteral(equals(0)))))));
2596}
2597
2598TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2599 // These tests verify that ignoringImpCasts does not match if the inner
2600 // matcher does not match.
2601 // Note that the first test creates an implicit const cast.
2602 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002603 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002604 unless(anything()))))));
2605 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002606 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002607 unless(anything()))))));
2608
2609 // These tests verify that ignoringImplictCasts does not look through explicit
2610 // casts or parentheses.
2611 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002612 varDecl(hasInitializer(ignoringImpCasts(
2613 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002614 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002615 varDecl(hasInitializer(ignoringImpCasts(
2616 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002617 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002618 varDecl(hasInitializer(ignoringImpCasts(
2619 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002620 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002621 varDecl(hasInitializer(ignoringImpCasts(
2622 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002623}
2624
2625TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2626 // This test verifies that expressions that do not have implicit casts
2627 // still match the inner matcher.
2628 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002629 varDecl(hasInitializer(ignoringImpCasts(
2630 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002631}
2632
2633TEST(IgnoringParenCasts, MatchesParenCasts) {
2634 // This test checks that ignoringParenCasts matches when parentheses and/or
2635 // casts are present and its inner matcher alone does not match.
2636 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002637 varDecl(hasInitializer(ignoringParenCasts(
2638 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002639 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002640 varDecl(hasInitializer(ignoringParenCasts(
2641 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002642
2643 // This test creates an implict cast from int to char in addition to the
2644 // parentheses.
2645 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002646 varDecl(hasInitializer(ignoringParenCasts(
2647 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002648
2649 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002650 varDecl(hasInitializer(ignoringParenCasts(
2651 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002652 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002653 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002654 integerLiteral(equals(0)))))));
2655}
2656
2657TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2658 // This test verifies that expressions that do not have any casts still match.
2659 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002660 varDecl(hasInitializer(ignoringParenCasts(
2661 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002662}
2663
2664TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2665 // These tests verify that ignoringImpCasts does not match if the inner
2666 // matcher does not match.
2667 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002668 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002669 unless(anything()))))));
2670
2671 // This test creates an implicit cast from int to char in addition to the
2672 // parentheses.
2673 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002674 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002675 unless(anything()))))));
2676
2677 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002678 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002679 unless(anything()))))));
2680}
2681
2682TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2683 // This test checks that ignoringParenAndImpCasts matches when
2684 // parentheses and/or implicit casts are present and its inner matcher alone
2685 // does not match.
2686 // Note that this test creates an implicit const cast.
2687 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002688 varDecl(hasInitializer(ignoringParenImpCasts(
2689 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002690 // This test creates an implicit cast from int to char.
2691 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002692 varDecl(hasInitializer(ignoringParenImpCasts(
2693 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002694}
2695
2696TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2697 // This test verifies that expressions that do not have parentheses or
2698 // implicit casts still match.
2699 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002700 varDecl(hasInitializer(ignoringParenImpCasts(
2701 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002702 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002703 varDecl(hasInitializer(ignoringParenImpCasts(
2704 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002705}
2706
2707TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2708 // These tests verify that ignoringParenImpCasts does not match if
2709 // the inner matcher does not match.
2710 // This test creates an implicit cast.
2711 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002712 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002713 unless(anything()))))));
2714 // These tests verify that ignoringParenAndImplictCasts does not look
2715 // through explicit casts.
2716 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 varDecl(hasInitializer(ignoringParenImpCasts(
2718 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002719 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002720 varDecl(hasInitializer(ignoringParenImpCasts(
2721 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002722 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002723 varDecl(hasInitializer(ignoringParenImpCasts(
2724 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002725}
2726
Manuel Klimek715c9562012-07-25 10:02:02 +00002727TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002728 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2729 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002730 implicitCastExpr(
2731 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002732}
2733
Manuel Klimek715c9562012-07-25 10:02:02 +00002734TEST(HasSourceExpression, MatchesExplicitCasts) {
2735 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002736 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002737 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002738 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002739}
2740
Manuel Klimek4da21662012-07-06 05:48:52 +00002741TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002742 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002743}
2744
2745TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002746 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002747}
2748
2749TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002750 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002751}
2752
2753TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002754 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002755}
2756
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002757TEST(InitListExpression, MatchesInitListExpression) {
2758 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2759 initListExpr(hasType(asString("int [2]")))));
2760 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002761 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002762}
2763
2764TEST(UsingDeclaration, MatchesUsingDeclarations) {
2765 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2766 usingDecl()));
2767}
2768
2769TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2770 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2771 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2772}
2773
2774TEST(UsingDeclaration, MatchesSpecificTarget) {
2775 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2776 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002777 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002778 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2779 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002780 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002781}
2782
2783TEST(UsingDeclaration, ThroughUsingDeclaration) {
2784 EXPECT_TRUE(matches(
2785 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002786 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002787 EXPECT_TRUE(notMatches(
2788 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002789 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002790}
2791
Sam Panzer425f41b2012-08-16 17:20:59 +00002792TEST(SingleDecl, IsSingleDecl) {
2793 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002794 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002795 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2796 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2797 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2798 SingleDeclStmt));
2799}
2800
2801TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002802 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002803
2804 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002805 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002806 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002807 declStmt(containsDeclaration(0, MatchesInit),
2808 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002809 unsigned WrongIndex = 42;
2810 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002811 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002812 MatchesInit))));
2813}
2814
2815TEST(DeclCount, DeclCountIsCorrect) {
2816 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002817 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002818 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002819 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002820 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002821 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002822}
2823
Manuel Klimek4da21662012-07-06 05:48:52 +00002824TEST(While, MatchesWhileLoops) {
2825 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2826 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2827 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2828}
2829
2830TEST(Do, MatchesDoLoops) {
2831 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2832 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2833}
2834
2835TEST(Do, DoesNotMatchWhileLoops) {
2836 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2837}
2838
2839TEST(SwitchCase, MatchesCase) {
2840 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2841 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2842 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2843 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2844}
2845
Daniel Jasperb54b7642012-09-20 14:12:57 +00002846TEST(SwitchCase, MatchesSwitch) {
2847 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2848 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2849 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2850 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2851}
2852
2853TEST(ExceptionHandling, SimpleCases) {
2854 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2855 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2856 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2857 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2858 throwExpr()));
2859 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2860 throwExpr()));
2861}
2862
Manuel Klimek4da21662012-07-06 05:48:52 +00002863TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2864 EXPECT_TRUE(notMatches(
2865 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002866 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002867 EXPECT_TRUE(notMatches(
2868 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002869 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002870}
2871
2872TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2873 EXPECT_TRUE(matches(
2874 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002875 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002876}
2877
2878TEST(ForEach, BindsOneNode) {
2879 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002880 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002881 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002882}
2883
2884TEST(ForEach, BindsMultipleNodes) {
2885 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002886 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002887 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002888}
2889
2890TEST(ForEach, BindsRecursiveCombinations) {
2891 EXPECT_TRUE(matchAndVerifyResultTrue(
2892 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002893 recordDecl(hasName("C"),
2894 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002895 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002896}
2897
2898TEST(ForEachDescendant, BindsOneNode) {
2899 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002900 recordDecl(hasName("C"),
2901 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002902 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002903}
2904
Daniel Jasper5f684e92012-11-16 18:39:22 +00002905TEST(ForEachDescendant, NestedForEachDescendant) {
2906 DeclarationMatcher m = recordDecl(
2907 isDefinition(), decl().bind("x"), hasName("C"));
2908 EXPECT_TRUE(matchAndVerifyResultTrue(
2909 "class A { class B { class C {}; }; };",
2910 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2911 new VerifyIdIsBoundTo<Decl>("x", "C")));
2912
2913 // FIXME: This is not really a useful matcher, but the result is still
2914 // surprising (currently binds "A").
2915 //EXPECT_TRUE(matchAndVerifyResultTrue(
2916 // "class A { class B { class C {}; }; };",
2917 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2918 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2919}
2920
Manuel Klimek4da21662012-07-06 05:48:52 +00002921TEST(ForEachDescendant, BindsMultipleNodes) {
2922 EXPECT_TRUE(matchAndVerifyResultTrue(
2923 "class C { class D { int x; int y; }; "
2924 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002925 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002926 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002927}
2928
2929TEST(ForEachDescendant, BindsRecursiveCombinations) {
2930 EXPECT_TRUE(matchAndVerifyResultTrue(
2931 "class C { class D { "
2932 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002933 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2934 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002935 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002936}
2937
Daniel Jasper11c98772012-11-11 22:14:55 +00002938TEST(ForEachDescendant, BindsCorrectNodes) {
2939 EXPECT_TRUE(matchAndVerifyResultTrue(
2940 "class C { void f(); int i; };",
2941 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2942 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2943 EXPECT_TRUE(matchAndVerifyResultTrue(
2944 "class C { void f() {} int i; };",
2945 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2946 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2947}
2948
Manuel Klimek152ea0e2013-02-04 10:59:20 +00002949TEST(FindAll, BindsNodeOnMatch) {
2950 EXPECT_TRUE(matchAndVerifyResultTrue(
2951 "class A {};",
2952 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
2953 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
2954}
2955
2956TEST(FindAll, BindsDescendantNodeOnMatch) {
2957 EXPECT_TRUE(matchAndVerifyResultTrue(
2958 "class A { int a; int b; };",
2959 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
2960 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2961}
2962
2963TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
2964 EXPECT_TRUE(matchAndVerifyResultTrue(
2965 "class A { int a; int b; };",
2966 recordDecl(hasName("::A"),
2967 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
2968 fieldDecl().bind("v"))))),
2969 new VerifyIdIsBoundTo<Decl>("v", 3)));
2970
2971 EXPECT_TRUE(matchAndVerifyResultTrue(
2972 "class A { class B {}; class C {}; };",
2973 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
2974 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
2975}
2976
Manuel Klimek73876732013-02-04 09:42:38 +00002977TEST(EachOf, TriggersForEachMatch) {
2978 EXPECT_TRUE(matchAndVerifyResultTrue(
2979 "class A { int a; int b; };",
2980 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2981 has(fieldDecl(hasName("b")).bind("v")))),
2982 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2983}
2984
2985TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
2986 EXPECT_TRUE(matchAndVerifyResultTrue(
2987 "class A { int a; int c; };",
2988 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2989 has(fieldDecl(hasName("b")).bind("v")))),
2990 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
2991 EXPECT_TRUE(matchAndVerifyResultTrue(
2992 "class A { int c; int b; };",
2993 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2994 has(fieldDecl(hasName("b")).bind("v")))),
2995 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
2996 EXPECT_TRUE(notMatches(
2997 "class A { int c; int d; };",
2998 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2999 has(fieldDecl(hasName("b")).bind("v"))))));
3000}
Manuel Klimek4da21662012-07-06 05:48:52 +00003001
3002TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3003 // Make sure that we can both match the class by name (::X) and by the type
3004 // the template was instantiated with (via a field).
3005
3006 EXPECT_TRUE(matches(
3007 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003008 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003009
3010 EXPECT_TRUE(matches(
3011 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003012 recordDecl(isTemplateInstantiation(), hasDescendant(
3013 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003014}
3015
3016TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3017 EXPECT_TRUE(matches(
3018 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003019 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003020 isTemplateInstantiation())));
3021}
3022
3023TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3024 EXPECT_TRUE(matches(
3025 "template <typename T> class X { T t; }; class A {};"
3026 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003027 recordDecl(isTemplateInstantiation(), hasDescendant(
3028 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003029}
3030
3031TEST(IsTemplateInstantiation,
3032 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3033 EXPECT_TRUE(matches(
3034 "template <typename T> class X {};"
3035 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003036 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003037}
3038
3039TEST(IsTemplateInstantiation,
3040 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3041 EXPECT_TRUE(matches(
3042 "class A {};"
3043 "class X {"
3044 " template <typename U> class Y { U u; };"
3045 " Y<A> y;"
3046 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003047 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003048}
3049
3050TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3051 // FIXME: Figure out whether this makes sense. It doesn't affect the
3052 // normal use case as long as the uppermost instantiation always is marked
3053 // as template instantiation, but it might be confusing as a predicate.
3054 EXPECT_TRUE(matches(
3055 "class A {};"
3056 "template <typename T> class X {"
3057 " template <typename U> class Y { U u; };"
3058 " Y<T> y;"
3059 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003060 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003061}
3062
3063TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3064 EXPECT_TRUE(notMatches(
3065 "template <typename T> class X {}; class A {};"
3066 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003067 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003068}
3069
3070TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3071 EXPECT_TRUE(notMatches(
3072 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003073 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003074}
3075
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003076TEST(IsExplicitTemplateSpecialization,
3077 DoesNotMatchPrimaryTemplate) {
3078 EXPECT_TRUE(notMatches(
3079 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003080 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003081 EXPECT_TRUE(notMatches(
3082 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003083 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003084}
3085
3086TEST(IsExplicitTemplateSpecialization,
3087 DoesNotMatchExplicitTemplateInstantiations) {
3088 EXPECT_TRUE(notMatches(
3089 "template <typename T> class X {};"
3090 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003091 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003092 EXPECT_TRUE(notMatches(
3093 "template <typename T> void f(T t) {}"
3094 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003095 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003096}
3097
3098TEST(IsExplicitTemplateSpecialization,
3099 DoesNotMatchImplicitTemplateInstantiations) {
3100 EXPECT_TRUE(notMatches(
3101 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003102 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003103 EXPECT_TRUE(notMatches(
3104 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003105 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003106}
3107
3108TEST(IsExplicitTemplateSpecialization,
3109 MatchesExplicitTemplateSpecializations) {
3110 EXPECT_TRUE(matches(
3111 "template <typename T> class X {};"
3112 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003113 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003114 EXPECT_TRUE(matches(
3115 "template <typename T> void f(T t) {}"
3116 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003117 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003118}
3119
Manuel Klimek579b1202012-09-07 09:26:10 +00003120TEST(HasAncenstor, MatchesDeclarationAncestors) {
3121 EXPECT_TRUE(matches(
3122 "class A { class B { class C {}; }; };",
3123 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3124}
3125
3126TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3127 EXPECT_TRUE(notMatches(
3128 "class A { class B { class C {}; }; };",
3129 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3130}
3131
3132TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3133 EXPECT_TRUE(matches(
3134 "class A { class B { void f() { C c; } class C {}; }; };",
3135 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3136 hasAncestor(recordDecl(hasName("A"))))))));
3137}
3138
3139TEST(HasAncenstor, MatchesStatementAncestors) {
3140 EXPECT_TRUE(matches(
3141 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003142 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003143}
3144
3145TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3146 EXPECT_TRUE(matches(
3147 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003148 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003149}
3150
3151TEST(HasAncestor, BindsRecursiveCombinations) {
3152 EXPECT_TRUE(matchAndVerifyResultTrue(
3153 "class C { class D { class E { class F { int y; }; }; }; };",
3154 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003155 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003156}
3157
3158TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3159 EXPECT_TRUE(matchAndVerifyResultTrue(
3160 "class C { class D { class E { class F { int y; }; }; }; };",
3161 fieldDecl(hasAncestor(
3162 decl(
3163 hasDescendant(recordDecl(isDefinition(),
3164 hasAncestor(recordDecl())))
3165 ).bind("d")
3166 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003167 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003168}
3169
3170TEST(HasAncestor, MatchesInTemplateInstantiations) {
3171 EXPECT_TRUE(matches(
3172 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3173 "A<int>::B::C a;",
3174 fieldDecl(hasType(asString("int")),
3175 hasAncestor(recordDecl(hasName("A"))))));
3176}
3177
3178TEST(HasAncestor, MatchesInImplicitCode) {
3179 EXPECT_TRUE(matches(
3180 "struct X {}; struct A { A() {} X x; };",
3181 constructorDecl(
3182 hasAnyConstructorInitializer(withInitializer(expr(
3183 hasAncestor(recordDecl(hasName("A")))))))));
3184}
3185
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003186TEST(HasParent, MatchesOnlyParent) {
3187 EXPECT_TRUE(matches(
3188 "void f() { if (true) { int x = 42; } }",
3189 compoundStmt(hasParent(ifStmt()))));
3190 EXPECT_TRUE(notMatches(
3191 "void f() { for (;;) { int x = 42; } }",
3192 compoundStmt(hasParent(ifStmt()))));
3193 EXPECT_TRUE(notMatches(
3194 "void f() { if (true) for (;;) { int x = 42; } }",
3195 compoundStmt(hasParent(ifStmt()))));
3196}
3197
Manuel Klimek30ace372012-12-06 14:42:48 +00003198TEST(HasAncestor, MatchesAllAncestors) {
3199 EXPECT_TRUE(matches(
3200 "template <typename T> struct C { static void f() { 42; } };"
3201 "void t() { C<int>::f(); }",
3202 integerLiteral(
3203 equals(42),
3204 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3205 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3206}
3207
3208TEST(HasParent, MatchesAllParents) {
3209 EXPECT_TRUE(matches(
3210 "template <typename T> struct C { static void f() { 42; } };"
3211 "void t() { C<int>::f(); }",
3212 integerLiteral(
3213 equals(42),
3214 hasParent(compoundStmt(hasParent(functionDecl(
3215 hasParent(recordDecl(isTemplateInstantiation())))))))));
3216 EXPECT_TRUE(matches(
3217 "template <typename T> struct C { static void f() { 42; } };"
3218 "void t() { C<int>::f(); }",
3219 integerLiteral(
3220 equals(42),
3221 hasParent(compoundStmt(hasParent(functionDecl(
3222 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3223 EXPECT_TRUE(matches(
3224 "template <typename T> struct C { static void f() { 42; } };"
3225 "void t() { C<int>::f(); }",
3226 integerLiteral(equals(42),
3227 hasParent(compoundStmt(allOf(
3228 hasParent(functionDecl(
3229 hasParent(recordDecl(isTemplateInstantiation())))),
3230 hasParent(functionDecl(hasParent(recordDecl(
3231 unless(isTemplateInstantiation())))))))))));
3232}
3233
Daniel Jasperce620072012-10-17 08:52:59 +00003234TEST(TypeMatching, MatchesTypes) {
3235 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3236}
3237
3238TEST(TypeMatching, MatchesArrayTypes) {
3239 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3240 EXPECT_TRUE(matches("int a[42];", arrayType()));
3241 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3242
3243 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3244 arrayType(hasElementType(builtinType()))));
3245
3246 EXPECT_TRUE(matches(
3247 "int const a[] = { 2, 3 };",
3248 qualType(arrayType(hasElementType(builtinType())))));
3249 EXPECT_TRUE(matches(
3250 "int const a[] = { 2, 3 };",
3251 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3252 EXPECT_TRUE(matches(
3253 "typedef const int T; T x[] = { 1, 2 };",
3254 qualType(isConstQualified(), arrayType())));
3255
3256 EXPECT_TRUE(notMatches(
3257 "int a[] = { 2, 3 };",
3258 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3259 EXPECT_TRUE(notMatches(
3260 "int a[] = { 2, 3 };",
3261 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3262 EXPECT_TRUE(notMatches(
3263 "int const a[] = { 2, 3 };",
3264 qualType(arrayType(hasElementType(builtinType())),
3265 unless(isConstQualified()))));
3266
3267 EXPECT_TRUE(matches("int a[2];",
3268 constantArrayType(hasElementType(builtinType()))));
3269 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3270}
3271
3272TEST(TypeMatching, MatchesComplexTypes) {
3273 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3274 EXPECT_TRUE(matches(
3275 "_Complex float f;",
3276 complexType(hasElementType(builtinType()))));
3277 EXPECT_TRUE(notMatches(
3278 "_Complex float f;",
3279 complexType(hasElementType(isInteger()))));
3280}
3281
3282TEST(TypeMatching, MatchesConstantArrayTypes) {
3283 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3284 EXPECT_TRUE(notMatches(
3285 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3286 constantArrayType(hasElementType(builtinType()))));
3287
3288 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3289 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3290 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3291}
3292
3293TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3294 EXPECT_TRUE(matches(
3295 "template <typename T, int Size> class array { T data[Size]; };",
3296 dependentSizedArrayType()));
3297 EXPECT_TRUE(notMatches(
3298 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3299 dependentSizedArrayType()));
3300}
3301
3302TEST(TypeMatching, MatchesIncompleteArrayType) {
3303 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3304 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3305
3306 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3307 incompleteArrayType()));
3308}
3309
3310TEST(TypeMatching, MatchesVariableArrayType) {
3311 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3312 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3313
3314 EXPECT_TRUE(matches(
3315 "void f(int b) { int a[b]; }",
3316 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3317 varDecl(hasName("b")))))))));
3318}
3319
3320TEST(TypeMatching, MatchesAtomicTypes) {
3321 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3322
3323 EXPECT_TRUE(matches("_Atomic(int) i;",
3324 atomicType(hasValueType(isInteger()))));
3325 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3326 atomicType(hasValueType(isInteger()))));
3327}
3328
3329TEST(TypeMatching, MatchesAutoTypes) {
3330 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3331 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3332 autoType()));
3333
3334 EXPECT_TRUE(matches("auto a = 1;",
3335 autoType(hasDeducedType(isInteger()))));
3336 EXPECT_TRUE(notMatches("auto b = 2.0;",
3337 autoType(hasDeducedType(isInteger()))));
3338}
3339
Daniel Jaspera267cf62012-10-29 10:14:44 +00003340TEST(TypeMatching, MatchesFunctionTypes) {
3341 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3342 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3343}
3344
Daniel Jasperce620072012-10-17 08:52:59 +00003345TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003346 // FIXME: Reactive when these tests can be more specific (not matching
3347 // implicit code on certain platforms), likely when we have hasDescendant for
3348 // Types/TypeLocs.
3349 //EXPECT_TRUE(matchAndVerifyResultTrue(
3350 // "int* a;",
3351 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3352 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3353 //EXPECT_TRUE(matchAndVerifyResultTrue(
3354 // "int* a;",
3355 // pointerTypeLoc().bind("loc"),
3356 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003357 EXPECT_TRUE(matches(
3358 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003359 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003360 EXPECT_TRUE(matches(
3361 "int** a;",
3362 loc(pointerType(pointee(pointerType())))));
3363 EXPECT_TRUE(matches(
3364 "int* b; int* * const a = &b;",
3365 loc(qualType(isConstQualified(), pointerType()))));
3366
3367 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003368 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3369 hasType(blockPointerType()))));
3370 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3371 hasType(memberPointerType()))));
3372 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3373 hasType(pointerType()))));
3374 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3375 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003376
Daniel Jasper1802daf2012-10-17 13:35:36 +00003377 Fragment = "int *ptr;";
3378 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3379 hasType(blockPointerType()))));
3380 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3381 hasType(memberPointerType()))));
3382 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3383 hasType(pointerType()))));
3384 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3385 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003386
Daniel Jasper1802daf2012-10-17 13:35:36 +00003387 Fragment = "int a; int &ref = a;";
3388 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3389 hasType(blockPointerType()))));
3390 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3391 hasType(memberPointerType()))));
3392 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3393 hasType(pointerType()))));
3394 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3395 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003396}
3397
3398TEST(TypeMatching, PointeeTypes) {
3399 EXPECT_TRUE(matches("int b; int &a = b;",
3400 referenceType(pointee(builtinType()))));
3401 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3402
3403 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003404 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003405
3406 EXPECT_TRUE(matches(
3407 "int const *A;",
3408 pointerType(pointee(isConstQualified(), builtinType()))));
3409 EXPECT_TRUE(notMatches(
3410 "int *A;",
3411 pointerType(pointee(isConstQualified(), builtinType()))));
3412}
3413
3414TEST(TypeMatching, MatchesPointersToConstTypes) {
3415 EXPECT_TRUE(matches("int b; int * const a = &b;",
3416 loc(pointerType())));
3417 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003418 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003419 EXPECT_TRUE(matches(
3420 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003421 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003422 EXPECT_TRUE(matches(
3423 "int b; const int * a = &b;",
3424 pointerType(pointee(builtinType()))));
3425}
3426
3427TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003428 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3429 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003430}
3431
Edwin Vane3abf7782013-02-25 14:49:29 +00003432TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003433 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003434 templateSpecializationType()));
3435}
3436
Edwin Vane742d9e72013-02-25 20:43:32 +00003437TEST(TypeMatching, MatchesRecordType) {
3438 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
3439 EXPECT_TRUE(matches("struct S{}; S s;", recordType()));
3440 EXPECT_TRUE(notMatches("int i;", recordType()));
3441}
3442
3443TEST(TypeMatching, MatchesElaboratedType) {
3444 EXPECT_TRUE(matches(
3445 "namespace N {"
3446 " namespace M {"
3447 " class D {};"
3448 " }"
3449 "}"
3450 "N::M::D d;", elaboratedType()));
3451 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3452 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3453}
3454
3455TEST(ElaboratedTypeNarrowing, hasQualifier) {
3456 EXPECT_TRUE(matches(
3457 "namespace N {"
3458 " namespace M {"
3459 " class D {};"
3460 " }"
3461 "}"
3462 "N::M::D d;",
3463 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3464 EXPECT_TRUE(notMatches(
3465 "namespace M {"
3466 " class D {};"
3467 "}"
3468 "M::D d;",
3469 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3470}
3471
3472TEST(ElaboratedTypeNarrowing, namesType) {
3473 EXPECT_TRUE(matches(
3474 "namespace N {"
3475 " namespace M {"
3476 " class D {};"
3477 " }"
3478 "}"
3479 "N::M::D d;",
3480 elaboratedType(elaboratedType(namesType(recordType(
3481 hasDeclaration(namedDecl(hasName("D")))))))));
3482 EXPECT_TRUE(notMatches(
3483 "namespace M {"
3484 " class D {};"
3485 "}"
3486 "M::D d;",
3487 elaboratedType(elaboratedType(namesType(typedefType())))));
3488}
3489
Daniel Jaspera7564432012-09-13 13:11:25 +00003490TEST(NNS, MatchesNestedNameSpecifiers) {
3491 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3492 nestedNameSpecifier()));
3493 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3494 nestedNameSpecifier()));
3495 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3496 nestedNameSpecifier()));
3497
3498 EXPECT_TRUE(matches(
3499 "struct A { static void f() {} }; void g() { A::f(); }",
3500 nestedNameSpecifier()));
3501 EXPECT_TRUE(notMatches(
3502 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3503 nestedNameSpecifier()));
3504}
3505
Daniel Jasperb54b7642012-09-20 14:12:57 +00003506TEST(NullStatement, SimpleCases) {
3507 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3508 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3509}
3510
Daniel Jaspera7564432012-09-13 13:11:25 +00003511TEST(NNS, MatchesTypes) {
3512 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3513 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3514 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3515 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3516 Matcher));
3517 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3518}
3519
3520TEST(NNS, MatchesNamespaceDecls) {
3521 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3522 specifiesNamespace(hasName("ns")));
3523 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3524 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3525 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3526}
3527
3528TEST(NNS, BindsNestedNameSpecifiers) {
3529 EXPECT_TRUE(matchAndVerifyResultTrue(
3530 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3531 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3532 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3533}
3534
3535TEST(NNS, BindsNestedNameSpecifierLocs) {
3536 EXPECT_TRUE(matchAndVerifyResultTrue(
3537 "namespace ns { struct B {}; } ns::B b;",
3538 loc(nestedNameSpecifier()).bind("loc"),
3539 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3540}
3541
3542TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3543 EXPECT_TRUE(matches(
3544 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3545 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3546 EXPECT_TRUE(matches(
3547 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003548 nestedNameSpecifierLoc(hasPrefix(
3549 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003550}
3551
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003552TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3553 std::string Fragment =
3554 "namespace a { struct A { struct B { struct C {}; }; }; };"
3555 "void f() { a::A::B::C c; }";
3556 EXPECT_TRUE(matches(
3557 Fragment,
3558 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3559 hasDescendant(nestedNameSpecifier(
3560 specifiesNamespace(hasName("a")))))));
3561 EXPECT_TRUE(notMatches(
3562 Fragment,
3563 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3564 has(nestedNameSpecifier(
3565 specifiesNamespace(hasName("a")))))));
3566 EXPECT_TRUE(matches(
3567 Fragment,
3568 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3569 has(nestedNameSpecifier(
3570 specifiesNamespace(hasName("a")))))));
3571
3572 // Not really useful because a NestedNameSpecifier can af at most one child,
3573 // but to complete the interface.
3574 EXPECT_TRUE(matchAndVerifyResultTrue(
3575 Fragment,
3576 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3577 forEach(nestedNameSpecifier().bind("x"))),
3578 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3579}
3580
3581TEST(NNS, NestedNameSpecifiersAsDescendants) {
3582 std::string Fragment =
3583 "namespace a { struct A { struct B { struct C {}; }; }; };"
3584 "void f() { a::A::B::C c; }";
3585 EXPECT_TRUE(matches(
3586 Fragment,
3587 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3588 asString("struct a::A")))))));
3589 EXPECT_TRUE(matchAndVerifyResultTrue(
3590 Fragment,
3591 functionDecl(hasName("f"),
3592 forEachDescendant(nestedNameSpecifier().bind("x"))),
3593 // Nested names: a, a::A and a::A::B.
3594 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3595}
3596
3597TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3598 std::string Fragment =
3599 "namespace a { struct A { struct B { struct C {}; }; }; };"
3600 "void f() { a::A::B::C c; }";
3601 EXPECT_TRUE(matches(
3602 Fragment,
3603 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3604 hasDescendant(loc(nestedNameSpecifier(
3605 specifiesNamespace(hasName("a"))))))));
3606 EXPECT_TRUE(notMatches(
3607 Fragment,
3608 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3609 has(loc(nestedNameSpecifier(
3610 specifiesNamespace(hasName("a"))))))));
3611 EXPECT_TRUE(matches(
3612 Fragment,
3613 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3614 has(loc(nestedNameSpecifier(
3615 specifiesNamespace(hasName("a"))))))));
3616
3617 EXPECT_TRUE(matchAndVerifyResultTrue(
3618 Fragment,
3619 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3620 forEach(nestedNameSpecifierLoc().bind("x"))),
3621 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3622}
3623
3624TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3625 std::string Fragment =
3626 "namespace a { struct A { struct B { struct C {}; }; }; };"
3627 "void f() { a::A::B::C c; }";
3628 EXPECT_TRUE(matches(
3629 Fragment,
3630 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3631 asString("struct a::A"))))))));
3632 EXPECT_TRUE(matchAndVerifyResultTrue(
3633 Fragment,
3634 functionDecl(hasName("f"),
3635 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3636 // Nested names: a, a::A and a::A::B.
3637 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3638}
3639
Manuel Klimek60969f52013-02-01 13:41:35 +00003640template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003641public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003642 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3643 StringRef InnerId)
3644 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003645 }
3646
Manuel Klimek60969f52013-02-01 13:41:35 +00003647 virtual bool run(const BoundNodes *Nodes) { return false; }
3648
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003649 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3650 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003651 return selectFirst<const T>(InnerId,
3652 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003653 }
3654private:
3655 std::string Id;
3656 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003657 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003658};
3659
3660TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003661 EXPECT_TRUE(matchAndVerifyResultTrue(
3662 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3663 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003664 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
3665 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003666 EXPECT_TRUE(matchAndVerifyResultFalse(
3667 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3668 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003669 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
3670 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003671}
3672
3673TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003674 EXPECT_TRUE(matchAndVerifyResultTrue(
3675 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003676 new VerifyMatchOnNode<clang::Stmt>(
3677 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003678 EXPECT_TRUE(matchAndVerifyResultFalse(
3679 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003680 new VerifyMatchOnNode<clang::Stmt>(
3681 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003682}
3683
3684TEST(MatchFinder, CanMatchSingleNodesRecursively) {
3685 EXPECT_TRUE(matchAndVerifyResultTrue(
3686 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3687 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003688 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003689 EXPECT_TRUE(matchAndVerifyResultFalse(
3690 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3691 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003692 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003693}
3694
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00003695template <typename T>
3696class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
3697public:
3698 virtual bool run(const BoundNodes *Nodes) { return false; }
3699
3700 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3701 const T *Node = Nodes->getNodeAs<T>("");
3702 return verify(*Nodes, *Context, Node);
3703 }
3704
3705 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
3706 return selectFirst<const T>(
3707 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
3708 *Node, Context)) != NULL;
3709 }
3710 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
3711 return selectFirst<const T>(
3712 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
3713 *Node, Context)) != NULL;
3714 }
3715};
3716
3717TEST(IsEqualTo, MatchesNodesByIdentity) {
3718 EXPECT_TRUE(matchAndVerifyResultTrue(
3719 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
3720 new VerifyAncestorHasChildIsEqual<Decl>()));
3721 EXPECT_TRUE(
3722 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
3723 new VerifyAncestorHasChildIsEqual<Stmt>()));
3724}
3725
Manuel Klimeke5793282012-11-02 01:31:03 +00003726class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3727public:
3728 VerifyStartOfTranslationUnit() : Called(false) {}
3729 virtual void run(const MatchFinder::MatchResult &Result) {
3730 EXPECT_TRUE(Called);
3731 }
3732 virtual void onStartOfTranslationUnit() {
3733 Called = true;
3734 }
3735 bool Called;
3736};
3737
3738TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3739 MatchFinder Finder;
3740 VerifyStartOfTranslationUnit VerifyCallback;
3741 Finder.addMatcher(decl(), &VerifyCallback);
3742 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3743 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3744 EXPECT_TRUE(VerifyCallback.Called);
3745}
3746
Manuel Klimek4da21662012-07-06 05:48:52 +00003747} // end namespace ast_matchers
3748} // end namespace clang