blob: 9f0d7afb95770a0ced1a02f3e53d61f17d31d0ec [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 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000340 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000341 EXPECT_TRUE(notMatches(
342 "namespace N {"
343 " namespace M {"
344 " class D {};"
345 " }"
346 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000347 recordDecl(hasDeclContext(namespaceDecl(hasName("N"))))));
348
349 EXPECT_TRUE(matches("namespace {"
350 " namespace M {"
351 " class D {};"
352 " }"
353 "}",
354 recordDecl(hasDeclContext(namespaceDecl(
355 hasName("M"), hasDeclContext(namespaceDecl()))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000356}
357
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000358TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000359 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000360 EXPECT_TRUE(notMatches("class X;", ClassX));
361 EXPECT_TRUE(notMatches("class X {};", ClassX));
362}
363
364TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000365 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000366 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
367 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
368}
369
370TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
371 EXPECT_TRUE(notMatches("template<typename T> class X { };"
372 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000373 classTemplateDecl(hasName("X"),
374 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000375}
376
377TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
378 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
379 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000380 classTemplateDecl(hasName("X"),
381 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000382}
383
Daniel Jasper6a124492012-07-12 08:50:38 +0000384TEST(AllOf, AllOverloadsWork) {
385 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000386 "struct T { };"
387 "int f(int, T*, int, int);"
388 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000389 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000390 callExpr(allOf(callee(functionDecl(hasName("f"))),
391 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000392 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000393 callExpr(allOf(callee(functionDecl(hasName("f"))),
394 hasArgument(0, declRefExpr(to(varDecl()))),
395 hasArgument(1, hasType(pointsTo(
396 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000397 EXPECT_TRUE(matches(Program,
398 callExpr(allOf(callee(functionDecl(hasName("f"))),
399 hasArgument(0, declRefExpr(to(varDecl()))),
400 hasArgument(1, hasType(pointsTo(
401 recordDecl(hasName("T"))))),
402 hasArgument(2, integerLiteral(equals(3)))))));
403 EXPECT_TRUE(matches(Program,
404 callExpr(allOf(callee(functionDecl(hasName("f"))),
405 hasArgument(0, declRefExpr(to(varDecl()))),
406 hasArgument(1, hasType(pointsTo(
407 recordDecl(hasName("T"))))),
408 hasArgument(2, integerLiteral(equals(3))),
409 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000410}
411
Manuel Klimek4da21662012-07-06 05:48:52 +0000412TEST(DeclarationMatcher, MatchAnyOf) {
413 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000414 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000415 EXPECT_TRUE(
416 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
417 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
418 EXPECT_TRUE(
419 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
420 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
421
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000422 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000423 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000424 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
425 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
426
Manuel Klimek4da21662012-07-06 05:48:52 +0000427 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000428 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
429 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000430 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
431 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
432 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
433 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
434 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
435 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
436}
437
438TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000439 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000440 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
441 EXPECT_TRUE(matches("class X {};", HasClassX));
442
443 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000444 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000445 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
446 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
447 EXPECT_TRUE(
448 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
449}
450
451TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
452 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000453 recordDecl(
454 has(recordDecl(
455 has(recordDecl(hasName("X"))),
456 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000457 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000458 has(recordDecl(
459 has(recordDecl(hasName("A"))),
460 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000461 hasName("C"))),
462 hasName("F"));
463
464 EXPECT_TRUE(matches(
465 "class F {"
466 " class Z {"
467 " class X {};"
468 " class Y {};"
469 " };"
470 " class C {"
471 " class A {};"
472 " class B {};"
473 " };"
474 "};", Recursive));
475
476 EXPECT_TRUE(matches(
477 "class F {"
478 " class Z {"
479 " class A {};"
480 " class X {};"
481 " class Y {};"
482 " };"
483 " class C {"
484 " class X {};"
485 " class A {};"
486 " class B {};"
487 " };"
488 "};", Recursive));
489
490 EXPECT_TRUE(matches(
491 "class O1 {"
492 " class O2 {"
493 " class F {"
494 " class Z {"
495 " class A {};"
496 " class X {};"
497 " class Y {};"
498 " };"
499 " class C {"
500 " class X {};"
501 " class A {};"
502 " class B {};"
503 " };"
504 " };"
505 " };"
506 "};", Recursive));
507}
508
509TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
510 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000511 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000512 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000513 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000514 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000516 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000517 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000518 hasName("Y"))),
519 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000520 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000521 anyOf(
522 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000523 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000524 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000525 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000526 hasName("B")))))),
527 hasName("F")));
528
529 EXPECT_TRUE(matches("class F {};", Recursive));
530 EXPECT_TRUE(matches("class Z {};", Recursive));
531 EXPECT_TRUE(matches("class C {};", Recursive));
532 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
533 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
534 EXPECT_TRUE(
535 matches("class O1 { class O2 {"
536 " class M { class N { class B {}; }; }; "
537 "}; };", Recursive));
538}
539
540TEST(DeclarationMatcher, MatchNot) {
541 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000542 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000543 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000544 unless(hasName("X")));
545 EXPECT_TRUE(notMatches("", NotClassX));
546 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
547 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
548 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
549 EXPECT_TRUE(
550 notMatches("class Y {}; class Z {}; class X : public Y {};",
551 NotClassX));
552
553 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000554 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000555 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000556 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000557 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000558 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000559 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
560 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
561 ClassXHasNotClassY));
562}
563
564TEST(DeclarationMatcher, HasDescendant) {
565 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000566 recordDecl(
567 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000568 hasName("Z"));
569 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
570 EXPECT_TRUE(
571 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
572 EXPECT_TRUE(
573 matches("class Z { class A { class Y { class X {}; }; }; };",
574 ZDescendantClassX));
575 EXPECT_TRUE(
576 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
577 ZDescendantClassX));
578 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
579
580 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000581 recordDecl(
582 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000583 hasName("X"))),
584 hasName("Z"));
585 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
586 ZDescendantClassXHasClassY));
587 EXPECT_TRUE(
588 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
589 ZDescendantClassXHasClassY));
590 EXPECT_TRUE(notMatches(
591 "class Z {"
592 " class A {"
593 " class B {"
594 " class X {"
595 " class C {"
596 " class Y {};"
597 " };"
598 " };"
599 " }; "
600 " };"
601 "};", ZDescendantClassXHasClassY));
602
603 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000604 recordDecl(
605 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
606 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000607 hasName("Z"));
608 EXPECT_TRUE(
609 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
610 ZDescendantClassXDescendantClassY));
611 EXPECT_TRUE(matches(
612 "class Z {"
613 " class A {"
614 " class X {"
615 " class B {"
616 " class Y {};"
617 " };"
618 " class Y {};"
619 " };"
620 " };"
621 "};", ZDescendantClassXDescendantClassY));
622}
623
Daniel Jaspera267cf62012-10-29 10:14:44 +0000624// Implements a run method that returns whether BoundNodes contains a
625// Decl bound to Id that can be dynamically cast to T.
626// Optionally checks that the check succeeded a specific number of times.
627template <typename T>
628class VerifyIdIsBoundTo : public BoundNodesCallback {
629public:
630 // Create an object that checks that a node of type \c T was bound to \c Id.
631 // Does not check for a certain number of matches.
632 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
633 : Id(Id), ExpectedCount(-1), Count(0) {}
634
635 // Create an object that checks that a node of type \c T was bound to \c Id.
636 // Checks that there were exactly \c ExpectedCount matches.
637 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
638 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
639
640 // Create an object that checks that a node of type \c T was bound to \c Id.
641 // Checks that there was exactly one match with the name \c ExpectedName.
642 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimek374516c2013-03-14 16:33:21 +0000643 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
644 int ExpectedCount = 1)
645 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
646 ExpectedName(ExpectedName) {}
Daniel Jaspera267cf62012-10-29 10:14:44 +0000647
648 ~VerifyIdIsBoundTo() {
649 if (ExpectedCount != -1)
650 EXPECT_EQ(ExpectedCount, Count);
651 if (!ExpectedName.empty())
652 EXPECT_EQ(ExpectedName, Name);
653 }
654
655 virtual bool run(const BoundNodes *Nodes) {
656 if (Nodes->getNodeAs<T>(Id)) {
657 ++Count;
658 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
659 Name = Named->getNameAsString();
660 } else if (const NestedNameSpecifier *NNS =
661 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
662 llvm::raw_string_ostream OS(Name);
663 NNS->print(OS, PrintingPolicy(LangOptions()));
664 }
665 return true;
666 }
667 return false;
668 }
669
Daniel Jasper452abbc2012-10-29 10:48:25 +0000670 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
671 return run(Nodes);
672 }
673
Daniel Jaspera267cf62012-10-29 10:14:44 +0000674private:
675 const std::string Id;
676 const int ExpectedCount;
677 int Count;
678 const std::string ExpectedName;
679 std::string Name;
680};
681
682TEST(HasDescendant, MatchesDescendantTypes) {
683 EXPECT_TRUE(matches("void f() { int i = 3; }",
684 decl(hasDescendant(loc(builtinType())))));
685 EXPECT_TRUE(matches("void f() { int i = 3; }",
686 stmt(hasDescendant(builtinType()))));
687
688 EXPECT_TRUE(matches("void f() { int i = 3; }",
689 stmt(hasDescendant(loc(builtinType())))));
690 EXPECT_TRUE(matches("void f() { int i = 3; }",
691 stmt(hasDescendant(qualType(builtinType())))));
692
693 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
694 stmt(hasDescendant(isInteger()))));
695
696 EXPECT_TRUE(matchAndVerifyResultTrue(
697 "void f() { int a; float c; int d; int e; }",
698 functionDecl(forEachDescendant(
699 varDecl(hasDescendant(isInteger())).bind("x"))),
700 new VerifyIdIsBoundTo<Decl>("x", 3)));
701}
702
703TEST(HasDescendant, MatchesDescendantsOfTypes) {
704 EXPECT_TRUE(matches("void f() { int*** i; }",
705 qualType(hasDescendant(builtinType()))));
706 EXPECT_TRUE(matches("void f() { int*** i; }",
707 qualType(hasDescendant(
708 pointerType(pointee(builtinType()))))));
709 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000710 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000711
712 EXPECT_TRUE(matchAndVerifyResultTrue(
713 "void f() { int*** i; }",
714 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
715 new VerifyIdIsBoundTo<Type>("x", 2)));
716}
717
718TEST(Has, MatchesChildrenOfTypes) {
719 EXPECT_TRUE(matches("int i;",
720 varDecl(hasName("i"), has(isInteger()))));
721 EXPECT_TRUE(notMatches("int** i;",
722 varDecl(hasName("i"), has(isInteger()))));
723 EXPECT_TRUE(matchAndVerifyResultTrue(
724 "int (*f)(float, int);",
725 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
726 new VerifyIdIsBoundTo<QualType>("x", 2)));
727}
728
729TEST(Has, MatchesChildTypes) {
730 EXPECT_TRUE(matches(
731 "int* i;",
732 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
733 EXPECT_TRUE(notMatches(
734 "int* i;",
735 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
736}
737
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000738TEST(Enum, DoesNotMatchClasses) {
739 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
740}
741
742TEST(Enum, MatchesEnums) {
743 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
744}
745
746TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000747 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000748 EXPECT_TRUE(matches("enum X{ A };", Matcher));
749 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
750 EXPECT_TRUE(notMatches("enum X {};", Matcher));
751}
752
Manuel Klimek4da21662012-07-06 05:48:52 +0000753TEST(StatementMatcher, Has) {
754 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000755 expr(hasType(pointsTo(recordDecl(hasName("X")))),
756 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000757
758 EXPECT_TRUE(matches(
759 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
760 EXPECT_TRUE(notMatches(
761 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
762}
763
764TEST(StatementMatcher, HasDescendant) {
765 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000766 expr(hasType(pointsTo(recordDecl(hasName("X")))),
767 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000768
769 EXPECT_TRUE(matches(
770 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
771 HasDescendantVariableI));
772 EXPECT_TRUE(notMatches(
773 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
774 HasDescendantVariableI));
775}
776
777TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000779
780 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
781 EXPECT_TRUE(notMatches("class A {};", TypeA));
782
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000783 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000784
785 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
786 TypeDerivedFromA));
787 EXPECT_TRUE(notMatches("class A {};", TypeA));
788
789 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000790 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000791
792 EXPECT_TRUE(
793 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
794}
795
Manuel Klimek4da21662012-07-06 05:48:52 +0000796TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000797 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000798
799 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000800 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000801
802 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000803 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000804
805 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000806 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000807
808 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
809 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000810 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000811
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 StatementMatcher MethodX =
813 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000814
815 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
816 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000817 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000818}
819
820TEST(Matcher, BindTheSameNameInAlternatives) {
821 StatementMatcher matcher = anyOf(
822 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000823 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000824 hasRHS(integerLiteral(equals(0)))),
825 binaryOperator(hasOperatorName("+"),
826 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000827 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000828
829 EXPECT_TRUE(matchAndVerifyResultTrue(
830 // The first branch of the matcher binds x to 0 but then fails.
831 // The second branch binds x to f() and succeeds.
832 "int f() { return 0 + f(); }",
833 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000834 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000835}
836
Manuel Klimek66341c52012-08-30 19:41:06 +0000837TEST(Matcher, BindsIDForMemoizedResults) {
838 // Using the same matcher in two match expressions will make memoization
839 // kick in.
840 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
841 EXPECT_TRUE(matchAndVerifyResultTrue(
842 "class A { class B { class X {}; }; };",
843 DeclarationMatcher(anyOf(
844 recordDecl(hasName("A"), hasDescendant(ClassX)),
845 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000846 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000847}
848
Daniel Jasper189f2e42012-12-03 15:43:25 +0000849TEST(HasDeclaration, HasDeclarationOfEnumType) {
850 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
851 expr(hasType(pointsTo(
852 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
853}
854
Edwin Vaneb45083d2013-02-25 14:32:42 +0000855TEST(HasDeclaration, HasGetDeclTraitTest) {
856 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
857 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
858 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
859}
860
Edwin Vane52380602013-02-19 17:14:34 +0000861TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
862 EXPECT_TRUE(matches("typedef int X; X a;",
863 varDecl(hasName("a"),
864 hasType(typedefType(hasDeclaration(decl()))))));
865
866 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
867}
868
Edwin Vane3abf7782013-02-25 14:49:29 +0000869TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
870 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
871 varDecl(hasType(templateSpecializationType(
872 hasDeclaration(namedDecl(hasName("A"))))))));
873}
874
Manuel Klimek4da21662012-07-06 05:48:52 +0000875TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000876 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000877 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000878 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000879 EXPECT_TRUE(
880 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000881 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000882 EXPECT_TRUE(
883 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000884 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000885}
886
887TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000888 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000889 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000890 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000891 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000892 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000893 EXPECT_TRUE(
894 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000895 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000896}
897
898TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000899 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000900 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000901 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000902 EXPECT_TRUE(
903 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000904 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000905}
906
907TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000908 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000909 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000910 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000911 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000912 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000913}
914
915TEST(Matcher, Call) {
916 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000917 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000918 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000919
920 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
921 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
922
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000923 StatementMatcher MethodOnY =
924 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000925
926 EXPECT_TRUE(
927 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
928 MethodOnY));
929 EXPECT_TRUE(
930 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
931 MethodOnY));
932 EXPECT_TRUE(
933 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
934 MethodOnY));
935 EXPECT_TRUE(
936 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
937 MethodOnY));
938 EXPECT_TRUE(
939 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
940 MethodOnY));
941
942 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000943 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000944
945 EXPECT_TRUE(
946 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
947 MethodOnYPointer));
948 EXPECT_TRUE(
949 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
950 MethodOnYPointer));
951 EXPECT_TRUE(
952 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
953 MethodOnYPointer));
954 EXPECT_TRUE(
955 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
956 MethodOnYPointer));
957 EXPECT_TRUE(
958 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
959 MethodOnYPointer));
960}
961
Daniel Jasper31f7c082012-10-01 13:40:41 +0000962TEST(Matcher, Lambda) {
963 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
964 lambdaExpr()));
965}
966
967TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000968 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
969 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000970 forRangeStmt()));
971 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
972 forRangeStmt()));
973}
974
975TEST(Matcher, UserDefinedLiteral) {
976 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
977 " return i + 1;"
978 "}"
979 "char c = 'a'_inc;",
980 userDefinedLiteral()));
981}
982
Daniel Jasperb54b7642012-09-20 14:12:57 +0000983TEST(Matcher, FlowControl) {
984 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
985 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
986 continueStmt()));
987 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
988 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
989 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
990}
991
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000992TEST(HasType, MatchesAsString) {
993 EXPECT_TRUE(
994 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000995 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000996 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000997 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000998 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000999 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001000 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001001 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001002}
1003
Manuel Klimek4da21662012-07-06 05:48:52 +00001004TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001005 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001006 // Unary operator
1007 EXPECT_TRUE(matches("class Y { }; "
1008 "bool operator!(Y x) { return false; }; "
1009 "Y y; bool c = !y;", OpCall));
1010 // No match -- special operators like "new", "delete"
1011 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1012 // we need to figure out include paths in the test.
1013 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1014 // "class Y { }; "
1015 // "void *operator new(size_t size) { return 0; } "
1016 // "Y *y = new Y;", OpCall));
1017 EXPECT_TRUE(notMatches("class Y { }; "
1018 "void operator delete(void *p) { } "
1019 "void a() {Y *y = new Y; delete y;}", OpCall));
1020 // Binary operator
1021 EXPECT_TRUE(matches("class Y { }; "
1022 "bool operator&&(Y x, Y y) { return true; }; "
1023 "Y a; Y b; bool c = a && b;",
1024 OpCall));
1025 // No match -- normal operator, not an overloaded one.
1026 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1027 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1028}
1029
1030TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1031 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001032 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001033 EXPECT_TRUE(matches("class Y { }; "
1034 "bool operator&&(Y x, Y y) { return true; }; "
1035 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1036 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001037 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001038 EXPECT_TRUE(notMatches("class Y { }; "
1039 "bool operator&&(Y x, Y y) { return true; }; "
1040 "Y a; Y b; bool c = a && b;",
1041 OpCallLessLess));
Edwin Vane6a19a972013-03-06 17:02:57 +00001042 DeclarationMatcher ClassWithOpStar =
1043 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1044 EXPECT_TRUE(matches("class Y { int operator*(); };",
1045 ClassWithOpStar));
1046 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1047 ClassWithOpStar)) ;
Manuel Klimek4da21662012-07-06 05:48:52 +00001048}
1049
Daniel Jasper278057f2012-11-15 03:29:05 +00001050TEST(Matcher, NestedOverloadedOperatorCalls) {
1051 EXPECT_TRUE(matchAndVerifyResultTrue(
1052 "class Y { }; "
1053 "Y& operator&&(Y& x, Y& y) { return x; }; "
1054 "Y a; Y b; Y c; Y d = a && b && c;",
1055 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1056 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1057 EXPECT_TRUE(matches(
1058 "class Y { }; "
1059 "Y& operator&&(Y& x, Y& y) { return x; }; "
1060 "Y a; Y b; Y c; Y d = a && b && c;",
1061 operatorCallExpr(hasParent(operatorCallExpr()))));
1062 EXPECT_TRUE(matches(
1063 "class Y { }; "
1064 "Y& operator&&(Y& x, Y& y) { return x; }; "
1065 "Y a; Y b; Y c; Y d = a && b && c;",
1066 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1067}
1068
Manuel Klimek4da21662012-07-06 05:48:52 +00001069TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001070 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001071 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001072
1073 EXPECT_TRUE(
1074 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1075 MethodOnY));
1076 EXPECT_TRUE(
1077 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1078 MethodOnY));
1079 EXPECT_TRUE(
1080 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1081 MethodOnY));
1082 EXPECT_TRUE(
1083 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1084 MethodOnY));
1085 EXPECT_TRUE(
1086 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1087 MethodOnY));
1088
1089 EXPECT_TRUE(matches(
1090 "class Y {"
1091 " public: virtual void x();"
1092 "};"
1093 "class X : public Y {"
1094 " public: virtual void x();"
1095 "};"
1096 "void z() { X *x; x->Y::x(); }", MethodOnY));
1097}
1098
1099TEST(Matcher, VariableUsage) {
1100 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001101 declRefExpr(to(
1102 varDecl(hasInitializer(
1103 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001104
1105 EXPECT_TRUE(matches(
1106 "class Y {"
1107 " public:"
1108 " bool x() const;"
1109 "};"
1110 "void z(const Y &y) {"
1111 " bool b = y.x();"
1112 " if (b) {}"
1113 "}", Reference));
1114
1115 EXPECT_TRUE(notMatches(
1116 "class Y {"
1117 " public:"
1118 " bool x() const;"
1119 "};"
1120 "void z(const Y &y) {"
1121 " bool b = y.x();"
1122 "}", Reference));
1123}
1124
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001125TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001126 EXPECT_TRUE(matches(
1127 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001128 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001129}
1130
Manuel Klimek4da21662012-07-06 05:48:52 +00001131TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001132 StatementMatcher CallOnVariableY =
1133 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001134
1135 EXPECT_TRUE(matches(
1136 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1137 EXPECT_TRUE(matches(
1138 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1139 EXPECT_TRUE(matches(
1140 "class Y { public: void x(); };"
1141 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1142 EXPECT_TRUE(matches(
1143 "class Y { public: void x(); };"
1144 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1145 EXPECT_TRUE(notMatches(
1146 "class Y { public: void x(); };"
1147 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1148 CallOnVariableY));
1149}
1150
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001151TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1152 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1153 unaryExprOrTypeTraitExpr()));
1154 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1155 alignOfExpr(anything())));
1156 // FIXME: Uncomment once alignof is enabled.
1157 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1158 // unaryExprOrTypeTraitExpr()));
1159 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1160 // sizeOfExpr()));
1161}
1162
1163TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1164 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1165 hasArgumentOfType(asString("int")))));
1166 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1167 hasArgumentOfType(asString("float")))));
1168 EXPECT_TRUE(matches(
1169 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001170 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001171 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001172 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001173}
1174
Manuel Klimek4da21662012-07-06 05:48:52 +00001175TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001176 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001177}
1178
1179TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001180 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001181}
1182
1183TEST(MemberExpression, MatchesVariable) {
1184 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001185 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001186 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001187 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001188 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001189 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001190}
1191
1192TEST(MemberExpression, MatchesStaticVariable) {
1193 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001194 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001195 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001196 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001197 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001198 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001199}
1200
Daniel Jasper6a124492012-07-12 08:50:38 +00001201TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001202 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1203 EXPECT_TRUE(matches(
1204 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1205 callExpr(hasArgument(0, declRefExpr(
1206 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001207}
1208
1209TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001210 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001211 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001212 callExpr(hasArgument(0, declRefExpr(
1213 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001214}
1215
Manuel Klimek4da21662012-07-06 05:48:52 +00001216TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1217 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001218 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001219 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001220 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001221 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001222 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001223}
1224
1225TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1226 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001227 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001228 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001229 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001230 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001231 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001232}
1233
1234TEST(IsArrow, MatchesMemberCallsViaArrow) {
1235 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001236 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001237 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001238 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001239 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001240 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001241}
1242
1243TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001244 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001245
1246 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1247 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1248}
1249
1250TEST(Callee, MatchesMemberExpressions) {
1251 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001252 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001253 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001254 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001255}
1256
1257TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001258 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001259
1260 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1261 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1262
Manuel Klimeke265c872012-07-10 14:21:30 +00001263#if !defined(_MSC_VER)
1264 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001265 // Dependent contexts, but a non-dependent call.
1266 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1267 CallFunctionF));
1268 EXPECT_TRUE(
1269 matches("void f(); template <int N> struct S { void g() { f(); } };",
1270 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001271#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001272
1273 // Depedent calls don't match.
1274 EXPECT_TRUE(
1275 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1276 CallFunctionF));
1277 EXPECT_TRUE(
1278 notMatches("void f(int);"
1279 "template <typename T> struct S { void g(T t) { f(t); } };",
1280 CallFunctionF));
1281}
1282
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001283TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1284 EXPECT_TRUE(
1285 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001286 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001287}
1288
1289TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1290 EXPECT_TRUE(
1291 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001292 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001293}
1294
1295TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1296 EXPECT_TRUE(
1297 notMatches("void g(); template <typename T> void f(T t) {}"
1298 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001299 functionTemplateDecl(hasName("f"),
1300 hasDescendant(declRefExpr(to(
1301 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001302}
1303
Manuel Klimek4da21662012-07-06 05:48:52 +00001304TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001305 StatementMatcher CallArgumentY = callExpr(
1306 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001307
1308 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1309 EXPECT_TRUE(
1310 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1311 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1312
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001313 StatementMatcher WrongIndex = callExpr(
1314 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001315 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1316}
1317
1318TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001319 StatementMatcher CallArgumentY = callExpr(
1320 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001321 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1322 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1323 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1324}
1325
1326TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001327 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001328
1329 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1330 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1331 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1332}
1333
Daniel Jasper36e29d62012-12-04 11:54:27 +00001334TEST(Matcher, ParameterCount) {
1335 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1336 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1337 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1338 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1339 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1340}
1341
Manuel Klimek4da21662012-07-06 05:48:52 +00001342TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001343 DeclarationMatcher ReferenceClassX = varDecl(
1344 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001345 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1346 ReferenceClassX));
1347 EXPECT_TRUE(
1348 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1349 EXPECT_TRUE(
1350 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1351 EXPECT_TRUE(
1352 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1353}
1354
Edwin Vane6a19a972013-03-06 17:02:57 +00001355TEST(QualType, hasCanonicalType) {
1356 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1357 "int a;"
1358 "int_ref b = a;",
1359 varDecl(hasType(qualType(referenceType())))));
1360 EXPECT_TRUE(
1361 matches("typedef int &int_ref;"
1362 "int a;"
1363 "int_ref b = a;",
1364 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1365}
1366
Edwin Vane7b69cd02013-04-02 18:15:55 +00001367TEST(QualType, hasLocalQualifiers) {
1368 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1369 varDecl(hasType(hasLocalQualifiers()))));
1370 EXPECT_TRUE(matches("int *const j = nullptr;",
1371 varDecl(hasType(hasLocalQualifiers()))));
1372 EXPECT_TRUE(matches("int *volatile k;",
1373 varDecl(hasType(hasLocalQualifiers()))));
1374 EXPECT_TRUE(notMatches("int m;",
1375 varDecl(hasType(hasLocalQualifiers()))));
1376}
1377
Manuel Klimek4da21662012-07-06 05:48:52 +00001378TEST(HasParameter, CallsInnerMatcher) {
1379 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001380 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001381 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001382 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001383}
1384
1385TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1386 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001387 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001388}
1389
1390TEST(HasType, MatchesParameterVariableTypesStrictly) {
1391 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001392 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001393 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001394 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001395 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001396 methodDecl(hasParameter(0,
1397 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001398 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001399 methodDecl(hasParameter(0,
1400 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001401}
1402
1403TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1404 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001405 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001406 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001407 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001408}
1409
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001410TEST(Returns, MatchesReturnTypes) {
1411 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001412 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001413 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001414 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001415 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001416 functionDecl(returns(hasDeclaration(
1417 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001418}
1419
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001420TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001421 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1422 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1423 functionDecl(isExternC())));
1424 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001425}
1426
Manuel Klimek4da21662012-07-06 05:48:52 +00001427TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1428 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001429 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001430}
1431
1432TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1433 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001434 methodDecl(hasAnyParameter(hasType(pointsTo(
1435 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001436}
1437
1438TEST(HasName, MatchesParameterVariableDeclartions) {
1439 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001440 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001441 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001442 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001443}
1444
Daniel Jasper371f9392012-08-01 08:40:24 +00001445TEST(Matcher, MatchesClassTemplateSpecialization) {
1446 EXPECT_TRUE(matches("template<typename T> struct A {};"
1447 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001448 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001449 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001450 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001451 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001452 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001453}
1454
1455TEST(Matcher, MatchesTypeTemplateArgument) {
1456 EXPECT_TRUE(matches(
1457 "template<typename T> struct B {};"
1458 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001459 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001460 asString("int"))))));
1461}
1462
1463TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1464 EXPECT_TRUE(matches(
1465 "struct B { int next; };"
1466 "template<int(B::*next_ptr)> struct A {};"
1467 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001468 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1469 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001470
1471 EXPECT_TRUE(notMatches(
1472 "template <typename T> struct A {};"
1473 "A<int> a;",
1474 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1475 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001476}
1477
1478TEST(Matcher, MatchesSpecificArgument) {
1479 EXPECT_TRUE(matches(
1480 "template<typename T, typename U> class A {};"
1481 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001482 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001483 1, refersToType(asString("int"))))));
1484 EXPECT_TRUE(notMatches(
1485 "template<typename T, typename U> class A {};"
1486 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001487 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001488 1, refersToType(asString("int"))))));
1489}
1490
Daniel Jasperf3197e92013-02-25 12:02:08 +00001491TEST(Matcher, MatchesAccessSpecDecls) {
1492 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1493 EXPECT_TRUE(
1494 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1495 EXPECT_TRUE(
1496 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1497 EXPECT_TRUE(
1498 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1499
1500 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1501}
1502
Edwin Vane5771a2f2013-04-09 20:46:36 +00001503TEST(Matcher, MatchesVirtualMethod) {
1504 EXPECT_TRUE(matches("class X { virtual int f(); };",
1505 methodDecl(isVirtual(), hasName("::X::f"))));
1506 EXPECT_TRUE(notMatches("class X { int f(); };",
1507 methodDecl(isVirtual())));
1508}
1509
Edwin Vane32a6ebc2013-05-09 17:00:17 +00001510TEST(Matcher, MatchesConstMethod) {
1511 EXPECT_TRUE(matches("struct A { void foo() const; };",
1512 methodDecl(isConst())));
1513 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1514 methodDecl(isConst())));
1515}
1516
Edwin Vane5771a2f2013-04-09 20:46:36 +00001517TEST(Matcher, MatchesOverridingMethod) {
1518 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1519 "class Y : public X { int f(); };",
1520 methodDecl(isOverride(), hasName("::Y::f"))));
1521 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1522 "class Y : public X { int f(); };",
1523 methodDecl(isOverride(), hasName("::X::f"))));
1524 EXPECT_TRUE(notMatches("class X { int f(); }; "
1525 "class Y : public X { int f(); };",
1526 methodDecl(isOverride())));
1527 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1528 methodDecl(isOverride())));
1529}
1530
Manuel Klimek4da21662012-07-06 05:48:52 +00001531TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001532 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001533
1534 EXPECT_TRUE(
1535 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1536 EXPECT_TRUE(
1537 matches("class X { public: X(); }; void x() { X x = X(); }",
1538 Constructor));
1539 EXPECT_TRUE(
1540 matches("class X { public: X(int); }; void x() { X x = 0; }",
1541 Constructor));
1542 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1543}
1544
1545TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001546 StatementMatcher Constructor = constructExpr(
1547 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001548
1549 EXPECT_TRUE(
1550 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1551 Constructor));
1552 EXPECT_TRUE(
1553 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1554 Constructor));
1555 EXPECT_TRUE(
1556 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1557 Constructor));
1558 EXPECT_TRUE(
1559 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1560 Constructor));
1561
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001562 StatementMatcher WrongIndex = constructExpr(
1563 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001564 EXPECT_TRUE(
1565 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1566 WrongIndex));
1567}
1568
1569TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001570 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001571
1572 EXPECT_TRUE(
1573 matches("class X { public: X(int); }; void x() { X x(0); }",
1574 Constructor1Arg));
1575 EXPECT_TRUE(
1576 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1577 Constructor1Arg));
1578 EXPECT_TRUE(
1579 matches("class X { public: X(int); }; void x() { X x = 0; }",
1580 Constructor1Arg));
1581 EXPECT_TRUE(
1582 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1583 Constructor1Arg));
1584}
1585
Manuel Klimek70b9db92012-10-23 10:40:50 +00001586TEST(Matcher,ThisExpr) {
1587 EXPECT_TRUE(
1588 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1589 EXPECT_TRUE(
1590 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1591}
1592
Manuel Klimek4da21662012-07-06 05:48:52 +00001593TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001594 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001595
1596 std::string ClassString = "class string { public: string(); ~string(); }; ";
1597
1598 EXPECT_TRUE(
1599 matches(ClassString +
1600 "string GetStringByValue();"
1601 "void FunctionTakesString(string s);"
1602 "void run() { FunctionTakesString(GetStringByValue()); }",
1603 TempExpression));
1604
1605 EXPECT_TRUE(
1606 notMatches(ClassString +
1607 "string* GetStringPointer(); "
1608 "void FunctionTakesStringPtr(string* s);"
1609 "void run() {"
1610 " string* s = GetStringPointer();"
1611 " FunctionTakesStringPtr(GetStringPointer());"
1612 " FunctionTakesStringPtr(s);"
1613 "}",
1614 TempExpression));
1615
1616 EXPECT_TRUE(
1617 notMatches("class no_dtor {};"
1618 "no_dtor GetObjByValue();"
1619 "void ConsumeObj(no_dtor param);"
1620 "void run() { ConsumeObj(GetObjByValue()); }",
1621 TempExpression));
1622}
1623
Sam Panzere16acd32012-08-24 22:04:44 +00001624TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1625 std::string ClassString =
1626 "class string { public: string(); int length(); }; ";
1627
1628 EXPECT_TRUE(
1629 matches(ClassString +
1630 "string GetStringByValue();"
1631 "void FunctionTakesString(string s);"
1632 "void run() { FunctionTakesString(GetStringByValue()); }",
1633 materializeTemporaryExpr()));
1634
1635 EXPECT_TRUE(
1636 notMatches(ClassString +
1637 "string* GetStringPointer(); "
1638 "void FunctionTakesStringPtr(string* s);"
1639 "void run() {"
1640 " string* s = GetStringPointer();"
1641 " FunctionTakesStringPtr(GetStringPointer());"
1642 " FunctionTakesStringPtr(s);"
1643 "}",
1644 materializeTemporaryExpr()));
1645
1646 EXPECT_TRUE(
1647 notMatches(ClassString +
1648 "string GetStringByValue();"
1649 "void run() { int k = GetStringByValue().length(); }",
1650 materializeTemporaryExpr()));
1651
1652 EXPECT_TRUE(
1653 notMatches(ClassString +
1654 "string GetStringByValue();"
1655 "void run() { GetStringByValue(); }",
1656 materializeTemporaryExpr()));
1657}
1658
Manuel Klimek4da21662012-07-06 05:48:52 +00001659TEST(ConstructorDeclaration, SimpleCase) {
1660 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001661 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001662 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001663 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001664}
1665
1666TEST(ConstructorDeclaration, IsImplicit) {
1667 // This one doesn't match because the constructor is not added by the
1668 // compiler (it is not needed).
1669 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001670 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001671 // The compiler added the implicit default constructor.
1672 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001673 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001674 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001675 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001676}
1677
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001678TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1679 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001680 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001681}
1682
1683TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001684 EXPECT_TRUE(notMatches("class Foo {};",
1685 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001686}
1687
Manuel Klimek4da21662012-07-06 05:48:52 +00001688TEST(HasAnyConstructorInitializer, SimpleCase) {
1689 EXPECT_TRUE(notMatches(
1690 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001691 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001692 EXPECT_TRUE(matches(
1693 "class Foo {"
1694 " Foo() : foo_() { }"
1695 " int foo_;"
1696 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001697 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001698}
1699
1700TEST(HasAnyConstructorInitializer, ForField) {
1701 static const char Code[] =
1702 "class Baz { };"
1703 "class Foo {"
1704 " Foo() : foo_() { }"
1705 " Baz foo_;"
1706 " Baz bar_;"
1707 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001708 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1709 forField(hasType(recordDecl(hasName("Baz"))))))));
1710 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001711 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001712 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1713 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001714}
1715
1716TEST(HasAnyConstructorInitializer, WithInitializer) {
1717 static const char Code[] =
1718 "class Foo {"
1719 " Foo() : foo_(0) { }"
1720 " int foo_;"
1721 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001722 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001723 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001724 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001725 withInitializer(integerLiteral(equals(1)))))));
1726}
1727
1728TEST(HasAnyConstructorInitializer, IsWritten) {
1729 static const char Code[] =
1730 "struct Bar { Bar(){} };"
1731 "class Foo {"
1732 " Foo() : foo_() { }"
1733 " Bar foo_;"
1734 " Bar bar_;"
1735 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001736 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001737 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001738 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001739 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001740 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001741 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1742}
1743
1744TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001745 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001746
1747 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1748 EXPECT_TRUE(
1749 matches("class X { public: X(); }; void x() { new X(); }", New));
1750 EXPECT_TRUE(
1751 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1752 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1753}
1754
1755TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001756 StatementMatcher New = constructExpr(
1757 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001758
1759 EXPECT_TRUE(
1760 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1761 New));
1762 EXPECT_TRUE(
1763 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1764 New));
1765 EXPECT_TRUE(
1766 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1767 New));
1768
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001769 StatementMatcher WrongIndex = constructExpr(
1770 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001771 EXPECT_TRUE(
1772 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1773 WrongIndex));
1774}
1775
1776TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001777 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001778
1779 EXPECT_TRUE(
1780 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1781 EXPECT_TRUE(
1782 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1783 New));
1784}
1785
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001786TEST(Matcher, DeleteExpression) {
1787 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001788 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001789}
1790
Manuel Klimek4da21662012-07-06 05:48:52 +00001791TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001792 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001793
1794 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1795 EXPECT_TRUE(
1796 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1797 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1798}
1799
1800TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001801 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001802 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1803 // wide string
1804 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1805 // with escaped characters
1806 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1807 // no matching -- though the data type is the same, there is no string literal
1808 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1809}
1810
1811TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001812 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001813 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1814 // wide character
1815 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1816 // wide character, Hex encoded, NOT MATCHED!
1817 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1818 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1819}
1820
1821TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001822 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001823 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1824 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1825 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1826 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1827
1828 // Non-matching cases (character literals, float and double)
1829 EXPECT_TRUE(notMatches("int i = L'a';",
1830 HasIntLiteral)); // this is actually a character
1831 // literal cast to int
1832 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1833 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1834 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1835}
1836
Daniel Jasper31f7c082012-10-01 13:40:41 +00001837TEST(Matcher, NullPtrLiteral) {
1838 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1839}
1840
Daniel Jasperb54b7642012-09-20 14:12:57 +00001841TEST(Matcher, AsmStatement) {
1842 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1843}
1844
Manuel Klimek4da21662012-07-06 05:48:52 +00001845TEST(Matcher, Conditions) {
1846 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1847
1848 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1849 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1850 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1851 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1852 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1853}
1854
1855TEST(MatchBinaryOperator, HasOperatorName) {
1856 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1857
1858 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1859 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1860}
1861
1862TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1863 StatementMatcher OperatorTrueFalse =
1864 binaryOperator(hasLHS(boolLiteral(equals(true))),
1865 hasRHS(boolLiteral(equals(false))));
1866
1867 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1868 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1869 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1870}
1871
1872TEST(MatchBinaryOperator, HasEitherOperand) {
1873 StatementMatcher HasOperand =
1874 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1875
1876 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1877 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1878 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1879}
1880
1881TEST(Matcher, BinaryOperatorTypes) {
1882 // Integration test that verifies the AST provides all binary operators in
1883 // a way we expect.
1884 // FIXME: Operator ','
1885 EXPECT_TRUE(
1886 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1887 EXPECT_TRUE(
1888 matches("bool b; bool c = (b = true);",
1889 binaryOperator(hasOperatorName("="))));
1890 EXPECT_TRUE(
1891 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1892 EXPECT_TRUE(
1893 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1894 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1895 EXPECT_TRUE(
1896 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1897 EXPECT_TRUE(
1898 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1899 EXPECT_TRUE(
1900 matches("int i = 1; int j = (i <<= 2);",
1901 binaryOperator(hasOperatorName("<<="))));
1902 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1903 EXPECT_TRUE(
1904 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1905 EXPECT_TRUE(
1906 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1907 EXPECT_TRUE(
1908 matches("int i = 1; int j = (i >>= 2);",
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 ^= 42);",
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 %= 42);",
1919 binaryOperator(hasOperatorName("%="))));
1920 EXPECT_TRUE(
1921 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1922 EXPECT_TRUE(
1923 matches("bool b = true && false;",
1924 binaryOperator(hasOperatorName("&&"))));
1925 EXPECT_TRUE(
1926 matches("bool b = true; bool c = (b &= false);",
1927 binaryOperator(hasOperatorName("&="))));
1928 EXPECT_TRUE(
1929 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1930 EXPECT_TRUE(
1931 matches("bool b = true || false;",
1932 binaryOperator(hasOperatorName("||"))));
1933 EXPECT_TRUE(
1934 matches("bool b = true; bool c = (b |= false);",
1935 binaryOperator(hasOperatorName("|="))));
1936 EXPECT_TRUE(
1937 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1938 EXPECT_TRUE(
1939 matches("int i = 42; int j = (i *= 23);",
1940 binaryOperator(hasOperatorName("*="))));
1941 EXPECT_TRUE(
1942 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1943 EXPECT_TRUE(
1944 matches("int i = 42; int j = (i /= 23);",
1945 binaryOperator(hasOperatorName("/="))));
1946 EXPECT_TRUE(
1947 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1948 EXPECT_TRUE(
1949 matches("int i = 42; int j = (i += 23);",
1950 binaryOperator(hasOperatorName("+="))));
1951 EXPECT_TRUE(
1952 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1953 EXPECT_TRUE(
1954 matches("int i = 42; int j = (i -= 23);",
1955 binaryOperator(hasOperatorName("-="))));
1956 EXPECT_TRUE(
1957 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1958 binaryOperator(hasOperatorName("->*"))));
1959 EXPECT_TRUE(
1960 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1961 binaryOperator(hasOperatorName(".*"))));
1962
1963 // Member expressions as operators are not supported in matches.
1964 EXPECT_TRUE(
1965 notMatches("struct A { void x(A *a) { a->x(this); } };",
1966 binaryOperator(hasOperatorName("->"))));
1967
1968 // Initializer assignments are not represented as operator equals.
1969 EXPECT_TRUE(
1970 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1971
1972 // Array indexing is not represented as operator.
1973 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1974
1975 // Overloaded operators do not match at all.
1976 EXPECT_TRUE(notMatches(
1977 "struct A { bool operator&&(const A &a) const { return false; } };"
1978 "void x() { A a, b; a && b; }",
1979 binaryOperator()));
1980}
1981
1982TEST(MatchUnaryOperator, HasOperatorName) {
1983 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1984
1985 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1986 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1987}
1988
1989TEST(MatchUnaryOperator, HasUnaryOperand) {
1990 StatementMatcher OperatorOnFalse =
1991 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1992
1993 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1994 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1995}
1996
1997TEST(Matcher, UnaryOperatorTypes) {
1998 // Integration test that verifies the AST provides all unary operators in
1999 // a way we expect.
2000 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2001 EXPECT_TRUE(
2002 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2003 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2004 EXPECT_TRUE(
2005 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2006 EXPECT_TRUE(
2007 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2008 EXPECT_TRUE(
2009 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2010 EXPECT_TRUE(
2011 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2012 EXPECT_TRUE(
2013 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2014 EXPECT_TRUE(
2015 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2016 EXPECT_TRUE(
2017 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2018
2019 // We don't match conversion operators.
2020 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2021
2022 // Function calls are not represented as operator.
2023 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2024
2025 // Overloaded operators do not match at all.
2026 // FIXME: We probably want to add that.
2027 EXPECT_TRUE(notMatches(
2028 "struct A { bool operator!() const { return false; } };"
2029 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2030}
2031
2032TEST(Matcher, ConditionalOperator) {
2033 StatementMatcher Conditional = conditionalOperator(
2034 hasCondition(boolLiteral(equals(true))),
2035 hasTrueExpression(boolLiteral(equals(false))));
2036
2037 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2038 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2039 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2040
2041 StatementMatcher ConditionalFalse = conditionalOperator(
2042 hasFalseExpression(boolLiteral(equals(false))));
2043
2044 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2045 EXPECT_TRUE(
2046 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2047}
2048
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002049TEST(ArraySubscriptMatchers, ArraySubscripts) {
2050 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2051 arraySubscriptExpr()));
2052 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2053 arraySubscriptExpr()));
2054}
2055
2056TEST(ArraySubscriptMatchers, ArrayIndex) {
2057 EXPECT_TRUE(matches(
2058 "int i[2]; void f() { i[1] = 1; }",
2059 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2060 EXPECT_TRUE(matches(
2061 "int i[2]; void f() { 1[i] = 1; }",
2062 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2063 EXPECT_TRUE(notMatches(
2064 "int i[2]; void f() { i[1] = 1; }",
2065 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2066}
2067
2068TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2069 EXPECT_TRUE(matches(
2070 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002071 arraySubscriptExpr(hasBase(implicitCastExpr(
2072 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002073}
2074
Manuel Klimek4da21662012-07-06 05:48:52 +00002075TEST(Matcher, HasNameSupportsNamespaces) {
2076 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002077 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002078 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002079 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002080 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002081 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002082 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002083 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002084 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002085 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002086 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002087 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002088 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002089 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002090 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002091 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002092 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002093 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002094 EXPECT_TRUE(notMatches("namespace a { namespace 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(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002097 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002098 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002099 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002100}
2101
2102TEST(Matcher, HasNameSupportsOuterClasses) {
2103 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002104 matches("class A { class B { class C; }; };",
2105 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002106 EXPECT_TRUE(
2107 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002108 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002109 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002110 matches("class A { class B { class C; }; };",
2111 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002112 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 matches("class A { class B { class C; }; };",
2114 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002115 EXPECT_TRUE(
2116 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118 EXPECT_TRUE(
2119 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002120 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002121 EXPECT_TRUE(
2122 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002123 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124 EXPECT_TRUE(
2125 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002126 recordDecl(hasName("::C"))));
2127 EXPECT_TRUE(
2128 notMatches("class A { class B { class C; }; };",
2129 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002131 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002132 EXPECT_TRUE(
2133 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002134 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002135}
2136
2137TEST(Matcher, IsDefinition) {
2138 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002139 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002140 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2141 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2142
2143 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002144 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002145 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2146 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2147
2148 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002149 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002150 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2151 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2152}
2153
2154TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002155 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002156 ofClass(hasName("X")))));
2157
2158 EXPECT_TRUE(
2159 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2160 EXPECT_TRUE(
2161 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2162 Constructor));
2163 EXPECT_TRUE(
2164 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2165 Constructor));
2166}
2167
2168TEST(Matcher, VisitsTemplateInstantiations) {
2169 EXPECT_TRUE(matches(
2170 "class A { public: void x(); };"
2171 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002172 "void f() { B<A> b; b.y(); }",
2173 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002174
2175 EXPECT_TRUE(matches(
2176 "class A { public: void x(); };"
2177 "class C {"
2178 " public:"
2179 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2180 "};"
2181 "void f() {"
2182 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002183 "}",
2184 recordDecl(hasName("C"),
2185 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002186}
2187
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002188TEST(Matcher, HandlesNullQualTypes) {
2189 // FIXME: Add a Type matcher so we can replace uses of this
2190 // variable with Type(True())
2191 const TypeMatcher AnyType = anything();
2192
2193 // We don't really care whether this matcher succeeds; we're testing that
2194 // it completes without crashing.
2195 EXPECT_TRUE(matches(
2196 "struct A { };"
2197 "template <typename T>"
2198 "void f(T t) {"
2199 " T local_t(t /* this becomes a null QualType in the AST */);"
2200 "}"
2201 "void g() {"
2202 " f(0);"
2203 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002205 anyOf(
2206 TypeMatcher(hasDeclaration(anything())),
2207 pointsTo(AnyType),
2208 references(AnyType)
2209 // Other QualType matchers should go here.
2210 ))))));
2211}
2212
Manuel Klimek4da21662012-07-06 05:48:52 +00002213// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002214AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002215 // Make sure all special variables are used: node, match_finder,
2216 // bound_nodes_builder, and the parameter named 'AMatcher'.
2217 return AMatcher.matches(Node, Finder, Builder);
2218}
2219
2220TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002221 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002222
2223 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002224 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002225
2226 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002227 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002228
2229 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002230 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002231}
2232
2233AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002234 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2235 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2236 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002237 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002238 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002239 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002240 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2241 ASTMatchFinder::BK_First);
2242}
2243
2244TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002245 DeclarationMatcher HasClassB =
2246 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002247
2248 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002249 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002250
2251 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002252 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002253
2254 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002255 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002256
2257 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002258 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002259
2260 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2261}
2262
2263TEST(For, FindsForLoops) {
2264 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2265 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002266 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2267 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002268 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002269}
2270
Daniel Jasper6a124492012-07-12 08:50:38 +00002271TEST(For, ForLoopInternals) {
2272 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2273 forStmt(hasCondition(anything()))));
2274 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2275 forStmt(hasLoopInit(anything()))));
2276}
2277
2278TEST(For, NegativeForLoopInternals) {
2279 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002280 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002281 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2282 forStmt(hasLoopInit(anything()))));
2283}
2284
Manuel Klimek4da21662012-07-06 05:48:52 +00002285TEST(For, ReportsNoFalsePositives) {
2286 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2287 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2288}
2289
2290TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002291 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2292 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2293 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002294}
2295
2296TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2297 // It's not a compound statement just because there's "{}" in the source
2298 // text. This is an AST search, not grep.
2299 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002300 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002301 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002302 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002303}
2304
Daniel Jasper6a124492012-07-12 08:50:38 +00002305TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002306 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002307 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002308 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002309 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002310 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002311 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002312 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002313 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002314}
2315
2316TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2317 // The simplest case: every compound statement is in a function
2318 // definition, and the function body itself must be a compound
2319 // statement.
2320 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002321 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002322}
2323
2324TEST(HasAnySubstatement, IsNotRecursive) {
2325 // It's really "has any immediate substatement".
2326 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002327 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002328}
2329
2330TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2331 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002332 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002333}
2334
2335TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2336 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002337 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002338}
2339
2340TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2341 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002342 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002343 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002344 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002345}
2346
2347TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2348 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002349 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002350 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002351 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002352 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002353 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002354}
2355
2356TEST(StatementCountIs, WorksWithMultipleStatements) {
2357 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002358 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002359}
2360
2361TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2362 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002363 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002364 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002365 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002366 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002367 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002368 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002369 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002370}
2371
2372TEST(Member, WorksInSimplestCase) {
2373 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002374 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002375}
2376
2377TEST(Member, DoesNotMatchTheBaseExpression) {
2378 // Don't pick out the wrong part of the member expression, this should
2379 // be checking the member (name) only.
2380 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002381 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002382}
2383
2384TEST(Member, MatchesInMemberFunctionCall) {
2385 EXPECT_TRUE(matches("void f() {"
2386 " struct { void first() {}; } s;"
2387 " s.first();"
2388 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002389 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002390}
2391
Daniel Jasperc711af22012-10-23 15:46:39 +00002392TEST(Member, MatchesMember) {
2393 EXPECT_TRUE(matches(
2394 "struct A { int i; }; void f() { A a; a.i = 2; }",
2395 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2396 EXPECT_TRUE(notMatches(
2397 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2398 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2399}
2400
Daniel Jasperf3197e92013-02-25 12:02:08 +00002401TEST(Member, UnderstandsAccess) {
2402 EXPECT_TRUE(matches(
2403 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2404 EXPECT_TRUE(notMatches(
2405 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2406 EXPECT_TRUE(notMatches(
2407 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2408
2409 EXPECT_TRUE(notMatches(
2410 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2411 EXPECT_TRUE(notMatches(
2412 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2413 EXPECT_TRUE(matches(
2414 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2415
2416 EXPECT_TRUE(notMatches(
2417 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2418 EXPECT_TRUE(matches("class A { protected: int i; };",
2419 fieldDecl(isProtected(), hasName("i"))));
2420 EXPECT_TRUE(notMatches(
2421 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2422
2423 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2424 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2425 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2426 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2427}
2428
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002429TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002430 // Fails in C++11 mode
2431 EXPECT_TRUE(matchesConditionally(
2432 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2433 "class X { void *operator new(std::size_t); };",
2434 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002435
2436 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002437 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002438
Daniel Jasper31f7c082012-10-01 13:40:41 +00002439 // Fails in C++11 mode
2440 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002441 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2442 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002443 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002444}
2445
Manuel Klimek4da21662012-07-06 05:48:52 +00002446TEST(HasObjectExpression, DoesNotMatchMember) {
2447 EXPECT_TRUE(notMatches(
2448 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002449 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002450}
2451
2452TEST(HasObjectExpression, MatchesBaseOfVariable) {
2453 EXPECT_TRUE(matches(
2454 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002455 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002456 EXPECT_TRUE(matches(
2457 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002458 memberExpr(hasObjectExpression(
2459 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002460}
2461
2462TEST(HasObjectExpression,
2463 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2464 EXPECT_TRUE(matches(
2465 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002466 memberExpr(hasObjectExpression(
2467 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002468 EXPECT_TRUE(matches(
2469 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002470 memberExpr(hasObjectExpression(
2471 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002472}
2473
2474TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002475 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2476 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2477 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2478 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002479}
2480
2481TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002482 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002483}
2484
2485TEST(IsConstQualified, MatchesConstInt) {
2486 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002487 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002488}
2489
2490TEST(IsConstQualified, MatchesConstPointer) {
2491 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002492 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002493}
2494
2495TEST(IsConstQualified, MatchesThroughTypedef) {
2496 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002497 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002498 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002499 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002500}
2501
2502TEST(IsConstQualified, DoesNotMatchInappropriately) {
2503 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002504 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002505 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002506 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002507}
2508
Sam Panzer089e5b32012-08-16 16:58:10 +00002509TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002510 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2511 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2512 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2513 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002514}
2515TEST(CastExpression, MatchesImplicitCasts) {
2516 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002517 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002518 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002519 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002520}
2521
2522TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002523 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2524 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2525 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2526 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002527}
2528
Manuel Klimek4da21662012-07-06 05:48:52 +00002529TEST(ReinterpretCast, MatchesSimpleCase) {
2530 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002531 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002532}
2533
2534TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002535 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002536 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002537 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002538 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002539 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002540 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2541 "B b;"
2542 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002543 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002544}
2545
2546TEST(FunctionalCast, MatchesSimpleCase) {
2547 std::string foo_class = "class Foo { public: Foo(char*); };";
2548 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002549 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002550}
2551
2552TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2553 std::string FooClass = "class Foo { public: Foo(char*); };";
2554 EXPECT_TRUE(
2555 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002556 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002557 EXPECT_TRUE(
2558 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002559 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002560}
2561
2562TEST(DynamicCast, MatchesSimpleCase) {
2563 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2564 "B b;"
2565 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002566 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002567}
2568
2569TEST(StaticCast, MatchesSimpleCase) {
2570 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002571 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002572}
2573
2574TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002575 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002576 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002577 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002578 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002579 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002580 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2581 "B b;"
2582 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002583 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002584}
2585
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002586TEST(CStyleCast, MatchesSimpleCase) {
2587 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2588}
2589
2590TEST(CStyleCast, DoesNotMatchOtherCasts) {
2591 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2592 "char q, *r = const_cast<char*>(&q);"
2593 "void* s = reinterpret_cast<char*>(&s);"
2594 "struct B { virtual ~B() {} }; struct D : B {};"
2595 "B b;"
2596 "D* t = dynamic_cast<D*>(&b);",
2597 cStyleCastExpr()));
2598}
2599
Manuel Klimek4da21662012-07-06 05:48:52 +00002600TEST(HasDestinationType, MatchesSimpleCase) {
2601 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002602 staticCastExpr(hasDestinationType(
2603 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002604}
2605
Sam Panzer089e5b32012-08-16 16:58:10 +00002606TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2607 // This test creates an implicit const cast.
2608 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002609 implicitCastExpr(
2610 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002611 // This test creates an implicit array-to-pointer cast.
2612 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002613 implicitCastExpr(hasImplicitDestinationType(
2614 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002615}
2616
2617TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2618 // This test creates an implicit cast from int to char.
2619 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002620 implicitCastExpr(hasImplicitDestinationType(
2621 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002622 // This test creates an implicit array-to-pointer cast.
2623 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002624 implicitCastExpr(hasImplicitDestinationType(
2625 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002626}
2627
2628TEST(ImplicitCast, MatchesSimpleCase) {
2629 // This test creates an implicit const cast.
2630 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002631 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002632 // This test creates an implicit cast from int to char.
2633 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002634 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002635 // This test creates an implicit array-to-pointer cast.
2636 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002637 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002638}
2639
2640TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002641 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002642 // are present, and that it ignores explicit and paren casts.
2643
2644 // These two test cases have no casts.
2645 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002646 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002647 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002648 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002649
2650 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002651 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002652 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002653 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002654
2655 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002656 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002657}
2658
2659TEST(IgnoringImpCasts, MatchesImpCasts) {
2660 // This test checks that ignoringImpCasts matches when implicit casts are
2661 // present and its inner matcher alone does not match.
2662 // Note that this test creates an implicit const cast.
2663 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002664 varDecl(hasInitializer(ignoringImpCasts(
2665 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002666 // This test creates an implict cast from int to char.
2667 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002668 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002669 integerLiteral(equals(0)))))));
2670}
2671
2672TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2673 // These tests verify that ignoringImpCasts does not match if the inner
2674 // matcher does not match.
2675 // Note that the first test creates an implicit const cast.
2676 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002677 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002678 unless(anything()))))));
2679 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002680 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002681 unless(anything()))))));
2682
2683 // These tests verify that ignoringImplictCasts does not look through explicit
2684 // casts or parentheses.
2685 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002686 varDecl(hasInitializer(ignoringImpCasts(
2687 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002688 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002689 varDecl(hasInitializer(ignoringImpCasts(
2690 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002691 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002692 varDecl(hasInitializer(ignoringImpCasts(
2693 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002694 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002695 varDecl(hasInitializer(ignoringImpCasts(
2696 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002697}
2698
2699TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2700 // This test verifies that expressions that do not have implicit casts
2701 // still match the inner matcher.
2702 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002703 varDecl(hasInitializer(ignoringImpCasts(
2704 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002705}
2706
2707TEST(IgnoringParenCasts, MatchesParenCasts) {
2708 // This test checks that ignoringParenCasts matches when parentheses and/or
2709 // casts are present and its inner matcher alone does not match.
2710 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002711 varDecl(hasInitializer(ignoringParenCasts(
2712 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002713 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002714 varDecl(hasInitializer(ignoringParenCasts(
2715 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002716
2717 // This test creates an implict cast from int to char in addition to the
2718 // parentheses.
2719 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002720 varDecl(hasInitializer(ignoringParenCasts(
2721 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002722
2723 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002724 varDecl(hasInitializer(ignoringParenCasts(
2725 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002726 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002727 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002728 integerLiteral(equals(0)))))));
2729}
2730
2731TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2732 // This test verifies that expressions that do not have any casts still match.
2733 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002734 varDecl(hasInitializer(ignoringParenCasts(
2735 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002736}
2737
2738TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2739 // These tests verify that ignoringImpCasts does not match if the inner
2740 // matcher does not match.
2741 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002742 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002743 unless(anything()))))));
2744
2745 // This test creates an implicit cast from int to char in addition to the
2746 // parentheses.
2747 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002748 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002749 unless(anything()))))));
2750
2751 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002752 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002753 unless(anything()))))));
2754}
2755
2756TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2757 // This test checks that ignoringParenAndImpCasts matches when
2758 // parentheses and/or implicit casts are present and its inner matcher alone
2759 // does not match.
2760 // Note that this test creates an implicit const cast.
2761 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002762 varDecl(hasInitializer(ignoringParenImpCasts(
2763 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002764 // This test creates an implicit cast from int to char.
2765 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002766 varDecl(hasInitializer(ignoringParenImpCasts(
2767 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002768}
2769
2770TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2771 // This test verifies that expressions that do not have parentheses or
2772 // implicit casts still match.
2773 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002774 varDecl(hasInitializer(ignoringParenImpCasts(
2775 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002776 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002777 varDecl(hasInitializer(ignoringParenImpCasts(
2778 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002779}
2780
2781TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2782 // These tests verify that ignoringParenImpCasts does not match if
2783 // the inner matcher does not match.
2784 // This test creates an implicit cast.
2785 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002786 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002787 unless(anything()))))));
2788 // These tests verify that ignoringParenAndImplictCasts does not look
2789 // through explicit casts.
2790 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002791 varDecl(hasInitializer(ignoringParenImpCasts(
2792 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002793 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002794 varDecl(hasInitializer(ignoringParenImpCasts(
2795 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002796 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002797 varDecl(hasInitializer(ignoringParenImpCasts(
2798 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002799}
2800
Manuel Klimek715c9562012-07-25 10:02:02 +00002801TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002802 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2803 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002804 implicitCastExpr(
2805 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002806}
2807
Manuel Klimek715c9562012-07-25 10:02:02 +00002808TEST(HasSourceExpression, MatchesExplicitCasts) {
2809 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002810 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002811 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002812 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002813}
2814
Manuel Klimek4da21662012-07-06 05:48:52 +00002815TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002816 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002817}
2818
2819TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002820 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002821}
2822
2823TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002824 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002825}
2826
2827TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002828 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002829}
2830
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002831TEST(InitListExpression, MatchesInitListExpression) {
2832 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2833 initListExpr(hasType(asString("int [2]")))));
2834 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002835 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002836}
2837
2838TEST(UsingDeclaration, MatchesUsingDeclarations) {
2839 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2840 usingDecl()));
2841}
2842
2843TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2844 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2845 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2846}
2847
2848TEST(UsingDeclaration, MatchesSpecificTarget) {
2849 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2850 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002851 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002852 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2853 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002854 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002855}
2856
2857TEST(UsingDeclaration, ThroughUsingDeclaration) {
2858 EXPECT_TRUE(matches(
2859 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002860 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002861 EXPECT_TRUE(notMatches(
2862 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002863 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002864}
2865
Sam Panzer425f41b2012-08-16 17:20:59 +00002866TEST(SingleDecl, IsSingleDecl) {
2867 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002868 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002869 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2870 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2871 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2872 SingleDeclStmt));
2873}
2874
2875TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002876 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002877
2878 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002879 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002880 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002881 declStmt(containsDeclaration(0, MatchesInit),
2882 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002883 unsigned WrongIndex = 42;
2884 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002885 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002886 MatchesInit))));
2887}
2888
2889TEST(DeclCount, DeclCountIsCorrect) {
2890 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002891 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002892 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002893 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002894 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002895 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002896}
2897
Manuel Klimek4da21662012-07-06 05:48:52 +00002898TEST(While, MatchesWhileLoops) {
2899 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2900 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2901 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2902}
2903
2904TEST(Do, MatchesDoLoops) {
2905 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2906 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2907}
2908
2909TEST(Do, DoesNotMatchWhileLoops) {
2910 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2911}
2912
2913TEST(SwitchCase, MatchesCase) {
2914 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2915 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2916 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2917 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2918}
2919
Daniel Jasperb54b7642012-09-20 14:12:57 +00002920TEST(SwitchCase, MatchesSwitch) {
2921 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2922 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2923 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2924 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2925}
2926
Peter Collingbourneacf02712013-05-10 11:52:02 +00002927TEST(SwitchCase, MatchesEachCase) {
2928 EXPECT_TRUE(notMatches("void x() { switch(42); }",
2929 switchStmt(forEachSwitchCase(caseStmt()))));
2930 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
2931 switchStmt(forEachSwitchCase(caseStmt()))));
2932 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
2933 switchStmt(forEachSwitchCase(caseStmt()))));
2934 EXPECT_TRUE(notMatches(
2935 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
2936 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
2937 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
2938 switchStmt(forEachSwitchCase(
2939 caseStmt(hasCaseConstant(integerLiteral()))))));
2940 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
2941 switchStmt(forEachSwitchCase(
2942 caseStmt(hasCaseConstant(integerLiteral()))))));
2943 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
2944 switchStmt(forEachSwitchCase(
2945 caseStmt(hasCaseConstant(integerLiteral()))))));
2946 EXPECT_TRUE(matchAndVerifyResultTrue(
2947 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
2948 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
2949 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
2950}
2951
Daniel Jasperb54b7642012-09-20 14:12:57 +00002952TEST(ExceptionHandling, SimpleCases) {
2953 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2954 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2955 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2956 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2957 throwExpr()));
2958 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2959 throwExpr()));
2960}
2961
Manuel Klimek4da21662012-07-06 05:48:52 +00002962TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2963 EXPECT_TRUE(notMatches(
2964 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002965 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002966 EXPECT_TRUE(notMatches(
2967 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002968 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002969}
2970
2971TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2972 EXPECT_TRUE(matches(
2973 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002974 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002975}
2976
2977TEST(ForEach, BindsOneNode) {
2978 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002979 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002980 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002981}
2982
2983TEST(ForEach, BindsMultipleNodes) {
2984 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002985 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002986 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002987}
2988
2989TEST(ForEach, BindsRecursiveCombinations) {
2990 EXPECT_TRUE(matchAndVerifyResultTrue(
2991 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002992 recordDecl(hasName("C"),
2993 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002994 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002995}
2996
2997TEST(ForEachDescendant, BindsOneNode) {
2998 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002999 recordDecl(hasName("C"),
3000 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003001 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003002}
3003
Daniel Jasper5f684e92012-11-16 18:39:22 +00003004TEST(ForEachDescendant, NestedForEachDescendant) {
3005 DeclarationMatcher m = recordDecl(
3006 isDefinition(), decl().bind("x"), hasName("C"));
3007 EXPECT_TRUE(matchAndVerifyResultTrue(
3008 "class A { class B { class C {}; }; };",
3009 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3010 new VerifyIdIsBoundTo<Decl>("x", "C")));
3011
Manuel Klimek054d0492013-06-19 15:42:45 +00003012 // Check that a partial match of 'm' that binds 'x' in the
3013 // first part of anyOf(m, anything()) will not overwrite the
3014 // binding created by the earlier binding in the hasDescendant.
3015 EXPECT_TRUE(matchAndVerifyResultTrue(
3016 "class A { class B { class C {}; }; };",
3017 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3018 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper5f684e92012-11-16 18:39:22 +00003019}
3020
Manuel Klimek4da21662012-07-06 05:48:52 +00003021TEST(ForEachDescendant, BindsMultipleNodes) {
3022 EXPECT_TRUE(matchAndVerifyResultTrue(
3023 "class C { class D { int x; int y; }; "
3024 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003025 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003026 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003027}
3028
3029TEST(ForEachDescendant, BindsRecursiveCombinations) {
3030 EXPECT_TRUE(matchAndVerifyResultTrue(
3031 "class C { class D { "
3032 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003033 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3034 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003035 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003036}
3037
Manuel Klimek054d0492013-06-19 15:42:45 +00003038TEST(ForEachDescendant, BindsCombinations) {
3039 EXPECT_TRUE(matchAndVerifyResultTrue(
3040 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3041 "(true) {} }",
3042 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3043 forEachDescendant(whileStmt().bind("while"))),
3044 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3045}
3046
3047TEST(Has, DoesNotDeleteBindings) {
3048 EXPECT_TRUE(matchAndVerifyResultTrue(
3049 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3050 new VerifyIdIsBoundTo<Decl>("x", 1)));
3051}
3052
3053TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3054 // Those matchers cover all the cases where an inner matcher is called
3055 // and there is not a 1:1 relationship between the match of the outer
3056 // matcher and the match of the inner matcher.
3057 // The pattern to look for is:
3058 // ... return InnerMatcher.matches(...); ...
3059 // In which case no special handling is needed.
3060 //
3061 // On the other hand, if there are multiple alternative matches
3062 // (for example forEach*) or matches might be discarded (for example has*)
3063 // the implementation must make sure that the discarded matches do not
3064 // affect the bindings.
3065 // When new such matchers are added, add a test here that:
3066 // - matches a simple node, and binds it as the first thing in the matcher:
3067 // recordDecl(decl().bind("x"), hasName("X")))
3068 // - uses the matcher under test afterwards in a way that not the first
3069 // alternative is matched; for anyOf, that means the first branch
3070 // would need to return false; for hasAncestor, it means that not
3071 // the direct parent matches the inner matcher.
3072
3073 EXPECT_TRUE(matchAndVerifyResultTrue(
3074 "class X { int y; };",
3075 recordDecl(
3076 recordDecl().bind("x"), hasName("::X"),
3077 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3078 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3079 EXPECT_TRUE(matchAndVerifyResultTrue(
3080 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3081 anyOf(unless(anything()), anything())),
3082 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3083 EXPECT_TRUE(matchAndVerifyResultTrue(
3084 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3085 classTemplateSpecializationDecl(
3086 decl().bind("x"),
3087 hasAnyTemplateArgument(refersToType(asString("int")))),
3088 new VerifyIdIsBoundTo<Decl>("x", 1)));
3089 EXPECT_TRUE(matchAndVerifyResultTrue(
3090 "class X { void f(); void g(); };",
3091 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3092 new VerifyIdIsBoundTo<Decl>("x", 1)));
3093 EXPECT_TRUE(matchAndVerifyResultTrue(
3094 "class X { X() : a(1), b(2) {} double a; int b; };",
3095 recordDecl(decl().bind("x"),
3096 has(constructorDecl(
3097 hasAnyConstructorInitializer(forField(hasName("b")))))),
3098 new VerifyIdIsBoundTo<Decl>("x", 1)));
3099 EXPECT_TRUE(matchAndVerifyResultTrue(
3100 "void x(int, int) { x(0, 42); }",
3101 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3102 new VerifyIdIsBoundTo<Expr>("x", 1)));
3103 EXPECT_TRUE(matchAndVerifyResultTrue(
3104 "void x(int, int y) {}",
3105 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3106 new VerifyIdIsBoundTo<Decl>("x", 1)));
3107 EXPECT_TRUE(matchAndVerifyResultTrue(
3108 "void x() { return; if (true) {} }",
3109 functionDecl(decl().bind("x"),
3110 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3111 new VerifyIdIsBoundTo<Decl>("x", 1)));
3112 EXPECT_TRUE(matchAndVerifyResultTrue(
3113 "namespace X { void b(int); void b(); }"
3114 "using X::b;",
3115 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3116 functionDecl(parameterCountIs(1))))),
3117 new VerifyIdIsBoundTo<Decl>("x", 1)));
3118 EXPECT_TRUE(matchAndVerifyResultTrue(
3119 "class A{}; class B{}; class C : B, A {};",
3120 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3121 new VerifyIdIsBoundTo<Decl>("x", 1)));
3122 EXPECT_TRUE(matchAndVerifyResultTrue(
3123 "class A{}; typedef A B; typedef A C; typedef A D;"
3124 "class E : A {};",
3125 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3126 new VerifyIdIsBoundTo<Decl>("x", 1)));
3127 EXPECT_TRUE(matchAndVerifyResultTrue(
3128 "class A { class B { void f() {} }; };",
3129 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3130 new VerifyIdIsBoundTo<Decl>("x", 1)));
3131 EXPECT_TRUE(matchAndVerifyResultTrue(
3132 "template <typename T> struct A { struct B {"
3133 " void f() { if(true) {} }"
3134 "}; };"
3135 "void t() { A<int>::B b; b.f(); }",
3136 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3137 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3138 EXPECT_TRUE(matchAndVerifyResultTrue(
3139 "class A {};",
3140 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3141 new VerifyIdIsBoundTo<Decl>("x", 1)));
3142}
3143
Daniel Jasper11c98772012-11-11 22:14:55 +00003144TEST(ForEachDescendant, BindsCorrectNodes) {
3145 EXPECT_TRUE(matchAndVerifyResultTrue(
3146 "class C { void f(); int i; };",
3147 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3148 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3149 EXPECT_TRUE(matchAndVerifyResultTrue(
3150 "class C { void f() {} int i; };",
3151 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3152 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3153}
3154
Manuel Klimek152ea0e2013-02-04 10:59:20 +00003155TEST(FindAll, BindsNodeOnMatch) {
3156 EXPECT_TRUE(matchAndVerifyResultTrue(
3157 "class A {};",
3158 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3159 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3160}
3161
3162TEST(FindAll, BindsDescendantNodeOnMatch) {
3163 EXPECT_TRUE(matchAndVerifyResultTrue(
3164 "class A { int a; int b; };",
3165 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3166 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3167}
3168
3169TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3170 EXPECT_TRUE(matchAndVerifyResultTrue(
3171 "class A { int a; int b; };",
3172 recordDecl(hasName("::A"),
3173 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3174 fieldDecl().bind("v"))))),
3175 new VerifyIdIsBoundTo<Decl>("v", 3)));
3176
3177 EXPECT_TRUE(matchAndVerifyResultTrue(
3178 "class A { class B {}; class C {}; };",
3179 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3180 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3181}
3182
Manuel Klimek73876732013-02-04 09:42:38 +00003183TEST(EachOf, TriggersForEachMatch) {
3184 EXPECT_TRUE(matchAndVerifyResultTrue(
3185 "class A { int a; int b; };",
3186 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3187 has(fieldDecl(hasName("b")).bind("v")))),
3188 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3189}
3190
3191TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3192 EXPECT_TRUE(matchAndVerifyResultTrue(
3193 "class A { int a; int c; };",
3194 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3195 has(fieldDecl(hasName("b")).bind("v")))),
3196 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3197 EXPECT_TRUE(matchAndVerifyResultTrue(
3198 "class A { int c; int b; };",
3199 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3200 has(fieldDecl(hasName("b")).bind("v")))),
3201 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3202 EXPECT_TRUE(notMatches(
3203 "class A { int c; int d; };",
3204 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3205 has(fieldDecl(hasName("b")).bind("v"))))));
3206}
Manuel Klimek4da21662012-07-06 05:48:52 +00003207
3208TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3209 // Make sure that we can both match the class by name (::X) and by the type
3210 // the template was instantiated with (via a field).
3211
3212 EXPECT_TRUE(matches(
3213 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003214 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003215
3216 EXPECT_TRUE(matches(
3217 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003218 recordDecl(isTemplateInstantiation(), hasDescendant(
3219 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003220}
3221
3222TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3223 EXPECT_TRUE(matches(
3224 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003225 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003226 isTemplateInstantiation())));
3227}
3228
3229TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3230 EXPECT_TRUE(matches(
3231 "template <typename T> class X { T t; }; class A {};"
3232 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003233 recordDecl(isTemplateInstantiation(), hasDescendant(
3234 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003235}
3236
3237TEST(IsTemplateInstantiation,
3238 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3239 EXPECT_TRUE(matches(
3240 "template <typename T> class X {};"
3241 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003242 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003243}
3244
3245TEST(IsTemplateInstantiation,
3246 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3247 EXPECT_TRUE(matches(
3248 "class A {};"
3249 "class X {"
3250 " template <typename U> class Y { U u; };"
3251 " Y<A> y;"
3252 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003253 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003254}
3255
3256TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3257 // FIXME: Figure out whether this makes sense. It doesn't affect the
3258 // normal use case as long as the uppermost instantiation always is marked
3259 // as template instantiation, but it might be confusing as a predicate.
3260 EXPECT_TRUE(matches(
3261 "class A {};"
3262 "template <typename T> class X {"
3263 " template <typename U> class Y { U u; };"
3264 " Y<T> y;"
3265 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003266 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003267}
3268
3269TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3270 EXPECT_TRUE(notMatches(
3271 "template <typename T> class X {}; class A {};"
3272 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003273 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003274}
3275
3276TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3277 EXPECT_TRUE(notMatches(
3278 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003279 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003280}
3281
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003282TEST(IsExplicitTemplateSpecialization,
3283 DoesNotMatchPrimaryTemplate) {
3284 EXPECT_TRUE(notMatches(
3285 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003286 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003287 EXPECT_TRUE(notMatches(
3288 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003289 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003290}
3291
3292TEST(IsExplicitTemplateSpecialization,
3293 DoesNotMatchExplicitTemplateInstantiations) {
3294 EXPECT_TRUE(notMatches(
3295 "template <typename T> class X {};"
3296 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003297 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003298 EXPECT_TRUE(notMatches(
3299 "template <typename T> void f(T t) {}"
3300 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003301 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003302}
3303
3304TEST(IsExplicitTemplateSpecialization,
3305 DoesNotMatchImplicitTemplateInstantiations) {
3306 EXPECT_TRUE(notMatches(
3307 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003308 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003309 EXPECT_TRUE(notMatches(
3310 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003311 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003312}
3313
3314TEST(IsExplicitTemplateSpecialization,
3315 MatchesExplicitTemplateSpecializations) {
3316 EXPECT_TRUE(matches(
3317 "template <typename T> class X {};"
3318 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003319 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003320 EXPECT_TRUE(matches(
3321 "template <typename T> void f(T t) {}"
3322 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003323 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003324}
3325
Manuel Klimek579b1202012-09-07 09:26:10 +00003326TEST(HasAncenstor, MatchesDeclarationAncestors) {
3327 EXPECT_TRUE(matches(
3328 "class A { class B { class C {}; }; };",
3329 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3330}
3331
3332TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3333 EXPECT_TRUE(notMatches(
3334 "class A { class B { class C {}; }; };",
3335 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3336}
3337
3338TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3339 EXPECT_TRUE(matches(
3340 "class A { class B { void f() { C c; } class C {}; }; };",
3341 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3342 hasAncestor(recordDecl(hasName("A"))))))));
3343}
3344
3345TEST(HasAncenstor, MatchesStatementAncestors) {
3346 EXPECT_TRUE(matches(
3347 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003348 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003349}
3350
3351TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3352 EXPECT_TRUE(matches(
3353 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003354 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003355}
3356
3357TEST(HasAncestor, BindsRecursiveCombinations) {
3358 EXPECT_TRUE(matchAndVerifyResultTrue(
3359 "class C { class D { class E { class F { int y; }; }; }; };",
3360 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003361 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003362}
3363
3364TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3365 EXPECT_TRUE(matchAndVerifyResultTrue(
3366 "class C { class D { class E { class F { int y; }; }; }; };",
3367 fieldDecl(hasAncestor(
3368 decl(
3369 hasDescendant(recordDecl(isDefinition(),
3370 hasAncestor(recordDecl())))
3371 ).bind("d")
3372 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003373 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003374}
3375
Manuel Klimek374516c2013-03-14 16:33:21 +00003376TEST(HasAncestor, MatchesClosestAncestor) {
3377 EXPECT_TRUE(matchAndVerifyResultTrue(
3378 "template <typename T> struct C {"
3379 " void f(int) {"
3380 " struct I { void g(T) { int x; } } i; i.g(42);"
3381 " }"
3382 "};"
3383 "template struct C<int>;",
3384 varDecl(hasName("x"),
3385 hasAncestor(functionDecl(hasParameter(
3386 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3387 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3388}
3389
Manuel Klimek579b1202012-09-07 09:26:10 +00003390TEST(HasAncestor, MatchesInTemplateInstantiations) {
3391 EXPECT_TRUE(matches(
3392 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3393 "A<int>::B::C a;",
3394 fieldDecl(hasType(asString("int")),
3395 hasAncestor(recordDecl(hasName("A"))))));
3396}
3397
3398TEST(HasAncestor, MatchesInImplicitCode) {
3399 EXPECT_TRUE(matches(
3400 "struct X {}; struct A { A() {} X x; };",
3401 constructorDecl(
3402 hasAnyConstructorInitializer(withInitializer(expr(
3403 hasAncestor(recordDecl(hasName("A")))))))));
3404}
3405
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003406TEST(HasParent, MatchesOnlyParent) {
3407 EXPECT_TRUE(matches(
3408 "void f() { if (true) { int x = 42; } }",
3409 compoundStmt(hasParent(ifStmt()))));
3410 EXPECT_TRUE(notMatches(
3411 "void f() { for (;;) { int x = 42; } }",
3412 compoundStmt(hasParent(ifStmt()))));
3413 EXPECT_TRUE(notMatches(
3414 "void f() { if (true) for (;;) { int x = 42; } }",
3415 compoundStmt(hasParent(ifStmt()))));
3416}
3417
Manuel Klimek30ace372012-12-06 14:42:48 +00003418TEST(HasAncestor, MatchesAllAncestors) {
3419 EXPECT_TRUE(matches(
3420 "template <typename T> struct C { static void f() { 42; } };"
3421 "void t() { C<int>::f(); }",
3422 integerLiteral(
3423 equals(42),
3424 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3425 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3426}
3427
3428TEST(HasParent, MatchesAllParents) {
3429 EXPECT_TRUE(matches(
3430 "template <typename T> struct C { static void f() { 42; } };"
3431 "void t() { C<int>::f(); }",
3432 integerLiteral(
3433 equals(42),
3434 hasParent(compoundStmt(hasParent(functionDecl(
3435 hasParent(recordDecl(isTemplateInstantiation())))))))));
3436 EXPECT_TRUE(matches(
3437 "template <typename T> struct C { static void f() { 42; } };"
3438 "void t() { C<int>::f(); }",
3439 integerLiteral(
3440 equals(42),
3441 hasParent(compoundStmt(hasParent(functionDecl(
3442 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3443 EXPECT_TRUE(matches(
3444 "template <typename T> struct C { static void f() { 42; } };"
3445 "void t() { C<int>::f(); }",
3446 integerLiteral(equals(42),
3447 hasParent(compoundStmt(allOf(
3448 hasParent(functionDecl(
3449 hasParent(recordDecl(isTemplateInstantiation())))),
3450 hasParent(functionDecl(hasParent(recordDecl(
3451 unless(isTemplateInstantiation())))))))))));
Manuel Klimek374516c2013-03-14 16:33:21 +00003452 EXPECT_TRUE(
3453 notMatches("template <typename T> struct C { static void f() {} };"
3454 "void t() { C<int>::f(); }",
3455 compoundStmt(hasParent(recordDecl()))));
Manuel Klimek30ace372012-12-06 14:42:48 +00003456}
3457
Daniel Jasperce620072012-10-17 08:52:59 +00003458TEST(TypeMatching, MatchesTypes) {
3459 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3460}
3461
3462TEST(TypeMatching, MatchesArrayTypes) {
3463 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3464 EXPECT_TRUE(matches("int a[42];", arrayType()));
3465 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3466
3467 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3468 arrayType(hasElementType(builtinType()))));
3469
3470 EXPECT_TRUE(matches(
3471 "int const a[] = { 2, 3 };",
3472 qualType(arrayType(hasElementType(builtinType())))));
3473 EXPECT_TRUE(matches(
3474 "int const a[] = { 2, 3 };",
3475 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3476 EXPECT_TRUE(matches(
3477 "typedef const int T; T x[] = { 1, 2 };",
3478 qualType(isConstQualified(), arrayType())));
3479
3480 EXPECT_TRUE(notMatches(
3481 "int a[] = { 2, 3 };",
3482 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3483 EXPECT_TRUE(notMatches(
3484 "int a[] = { 2, 3 };",
3485 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3486 EXPECT_TRUE(notMatches(
3487 "int const a[] = { 2, 3 };",
3488 qualType(arrayType(hasElementType(builtinType())),
3489 unless(isConstQualified()))));
3490
3491 EXPECT_TRUE(matches("int a[2];",
3492 constantArrayType(hasElementType(builtinType()))));
3493 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3494}
3495
3496TEST(TypeMatching, MatchesComplexTypes) {
3497 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3498 EXPECT_TRUE(matches(
3499 "_Complex float f;",
3500 complexType(hasElementType(builtinType()))));
3501 EXPECT_TRUE(notMatches(
3502 "_Complex float f;",
3503 complexType(hasElementType(isInteger()))));
3504}
3505
3506TEST(TypeMatching, MatchesConstantArrayTypes) {
3507 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3508 EXPECT_TRUE(notMatches(
3509 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3510 constantArrayType(hasElementType(builtinType()))));
3511
3512 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3513 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3514 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3515}
3516
3517TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3518 EXPECT_TRUE(matches(
3519 "template <typename T, int Size> class array { T data[Size]; };",
3520 dependentSizedArrayType()));
3521 EXPECT_TRUE(notMatches(
3522 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3523 dependentSizedArrayType()));
3524}
3525
3526TEST(TypeMatching, MatchesIncompleteArrayType) {
3527 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3528 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3529
3530 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3531 incompleteArrayType()));
3532}
3533
3534TEST(TypeMatching, MatchesVariableArrayType) {
3535 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3536 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3537
3538 EXPECT_TRUE(matches(
3539 "void f(int b) { int a[b]; }",
3540 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3541 varDecl(hasName("b")))))))));
3542}
3543
3544TEST(TypeMatching, MatchesAtomicTypes) {
3545 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3546
3547 EXPECT_TRUE(matches("_Atomic(int) i;",
3548 atomicType(hasValueType(isInteger()))));
3549 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3550 atomicType(hasValueType(isInteger()))));
3551}
3552
3553TEST(TypeMatching, MatchesAutoTypes) {
3554 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3555 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3556 autoType()));
3557
Richard Smith9b131752013-04-30 21:23:01 +00003558 // FIXME: Matching against the type-as-written can't work here, because the
3559 // type as written was not deduced.
3560 //EXPECT_TRUE(matches("auto a = 1;",
3561 // autoType(hasDeducedType(isInteger()))));
3562 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3563 // autoType(hasDeducedType(isInteger()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003564}
3565
Daniel Jaspera267cf62012-10-29 10:14:44 +00003566TEST(TypeMatching, MatchesFunctionTypes) {
3567 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3568 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3569}
3570
Edwin Vane88be2fd2013-04-01 18:33:34 +00003571TEST(TypeMatching, MatchesParenType) {
3572 EXPECT_TRUE(
3573 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3574 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3575
3576 EXPECT_TRUE(matches(
3577 "int (*ptr_to_func)(int);",
3578 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3579 EXPECT_TRUE(notMatches(
3580 "int (*ptr_to_array)[4];",
3581 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3582}
3583
Daniel Jasperce620072012-10-17 08:52:59 +00003584TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003585 // FIXME: Reactive when these tests can be more specific (not matching
3586 // implicit code on certain platforms), likely when we have hasDescendant for
3587 // Types/TypeLocs.
3588 //EXPECT_TRUE(matchAndVerifyResultTrue(
3589 // "int* a;",
3590 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3591 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3592 //EXPECT_TRUE(matchAndVerifyResultTrue(
3593 // "int* a;",
3594 // pointerTypeLoc().bind("loc"),
3595 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003596 EXPECT_TRUE(matches(
3597 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003598 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003599 EXPECT_TRUE(matches(
3600 "int** a;",
3601 loc(pointerType(pointee(pointerType())))));
3602 EXPECT_TRUE(matches(
3603 "int* b; int* * const a = &b;",
3604 loc(qualType(isConstQualified(), pointerType()))));
3605
3606 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003607 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3608 hasType(blockPointerType()))));
3609 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3610 hasType(memberPointerType()))));
3611 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3612 hasType(pointerType()))));
3613 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3614 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003615 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3616 hasType(lValueReferenceType()))));
3617 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3618 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003619
Daniel Jasper1802daf2012-10-17 13:35:36 +00003620 Fragment = "int *ptr;";
3621 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3622 hasType(blockPointerType()))));
3623 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3624 hasType(memberPointerType()))));
3625 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3626 hasType(pointerType()))));
3627 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3628 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003629
Daniel Jasper1802daf2012-10-17 13:35:36 +00003630 Fragment = "int a; int &ref = a;";
3631 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3632 hasType(blockPointerType()))));
3633 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3634 hasType(memberPointerType()))));
3635 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3636 hasType(pointerType()))));
3637 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3638 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003639 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3640 hasType(lValueReferenceType()))));
3641 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3642 hasType(rValueReferenceType()))));
3643
3644 Fragment = "int &&ref = 2;";
3645 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3646 hasType(blockPointerType()))));
3647 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3648 hasType(memberPointerType()))));
3649 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3650 hasType(pointerType()))));
3651 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3652 hasType(referenceType()))));
3653 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3654 hasType(lValueReferenceType()))));
3655 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3656 hasType(rValueReferenceType()))));
3657}
3658
3659TEST(TypeMatching, AutoRefTypes) {
3660 std::string Fragment = "auto a = 1;"
3661 "auto b = a;"
3662 "auto &c = a;"
3663 "auto &&d = c;"
3664 "auto &&e = 2;";
3665 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3666 hasType(referenceType()))));
3667 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3668 hasType(referenceType()))));
3669 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3670 hasType(referenceType()))));
3671 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3672 hasType(lValueReferenceType()))));
3673 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3674 hasType(rValueReferenceType()))));
3675 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3676 hasType(referenceType()))));
3677 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3678 hasType(lValueReferenceType()))));
3679 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3680 hasType(rValueReferenceType()))));
3681 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3682 hasType(referenceType()))));
3683 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3684 hasType(lValueReferenceType()))));
3685 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3686 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003687}
3688
3689TEST(TypeMatching, PointeeTypes) {
3690 EXPECT_TRUE(matches("int b; int &a = b;",
3691 referenceType(pointee(builtinType()))));
3692 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3693
3694 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003695 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003696
3697 EXPECT_TRUE(matches(
3698 "int const *A;",
3699 pointerType(pointee(isConstQualified(), builtinType()))));
3700 EXPECT_TRUE(notMatches(
3701 "int *A;",
3702 pointerType(pointee(isConstQualified(), builtinType()))));
3703}
3704
3705TEST(TypeMatching, MatchesPointersToConstTypes) {
3706 EXPECT_TRUE(matches("int b; int * const a = &b;",
3707 loc(pointerType())));
3708 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003709 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003710 EXPECT_TRUE(matches(
3711 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003712 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003713 EXPECT_TRUE(matches(
3714 "int b; const int * a = &b;",
3715 pointerType(pointee(builtinType()))));
3716}
3717
3718TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003719 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3720 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003721}
3722
Edwin Vane3abf7782013-02-25 14:49:29 +00003723TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003724 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003725 templateSpecializationType()));
3726}
3727
Edwin Vane742d9e72013-02-25 20:43:32 +00003728TEST(TypeMatching, MatchesRecordType) {
3729 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003730 EXPECT_TRUE(matches("struct S{}; S s;",
3731 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3732 EXPECT_TRUE(notMatches("int i;",
3733 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003734}
3735
3736TEST(TypeMatching, MatchesElaboratedType) {
3737 EXPECT_TRUE(matches(
3738 "namespace N {"
3739 " namespace M {"
3740 " class D {};"
3741 " }"
3742 "}"
3743 "N::M::D d;", elaboratedType()));
3744 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3745 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3746}
3747
3748TEST(ElaboratedTypeNarrowing, hasQualifier) {
3749 EXPECT_TRUE(matches(
3750 "namespace N {"
3751 " namespace M {"
3752 " class D {};"
3753 " }"
3754 "}"
3755 "N::M::D d;",
3756 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3757 EXPECT_TRUE(notMatches(
3758 "namespace M {"
3759 " class D {};"
3760 "}"
3761 "M::D d;",
3762 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003763 EXPECT_TRUE(notMatches(
3764 "struct D {"
3765 "} d;",
3766 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003767}
3768
3769TEST(ElaboratedTypeNarrowing, namesType) {
3770 EXPECT_TRUE(matches(
3771 "namespace N {"
3772 " namespace M {"
3773 " class D {};"
3774 " }"
3775 "}"
3776 "N::M::D d;",
3777 elaboratedType(elaboratedType(namesType(recordType(
3778 hasDeclaration(namedDecl(hasName("D")))))))));
3779 EXPECT_TRUE(notMatches(
3780 "namespace M {"
3781 " class D {};"
3782 "}"
3783 "M::D d;",
3784 elaboratedType(elaboratedType(namesType(typedefType())))));
3785}
3786
Daniel Jaspera7564432012-09-13 13:11:25 +00003787TEST(NNS, MatchesNestedNameSpecifiers) {
3788 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3789 nestedNameSpecifier()));
3790 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3791 nestedNameSpecifier()));
3792 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3793 nestedNameSpecifier()));
3794
3795 EXPECT_TRUE(matches(
3796 "struct A { static void f() {} }; void g() { A::f(); }",
3797 nestedNameSpecifier()));
3798 EXPECT_TRUE(notMatches(
3799 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3800 nestedNameSpecifier()));
3801}
3802
Daniel Jasperb54b7642012-09-20 14:12:57 +00003803TEST(NullStatement, SimpleCases) {
3804 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3805 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3806}
3807
Daniel Jaspera7564432012-09-13 13:11:25 +00003808TEST(NNS, MatchesTypes) {
3809 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3810 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3811 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3812 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3813 Matcher));
3814 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3815}
3816
3817TEST(NNS, MatchesNamespaceDecls) {
3818 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3819 specifiesNamespace(hasName("ns")));
3820 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3821 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3822 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3823}
3824
3825TEST(NNS, BindsNestedNameSpecifiers) {
3826 EXPECT_TRUE(matchAndVerifyResultTrue(
3827 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3828 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3829 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3830}
3831
3832TEST(NNS, BindsNestedNameSpecifierLocs) {
3833 EXPECT_TRUE(matchAndVerifyResultTrue(
3834 "namespace ns { struct B {}; } ns::B b;",
3835 loc(nestedNameSpecifier()).bind("loc"),
3836 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3837}
3838
3839TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3840 EXPECT_TRUE(matches(
3841 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3842 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3843 EXPECT_TRUE(matches(
3844 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003845 nestedNameSpecifierLoc(hasPrefix(
3846 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003847}
3848
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003849TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3850 std::string Fragment =
3851 "namespace a { struct A { struct B { struct C {}; }; }; };"
3852 "void f() { a::A::B::C c; }";
3853 EXPECT_TRUE(matches(
3854 Fragment,
3855 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3856 hasDescendant(nestedNameSpecifier(
3857 specifiesNamespace(hasName("a")))))));
3858 EXPECT_TRUE(notMatches(
3859 Fragment,
3860 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3861 has(nestedNameSpecifier(
3862 specifiesNamespace(hasName("a")))))));
3863 EXPECT_TRUE(matches(
3864 Fragment,
3865 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3866 has(nestedNameSpecifier(
3867 specifiesNamespace(hasName("a")))))));
3868
3869 // Not really useful because a NestedNameSpecifier can af at most one child,
3870 // but to complete the interface.
3871 EXPECT_TRUE(matchAndVerifyResultTrue(
3872 Fragment,
3873 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3874 forEach(nestedNameSpecifier().bind("x"))),
3875 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3876}
3877
3878TEST(NNS, NestedNameSpecifiersAsDescendants) {
3879 std::string Fragment =
3880 "namespace a { struct A { struct B { struct C {}; }; }; };"
3881 "void f() { a::A::B::C c; }";
3882 EXPECT_TRUE(matches(
3883 Fragment,
3884 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3885 asString("struct a::A")))))));
3886 EXPECT_TRUE(matchAndVerifyResultTrue(
3887 Fragment,
3888 functionDecl(hasName("f"),
3889 forEachDescendant(nestedNameSpecifier().bind("x"))),
3890 // Nested names: a, a::A and a::A::B.
3891 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3892}
3893
3894TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3895 std::string Fragment =
3896 "namespace a { struct A { struct B { struct C {}; }; }; };"
3897 "void f() { a::A::B::C c; }";
3898 EXPECT_TRUE(matches(
3899 Fragment,
3900 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3901 hasDescendant(loc(nestedNameSpecifier(
3902 specifiesNamespace(hasName("a"))))))));
3903 EXPECT_TRUE(notMatches(
3904 Fragment,
3905 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3906 has(loc(nestedNameSpecifier(
3907 specifiesNamespace(hasName("a"))))))));
3908 EXPECT_TRUE(matches(
3909 Fragment,
3910 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3911 has(loc(nestedNameSpecifier(
3912 specifiesNamespace(hasName("a"))))))));
3913
3914 EXPECT_TRUE(matchAndVerifyResultTrue(
3915 Fragment,
3916 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3917 forEach(nestedNameSpecifierLoc().bind("x"))),
3918 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3919}
3920
3921TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3922 std::string Fragment =
3923 "namespace a { struct A { struct B { struct C {}; }; }; };"
3924 "void f() { a::A::B::C c; }";
3925 EXPECT_TRUE(matches(
3926 Fragment,
3927 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3928 asString("struct a::A"))))))));
3929 EXPECT_TRUE(matchAndVerifyResultTrue(
3930 Fragment,
3931 functionDecl(hasName("f"),
3932 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3933 // Nested names: a, a::A and a::A::B.
3934 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3935}
3936
Manuel Klimek60969f52013-02-01 13:41:35 +00003937template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003938public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003939 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3940 StringRef InnerId)
3941 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003942 }
3943
Manuel Klimek60969f52013-02-01 13:41:35 +00003944 virtual bool run(const BoundNodes *Nodes) { return false; }
3945
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003946 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3947 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003948 return selectFirst<const T>(InnerId,
3949 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003950 }
3951private:
3952 std::string Id;
3953 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003954 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003955};
3956
3957TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003958 EXPECT_TRUE(matchAndVerifyResultTrue(
3959 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3960 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003961 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
3962 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003963 EXPECT_TRUE(matchAndVerifyResultFalse(
3964 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3965 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003966 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
3967 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003968}
3969
3970TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003971 EXPECT_TRUE(matchAndVerifyResultTrue(
3972 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003973 new VerifyMatchOnNode<clang::Stmt>(
3974 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003975 EXPECT_TRUE(matchAndVerifyResultFalse(
3976 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003977 new VerifyMatchOnNode<clang::Stmt>(
3978 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003979}
3980
3981TEST(MatchFinder, CanMatchSingleNodesRecursively) {
3982 EXPECT_TRUE(matchAndVerifyResultTrue(
3983 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3984 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003985 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003986 EXPECT_TRUE(matchAndVerifyResultFalse(
3987 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3988 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003989 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003990}
3991
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00003992template <typename T>
3993class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
3994public:
3995 virtual bool run(const BoundNodes *Nodes) { return false; }
3996
3997 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3998 const T *Node = Nodes->getNodeAs<T>("");
3999 return verify(*Nodes, *Context, Node);
4000 }
4001
4002 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4003 return selectFirst<const T>(
4004 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4005 *Node, Context)) != NULL;
4006 }
4007 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4008 return selectFirst<const T>(
4009 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4010 *Node, Context)) != NULL;
4011 }
4012};
4013
4014TEST(IsEqualTo, MatchesNodesByIdentity) {
4015 EXPECT_TRUE(matchAndVerifyResultTrue(
4016 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4017 new VerifyAncestorHasChildIsEqual<Decl>()));
4018 EXPECT_TRUE(
4019 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4020 new VerifyAncestorHasChildIsEqual<Stmt>()));
4021}
4022
Manuel Klimeke5793282012-11-02 01:31:03 +00004023class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4024public:
4025 VerifyStartOfTranslationUnit() : Called(false) {}
4026 virtual void run(const MatchFinder::MatchResult &Result) {
4027 EXPECT_TRUE(Called);
4028 }
4029 virtual void onStartOfTranslationUnit() {
4030 Called = true;
4031 }
4032 bool Called;
4033};
4034
4035TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4036 MatchFinder Finder;
4037 VerifyStartOfTranslationUnit VerifyCallback;
4038 Finder.addMatcher(decl(), &VerifyCallback);
4039 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4040 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4041 EXPECT_TRUE(VerifyCallback.Called);
4042}
4043
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004044class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4045public:
4046 VerifyEndOfTranslationUnit() : Called(false) {}
4047 virtual void run(const MatchFinder::MatchResult &Result) {
4048 EXPECT_FALSE(Called);
4049 }
4050 virtual void onEndOfTranslationUnit() {
4051 Called = true;
4052 }
4053 bool Called;
4054};
4055
4056TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4057 MatchFinder Finder;
4058 VerifyEndOfTranslationUnit VerifyCallback;
4059 Finder.addMatcher(decl(), &VerifyCallback);
4060 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4061 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4062 EXPECT_TRUE(VerifyCallback.Called);
4063}
4064
Manuel Klimek4da21662012-07-06 05:48:52 +00004065} // end namespace ast_matchers
4066} // end namespace clang