blob: 301b4f7c8a8a707fc2482c318a8584cb1f478c77 [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
Edwin Vane6a19a972013-03-06 17:02:57 +0000316TEST(DeclarationMatcher, hasMethod) {
317 EXPECT_TRUE(matches("class A { void func(); };",
318 recordDecl(hasMethod(hasName("func")))));
319 EXPECT_TRUE(notMatches("class A { void func(); };",
320 recordDecl(hasMethod(isPublic()))));
321}
322
Daniel Jasper08f0c532012-09-18 14:17:42 +0000323TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
324 EXPECT_TRUE(matches(
325 "template <typename T> struct A {"
326 " template <typename T2> struct F {};"
327 "};"
328 "template <typename T> struct B : A<T>::template F<T> {};"
329 "B<int> b;",
330 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
331}
332
Edwin Vane742d9e72013-02-25 20:43:32 +0000333TEST(DeclarationMatcher, hasDeclContext) {
334 EXPECT_TRUE(matches(
335 "namespace N {"
336 " namespace M {"
337 " class D {};"
338 " }"
339 "}",
340 recordDecl(hasDeclContext(namedDecl(hasName("M"))))));
341 EXPECT_TRUE(notMatches(
342 "namespace N {"
343 " namespace M {"
344 " class D {};"
345 " }"
346 "}",
347 recordDecl(hasDeclContext(namedDecl(hasName("N"))))));
348}
349
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000350TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000351 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000352 EXPECT_TRUE(notMatches("class X;", ClassX));
353 EXPECT_TRUE(notMatches("class X {};", ClassX));
354}
355
356TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000357 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000358 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
359 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
360}
361
362TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
363 EXPECT_TRUE(notMatches("template<typename T> class X { };"
364 "template<> class X<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
369TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
370 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
371 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000372 classTemplateDecl(hasName("X"),
373 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000374}
375
Daniel Jasper6a124492012-07-12 08:50:38 +0000376TEST(AllOf, AllOverloadsWork) {
377 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000378 "struct T { };"
379 "int f(int, T*, int, int);"
380 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000381 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000382 callExpr(allOf(callee(functionDecl(hasName("f"))),
383 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000384 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000385 callExpr(allOf(callee(functionDecl(hasName("f"))),
386 hasArgument(0, declRefExpr(to(varDecl()))),
387 hasArgument(1, hasType(pointsTo(
388 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000389 EXPECT_TRUE(matches(Program,
390 callExpr(allOf(callee(functionDecl(hasName("f"))),
391 hasArgument(0, declRefExpr(to(varDecl()))),
392 hasArgument(1, hasType(pointsTo(
393 recordDecl(hasName("T"))))),
394 hasArgument(2, integerLiteral(equals(3)))))));
395 EXPECT_TRUE(matches(Program,
396 callExpr(allOf(callee(functionDecl(hasName("f"))),
397 hasArgument(0, declRefExpr(to(varDecl()))),
398 hasArgument(1, hasType(pointsTo(
399 recordDecl(hasName("T"))))),
400 hasArgument(2, integerLiteral(equals(3))),
401 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000402}
403
Manuel Klimek4da21662012-07-06 05:48:52 +0000404TEST(DeclarationMatcher, MatchAnyOf) {
405 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000406 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000407 EXPECT_TRUE(
408 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
409 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
410 EXPECT_TRUE(
411 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
412 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
413
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000414 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000415 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000416 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
417 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
418
Manuel Klimek4da21662012-07-06 05:48:52 +0000419 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000420 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
421 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000422 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
423 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
424 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
425 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
426 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
427 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
428}
429
430TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000431 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000432 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
433 EXPECT_TRUE(matches("class X {};", HasClassX));
434
435 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000436 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000437 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
438 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
439 EXPECT_TRUE(
440 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
441}
442
443TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
444 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000445 recordDecl(
446 has(recordDecl(
447 has(recordDecl(hasName("X"))),
448 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000449 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000450 has(recordDecl(
451 has(recordDecl(hasName("A"))),
452 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000453 hasName("C"))),
454 hasName("F"));
455
456 EXPECT_TRUE(matches(
457 "class F {"
458 " class Z {"
459 " class X {};"
460 " class Y {};"
461 " };"
462 " class C {"
463 " class A {};"
464 " class B {};"
465 " };"
466 "};", Recursive));
467
468 EXPECT_TRUE(matches(
469 "class F {"
470 " class Z {"
471 " class A {};"
472 " class X {};"
473 " class Y {};"
474 " };"
475 " class C {"
476 " class X {};"
477 " class A {};"
478 " class B {};"
479 " };"
480 "};", Recursive));
481
482 EXPECT_TRUE(matches(
483 "class O1 {"
484 " class O2 {"
485 " class F {"
486 " class Z {"
487 " class A {};"
488 " class X {};"
489 " class Y {};"
490 " };"
491 " class C {"
492 " class X {};"
493 " class A {};"
494 " class B {};"
495 " };"
496 " };"
497 " };"
498 "};", Recursive));
499}
500
501TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
502 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000503 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000504 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000505 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000506 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000507 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000508 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000509 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000510 hasName("Y"))),
511 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000512 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000513 anyOf(
514 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000516 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000517 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000518 hasName("B")))))),
519 hasName("F")));
520
521 EXPECT_TRUE(matches("class F {};", Recursive));
522 EXPECT_TRUE(matches("class Z {};", Recursive));
523 EXPECT_TRUE(matches("class C {};", Recursive));
524 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
525 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
526 EXPECT_TRUE(
527 matches("class O1 { class O2 {"
528 " class M { class N { class B {}; }; }; "
529 "}; };", Recursive));
530}
531
532TEST(DeclarationMatcher, MatchNot) {
533 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000534 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000535 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000536 unless(hasName("X")));
537 EXPECT_TRUE(notMatches("", NotClassX));
538 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
539 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
540 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
541 EXPECT_TRUE(
542 notMatches("class Y {}; class Z {}; class X : public Y {};",
543 NotClassX));
544
545 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000546 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000547 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000548 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000549 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000550 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000551 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
552 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
553 ClassXHasNotClassY));
554}
555
556TEST(DeclarationMatcher, HasDescendant) {
557 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000558 recordDecl(
559 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000560 hasName("Z"));
561 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
562 EXPECT_TRUE(
563 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
564 EXPECT_TRUE(
565 matches("class Z { class A { class Y { class X {}; }; }; };",
566 ZDescendantClassX));
567 EXPECT_TRUE(
568 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
569 ZDescendantClassX));
570 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
571
572 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000573 recordDecl(
574 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000575 hasName("X"))),
576 hasName("Z"));
577 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
578 ZDescendantClassXHasClassY));
579 EXPECT_TRUE(
580 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
581 ZDescendantClassXHasClassY));
582 EXPECT_TRUE(notMatches(
583 "class Z {"
584 " class A {"
585 " class B {"
586 " class X {"
587 " class C {"
588 " class Y {};"
589 " };"
590 " };"
591 " }; "
592 " };"
593 "};", ZDescendantClassXHasClassY));
594
595 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000596 recordDecl(
597 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
598 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000599 hasName("Z"));
600 EXPECT_TRUE(
601 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
602 ZDescendantClassXDescendantClassY));
603 EXPECT_TRUE(matches(
604 "class Z {"
605 " class A {"
606 " class X {"
607 " class B {"
608 " class Y {};"
609 " };"
610 " class Y {};"
611 " };"
612 " };"
613 "};", ZDescendantClassXDescendantClassY));
614}
615
Daniel Jaspera267cf62012-10-29 10:14:44 +0000616// Implements a run method that returns whether BoundNodes contains a
617// Decl bound to Id that can be dynamically cast to T.
618// Optionally checks that the check succeeded a specific number of times.
619template <typename T>
620class VerifyIdIsBoundTo : public BoundNodesCallback {
621public:
622 // Create an object that checks that a node of type \c T was bound to \c Id.
623 // Does not check for a certain number of matches.
624 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
625 : Id(Id), ExpectedCount(-1), Count(0) {}
626
627 // Create an object that checks that a node of type \c T was bound to \c Id.
628 // Checks that there were exactly \c ExpectedCount matches.
629 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
630 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
631
632 // Create an object that checks that a node of type \c T was bound to \c Id.
633 // Checks that there was exactly one match with the name \c ExpectedName.
634 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimek374516c2013-03-14 16:33:21 +0000635 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
636 int ExpectedCount = 1)
637 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
638 ExpectedName(ExpectedName) {}
Daniel Jaspera267cf62012-10-29 10:14:44 +0000639
640 ~VerifyIdIsBoundTo() {
641 if (ExpectedCount != -1)
642 EXPECT_EQ(ExpectedCount, Count);
643 if (!ExpectedName.empty())
644 EXPECT_EQ(ExpectedName, Name);
645 }
646
647 virtual bool run(const BoundNodes *Nodes) {
648 if (Nodes->getNodeAs<T>(Id)) {
649 ++Count;
650 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
651 Name = Named->getNameAsString();
652 } else if (const NestedNameSpecifier *NNS =
653 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
654 llvm::raw_string_ostream OS(Name);
655 NNS->print(OS, PrintingPolicy(LangOptions()));
656 }
657 return true;
658 }
659 return false;
660 }
661
Daniel Jasper452abbc2012-10-29 10:48:25 +0000662 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
663 return run(Nodes);
664 }
665
Daniel Jaspera267cf62012-10-29 10:14:44 +0000666private:
667 const std::string Id;
668 const int ExpectedCount;
669 int Count;
670 const std::string ExpectedName;
671 std::string Name;
672};
673
674TEST(HasDescendant, MatchesDescendantTypes) {
675 EXPECT_TRUE(matches("void f() { int i = 3; }",
676 decl(hasDescendant(loc(builtinType())))));
677 EXPECT_TRUE(matches("void f() { int i = 3; }",
678 stmt(hasDescendant(builtinType()))));
679
680 EXPECT_TRUE(matches("void f() { int i = 3; }",
681 stmt(hasDescendant(loc(builtinType())))));
682 EXPECT_TRUE(matches("void f() { int i = 3; }",
683 stmt(hasDescendant(qualType(builtinType())))));
684
685 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
686 stmt(hasDescendant(isInteger()))));
687
688 EXPECT_TRUE(matchAndVerifyResultTrue(
689 "void f() { int a; float c; int d; int e; }",
690 functionDecl(forEachDescendant(
691 varDecl(hasDescendant(isInteger())).bind("x"))),
692 new VerifyIdIsBoundTo<Decl>("x", 3)));
693}
694
695TEST(HasDescendant, MatchesDescendantsOfTypes) {
696 EXPECT_TRUE(matches("void f() { int*** i; }",
697 qualType(hasDescendant(builtinType()))));
698 EXPECT_TRUE(matches("void f() { int*** i; }",
699 qualType(hasDescendant(
700 pointerType(pointee(builtinType()))))));
701 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000702 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000703
704 EXPECT_TRUE(matchAndVerifyResultTrue(
705 "void f() { int*** i; }",
706 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
707 new VerifyIdIsBoundTo<Type>("x", 2)));
708}
709
710TEST(Has, MatchesChildrenOfTypes) {
711 EXPECT_TRUE(matches("int i;",
712 varDecl(hasName("i"), has(isInteger()))));
713 EXPECT_TRUE(notMatches("int** i;",
714 varDecl(hasName("i"), has(isInteger()))));
715 EXPECT_TRUE(matchAndVerifyResultTrue(
716 "int (*f)(float, int);",
717 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
718 new VerifyIdIsBoundTo<QualType>("x", 2)));
719}
720
721TEST(Has, MatchesChildTypes) {
722 EXPECT_TRUE(matches(
723 "int* i;",
724 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
725 EXPECT_TRUE(notMatches(
726 "int* i;",
727 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
728}
729
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000730TEST(Enum, DoesNotMatchClasses) {
731 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
732}
733
734TEST(Enum, MatchesEnums) {
735 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
736}
737
738TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000739 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000740 EXPECT_TRUE(matches("enum X{ A };", Matcher));
741 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
742 EXPECT_TRUE(notMatches("enum X {};", Matcher));
743}
744
Manuel Klimek4da21662012-07-06 05:48:52 +0000745TEST(StatementMatcher, Has) {
746 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000747 expr(hasType(pointsTo(recordDecl(hasName("X")))),
748 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000749
750 EXPECT_TRUE(matches(
751 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
752 EXPECT_TRUE(notMatches(
753 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
754}
755
756TEST(StatementMatcher, HasDescendant) {
757 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000758 expr(hasType(pointsTo(recordDecl(hasName("X")))),
759 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000760
761 EXPECT_TRUE(matches(
762 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
763 HasDescendantVariableI));
764 EXPECT_TRUE(notMatches(
765 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
766 HasDescendantVariableI));
767}
768
769TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000770 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000771
772 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
773 EXPECT_TRUE(notMatches("class A {};", TypeA));
774
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000775 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000776
777 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
778 TypeDerivedFromA));
779 EXPECT_TRUE(notMatches("class A {};", TypeA));
780
781 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000782 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000783
784 EXPECT_TRUE(
785 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
786}
787
Manuel Klimek4da21662012-07-06 05:48:52 +0000788TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000790
791 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000792 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000793
794 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000795 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000796
797 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000798 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000799
800 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
801 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000802 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000803
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000804 StatementMatcher MethodX =
805 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000806
807 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
808 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000809 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000810}
811
812TEST(Matcher, BindTheSameNameInAlternatives) {
813 StatementMatcher matcher = anyOf(
814 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000815 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000816 hasRHS(integerLiteral(equals(0)))),
817 binaryOperator(hasOperatorName("+"),
818 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000819 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000820
821 EXPECT_TRUE(matchAndVerifyResultTrue(
822 // The first branch of the matcher binds x to 0 but then fails.
823 // The second branch binds x to f() and succeeds.
824 "int f() { return 0 + f(); }",
825 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000826 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000827}
828
Manuel Klimek66341c52012-08-30 19:41:06 +0000829TEST(Matcher, BindsIDForMemoizedResults) {
830 // Using the same matcher in two match expressions will make memoization
831 // kick in.
832 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
833 EXPECT_TRUE(matchAndVerifyResultTrue(
834 "class A { class B { class X {}; }; };",
835 DeclarationMatcher(anyOf(
836 recordDecl(hasName("A"), hasDescendant(ClassX)),
837 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000838 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000839}
840
Daniel Jasper189f2e42012-12-03 15:43:25 +0000841TEST(HasDeclaration, HasDeclarationOfEnumType) {
842 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
843 expr(hasType(pointsTo(
844 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
845}
846
Edwin Vaneb45083d2013-02-25 14:32:42 +0000847TEST(HasDeclaration, HasGetDeclTraitTest) {
848 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
849 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
850 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
851}
852
Edwin Vane52380602013-02-19 17:14:34 +0000853TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
854 EXPECT_TRUE(matches("typedef int X; X a;",
855 varDecl(hasName("a"),
856 hasType(typedefType(hasDeclaration(decl()))))));
857
858 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
859}
860
Edwin Vane3abf7782013-02-25 14:49:29 +0000861TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
862 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
863 varDecl(hasType(templateSpecializationType(
864 hasDeclaration(namedDecl(hasName("A"))))))));
865}
866
Manuel Klimek4da21662012-07-06 05:48:52 +0000867TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000868 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000869 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000870 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000871 EXPECT_TRUE(
872 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000873 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000874 EXPECT_TRUE(
875 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000876 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000877}
878
879TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000880 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000881 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000882 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000883 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000884 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000885 EXPECT_TRUE(
886 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000887 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000888}
889
890TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
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) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000894 EXPECT_TRUE(
895 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000896 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000897}
898
899TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000900 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000901 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000902 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000903 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000904 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000905}
906
907TEST(Matcher, Call) {
908 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000909 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000910 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000911
912 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
913 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
914
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000915 StatementMatcher MethodOnY =
916 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000917
918 EXPECT_TRUE(
919 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
920 MethodOnY));
921 EXPECT_TRUE(
922 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
923 MethodOnY));
924 EXPECT_TRUE(
925 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
926 MethodOnY));
927 EXPECT_TRUE(
928 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
929 MethodOnY));
930 EXPECT_TRUE(
931 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
932 MethodOnY));
933
934 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000935 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000936
937 EXPECT_TRUE(
938 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
939 MethodOnYPointer));
940 EXPECT_TRUE(
941 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
942 MethodOnYPointer));
943 EXPECT_TRUE(
944 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
945 MethodOnYPointer));
946 EXPECT_TRUE(
947 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
948 MethodOnYPointer));
949 EXPECT_TRUE(
950 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
951 MethodOnYPointer));
952}
953
Daniel Jasper31f7c082012-10-01 13:40:41 +0000954TEST(Matcher, Lambda) {
955 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
956 lambdaExpr()));
957}
958
959TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000960 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
961 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000962 forRangeStmt()));
963 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
964 forRangeStmt()));
965}
966
967TEST(Matcher, UserDefinedLiteral) {
968 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
969 " return i + 1;"
970 "}"
971 "char c = 'a'_inc;",
972 userDefinedLiteral()));
973}
974
Daniel Jasperb54b7642012-09-20 14:12:57 +0000975TEST(Matcher, FlowControl) {
976 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
977 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
978 continueStmt()));
979 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
980 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
981 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
982}
983
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000984TEST(HasType, MatchesAsString) {
985 EXPECT_TRUE(
986 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000987 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000988 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000989 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000990 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000991 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000992 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000993 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000994}
995
Manuel Klimek4da21662012-07-06 05:48:52 +0000996TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000997 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000998 // Unary operator
999 EXPECT_TRUE(matches("class Y { }; "
1000 "bool operator!(Y x) { return false; }; "
1001 "Y y; bool c = !y;", OpCall));
1002 // No match -- special operators like "new", "delete"
1003 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1004 // we need to figure out include paths in the test.
1005 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1006 // "class Y { }; "
1007 // "void *operator new(size_t size) { return 0; } "
1008 // "Y *y = new Y;", OpCall));
1009 EXPECT_TRUE(notMatches("class Y { }; "
1010 "void operator delete(void *p) { } "
1011 "void a() {Y *y = new Y; delete y;}", OpCall));
1012 // Binary operator
1013 EXPECT_TRUE(matches("class Y { }; "
1014 "bool operator&&(Y x, Y y) { return true; }; "
1015 "Y a; Y b; bool c = a && b;",
1016 OpCall));
1017 // No match -- normal operator, not an overloaded one.
1018 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1019 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1020}
1021
1022TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1023 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001024 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001025 EXPECT_TRUE(matches("class Y { }; "
1026 "bool operator&&(Y x, Y y) { return true; }; "
1027 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1028 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001029 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001030 EXPECT_TRUE(notMatches("class Y { }; "
1031 "bool operator&&(Y x, Y y) { return true; }; "
1032 "Y a; Y b; bool c = a && b;",
1033 OpCallLessLess));
Edwin Vane6a19a972013-03-06 17:02:57 +00001034 DeclarationMatcher ClassWithOpStar =
1035 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1036 EXPECT_TRUE(matches("class Y { int operator*(); };",
1037 ClassWithOpStar));
1038 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1039 ClassWithOpStar)) ;
Manuel Klimek4da21662012-07-06 05:48:52 +00001040}
1041
Daniel Jasper278057f2012-11-15 03:29:05 +00001042TEST(Matcher, NestedOverloadedOperatorCalls) {
1043 EXPECT_TRUE(matchAndVerifyResultTrue(
1044 "class Y { }; "
1045 "Y& operator&&(Y& x, Y& y) { return x; }; "
1046 "Y a; Y b; Y c; Y d = a && b && c;",
1047 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1048 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1049 EXPECT_TRUE(matches(
1050 "class Y { }; "
1051 "Y& operator&&(Y& x, Y& y) { return x; }; "
1052 "Y a; Y b; Y c; Y d = a && b && c;",
1053 operatorCallExpr(hasParent(operatorCallExpr()))));
1054 EXPECT_TRUE(matches(
1055 "class Y { }; "
1056 "Y& operator&&(Y& x, Y& y) { return x; }; "
1057 "Y a; Y b; Y c; Y d = a && b && c;",
1058 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1059}
1060
Manuel Klimek4da21662012-07-06 05:48:52 +00001061TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001062 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001063 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001064
1065 EXPECT_TRUE(
1066 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1067 MethodOnY));
1068 EXPECT_TRUE(
1069 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1070 MethodOnY));
1071 EXPECT_TRUE(
1072 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1073 MethodOnY));
1074 EXPECT_TRUE(
1075 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1076 MethodOnY));
1077 EXPECT_TRUE(
1078 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1079 MethodOnY));
1080
1081 EXPECT_TRUE(matches(
1082 "class Y {"
1083 " public: virtual void x();"
1084 "};"
1085 "class X : public Y {"
1086 " public: virtual void x();"
1087 "};"
1088 "void z() { X *x; x->Y::x(); }", MethodOnY));
1089}
1090
1091TEST(Matcher, VariableUsage) {
1092 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001093 declRefExpr(to(
1094 varDecl(hasInitializer(
1095 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001096
1097 EXPECT_TRUE(matches(
1098 "class Y {"
1099 " public:"
1100 " bool x() const;"
1101 "};"
1102 "void z(const Y &y) {"
1103 " bool b = y.x();"
1104 " if (b) {}"
1105 "}", Reference));
1106
1107 EXPECT_TRUE(notMatches(
1108 "class Y {"
1109 " public:"
1110 " bool x() const;"
1111 "};"
1112 "void z(const Y &y) {"
1113 " bool b = y.x();"
1114 "}", Reference));
1115}
1116
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001117TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001118 EXPECT_TRUE(matches(
1119 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001120 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001121}
1122
Manuel Klimek4da21662012-07-06 05:48:52 +00001123TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001124 StatementMatcher CallOnVariableY =
1125 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001126
1127 EXPECT_TRUE(matches(
1128 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1129 EXPECT_TRUE(matches(
1130 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1131 EXPECT_TRUE(matches(
1132 "class Y { public: void x(); };"
1133 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1134 EXPECT_TRUE(matches(
1135 "class Y { public: void x(); };"
1136 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1137 EXPECT_TRUE(notMatches(
1138 "class Y { public: void x(); };"
1139 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1140 CallOnVariableY));
1141}
1142
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001143TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1144 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1145 unaryExprOrTypeTraitExpr()));
1146 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1147 alignOfExpr(anything())));
1148 // FIXME: Uncomment once alignof is enabled.
1149 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1150 // unaryExprOrTypeTraitExpr()));
1151 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1152 // sizeOfExpr()));
1153}
1154
1155TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1156 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1157 hasArgumentOfType(asString("int")))));
1158 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1159 hasArgumentOfType(asString("float")))));
1160 EXPECT_TRUE(matches(
1161 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001162 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001163 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001164 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001165}
1166
Manuel Klimek4da21662012-07-06 05:48:52 +00001167TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001168 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001169}
1170
1171TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001172 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001173}
1174
1175TEST(MemberExpression, MatchesVariable) {
1176 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001177 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001178 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001179 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001180 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001181 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001182}
1183
1184TEST(MemberExpression, MatchesStaticVariable) {
1185 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001186 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001187 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001188 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001189 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001190 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001191}
1192
Daniel Jasper6a124492012-07-12 08:50:38 +00001193TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001194 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1195 EXPECT_TRUE(matches(
1196 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1197 callExpr(hasArgument(0, declRefExpr(
1198 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001199}
1200
1201TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001202 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001203 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001204 callExpr(hasArgument(0, declRefExpr(
1205 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001206}
1207
Manuel Klimek4da21662012-07-06 05:48:52 +00001208TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1209 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001210 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001211 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001212 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001213 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001214 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001215}
1216
1217TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1218 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001219 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001220 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001221 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001222 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001223 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001224}
1225
1226TEST(IsArrow, MatchesMemberCallsViaArrow) {
1227 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001228 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001229 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001230 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001231 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001232 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001233}
1234
1235TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001236 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001237
1238 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1239 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1240}
1241
1242TEST(Callee, MatchesMemberExpressions) {
1243 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001244 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001245 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001246 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001247}
1248
1249TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001250 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001251
1252 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1253 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1254
Manuel Klimeke265c872012-07-10 14:21:30 +00001255#if !defined(_MSC_VER)
1256 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001257 // Dependent contexts, but a non-dependent call.
1258 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1259 CallFunctionF));
1260 EXPECT_TRUE(
1261 matches("void f(); template <int N> struct S { void g() { f(); } };",
1262 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001263#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001264
1265 // Depedent calls don't match.
1266 EXPECT_TRUE(
1267 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1268 CallFunctionF));
1269 EXPECT_TRUE(
1270 notMatches("void f(int);"
1271 "template <typename T> struct S { void g(T t) { f(t); } };",
1272 CallFunctionF));
1273}
1274
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001275TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1276 EXPECT_TRUE(
1277 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001278 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001279}
1280
1281TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1282 EXPECT_TRUE(
1283 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001284 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001285}
1286
1287TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1288 EXPECT_TRUE(
1289 notMatches("void g(); template <typename T> void f(T t) {}"
1290 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001291 functionTemplateDecl(hasName("f"),
1292 hasDescendant(declRefExpr(to(
1293 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001294}
1295
Manuel Klimek4da21662012-07-06 05:48:52 +00001296TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001297 StatementMatcher CallArgumentY = callExpr(
1298 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001299
1300 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1301 EXPECT_TRUE(
1302 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1303 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1304
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001305 StatementMatcher WrongIndex = callExpr(
1306 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001307 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1308}
1309
1310TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001311 StatementMatcher CallArgumentY = callExpr(
1312 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001313 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1314 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1315 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1316}
1317
1318TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001319 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001320
1321 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1322 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1323 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1324}
1325
Daniel Jasper36e29d62012-12-04 11:54:27 +00001326TEST(Matcher, ParameterCount) {
1327 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1328 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1329 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1330 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1331 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1332}
1333
Manuel Klimek4da21662012-07-06 05:48:52 +00001334TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001335 DeclarationMatcher ReferenceClassX = varDecl(
1336 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001337 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1338 ReferenceClassX));
1339 EXPECT_TRUE(
1340 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1341 EXPECT_TRUE(
1342 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1343 EXPECT_TRUE(
1344 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1345}
1346
Edwin Vane6a19a972013-03-06 17:02:57 +00001347TEST(QualType, hasCanonicalType) {
1348 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1349 "int a;"
1350 "int_ref b = a;",
1351 varDecl(hasType(qualType(referenceType())))));
1352 EXPECT_TRUE(
1353 matches("typedef int &int_ref;"
1354 "int a;"
1355 "int_ref b = a;",
1356 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1357}
1358
Edwin Vane7b69cd02013-04-02 18:15:55 +00001359TEST(QualType, hasLocalQualifiers) {
1360 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1361 varDecl(hasType(hasLocalQualifiers()))));
1362 EXPECT_TRUE(matches("int *const j = nullptr;",
1363 varDecl(hasType(hasLocalQualifiers()))));
1364 EXPECT_TRUE(matches("int *volatile k;",
1365 varDecl(hasType(hasLocalQualifiers()))));
1366 EXPECT_TRUE(notMatches("int m;",
1367 varDecl(hasType(hasLocalQualifiers()))));
1368}
1369
Manuel Klimek4da21662012-07-06 05:48:52 +00001370TEST(HasParameter, CallsInnerMatcher) {
1371 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001372 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001373 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001374 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001375}
1376
1377TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1378 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001379 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001380}
1381
1382TEST(HasType, MatchesParameterVariableTypesStrictly) {
1383 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001384 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001385 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001386 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001387 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001388 methodDecl(hasParameter(0,
1389 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001390 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001391 methodDecl(hasParameter(0,
1392 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001393}
1394
1395TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1396 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001397 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001398 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001399 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001400}
1401
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001402TEST(Returns, MatchesReturnTypes) {
1403 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001404 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001405 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001406 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001407 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001408 functionDecl(returns(hasDeclaration(
1409 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001410}
1411
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001412TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1414 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1415 functionDecl(isExternC())));
1416 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001417}
1418
Manuel Klimek4da21662012-07-06 05:48:52 +00001419TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1420 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001421 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001422}
1423
1424TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1425 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001426 methodDecl(hasAnyParameter(hasType(pointsTo(
1427 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001428}
1429
1430TEST(HasName, MatchesParameterVariableDeclartions) {
1431 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001432 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001433 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001434 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001435}
1436
Daniel Jasper371f9392012-08-01 08:40:24 +00001437TEST(Matcher, MatchesClassTemplateSpecialization) {
1438 EXPECT_TRUE(matches("template<typename T> struct A {};"
1439 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001440 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001441 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001442 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001443 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001444 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001445}
1446
1447TEST(Matcher, MatchesTypeTemplateArgument) {
1448 EXPECT_TRUE(matches(
1449 "template<typename T> struct B {};"
1450 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001451 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001452 asString("int"))))));
1453}
1454
1455TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1456 EXPECT_TRUE(matches(
1457 "struct B { int next; };"
1458 "template<int(B::*next_ptr)> struct A {};"
1459 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001460 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1461 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001462
1463 EXPECT_TRUE(notMatches(
1464 "template <typename T> struct A {};"
1465 "A<int> a;",
1466 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1467 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001468}
1469
1470TEST(Matcher, MatchesSpecificArgument) {
1471 EXPECT_TRUE(matches(
1472 "template<typename T, typename U> class A {};"
1473 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001474 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001475 1, refersToType(asString("int"))))));
1476 EXPECT_TRUE(notMatches(
1477 "template<typename T, typename U> class A {};"
1478 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001479 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001480 1, refersToType(asString("int"))))));
1481}
1482
Daniel Jasperf3197e92013-02-25 12:02:08 +00001483TEST(Matcher, MatchesAccessSpecDecls) {
1484 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1485 EXPECT_TRUE(
1486 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1487 EXPECT_TRUE(
1488 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1489 EXPECT_TRUE(
1490 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1491
1492 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1493}
1494
Manuel Klimek4da21662012-07-06 05:48:52 +00001495TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001496 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001497
1498 EXPECT_TRUE(
1499 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1500 EXPECT_TRUE(
1501 matches("class X { public: X(); }; void x() { X x = X(); }",
1502 Constructor));
1503 EXPECT_TRUE(
1504 matches("class X { public: X(int); }; void x() { X x = 0; }",
1505 Constructor));
1506 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1507}
1508
1509TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001510 StatementMatcher Constructor = constructExpr(
1511 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001512
1513 EXPECT_TRUE(
1514 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1515 Constructor));
1516 EXPECT_TRUE(
1517 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1518 Constructor));
1519 EXPECT_TRUE(
1520 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1521 Constructor));
1522 EXPECT_TRUE(
1523 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1524 Constructor));
1525
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001526 StatementMatcher WrongIndex = constructExpr(
1527 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001528 EXPECT_TRUE(
1529 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1530 WrongIndex));
1531}
1532
1533TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001534 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001535
1536 EXPECT_TRUE(
1537 matches("class X { public: X(int); }; void x() { X x(0); }",
1538 Constructor1Arg));
1539 EXPECT_TRUE(
1540 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1541 Constructor1Arg));
1542 EXPECT_TRUE(
1543 matches("class X { public: X(int); }; void x() { X x = 0; }",
1544 Constructor1Arg));
1545 EXPECT_TRUE(
1546 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1547 Constructor1Arg));
1548}
1549
Manuel Klimek70b9db92012-10-23 10:40:50 +00001550TEST(Matcher,ThisExpr) {
1551 EXPECT_TRUE(
1552 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1553 EXPECT_TRUE(
1554 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1555}
1556
Manuel Klimek4da21662012-07-06 05:48:52 +00001557TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001558 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001559
1560 std::string ClassString = "class string { public: string(); ~string(); }; ";
1561
1562 EXPECT_TRUE(
1563 matches(ClassString +
1564 "string GetStringByValue();"
1565 "void FunctionTakesString(string s);"
1566 "void run() { FunctionTakesString(GetStringByValue()); }",
1567 TempExpression));
1568
1569 EXPECT_TRUE(
1570 notMatches(ClassString +
1571 "string* GetStringPointer(); "
1572 "void FunctionTakesStringPtr(string* s);"
1573 "void run() {"
1574 " string* s = GetStringPointer();"
1575 " FunctionTakesStringPtr(GetStringPointer());"
1576 " FunctionTakesStringPtr(s);"
1577 "}",
1578 TempExpression));
1579
1580 EXPECT_TRUE(
1581 notMatches("class no_dtor {};"
1582 "no_dtor GetObjByValue();"
1583 "void ConsumeObj(no_dtor param);"
1584 "void run() { ConsumeObj(GetObjByValue()); }",
1585 TempExpression));
1586}
1587
Sam Panzere16acd32012-08-24 22:04:44 +00001588TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1589 std::string ClassString =
1590 "class string { public: string(); int length(); }; ";
1591
1592 EXPECT_TRUE(
1593 matches(ClassString +
1594 "string GetStringByValue();"
1595 "void FunctionTakesString(string s);"
1596 "void run() { FunctionTakesString(GetStringByValue()); }",
1597 materializeTemporaryExpr()));
1598
1599 EXPECT_TRUE(
1600 notMatches(ClassString +
1601 "string* GetStringPointer(); "
1602 "void FunctionTakesStringPtr(string* s);"
1603 "void run() {"
1604 " string* s = GetStringPointer();"
1605 " FunctionTakesStringPtr(GetStringPointer());"
1606 " FunctionTakesStringPtr(s);"
1607 "}",
1608 materializeTemporaryExpr()));
1609
1610 EXPECT_TRUE(
1611 notMatches(ClassString +
1612 "string GetStringByValue();"
1613 "void run() { int k = GetStringByValue().length(); }",
1614 materializeTemporaryExpr()));
1615
1616 EXPECT_TRUE(
1617 notMatches(ClassString +
1618 "string GetStringByValue();"
1619 "void run() { GetStringByValue(); }",
1620 materializeTemporaryExpr()));
1621}
1622
Manuel Klimek4da21662012-07-06 05:48:52 +00001623TEST(ConstructorDeclaration, SimpleCase) {
1624 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001625 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001626 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001627 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001628}
1629
1630TEST(ConstructorDeclaration, IsImplicit) {
1631 // This one doesn't match because the constructor is not added by the
1632 // compiler (it is not needed).
1633 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001634 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001635 // The compiler added the implicit default constructor.
1636 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001637 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001638 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001639 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001640}
1641
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001642TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1643 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001644 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001645}
1646
1647TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001648 EXPECT_TRUE(notMatches("class Foo {};",
1649 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001650}
1651
Manuel Klimek4da21662012-07-06 05:48:52 +00001652TEST(HasAnyConstructorInitializer, SimpleCase) {
1653 EXPECT_TRUE(notMatches(
1654 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001655 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001656 EXPECT_TRUE(matches(
1657 "class Foo {"
1658 " Foo() : foo_() { }"
1659 " int foo_;"
1660 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001661 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001662}
1663
1664TEST(HasAnyConstructorInitializer, ForField) {
1665 static const char Code[] =
1666 "class Baz { };"
1667 "class Foo {"
1668 " Foo() : foo_() { }"
1669 " Baz foo_;"
1670 " Baz bar_;"
1671 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001672 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1673 forField(hasType(recordDecl(hasName("Baz"))))))));
1674 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001675 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001676 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1677 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001678}
1679
1680TEST(HasAnyConstructorInitializer, WithInitializer) {
1681 static const char Code[] =
1682 "class Foo {"
1683 " Foo() : foo_(0) { }"
1684 " int foo_;"
1685 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001686 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001687 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001688 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001689 withInitializer(integerLiteral(equals(1)))))));
1690}
1691
1692TEST(HasAnyConstructorInitializer, IsWritten) {
1693 static const char Code[] =
1694 "struct Bar { Bar(){} };"
1695 "class Foo {"
1696 " Foo() : foo_() { }"
1697 " Bar foo_;"
1698 " Bar bar_;"
1699 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001700 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001701 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001702 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001703 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001704 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001705 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1706}
1707
1708TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001709 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001710
1711 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1712 EXPECT_TRUE(
1713 matches("class X { public: X(); }; void x() { new X(); }", New));
1714 EXPECT_TRUE(
1715 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1716 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1717}
1718
1719TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001720 StatementMatcher New = constructExpr(
1721 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001722
1723 EXPECT_TRUE(
1724 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1725 New));
1726 EXPECT_TRUE(
1727 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1728 New));
1729 EXPECT_TRUE(
1730 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1731 New));
1732
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001733 StatementMatcher WrongIndex = constructExpr(
1734 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001735 EXPECT_TRUE(
1736 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1737 WrongIndex));
1738}
1739
1740TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001741 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001742
1743 EXPECT_TRUE(
1744 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1745 EXPECT_TRUE(
1746 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1747 New));
1748}
1749
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001750TEST(Matcher, DeleteExpression) {
1751 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001752 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001753}
1754
Manuel Klimek4da21662012-07-06 05:48:52 +00001755TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001756 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001757
1758 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1759 EXPECT_TRUE(
1760 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1761 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1762}
1763
1764TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001765 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001766 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1767 // wide string
1768 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1769 // with escaped characters
1770 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1771 // no matching -- though the data type is the same, there is no string literal
1772 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1773}
1774
1775TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001776 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001777 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1778 // wide character
1779 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1780 // wide character, Hex encoded, NOT MATCHED!
1781 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1782 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1783}
1784
1785TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001786 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001787 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1788 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1789 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1790 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1791
1792 // Non-matching cases (character literals, float and double)
1793 EXPECT_TRUE(notMatches("int i = L'a';",
1794 HasIntLiteral)); // this is actually a character
1795 // literal cast to int
1796 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1797 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1798 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1799}
1800
Daniel Jasper31f7c082012-10-01 13:40:41 +00001801TEST(Matcher, NullPtrLiteral) {
1802 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1803}
1804
Daniel Jasperb54b7642012-09-20 14:12:57 +00001805TEST(Matcher, AsmStatement) {
1806 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1807}
1808
Manuel Klimek4da21662012-07-06 05:48:52 +00001809TEST(Matcher, Conditions) {
1810 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1811
1812 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1813 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1814 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1815 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1816 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1817}
1818
1819TEST(MatchBinaryOperator, HasOperatorName) {
1820 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1821
1822 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1823 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1824}
1825
1826TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1827 StatementMatcher OperatorTrueFalse =
1828 binaryOperator(hasLHS(boolLiteral(equals(true))),
1829 hasRHS(boolLiteral(equals(false))));
1830
1831 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1832 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1833 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1834}
1835
1836TEST(MatchBinaryOperator, HasEitherOperand) {
1837 StatementMatcher HasOperand =
1838 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1839
1840 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1841 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1842 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1843}
1844
1845TEST(Matcher, BinaryOperatorTypes) {
1846 // Integration test that verifies the AST provides all binary operators in
1847 // a way we expect.
1848 // FIXME: Operator ','
1849 EXPECT_TRUE(
1850 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1851 EXPECT_TRUE(
1852 matches("bool b; bool c = (b = true);",
1853 binaryOperator(hasOperatorName("="))));
1854 EXPECT_TRUE(
1855 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1856 EXPECT_TRUE(
1857 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1858 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1859 EXPECT_TRUE(
1860 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1861 EXPECT_TRUE(
1862 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1863 EXPECT_TRUE(
1864 matches("int i = 1; int j = (i <<= 2);",
1865 binaryOperator(hasOperatorName("<<="))));
1866 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1867 EXPECT_TRUE(
1868 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1869 EXPECT_TRUE(
1870 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1871 EXPECT_TRUE(
1872 matches("int i = 1; int j = (i >>= 2);",
1873 binaryOperator(hasOperatorName(">>="))));
1874 EXPECT_TRUE(
1875 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1876 EXPECT_TRUE(
1877 matches("int i = 42; int j = (i ^= 42);",
1878 binaryOperator(hasOperatorName("^="))));
1879 EXPECT_TRUE(
1880 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1881 EXPECT_TRUE(
1882 matches("int i = 42; int j = (i %= 42);",
1883 binaryOperator(hasOperatorName("%="))));
1884 EXPECT_TRUE(
1885 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1886 EXPECT_TRUE(
1887 matches("bool b = true && false;",
1888 binaryOperator(hasOperatorName("&&"))));
1889 EXPECT_TRUE(
1890 matches("bool b = true; bool c = (b &= false);",
1891 binaryOperator(hasOperatorName("&="))));
1892 EXPECT_TRUE(
1893 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1894 EXPECT_TRUE(
1895 matches("bool b = true || false;",
1896 binaryOperator(hasOperatorName("||"))));
1897 EXPECT_TRUE(
1898 matches("bool b = true; bool c = (b |= false);",
1899 binaryOperator(hasOperatorName("|="))));
1900 EXPECT_TRUE(
1901 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1902 EXPECT_TRUE(
1903 matches("int i = 42; int j = (i *= 23);",
1904 binaryOperator(hasOperatorName("*="))));
1905 EXPECT_TRUE(
1906 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1907 EXPECT_TRUE(
1908 matches("int i = 42; int j = (i /= 23);",
1909 binaryOperator(hasOperatorName("/="))));
1910 EXPECT_TRUE(
1911 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1912 EXPECT_TRUE(
1913 matches("int i = 42; int j = (i += 23);",
1914 binaryOperator(hasOperatorName("+="))));
1915 EXPECT_TRUE(
1916 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1917 EXPECT_TRUE(
1918 matches("int i = 42; int j = (i -= 23);",
1919 binaryOperator(hasOperatorName("-="))));
1920 EXPECT_TRUE(
1921 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1922 binaryOperator(hasOperatorName("->*"))));
1923 EXPECT_TRUE(
1924 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1925 binaryOperator(hasOperatorName(".*"))));
1926
1927 // Member expressions as operators are not supported in matches.
1928 EXPECT_TRUE(
1929 notMatches("struct A { void x(A *a) { a->x(this); } };",
1930 binaryOperator(hasOperatorName("->"))));
1931
1932 // Initializer assignments are not represented as operator equals.
1933 EXPECT_TRUE(
1934 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1935
1936 // Array indexing is not represented as operator.
1937 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1938
1939 // Overloaded operators do not match at all.
1940 EXPECT_TRUE(notMatches(
1941 "struct A { bool operator&&(const A &a) const { return false; } };"
1942 "void x() { A a, b; a && b; }",
1943 binaryOperator()));
1944}
1945
1946TEST(MatchUnaryOperator, HasOperatorName) {
1947 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1948
1949 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1950 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1951}
1952
1953TEST(MatchUnaryOperator, HasUnaryOperand) {
1954 StatementMatcher OperatorOnFalse =
1955 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1956
1957 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1958 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1959}
1960
1961TEST(Matcher, UnaryOperatorTypes) {
1962 // Integration test that verifies the AST provides all unary operators in
1963 // a way we expect.
1964 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1965 EXPECT_TRUE(
1966 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1967 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1968 EXPECT_TRUE(
1969 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1970 EXPECT_TRUE(
1971 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1972 EXPECT_TRUE(
1973 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1974 EXPECT_TRUE(
1975 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1976 EXPECT_TRUE(
1977 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1978 EXPECT_TRUE(
1979 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1980 EXPECT_TRUE(
1981 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1982
1983 // We don't match conversion operators.
1984 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1985
1986 // Function calls are not represented as operator.
1987 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1988
1989 // Overloaded operators do not match at all.
1990 // FIXME: We probably want to add that.
1991 EXPECT_TRUE(notMatches(
1992 "struct A { bool operator!() const { return false; } };"
1993 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1994}
1995
1996TEST(Matcher, ConditionalOperator) {
1997 StatementMatcher Conditional = conditionalOperator(
1998 hasCondition(boolLiteral(equals(true))),
1999 hasTrueExpression(boolLiteral(equals(false))));
2000
2001 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2002 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2003 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2004
2005 StatementMatcher ConditionalFalse = conditionalOperator(
2006 hasFalseExpression(boolLiteral(equals(false))));
2007
2008 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2009 EXPECT_TRUE(
2010 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2011}
2012
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002013TEST(ArraySubscriptMatchers, ArraySubscripts) {
2014 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2015 arraySubscriptExpr()));
2016 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2017 arraySubscriptExpr()));
2018}
2019
2020TEST(ArraySubscriptMatchers, ArrayIndex) {
2021 EXPECT_TRUE(matches(
2022 "int i[2]; void f() { i[1] = 1; }",
2023 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2024 EXPECT_TRUE(matches(
2025 "int i[2]; void f() { 1[i] = 1; }",
2026 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2027 EXPECT_TRUE(notMatches(
2028 "int i[2]; void f() { i[1] = 1; }",
2029 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2030}
2031
2032TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2033 EXPECT_TRUE(matches(
2034 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002035 arraySubscriptExpr(hasBase(implicitCastExpr(
2036 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002037}
2038
Manuel Klimek4da21662012-07-06 05:48:52 +00002039TEST(Matcher, HasNameSupportsNamespaces) {
2040 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002041 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002042 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002043 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002044 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002045 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002046 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002047 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002048 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002049 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002050 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002051 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002052 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002053 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002054 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002055 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002056 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002057 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002058 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002059 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002060 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002061 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002062 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002063 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002064}
2065
2066TEST(Matcher, HasNameSupportsOuterClasses) {
2067 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002068 matches("class A { class B { class C; }; };",
2069 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002070 EXPECT_TRUE(
2071 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002072 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002073 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002074 matches("class A { class B { class C; }; };",
2075 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002076 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002077 matches("class A { class B { class C; }; };",
2078 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002079 EXPECT_TRUE(
2080 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002081 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002082 EXPECT_TRUE(
2083 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002084 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002085 EXPECT_TRUE(
2086 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002087 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002088 EXPECT_TRUE(
2089 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002090 recordDecl(hasName("::C"))));
2091 EXPECT_TRUE(
2092 notMatches("class A { class B { class C; }; };",
2093 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002094 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002095 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002096 EXPECT_TRUE(
2097 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002098 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002099}
2100
2101TEST(Matcher, IsDefinition) {
2102 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002103 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002104 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2105 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2106
2107 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002108 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002109 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2110 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2111
2112 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002114 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2115 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2116}
2117
2118TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002119 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002120 ofClass(hasName("X")))));
2121
2122 EXPECT_TRUE(
2123 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2124 EXPECT_TRUE(
2125 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2126 Constructor));
2127 EXPECT_TRUE(
2128 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2129 Constructor));
2130}
2131
2132TEST(Matcher, VisitsTemplateInstantiations) {
2133 EXPECT_TRUE(matches(
2134 "class A { public: void x(); };"
2135 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002136 "void f() { B<A> b; b.y(); }",
2137 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002138
2139 EXPECT_TRUE(matches(
2140 "class A { public: void x(); };"
2141 "class C {"
2142 " public:"
2143 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2144 "};"
2145 "void f() {"
2146 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002147 "}",
2148 recordDecl(hasName("C"),
2149 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002150}
2151
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002152TEST(Matcher, HandlesNullQualTypes) {
2153 // FIXME: Add a Type matcher so we can replace uses of this
2154 // variable with Type(True())
2155 const TypeMatcher AnyType = anything();
2156
2157 // We don't really care whether this matcher succeeds; we're testing that
2158 // it completes without crashing.
2159 EXPECT_TRUE(matches(
2160 "struct A { };"
2161 "template <typename T>"
2162 "void f(T t) {"
2163 " T local_t(t /* this becomes a null QualType in the AST */);"
2164 "}"
2165 "void g() {"
2166 " f(0);"
2167 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002168 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002169 anyOf(
2170 TypeMatcher(hasDeclaration(anything())),
2171 pointsTo(AnyType),
2172 references(AnyType)
2173 // Other QualType matchers should go here.
2174 ))))));
2175}
2176
Manuel Klimek4da21662012-07-06 05:48:52 +00002177// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002178AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002179 // Make sure all special variables are used: node, match_finder,
2180 // bound_nodes_builder, and the parameter named 'AMatcher'.
2181 return AMatcher.matches(Node, Finder, Builder);
2182}
2183
2184TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002185 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002186
2187 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002188 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002189
2190 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002191 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002192
2193 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002194 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195}
2196
2197AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002198 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2199 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2200 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002201 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002202 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002203 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002204 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2205 ASTMatchFinder::BK_First);
2206}
2207
2208TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002209 DeclarationMatcher HasClassB =
2210 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211
2212 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002213 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002214
2215 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002216 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002217
2218 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002219 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220
2221 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002222 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002223
2224 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2225}
2226
2227TEST(For, FindsForLoops) {
2228 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2229 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002230 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2231 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002232 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002233}
2234
Daniel Jasper6a124492012-07-12 08:50:38 +00002235TEST(For, ForLoopInternals) {
2236 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2237 forStmt(hasCondition(anything()))));
2238 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2239 forStmt(hasLoopInit(anything()))));
2240}
2241
2242TEST(For, NegativeForLoopInternals) {
2243 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002244 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002245 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2246 forStmt(hasLoopInit(anything()))));
2247}
2248
Manuel Klimek4da21662012-07-06 05:48:52 +00002249TEST(For, ReportsNoFalsePositives) {
2250 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2251 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2252}
2253
2254TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002255 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2256 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2257 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002258}
2259
2260TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2261 // It's not a compound statement just because there's "{}" in the source
2262 // text. This is an AST search, not grep.
2263 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002264 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002265 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002266 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002267}
2268
Daniel Jasper6a124492012-07-12 08:50:38 +00002269TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002270 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002271 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002272 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002273 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002274 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002275 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002276 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002277 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002278}
2279
2280TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2281 // The simplest case: every compound statement is in a function
2282 // definition, and the function body itself must be a compound
2283 // statement.
2284 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002285 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002286}
2287
2288TEST(HasAnySubstatement, IsNotRecursive) {
2289 // It's really "has any immediate substatement".
2290 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002291 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002292}
2293
2294TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2295 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002296 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002297}
2298
2299TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2300 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002301 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002302}
2303
2304TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2305 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002306 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002307 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002308 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002309}
2310
2311TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2312 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002313 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002314 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002315 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002316 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002317 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002318}
2319
2320TEST(StatementCountIs, WorksWithMultipleStatements) {
2321 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002322 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002323}
2324
2325TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2326 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002327 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002328 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002329 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002330 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002331 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002332 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002333 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002334}
2335
2336TEST(Member, WorksInSimplestCase) {
2337 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002338 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002339}
2340
2341TEST(Member, DoesNotMatchTheBaseExpression) {
2342 // Don't pick out the wrong part of the member expression, this should
2343 // be checking the member (name) only.
2344 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002345 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002346}
2347
2348TEST(Member, MatchesInMemberFunctionCall) {
2349 EXPECT_TRUE(matches("void f() {"
2350 " struct { void first() {}; } s;"
2351 " s.first();"
2352 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002353 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002354}
2355
Daniel Jasperc711af22012-10-23 15:46:39 +00002356TEST(Member, MatchesMember) {
2357 EXPECT_TRUE(matches(
2358 "struct A { int i; }; void f() { A a; a.i = 2; }",
2359 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2360 EXPECT_TRUE(notMatches(
2361 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2362 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2363}
2364
Daniel Jasperf3197e92013-02-25 12:02:08 +00002365TEST(Member, UnderstandsAccess) {
2366 EXPECT_TRUE(matches(
2367 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2368 EXPECT_TRUE(notMatches(
2369 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2370 EXPECT_TRUE(notMatches(
2371 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2372
2373 EXPECT_TRUE(notMatches(
2374 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2375 EXPECT_TRUE(notMatches(
2376 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2377 EXPECT_TRUE(matches(
2378 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2379
2380 EXPECT_TRUE(notMatches(
2381 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2382 EXPECT_TRUE(matches("class A { protected: int i; };",
2383 fieldDecl(isProtected(), hasName("i"))));
2384 EXPECT_TRUE(notMatches(
2385 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2386
2387 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2388 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2389 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2390 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2391}
2392
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002393TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002394 // Fails in C++11 mode
2395 EXPECT_TRUE(matchesConditionally(
2396 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2397 "class X { void *operator new(std::size_t); };",
2398 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002399
2400 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002401 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002402
Daniel Jasper31f7c082012-10-01 13:40:41 +00002403 // Fails in C++11 mode
2404 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002405 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2406 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002407 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002408}
2409
Manuel Klimek4da21662012-07-06 05:48:52 +00002410TEST(HasObjectExpression, DoesNotMatchMember) {
2411 EXPECT_TRUE(notMatches(
2412 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002413 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002414}
2415
2416TEST(HasObjectExpression, MatchesBaseOfVariable) {
2417 EXPECT_TRUE(matches(
2418 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002419 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002420 EXPECT_TRUE(matches(
2421 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002422 memberExpr(hasObjectExpression(
2423 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002424}
2425
2426TEST(HasObjectExpression,
2427 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2428 EXPECT_TRUE(matches(
2429 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002430 memberExpr(hasObjectExpression(
2431 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002432 EXPECT_TRUE(matches(
2433 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002434 memberExpr(hasObjectExpression(
2435 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002436}
2437
2438TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002439 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2440 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2441 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2442 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002443}
2444
2445TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002446 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002447}
2448
2449TEST(IsConstQualified, MatchesConstInt) {
2450 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002451 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002452}
2453
2454TEST(IsConstQualified, MatchesConstPointer) {
2455 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002456 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002457}
2458
2459TEST(IsConstQualified, MatchesThroughTypedef) {
2460 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002461 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002462 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002463 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002464}
2465
2466TEST(IsConstQualified, DoesNotMatchInappropriately) {
2467 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002468 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002469 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002470 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002471}
2472
Sam Panzer089e5b32012-08-16 16:58:10 +00002473TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002474 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2475 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2476 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2477 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002478}
2479TEST(CastExpression, MatchesImplicitCasts) {
2480 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002481 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002482 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002483 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002484}
2485
2486TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002487 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2488 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2489 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2490 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002491}
2492
Manuel Klimek4da21662012-07-06 05:48:52 +00002493TEST(ReinterpretCast, MatchesSimpleCase) {
2494 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002495 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002496}
2497
2498TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002499 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002500 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002501 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002502 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002503 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002504 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2505 "B b;"
2506 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002507 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002508}
2509
2510TEST(FunctionalCast, MatchesSimpleCase) {
2511 std::string foo_class = "class Foo { public: Foo(char*); };";
2512 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002513 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002514}
2515
2516TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2517 std::string FooClass = "class Foo { public: Foo(char*); };";
2518 EXPECT_TRUE(
2519 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002520 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002521 EXPECT_TRUE(
2522 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002523 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002524}
2525
2526TEST(DynamicCast, MatchesSimpleCase) {
2527 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2528 "B b;"
2529 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002530 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002531}
2532
2533TEST(StaticCast, MatchesSimpleCase) {
2534 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002535 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002536}
2537
2538TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002539 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002540 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002541 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002542 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002543 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002544 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2545 "B b;"
2546 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002547 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002548}
2549
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002550TEST(CStyleCast, MatchesSimpleCase) {
2551 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2552}
2553
2554TEST(CStyleCast, DoesNotMatchOtherCasts) {
2555 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2556 "char q, *r = const_cast<char*>(&q);"
2557 "void* s = reinterpret_cast<char*>(&s);"
2558 "struct B { virtual ~B() {} }; struct D : B {};"
2559 "B b;"
2560 "D* t = dynamic_cast<D*>(&b);",
2561 cStyleCastExpr()));
2562}
2563
Manuel Klimek4da21662012-07-06 05:48:52 +00002564TEST(HasDestinationType, MatchesSimpleCase) {
2565 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002566 staticCastExpr(hasDestinationType(
2567 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002568}
2569
Sam Panzer089e5b32012-08-16 16:58:10 +00002570TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2571 // This test creates an implicit const cast.
2572 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002573 implicitCastExpr(
2574 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002575 // This test creates an implicit array-to-pointer cast.
2576 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002577 implicitCastExpr(hasImplicitDestinationType(
2578 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002579}
2580
2581TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2582 // This test creates an implicit cast from int to char.
2583 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002584 implicitCastExpr(hasImplicitDestinationType(
2585 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002586 // This test creates an implicit array-to-pointer cast.
2587 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002588 implicitCastExpr(hasImplicitDestinationType(
2589 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002590}
2591
2592TEST(ImplicitCast, MatchesSimpleCase) {
2593 // This test creates an implicit const cast.
2594 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002595 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002596 // This test creates an implicit cast from int to char.
2597 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002598 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002599 // This test creates an implicit array-to-pointer cast.
2600 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002601 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002602}
2603
2604TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002605 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002606 // are present, and that it ignores explicit and paren casts.
2607
2608 // These two test cases have no casts.
2609 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002610 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002611 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002612 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002613
2614 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002615 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002616 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002617 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002618
2619 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002620 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002621}
2622
2623TEST(IgnoringImpCasts, MatchesImpCasts) {
2624 // This test checks that ignoringImpCasts matches when implicit casts are
2625 // present and its inner matcher alone does not match.
2626 // Note that this test creates an implicit const cast.
2627 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002628 varDecl(hasInitializer(ignoringImpCasts(
2629 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002630 // This test creates an implict cast from int to char.
2631 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002632 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002633 integerLiteral(equals(0)))))));
2634}
2635
2636TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2637 // These tests verify that ignoringImpCasts does not match if the inner
2638 // matcher does not match.
2639 // Note that the first test creates an implicit const cast.
2640 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002641 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002642 unless(anything()))))));
2643 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002644 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002645 unless(anything()))))));
2646
2647 // These tests verify that ignoringImplictCasts does not look through explicit
2648 // casts or parentheses.
2649 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002650 varDecl(hasInitializer(ignoringImpCasts(
2651 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002652 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002653 varDecl(hasInitializer(ignoringImpCasts(
2654 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002655 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002656 varDecl(hasInitializer(ignoringImpCasts(
2657 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002658 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002659 varDecl(hasInitializer(ignoringImpCasts(
2660 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002661}
2662
2663TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2664 // This test verifies that expressions that do not have implicit casts
2665 // still match the inner matcher.
2666 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002667 varDecl(hasInitializer(ignoringImpCasts(
2668 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002669}
2670
2671TEST(IgnoringParenCasts, MatchesParenCasts) {
2672 // This test checks that ignoringParenCasts matches when parentheses and/or
2673 // casts are present and its inner matcher alone does not match.
2674 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002675 varDecl(hasInitializer(ignoringParenCasts(
2676 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002677 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002678 varDecl(hasInitializer(ignoringParenCasts(
2679 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002680
2681 // This test creates an implict cast from int to char in addition to the
2682 // parentheses.
2683 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002684 varDecl(hasInitializer(ignoringParenCasts(
2685 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002686
2687 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002688 varDecl(hasInitializer(ignoringParenCasts(
2689 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002690 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002691 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002692 integerLiteral(equals(0)))))));
2693}
2694
2695TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2696 // This test verifies that expressions that do not have any casts still match.
2697 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002698 varDecl(hasInitializer(ignoringParenCasts(
2699 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002700}
2701
2702TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2703 // These tests verify that ignoringImpCasts does not match if the inner
2704 // matcher does not match.
2705 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002706 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002707 unless(anything()))))));
2708
2709 // This test creates an implicit cast from int to char in addition to the
2710 // parentheses.
2711 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002712 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002713 unless(anything()))))));
2714
2715 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002716 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002717 unless(anything()))))));
2718}
2719
2720TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2721 // This test checks that ignoringParenAndImpCasts matches when
2722 // parentheses and/or implicit casts are present and its inner matcher alone
2723 // does not match.
2724 // Note that this test creates an implicit const cast.
2725 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002726 varDecl(hasInitializer(ignoringParenImpCasts(
2727 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002728 // This test creates an implicit cast from int to char.
2729 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002730 varDecl(hasInitializer(ignoringParenImpCasts(
2731 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002732}
2733
2734TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2735 // This test verifies that expressions that do not have parentheses or
2736 // implicit casts still match.
2737 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002738 varDecl(hasInitializer(ignoringParenImpCasts(
2739 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002740 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002741 varDecl(hasInitializer(ignoringParenImpCasts(
2742 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002743}
2744
2745TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2746 // These tests verify that ignoringParenImpCasts does not match if
2747 // the inner matcher does not match.
2748 // This test creates an implicit cast.
2749 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002750 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002751 unless(anything()))))));
2752 // These tests verify that ignoringParenAndImplictCasts does not look
2753 // through explicit casts.
2754 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002755 varDecl(hasInitializer(ignoringParenImpCasts(
2756 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002757 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002758 varDecl(hasInitializer(ignoringParenImpCasts(
2759 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002760 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002761 varDecl(hasInitializer(ignoringParenImpCasts(
2762 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002763}
2764
Manuel Klimek715c9562012-07-25 10:02:02 +00002765TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002766 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2767 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002768 implicitCastExpr(
2769 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002770}
2771
Manuel Klimek715c9562012-07-25 10:02:02 +00002772TEST(HasSourceExpression, MatchesExplicitCasts) {
2773 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002774 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002775 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002776 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002777}
2778
Manuel Klimek4da21662012-07-06 05:48:52 +00002779TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002780 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002781}
2782
2783TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002784 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002785}
2786
2787TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002788 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002789}
2790
2791TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002792 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002793}
2794
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002795TEST(InitListExpression, MatchesInitListExpression) {
2796 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2797 initListExpr(hasType(asString("int [2]")))));
2798 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002799 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002800}
2801
2802TEST(UsingDeclaration, MatchesUsingDeclarations) {
2803 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2804 usingDecl()));
2805}
2806
2807TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2808 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2809 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2810}
2811
2812TEST(UsingDeclaration, MatchesSpecificTarget) {
2813 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2814 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002815 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002816 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2817 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002818 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002819}
2820
2821TEST(UsingDeclaration, ThroughUsingDeclaration) {
2822 EXPECT_TRUE(matches(
2823 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002824 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002825 EXPECT_TRUE(notMatches(
2826 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002827 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002828}
2829
Sam Panzer425f41b2012-08-16 17:20:59 +00002830TEST(SingleDecl, IsSingleDecl) {
2831 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002832 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002833 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2834 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2835 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2836 SingleDeclStmt));
2837}
2838
2839TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002840 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002841
2842 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002843 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002844 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002845 declStmt(containsDeclaration(0, MatchesInit),
2846 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002847 unsigned WrongIndex = 42;
2848 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002849 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002850 MatchesInit))));
2851}
2852
2853TEST(DeclCount, DeclCountIsCorrect) {
2854 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002855 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002856 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002857 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002858 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002859 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002860}
2861
Manuel Klimek4da21662012-07-06 05:48:52 +00002862TEST(While, MatchesWhileLoops) {
2863 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2864 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2865 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2866}
2867
2868TEST(Do, MatchesDoLoops) {
2869 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2870 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2871}
2872
2873TEST(Do, DoesNotMatchWhileLoops) {
2874 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2875}
2876
2877TEST(SwitchCase, MatchesCase) {
2878 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2879 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2880 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2881 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2882}
2883
Daniel Jasperb54b7642012-09-20 14:12:57 +00002884TEST(SwitchCase, MatchesSwitch) {
2885 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2886 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2887 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2888 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2889}
2890
2891TEST(ExceptionHandling, SimpleCases) {
2892 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2893 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2894 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2895 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2896 throwExpr()));
2897 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2898 throwExpr()));
2899}
2900
Manuel Klimek4da21662012-07-06 05:48:52 +00002901TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2902 EXPECT_TRUE(notMatches(
2903 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002904 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002905 EXPECT_TRUE(notMatches(
2906 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002907 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002908}
2909
2910TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2911 EXPECT_TRUE(matches(
2912 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002913 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002914}
2915
2916TEST(ForEach, BindsOneNode) {
2917 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002918 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002919 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002920}
2921
2922TEST(ForEach, BindsMultipleNodes) {
2923 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002924 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002925 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002926}
2927
2928TEST(ForEach, BindsRecursiveCombinations) {
2929 EXPECT_TRUE(matchAndVerifyResultTrue(
2930 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002931 recordDecl(hasName("C"),
2932 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002933 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002934}
2935
2936TEST(ForEachDescendant, BindsOneNode) {
2937 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002938 recordDecl(hasName("C"),
2939 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002940 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002941}
2942
Daniel Jasper5f684e92012-11-16 18:39:22 +00002943TEST(ForEachDescendant, NestedForEachDescendant) {
2944 DeclarationMatcher m = recordDecl(
2945 isDefinition(), decl().bind("x"), hasName("C"));
2946 EXPECT_TRUE(matchAndVerifyResultTrue(
2947 "class A { class B { class C {}; }; };",
2948 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2949 new VerifyIdIsBoundTo<Decl>("x", "C")));
2950
2951 // FIXME: This is not really a useful matcher, but the result is still
2952 // surprising (currently binds "A").
2953 //EXPECT_TRUE(matchAndVerifyResultTrue(
2954 // "class A { class B { class C {}; }; };",
2955 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2956 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2957}
2958
Manuel Klimek4da21662012-07-06 05:48:52 +00002959TEST(ForEachDescendant, BindsMultipleNodes) {
2960 EXPECT_TRUE(matchAndVerifyResultTrue(
2961 "class C { class D { int x; int y; }; "
2962 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002963 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002964 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002965}
2966
2967TEST(ForEachDescendant, BindsRecursiveCombinations) {
2968 EXPECT_TRUE(matchAndVerifyResultTrue(
2969 "class C { class D { "
2970 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002971 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2972 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002973 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002974}
2975
Daniel Jasper11c98772012-11-11 22:14:55 +00002976TEST(ForEachDescendant, BindsCorrectNodes) {
2977 EXPECT_TRUE(matchAndVerifyResultTrue(
2978 "class C { void f(); int i; };",
2979 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2980 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2981 EXPECT_TRUE(matchAndVerifyResultTrue(
2982 "class C { void f() {} int i; };",
2983 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2984 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2985}
2986
Manuel Klimek152ea0e2013-02-04 10:59:20 +00002987TEST(FindAll, BindsNodeOnMatch) {
2988 EXPECT_TRUE(matchAndVerifyResultTrue(
2989 "class A {};",
2990 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
2991 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
2992}
2993
2994TEST(FindAll, BindsDescendantNodeOnMatch) {
2995 EXPECT_TRUE(matchAndVerifyResultTrue(
2996 "class A { int a; int b; };",
2997 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
2998 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2999}
3000
3001TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3002 EXPECT_TRUE(matchAndVerifyResultTrue(
3003 "class A { int a; int b; };",
3004 recordDecl(hasName("::A"),
3005 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3006 fieldDecl().bind("v"))))),
3007 new VerifyIdIsBoundTo<Decl>("v", 3)));
3008
3009 EXPECT_TRUE(matchAndVerifyResultTrue(
3010 "class A { class B {}; class C {}; };",
3011 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3012 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3013}
3014
Manuel Klimek73876732013-02-04 09:42:38 +00003015TEST(EachOf, TriggersForEachMatch) {
3016 EXPECT_TRUE(matchAndVerifyResultTrue(
3017 "class A { int a; int b; };",
3018 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3019 has(fieldDecl(hasName("b")).bind("v")))),
3020 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3021}
3022
3023TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3024 EXPECT_TRUE(matchAndVerifyResultTrue(
3025 "class A { int a; int c; };",
3026 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3027 has(fieldDecl(hasName("b")).bind("v")))),
3028 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3029 EXPECT_TRUE(matchAndVerifyResultTrue(
3030 "class A { int c; int b; };",
3031 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3032 has(fieldDecl(hasName("b")).bind("v")))),
3033 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3034 EXPECT_TRUE(notMatches(
3035 "class A { int c; int d; };",
3036 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3037 has(fieldDecl(hasName("b")).bind("v"))))));
3038}
Manuel Klimek4da21662012-07-06 05:48:52 +00003039
3040TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3041 // Make sure that we can both match the class by name (::X) and by the type
3042 // the template was instantiated with (via a field).
3043
3044 EXPECT_TRUE(matches(
3045 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003046 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003047
3048 EXPECT_TRUE(matches(
3049 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003050 recordDecl(isTemplateInstantiation(), hasDescendant(
3051 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003052}
3053
3054TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3055 EXPECT_TRUE(matches(
3056 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003057 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003058 isTemplateInstantiation())));
3059}
3060
3061TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3062 EXPECT_TRUE(matches(
3063 "template <typename T> class X { T t; }; class A {};"
3064 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003065 recordDecl(isTemplateInstantiation(), hasDescendant(
3066 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003067}
3068
3069TEST(IsTemplateInstantiation,
3070 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3071 EXPECT_TRUE(matches(
3072 "template <typename T> class X {};"
3073 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003074 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003075}
3076
3077TEST(IsTemplateInstantiation,
3078 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3079 EXPECT_TRUE(matches(
3080 "class A {};"
3081 "class X {"
3082 " template <typename U> class Y { U u; };"
3083 " Y<A> y;"
3084 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003085 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003086}
3087
3088TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3089 // FIXME: Figure out whether this makes sense. It doesn't affect the
3090 // normal use case as long as the uppermost instantiation always is marked
3091 // as template instantiation, but it might be confusing as a predicate.
3092 EXPECT_TRUE(matches(
3093 "class A {};"
3094 "template <typename T> class X {"
3095 " template <typename U> class Y { U u; };"
3096 " Y<T> y;"
3097 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003098 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003099}
3100
3101TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3102 EXPECT_TRUE(notMatches(
3103 "template <typename T> class X {}; class A {};"
3104 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003105 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003106}
3107
3108TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3109 EXPECT_TRUE(notMatches(
3110 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003111 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003112}
3113
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003114TEST(IsExplicitTemplateSpecialization,
3115 DoesNotMatchPrimaryTemplate) {
3116 EXPECT_TRUE(notMatches(
3117 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003118 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003119 EXPECT_TRUE(notMatches(
3120 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003121 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003122}
3123
3124TEST(IsExplicitTemplateSpecialization,
3125 DoesNotMatchExplicitTemplateInstantiations) {
3126 EXPECT_TRUE(notMatches(
3127 "template <typename T> class X {};"
3128 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003129 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003130 EXPECT_TRUE(notMatches(
3131 "template <typename T> void f(T t) {}"
3132 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003133 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003134}
3135
3136TEST(IsExplicitTemplateSpecialization,
3137 DoesNotMatchImplicitTemplateInstantiations) {
3138 EXPECT_TRUE(notMatches(
3139 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003140 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003141 EXPECT_TRUE(notMatches(
3142 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003143 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003144}
3145
3146TEST(IsExplicitTemplateSpecialization,
3147 MatchesExplicitTemplateSpecializations) {
3148 EXPECT_TRUE(matches(
3149 "template <typename T> class X {};"
3150 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003151 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003152 EXPECT_TRUE(matches(
3153 "template <typename T> void f(T t) {}"
3154 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003155 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003156}
3157
Manuel Klimek579b1202012-09-07 09:26:10 +00003158TEST(HasAncenstor, MatchesDeclarationAncestors) {
3159 EXPECT_TRUE(matches(
3160 "class A { class B { class C {}; }; };",
3161 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3162}
3163
3164TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3165 EXPECT_TRUE(notMatches(
3166 "class A { class B { class C {}; }; };",
3167 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3168}
3169
3170TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3171 EXPECT_TRUE(matches(
3172 "class A { class B { void f() { C c; } class C {}; }; };",
3173 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3174 hasAncestor(recordDecl(hasName("A"))))))));
3175}
3176
3177TEST(HasAncenstor, MatchesStatementAncestors) {
3178 EXPECT_TRUE(matches(
3179 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003180 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003181}
3182
3183TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3184 EXPECT_TRUE(matches(
3185 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003186 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003187}
3188
3189TEST(HasAncestor, BindsRecursiveCombinations) {
3190 EXPECT_TRUE(matchAndVerifyResultTrue(
3191 "class C { class D { class E { class F { int y; }; }; }; };",
3192 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003193 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003194}
3195
3196TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3197 EXPECT_TRUE(matchAndVerifyResultTrue(
3198 "class C { class D { class E { class F { int y; }; }; }; };",
3199 fieldDecl(hasAncestor(
3200 decl(
3201 hasDescendant(recordDecl(isDefinition(),
3202 hasAncestor(recordDecl())))
3203 ).bind("d")
3204 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003205 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003206}
3207
Manuel Klimek374516c2013-03-14 16:33:21 +00003208TEST(HasAncestor, MatchesClosestAncestor) {
3209 EXPECT_TRUE(matchAndVerifyResultTrue(
3210 "template <typename T> struct C {"
3211 " void f(int) {"
3212 " struct I { void g(T) { int x; } } i; i.g(42);"
3213 " }"
3214 "};"
3215 "template struct C<int>;",
3216 varDecl(hasName("x"),
3217 hasAncestor(functionDecl(hasParameter(
3218 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3219 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3220}
3221
Manuel Klimek579b1202012-09-07 09:26:10 +00003222TEST(HasAncestor, MatchesInTemplateInstantiations) {
3223 EXPECT_TRUE(matches(
3224 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3225 "A<int>::B::C a;",
3226 fieldDecl(hasType(asString("int")),
3227 hasAncestor(recordDecl(hasName("A"))))));
3228}
3229
3230TEST(HasAncestor, MatchesInImplicitCode) {
3231 EXPECT_TRUE(matches(
3232 "struct X {}; struct A { A() {} X x; };",
3233 constructorDecl(
3234 hasAnyConstructorInitializer(withInitializer(expr(
3235 hasAncestor(recordDecl(hasName("A")))))))));
3236}
3237
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003238TEST(HasParent, MatchesOnlyParent) {
3239 EXPECT_TRUE(matches(
3240 "void f() { if (true) { int x = 42; } }",
3241 compoundStmt(hasParent(ifStmt()))));
3242 EXPECT_TRUE(notMatches(
3243 "void f() { for (;;) { int x = 42; } }",
3244 compoundStmt(hasParent(ifStmt()))));
3245 EXPECT_TRUE(notMatches(
3246 "void f() { if (true) for (;;) { int x = 42; } }",
3247 compoundStmt(hasParent(ifStmt()))));
3248}
3249
Manuel Klimek30ace372012-12-06 14:42:48 +00003250TEST(HasAncestor, MatchesAllAncestors) {
3251 EXPECT_TRUE(matches(
3252 "template <typename T> struct C { static void f() { 42; } };"
3253 "void t() { C<int>::f(); }",
3254 integerLiteral(
3255 equals(42),
3256 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3257 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3258}
3259
3260TEST(HasParent, MatchesAllParents) {
3261 EXPECT_TRUE(matches(
3262 "template <typename T> struct C { static void f() { 42; } };"
3263 "void t() { C<int>::f(); }",
3264 integerLiteral(
3265 equals(42),
3266 hasParent(compoundStmt(hasParent(functionDecl(
3267 hasParent(recordDecl(isTemplateInstantiation())))))))));
3268 EXPECT_TRUE(matches(
3269 "template <typename T> struct C { static void f() { 42; } };"
3270 "void t() { C<int>::f(); }",
3271 integerLiteral(
3272 equals(42),
3273 hasParent(compoundStmt(hasParent(functionDecl(
3274 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3275 EXPECT_TRUE(matches(
3276 "template <typename T> struct C { static void f() { 42; } };"
3277 "void t() { C<int>::f(); }",
3278 integerLiteral(equals(42),
3279 hasParent(compoundStmt(allOf(
3280 hasParent(functionDecl(
3281 hasParent(recordDecl(isTemplateInstantiation())))),
3282 hasParent(functionDecl(hasParent(recordDecl(
3283 unless(isTemplateInstantiation())))))))))));
Manuel Klimek374516c2013-03-14 16:33:21 +00003284 EXPECT_TRUE(
3285 notMatches("template <typename T> struct C { static void f() {} };"
3286 "void t() { C<int>::f(); }",
3287 compoundStmt(hasParent(recordDecl()))));
Manuel Klimek30ace372012-12-06 14:42:48 +00003288}
3289
Daniel Jasperce620072012-10-17 08:52:59 +00003290TEST(TypeMatching, MatchesTypes) {
3291 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3292}
3293
3294TEST(TypeMatching, MatchesArrayTypes) {
3295 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3296 EXPECT_TRUE(matches("int a[42];", arrayType()));
3297 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3298
3299 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3300 arrayType(hasElementType(builtinType()))));
3301
3302 EXPECT_TRUE(matches(
3303 "int const a[] = { 2, 3 };",
3304 qualType(arrayType(hasElementType(builtinType())))));
3305 EXPECT_TRUE(matches(
3306 "int const a[] = { 2, 3 };",
3307 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3308 EXPECT_TRUE(matches(
3309 "typedef const int T; T x[] = { 1, 2 };",
3310 qualType(isConstQualified(), arrayType())));
3311
3312 EXPECT_TRUE(notMatches(
3313 "int a[] = { 2, 3 };",
3314 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3315 EXPECT_TRUE(notMatches(
3316 "int a[] = { 2, 3 };",
3317 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3318 EXPECT_TRUE(notMatches(
3319 "int const a[] = { 2, 3 };",
3320 qualType(arrayType(hasElementType(builtinType())),
3321 unless(isConstQualified()))));
3322
3323 EXPECT_TRUE(matches("int a[2];",
3324 constantArrayType(hasElementType(builtinType()))));
3325 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3326}
3327
3328TEST(TypeMatching, MatchesComplexTypes) {
3329 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3330 EXPECT_TRUE(matches(
3331 "_Complex float f;",
3332 complexType(hasElementType(builtinType()))));
3333 EXPECT_TRUE(notMatches(
3334 "_Complex float f;",
3335 complexType(hasElementType(isInteger()))));
3336}
3337
3338TEST(TypeMatching, MatchesConstantArrayTypes) {
3339 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3340 EXPECT_TRUE(notMatches(
3341 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3342 constantArrayType(hasElementType(builtinType()))));
3343
3344 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3345 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3346 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3347}
3348
3349TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3350 EXPECT_TRUE(matches(
3351 "template <typename T, int Size> class array { T data[Size]; };",
3352 dependentSizedArrayType()));
3353 EXPECT_TRUE(notMatches(
3354 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3355 dependentSizedArrayType()));
3356}
3357
3358TEST(TypeMatching, MatchesIncompleteArrayType) {
3359 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3360 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3361
3362 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3363 incompleteArrayType()));
3364}
3365
3366TEST(TypeMatching, MatchesVariableArrayType) {
3367 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3368 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3369
3370 EXPECT_TRUE(matches(
3371 "void f(int b) { int a[b]; }",
3372 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3373 varDecl(hasName("b")))))))));
3374}
3375
3376TEST(TypeMatching, MatchesAtomicTypes) {
3377 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3378
3379 EXPECT_TRUE(matches("_Atomic(int) i;",
3380 atomicType(hasValueType(isInteger()))));
3381 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3382 atomicType(hasValueType(isInteger()))));
3383}
3384
3385TEST(TypeMatching, MatchesAutoTypes) {
3386 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3387 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3388 autoType()));
3389
3390 EXPECT_TRUE(matches("auto a = 1;",
3391 autoType(hasDeducedType(isInteger()))));
3392 EXPECT_TRUE(notMatches("auto b = 2.0;",
3393 autoType(hasDeducedType(isInteger()))));
3394}
3395
Daniel Jaspera267cf62012-10-29 10:14:44 +00003396TEST(TypeMatching, MatchesFunctionTypes) {
3397 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3398 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3399}
3400
Edwin Vane88be2fd2013-04-01 18:33:34 +00003401TEST(TypeMatching, MatchesParenType) {
3402 EXPECT_TRUE(
3403 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3404 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3405
3406 EXPECT_TRUE(matches(
3407 "int (*ptr_to_func)(int);",
3408 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3409 EXPECT_TRUE(notMatches(
3410 "int (*ptr_to_array)[4];",
3411 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3412}
3413
Daniel Jasperce620072012-10-17 08:52:59 +00003414TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003415 // FIXME: Reactive when these tests can be more specific (not matching
3416 // implicit code on certain platforms), likely when we have hasDescendant for
3417 // Types/TypeLocs.
3418 //EXPECT_TRUE(matchAndVerifyResultTrue(
3419 // "int* a;",
3420 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3421 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3422 //EXPECT_TRUE(matchAndVerifyResultTrue(
3423 // "int* a;",
3424 // pointerTypeLoc().bind("loc"),
3425 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003426 EXPECT_TRUE(matches(
3427 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003428 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003429 EXPECT_TRUE(matches(
3430 "int** a;",
3431 loc(pointerType(pointee(pointerType())))));
3432 EXPECT_TRUE(matches(
3433 "int* b; int* * const a = &b;",
3434 loc(qualType(isConstQualified(), pointerType()))));
3435
3436 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003437 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3438 hasType(blockPointerType()))));
3439 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3440 hasType(memberPointerType()))));
3441 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3442 hasType(pointerType()))));
3443 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3444 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003445 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3446 hasType(lValueReferenceType()))));
3447 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3448 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003449
Daniel Jasper1802daf2012-10-17 13:35:36 +00003450 Fragment = "int *ptr;";
3451 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3452 hasType(blockPointerType()))));
3453 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3454 hasType(memberPointerType()))));
3455 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3456 hasType(pointerType()))));
3457 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3458 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003459
Daniel Jasper1802daf2012-10-17 13:35:36 +00003460 Fragment = "int a; int &ref = a;";
3461 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3462 hasType(blockPointerType()))));
3463 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3464 hasType(memberPointerType()))));
3465 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3466 hasType(pointerType()))));
3467 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3468 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003469 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3470 hasType(lValueReferenceType()))));
3471 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3472 hasType(rValueReferenceType()))));
3473
3474 Fragment = "int &&ref = 2;";
3475 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3476 hasType(blockPointerType()))));
3477 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3478 hasType(memberPointerType()))));
3479 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3480 hasType(pointerType()))));
3481 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3482 hasType(referenceType()))));
3483 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3484 hasType(lValueReferenceType()))));
3485 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3486 hasType(rValueReferenceType()))));
3487}
3488
3489TEST(TypeMatching, AutoRefTypes) {
3490 std::string Fragment = "auto a = 1;"
3491 "auto b = a;"
3492 "auto &c = a;"
3493 "auto &&d = c;"
3494 "auto &&e = 2;";
3495 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3496 hasType(referenceType()))));
3497 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3498 hasType(referenceType()))));
3499 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3500 hasType(referenceType()))));
3501 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3502 hasType(lValueReferenceType()))));
3503 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3504 hasType(rValueReferenceType()))));
3505 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3506 hasType(referenceType()))));
3507 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3508 hasType(lValueReferenceType()))));
3509 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3510 hasType(rValueReferenceType()))));
3511 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3512 hasType(referenceType()))));
3513 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3514 hasType(lValueReferenceType()))));
3515 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3516 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003517}
3518
3519TEST(TypeMatching, PointeeTypes) {
3520 EXPECT_TRUE(matches("int b; int &a = b;",
3521 referenceType(pointee(builtinType()))));
3522 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3523
3524 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003525 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003526
3527 EXPECT_TRUE(matches(
3528 "int const *A;",
3529 pointerType(pointee(isConstQualified(), builtinType()))));
3530 EXPECT_TRUE(notMatches(
3531 "int *A;",
3532 pointerType(pointee(isConstQualified(), builtinType()))));
3533}
3534
3535TEST(TypeMatching, MatchesPointersToConstTypes) {
3536 EXPECT_TRUE(matches("int b; int * const a = &b;",
3537 loc(pointerType())));
3538 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003539 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003540 EXPECT_TRUE(matches(
3541 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003542 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003543 EXPECT_TRUE(matches(
3544 "int b; const int * a = &b;",
3545 pointerType(pointee(builtinType()))));
3546}
3547
3548TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003549 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3550 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003551}
3552
Edwin Vane3abf7782013-02-25 14:49:29 +00003553TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003554 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003555 templateSpecializationType()));
3556}
3557
Edwin Vane742d9e72013-02-25 20:43:32 +00003558TEST(TypeMatching, MatchesRecordType) {
3559 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003560 EXPECT_TRUE(matches("struct S{}; S s;",
3561 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3562 EXPECT_TRUE(notMatches("int i;",
3563 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003564}
3565
3566TEST(TypeMatching, MatchesElaboratedType) {
3567 EXPECT_TRUE(matches(
3568 "namespace N {"
3569 " namespace M {"
3570 " class D {};"
3571 " }"
3572 "}"
3573 "N::M::D d;", elaboratedType()));
3574 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3575 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3576}
3577
3578TEST(ElaboratedTypeNarrowing, hasQualifier) {
3579 EXPECT_TRUE(matches(
3580 "namespace N {"
3581 " namespace M {"
3582 " class D {};"
3583 " }"
3584 "}"
3585 "N::M::D d;",
3586 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3587 EXPECT_TRUE(notMatches(
3588 "namespace M {"
3589 " class D {};"
3590 "}"
3591 "M::D d;",
3592 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003593 EXPECT_TRUE(notMatches(
3594 "struct D {"
3595 "} d;",
3596 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003597}
3598
3599TEST(ElaboratedTypeNarrowing, namesType) {
3600 EXPECT_TRUE(matches(
3601 "namespace N {"
3602 " namespace M {"
3603 " class D {};"
3604 " }"
3605 "}"
3606 "N::M::D d;",
3607 elaboratedType(elaboratedType(namesType(recordType(
3608 hasDeclaration(namedDecl(hasName("D")))))))));
3609 EXPECT_TRUE(notMatches(
3610 "namespace M {"
3611 " class D {};"
3612 "}"
3613 "M::D d;",
3614 elaboratedType(elaboratedType(namesType(typedefType())))));
3615}
3616
Daniel Jaspera7564432012-09-13 13:11:25 +00003617TEST(NNS, MatchesNestedNameSpecifiers) {
3618 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3619 nestedNameSpecifier()));
3620 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3621 nestedNameSpecifier()));
3622 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3623 nestedNameSpecifier()));
3624
3625 EXPECT_TRUE(matches(
3626 "struct A { static void f() {} }; void g() { A::f(); }",
3627 nestedNameSpecifier()));
3628 EXPECT_TRUE(notMatches(
3629 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3630 nestedNameSpecifier()));
3631}
3632
Daniel Jasperb54b7642012-09-20 14:12:57 +00003633TEST(NullStatement, SimpleCases) {
3634 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3635 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3636}
3637
Daniel Jaspera7564432012-09-13 13:11:25 +00003638TEST(NNS, MatchesTypes) {
3639 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3640 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3641 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3642 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3643 Matcher));
3644 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3645}
3646
3647TEST(NNS, MatchesNamespaceDecls) {
3648 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3649 specifiesNamespace(hasName("ns")));
3650 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3651 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3652 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3653}
3654
3655TEST(NNS, BindsNestedNameSpecifiers) {
3656 EXPECT_TRUE(matchAndVerifyResultTrue(
3657 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3658 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3659 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3660}
3661
3662TEST(NNS, BindsNestedNameSpecifierLocs) {
3663 EXPECT_TRUE(matchAndVerifyResultTrue(
3664 "namespace ns { struct B {}; } ns::B b;",
3665 loc(nestedNameSpecifier()).bind("loc"),
3666 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3667}
3668
3669TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3670 EXPECT_TRUE(matches(
3671 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3672 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3673 EXPECT_TRUE(matches(
3674 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003675 nestedNameSpecifierLoc(hasPrefix(
3676 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003677}
3678
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003679TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3680 std::string Fragment =
3681 "namespace a { struct A { struct B { struct C {}; }; }; };"
3682 "void f() { a::A::B::C c; }";
3683 EXPECT_TRUE(matches(
3684 Fragment,
3685 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3686 hasDescendant(nestedNameSpecifier(
3687 specifiesNamespace(hasName("a")))))));
3688 EXPECT_TRUE(notMatches(
3689 Fragment,
3690 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3691 has(nestedNameSpecifier(
3692 specifiesNamespace(hasName("a")))))));
3693 EXPECT_TRUE(matches(
3694 Fragment,
3695 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3696 has(nestedNameSpecifier(
3697 specifiesNamespace(hasName("a")))))));
3698
3699 // Not really useful because a NestedNameSpecifier can af at most one child,
3700 // but to complete the interface.
3701 EXPECT_TRUE(matchAndVerifyResultTrue(
3702 Fragment,
3703 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3704 forEach(nestedNameSpecifier().bind("x"))),
3705 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3706}
3707
3708TEST(NNS, NestedNameSpecifiersAsDescendants) {
3709 std::string Fragment =
3710 "namespace a { struct A { struct B { struct C {}; }; }; };"
3711 "void f() { a::A::B::C c; }";
3712 EXPECT_TRUE(matches(
3713 Fragment,
3714 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3715 asString("struct a::A")))))));
3716 EXPECT_TRUE(matchAndVerifyResultTrue(
3717 Fragment,
3718 functionDecl(hasName("f"),
3719 forEachDescendant(nestedNameSpecifier().bind("x"))),
3720 // Nested names: a, a::A and a::A::B.
3721 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3722}
3723
3724TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3725 std::string Fragment =
3726 "namespace a { struct A { struct B { struct C {}; }; }; };"
3727 "void f() { a::A::B::C c; }";
3728 EXPECT_TRUE(matches(
3729 Fragment,
3730 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3731 hasDescendant(loc(nestedNameSpecifier(
3732 specifiesNamespace(hasName("a"))))))));
3733 EXPECT_TRUE(notMatches(
3734 Fragment,
3735 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3736 has(loc(nestedNameSpecifier(
3737 specifiesNamespace(hasName("a"))))))));
3738 EXPECT_TRUE(matches(
3739 Fragment,
3740 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3741 has(loc(nestedNameSpecifier(
3742 specifiesNamespace(hasName("a"))))))));
3743
3744 EXPECT_TRUE(matchAndVerifyResultTrue(
3745 Fragment,
3746 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3747 forEach(nestedNameSpecifierLoc().bind("x"))),
3748 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3749}
3750
3751TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3752 std::string Fragment =
3753 "namespace a { struct A { struct B { struct C {}; }; }; };"
3754 "void f() { a::A::B::C c; }";
3755 EXPECT_TRUE(matches(
3756 Fragment,
3757 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3758 asString("struct a::A"))))))));
3759 EXPECT_TRUE(matchAndVerifyResultTrue(
3760 Fragment,
3761 functionDecl(hasName("f"),
3762 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3763 // Nested names: a, a::A and a::A::B.
3764 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3765}
3766
Manuel Klimek60969f52013-02-01 13:41:35 +00003767template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003768public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003769 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3770 StringRef InnerId)
3771 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003772 }
3773
Manuel Klimek60969f52013-02-01 13:41:35 +00003774 virtual bool run(const BoundNodes *Nodes) { return false; }
3775
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003776 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3777 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003778 return selectFirst<const T>(InnerId,
3779 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003780 }
3781private:
3782 std::string Id;
3783 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003784 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003785};
3786
3787TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003788 EXPECT_TRUE(matchAndVerifyResultTrue(
3789 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3790 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003791 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
3792 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003793 EXPECT_TRUE(matchAndVerifyResultFalse(
3794 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3795 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003796 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
3797 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003798}
3799
3800TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003801 EXPECT_TRUE(matchAndVerifyResultTrue(
3802 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003803 new VerifyMatchOnNode<clang::Stmt>(
3804 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003805 EXPECT_TRUE(matchAndVerifyResultFalse(
3806 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003807 new VerifyMatchOnNode<clang::Stmt>(
3808 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003809}
3810
3811TEST(MatchFinder, CanMatchSingleNodesRecursively) {
3812 EXPECT_TRUE(matchAndVerifyResultTrue(
3813 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3814 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003815 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003816 EXPECT_TRUE(matchAndVerifyResultFalse(
3817 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3818 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003819 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003820}
3821
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00003822template <typename T>
3823class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
3824public:
3825 virtual bool run(const BoundNodes *Nodes) { return false; }
3826
3827 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3828 const T *Node = Nodes->getNodeAs<T>("");
3829 return verify(*Nodes, *Context, Node);
3830 }
3831
3832 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
3833 return selectFirst<const T>(
3834 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
3835 *Node, Context)) != NULL;
3836 }
3837 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
3838 return selectFirst<const T>(
3839 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
3840 *Node, Context)) != NULL;
3841 }
3842};
3843
3844TEST(IsEqualTo, MatchesNodesByIdentity) {
3845 EXPECT_TRUE(matchAndVerifyResultTrue(
3846 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
3847 new VerifyAncestorHasChildIsEqual<Decl>()));
3848 EXPECT_TRUE(
3849 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
3850 new VerifyAncestorHasChildIsEqual<Stmt>()));
3851}
3852
Manuel Klimeke5793282012-11-02 01:31:03 +00003853class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3854public:
3855 VerifyStartOfTranslationUnit() : Called(false) {}
3856 virtual void run(const MatchFinder::MatchResult &Result) {
3857 EXPECT_TRUE(Called);
3858 }
3859 virtual void onStartOfTranslationUnit() {
3860 Called = true;
3861 }
3862 bool Called;
3863};
3864
3865TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3866 MatchFinder Finder;
3867 VerifyStartOfTranslationUnit VerifyCallback;
3868 Finder.addMatcher(decl(), &VerifyCallback);
3869 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3870 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3871 EXPECT_TRUE(VerifyCallback.Called);
3872}
3873
Manuel Klimek4da21662012-07-06 05:48:52 +00003874} // end namespace ast_matchers
3875} // end namespace clang