blob: a759df9070f562a13db235f98a983eb987c38916 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
Benjamin Kramer8b9ed712012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000014#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace ast_matchers {
19
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000020#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000021TEST(HasNameDeathTest, DiesOnEmptyName) {
22 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000023 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000024 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
25 }, "");
26}
27
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000028TEST(HasNameDeathTest, DiesOnEmptyPattern) {
29 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000030 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000031 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
32 }, "");
33}
34
Manuel Klimek4da21662012-07-06 05:48:52 +000035TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
36 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000037 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000038 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
39 }, "");
40}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000041#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000042
Manuel Klimek715c9562012-07-25 10:02:02 +000043TEST(Decl, MatchesDeclarations) {
44 EXPECT_TRUE(notMatches("", decl(usingDecl())));
45 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
46 decl(usingDecl())));
47}
48
Manuel Klimek4da21662012-07-06 05:48:52 +000049TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000050 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000051 EXPECT_TRUE(matches("typedef int X;", NamedX));
52 EXPECT_TRUE(matches("int X;", NamedX));
53 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
54 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
55 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
56 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000057 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000058
59 EXPECT_TRUE(notMatches("#define X 1", NamedX));
60}
61
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000062TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000063 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000064 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
65 EXPECT_TRUE(matches("int Xb;", NamedX));
66 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
67 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
68 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
69 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
70 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
71
72 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
73
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000074 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000075 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
76 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
77
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000078 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000079 EXPECT_TRUE(matches("int abc;", Abc));
80 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
81 EXPECT_TRUE(notMatches("int cab;", Abc));
82 EXPECT_TRUE(matches("int cabc;", Abc));
Manuel Klimekec33b6f2012-12-10 07:08:53 +000083
84 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
85 EXPECT_TRUE(matches("int k;", StartsWithK));
86 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
87 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
88 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
89 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000090}
91
Manuel Klimek4da21662012-07-06 05:48:52 +000092TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000094#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000095 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000096#else
97 // Matches class type_info.
98 EXPECT_TRUE(matches("", ClassMatcher));
99#endif
Manuel Klimek4da21662012-07-06 05:48:52 +0000100
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102 EXPECT_TRUE(matches("class X;", ClassX));
103 EXPECT_TRUE(matches("class X {};", ClassX));
104 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
105 EXPECT_TRUE(notMatches("", ClassX));
106}
107
108TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000109 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000112 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
113 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000114 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
115 EXPECT_TRUE(notMatches("", IsDerivedFromX));
116
Daniel Jasper63d88722012-09-12 21:14:15 +0000117 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000118
119 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
120 EXPECT_TRUE(matches("class X {};", IsAX));
121 EXPECT_TRUE(matches("class X;", IsAX));
122 EXPECT_TRUE(notMatches("class Y;", IsAX));
123 EXPECT_TRUE(notMatches("", IsAX));
124
Manuel Klimek4da21662012-07-06 05:48:52 +0000125 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000126 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000127 EXPECT_TRUE(
128 matches("class X {}; class Y : public X {}; class Z : public Y {};",
129 ZIsDerivedFromX));
130 EXPECT_TRUE(
131 matches("class X {};"
132 "template<class T> class Y : public X {};"
133 "class Z : public Y<int> {};", ZIsDerivedFromX));
134 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 matches("template<class T> class X {}; "
138 "template<class T> class Z : public X<T> {};",
139 ZIsDerivedFromX));
140 EXPECT_TRUE(
141 matches("template<class T, class U=T> class X {}; "
142 "template<class T> class Z : public X<T> {};",
143 ZIsDerivedFromX));
144 EXPECT_TRUE(
145 notMatches("template<class X> class A { class Z : public X {}; };",
146 ZIsDerivedFromX));
147 EXPECT_TRUE(
148 matches("template<class X> class A { public: class Z : public X {}; }; "
149 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template <class T> class X {}; "
152 "template<class Y> class A { class Z : public X<Y> {}; };",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<template<class T> class X> class A { "
156 " class Z : public X<int> {}; };", ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<template<class T> class X> class A { "
159 " public: class Z : public X<int> {}; }; "
160 "template<class T> class X {}; void y() { A<X>::Z z; }",
161 ZIsDerivedFromX));
162 EXPECT_TRUE(
163 notMatches("template<class X> class A { class Z : public X::D {}; };",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class X> class A { public: "
167 " class Z : public X::D {}; }; "
168 "class Y { public: class X {}; typedef X D; }; "
169 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
170 EXPECT_TRUE(
171 matches("class X {}; typedef X Y; class Z : public Y {};",
172 ZIsDerivedFromX));
173 EXPECT_TRUE(
174 matches("template<class T> class Y { typedef typename T::U X; "
175 " class Z : public X {}; };", ZIsDerivedFromX));
176 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
177 ZIsDerivedFromX));
178 EXPECT_TRUE(
179 notMatches("template<class T> class X {}; "
180 "template<class T> class A { class Z : public X<T>::D {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("template<class T> class X { public: typedef X<T> D; }; "
184 "template<class T> class A { public: "
185 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
186 ZIsDerivedFromX));
187 EXPECT_TRUE(
188 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 matches("class X {}; class Y : public X {}; "
195 "typedef Y V; typedef V W; class Z : public W {};",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("template<class T, class U> class X {}; "
199 "template<class T> class A { class Z : public X<T, int> {}; };",
200 ZIsDerivedFromX));
201 EXPECT_TRUE(
202 notMatches("template<class X> class D { typedef X A; typedef A B; "
203 " typedef B C; class Z : public C {}; };",
204 ZIsDerivedFromX));
205 EXPECT_TRUE(
206 matches("class X {}; typedef X A; typedef A B; "
207 "class Z : public B {};", ZIsDerivedFromX));
208 EXPECT_TRUE(
209 matches("class X {}; typedef X A; typedef A B; typedef B C; "
210 "class Z : public C {};", ZIsDerivedFromX));
211 EXPECT_TRUE(
212 matches("class U {}; typedef U X; typedef X V; "
213 "class Z : public V {};", ZIsDerivedFromX));
214 EXPECT_TRUE(
215 matches("class Base {}; typedef Base X; "
216 "class Z : public Base {};", ZIsDerivedFromX));
217 EXPECT_TRUE(
218 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
219 "class Z : public Base {};", ZIsDerivedFromX));
220 EXPECT_TRUE(
221 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
222 "class Z : public Base {};", ZIsDerivedFromX));
223 EXPECT_TRUE(
224 matches("class A {}; typedef A X; typedef A Y; "
225 "class Z : public Y {};", ZIsDerivedFromX));
226 EXPECT_TRUE(
227 notMatches("template <typename T> class Z;"
228 "template <> class Z<void> {};"
229 "template <typename T> class Z : public Z<void> {};",
230 IsDerivedFromX));
231 EXPECT_TRUE(
232 matches("template <typename T> class X;"
233 "template <> class X<void> {};"
234 "template <typename T> class X : public X<void> {};",
235 IsDerivedFromX));
236 EXPECT_TRUE(matches(
237 "class X {};"
238 "template <typename T> class Z;"
239 "template <> class Z<void> {};"
240 "template <typename T> class Z : public Z<void>, public X {};",
241 ZIsDerivedFromX));
Manuel Klimek987c2f52012-12-04 13:40:29 +0000242 EXPECT_TRUE(
243 notMatches("template<int> struct X;"
244 "template<int i> struct X : public X<i-1> {};",
245 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
246 EXPECT_TRUE(matches(
247 "struct A {};"
248 "template<int> struct X;"
249 "template<int i> struct X : public X<i-1> {};"
250 "template<> struct X<0> : public A {};"
251 "struct B : public X<42> {};",
252 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000253
254 // FIXME: Once we have better matchers for template type matching,
255 // get rid of the Variable(...) matching and match the right template
256 // declarations directly.
257 const char *RecursiveTemplateOneParameter =
258 "class Base1 {}; class Base2 {};"
259 "template <typename T> class Z;"
260 "template <> class Z<void> : public Base1 {};"
261 "template <> class Z<int> : public Base2 {};"
262 "template <> class Z<float> : public Z<void> {};"
263 "template <> class Z<double> : public Z<int> {};"
264 "template <typename T> class Z : public Z<float>, public Z<double> {};"
265 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
266 EXPECT_TRUE(matches(
267 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000268 varDecl(hasName("z_float"),
269 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000270 EXPECT_TRUE(notMatches(
271 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000272 varDecl(hasName("z_float"),
273 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000274 EXPECT_TRUE(matches(
275 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000276 varDecl(hasName("z_char"),
277 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
278 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000279
280 const char *RecursiveTemplateTwoParameters =
281 "class Base1 {}; class Base2 {};"
282 "template <typename T1, typename T2> class Z;"
283 "template <typename T> class Z<void, T> : public Base1 {};"
284 "template <typename T> class Z<int, T> : public Base2 {};"
285 "template <typename T> class Z<float, T> : public Z<void, T> {};"
286 "template <typename T> class Z<double, T> : public Z<int, T> {};"
287 "template <typename T1, typename T2> class Z : "
288 " public Z<float, T2>, public Z<double, T2> {};"
289 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
290 " Z<char, void> z_char; }";
291 EXPECT_TRUE(matches(
292 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000293 varDecl(hasName("z_float"),
294 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295 EXPECT_TRUE(notMatches(
296 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000297 varDecl(hasName("z_float"),
298 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000299 EXPECT_TRUE(matches(
300 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000301 varDecl(hasName("z_char"),
302 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
303 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000304 EXPECT_TRUE(matches(
305 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000306 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000307 EXPECT_TRUE(notMatches(
308 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000309 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000310
311 EXPECT_TRUE(matches(
312 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000313 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000314}
315
Daniel Jasper08f0c532012-09-18 14:17:42 +0000316TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
317 EXPECT_TRUE(matches(
318 "template <typename T> struct A {"
319 " template <typename T2> struct F {};"
320 "};"
321 "template <typename T> struct B : A<T>::template F<T> {};"
322 "B<int> b;",
323 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
324}
325
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000326TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000327 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000328 EXPECT_TRUE(notMatches("class X;", ClassX));
329 EXPECT_TRUE(notMatches("class X {};", ClassX));
330}
331
332TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000333 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000334 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
335 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
336}
337
338TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
339 EXPECT_TRUE(notMatches("template<typename T> class X { };"
340 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000341 classTemplateDecl(hasName("X"),
342 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000343}
344
345TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
346 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
347 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000348 classTemplateDecl(hasName("X"),
349 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000350}
351
Daniel Jasper6a124492012-07-12 08:50:38 +0000352TEST(AllOf, AllOverloadsWork) {
353 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000354 "struct T { };"
355 "int f(int, T*, int, int);"
356 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000357 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000358 callExpr(allOf(callee(functionDecl(hasName("f"))),
359 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000360 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000361 callExpr(allOf(callee(functionDecl(hasName("f"))),
362 hasArgument(0, declRefExpr(to(varDecl()))),
363 hasArgument(1, hasType(pointsTo(
364 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000365 EXPECT_TRUE(matches(Program,
366 callExpr(allOf(callee(functionDecl(hasName("f"))),
367 hasArgument(0, declRefExpr(to(varDecl()))),
368 hasArgument(1, hasType(pointsTo(
369 recordDecl(hasName("T"))))),
370 hasArgument(2, integerLiteral(equals(3)))))));
371 EXPECT_TRUE(matches(Program,
372 callExpr(allOf(callee(functionDecl(hasName("f"))),
373 hasArgument(0, declRefExpr(to(varDecl()))),
374 hasArgument(1, hasType(pointsTo(
375 recordDecl(hasName("T"))))),
376 hasArgument(2, integerLiteral(equals(3))),
377 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000378}
379
Manuel Klimek4da21662012-07-06 05:48:52 +0000380TEST(DeclarationMatcher, MatchAnyOf) {
381 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000382 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000383 EXPECT_TRUE(
384 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
385 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
386 EXPECT_TRUE(
387 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
388 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
389
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000390 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000391 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000392 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
393 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
394
Manuel Klimek4da21662012-07-06 05:48:52 +0000395 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000396 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
397 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000398 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
399 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
400 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
401 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
402 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
403 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
404}
405
406TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000407 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000408 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
409 EXPECT_TRUE(matches("class X {};", HasClassX));
410
411 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000412 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000413 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
414 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
415 EXPECT_TRUE(
416 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
417}
418
419TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
420 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000421 recordDecl(
422 has(recordDecl(
423 has(recordDecl(hasName("X"))),
424 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000425 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000426 has(recordDecl(
427 has(recordDecl(hasName("A"))),
428 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000429 hasName("C"))),
430 hasName("F"));
431
432 EXPECT_TRUE(matches(
433 "class F {"
434 " class Z {"
435 " class X {};"
436 " class Y {};"
437 " };"
438 " class C {"
439 " class A {};"
440 " class B {};"
441 " };"
442 "};", Recursive));
443
444 EXPECT_TRUE(matches(
445 "class F {"
446 " class Z {"
447 " class A {};"
448 " class X {};"
449 " class Y {};"
450 " };"
451 " class C {"
452 " class X {};"
453 " class A {};"
454 " class B {};"
455 " };"
456 "};", Recursive));
457
458 EXPECT_TRUE(matches(
459 "class O1 {"
460 " class O2 {"
461 " class F {"
462 " class Z {"
463 " class A {};"
464 " class X {};"
465 " class Y {};"
466 " };"
467 " class C {"
468 " class X {};"
469 " class A {};"
470 " class B {};"
471 " };"
472 " };"
473 " };"
474 "};", Recursive));
475}
476
477TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
478 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000479 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000480 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000481 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000482 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000483 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000484 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000485 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000486 hasName("Y"))),
487 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000488 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000489 anyOf(
490 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000491 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000492 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000493 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000494 hasName("B")))))),
495 hasName("F")));
496
497 EXPECT_TRUE(matches("class F {};", Recursive));
498 EXPECT_TRUE(matches("class Z {};", Recursive));
499 EXPECT_TRUE(matches("class C {};", Recursive));
500 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
501 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
502 EXPECT_TRUE(
503 matches("class O1 { class O2 {"
504 " class M { class N { class B {}; }; }; "
505 "}; };", Recursive));
506}
507
508TEST(DeclarationMatcher, MatchNot) {
509 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000510 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000511 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000512 unless(hasName("X")));
513 EXPECT_TRUE(notMatches("", NotClassX));
514 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
515 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
516 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
517 EXPECT_TRUE(
518 notMatches("class Y {}; class Z {}; class X : public Y {};",
519 NotClassX));
520
521 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000522 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000523 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000524 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000525 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000526 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000527 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
528 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
529 ClassXHasNotClassY));
530}
531
532TEST(DeclarationMatcher, HasDescendant) {
533 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000534 recordDecl(
535 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000536 hasName("Z"));
537 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
538 EXPECT_TRUE(
539 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
540 EXPECT_TRUE(
541 matches("class Z { class A { class Y { class X {}; }; }; };",
542 ZDescendantClassX));
543 EXPECT_TRUE(
544 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
545 ZDescendantClassX));
546 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
547
548 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000549 recordDecl(
550 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000551 hasName("X"))),
552 hasName("Z"));
553 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
554 ZDescendantClassXHasClassY));
555 EXPECT_TRUE(
556 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
557 ZDescendantClassXHasClassY));
558 EXPECT_TRUE(notMatches(
559 "class Z {"
560 " class A {"
561 " class B {"
562 " class X {"
563 " class C {"
564 " class Y {};"
565 " };"
566 " };"
567 " }; "
568 " };"
569 "};", ZDescendantClassXHasClassY));
570
571 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000572 recordDecl(
573 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
574 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000575 hasName("Z"));
576 EXPECT_TRUE(
577 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
578 ZDescendantClassXDescendantClassY));
579 EXPECT_TRUE(matches(
580 "class Z {"
581 " class A {"
582 " class X {"
583 " class B {"
584 " class Y {};"
585 " };"
586 " class Y {};"
587 " };"
588 " };"
589 "};", ZDescendantClassXDescendantClassY));
590}
591
Daniel Jaspera267cf62012-10-29 10:14:44 +0000592// Implements a run method that returns whether BoundNodes contains a
593// Decl bound to Id that can be dynamically cast to T.
594// Optionally checks that the check succeeded a specific number of times.
595template <typename T>
596class VerifyIdIsBoundTo : public BoundNodesCallback {
597public:
598 // Create an object that checks that a node of type \c T was bound to \c Id.
599 // Does not check for a certain number of matches.
600 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
601 : Id(Id), ExpectedCount(-1), Count(0) {}
602
603 // Create an object that checks that a node of type \c T was bound to \c Id.
604 // Checks that there were exactly \c ExpectedCount matches.
605 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
606 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
607
608 // Create an object that checks that a node of type \c T was bound to \c Id.
609 // Checks that there was exactly one match with the name \c ExpectedName.
610 // Note that \c T must be a NamedDecl for this to work.
611 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
612 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
613
614 ~VerifyIdIsBoundTo() {
615 if (ExpectedCount != -1)
616 EXPECT_EQ(ExpectedCount, Count);
617 if (!ExpectedName.empty())
618 EXPECT_EQ(ExpectedName, Name);
619 }
620
621 virtual bool run(const BoundNodes *Nodes) {
622 if (Nodes->getNodeAs<T>(Id)) {
623 ++Count;
624 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
625 Name = Named->getNameAsString();
626 } else if (const NestedNameSpecifier *NNS =
627 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
628 llvm::raw_string_ostream OS(Name);
629 NNS->print(OS, PrintingPolicy(LangOptions()));
630 }
631 return true;
632 }
633 return false;
634 }
635
Daniel Jasper452abbc2012-10-29 10:48:25 +0000636 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
637 return run(Nodes);
638 }
639
Daniel Jaspera267cf62012-10-29 10:14:44 +0000640private:
641 const std::string Id;
642 const int ExpectedCount;
643 int Count;
644 const std::string ExpectedName;
645 std::string Name;
646};
647
648TEST(HasDescendant, MatchesDescendantTypes) {
649 EXPECT_TRUE(matches("void f() { int i = 3; }",
650 decl(hasDescendant(loc(builtinType())))));
651 EXPECT_TRUE(matches("void f() { int i = 3; }",
652 stmt(hasDescendant(builtinType()))));
653
654 EXPECT_TRUE(matches("void f() { int i = 3; }",
655 stmt(hasDescendant(loc(builtinType())))));
656 EXPECT_TRUE(matches("void f() { int i = 3; }",
657 stmt(hasDescendant(qualType(builtinType())))));
658
659 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
660 stmt(hasDescendant(isInteger()))));
661
662 EXPECT_TRUE(matchAndVerifyResultTrue(
663 "void f() { int a; float c; int d; int e; }",
664 functionDecl(forEachDescendant(
665 varDecl(hasDescendant(isInteger())).bind("x"))),
666 new VerifyIdIsBoundTo<Decl>("x", 3)));
667}
668
669TEST(HasDescendant, MatchesDescendantsOfTypes) {
670 EXPECT_TRUE(matches("void f() { int*** i; }",
671 qualType(hasDescendant(builtinType()))));
672 EXPECT_TRUE(matches("void f() { int*** i; }",
673 qualType(hasDescendant(
674 pointerType(pointee(builtinType()))))));
675 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000676 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000677
678 EXPECT_TRUE(matchAndVerifyResultTrue(
679 "void f() { int*** i; }",
680 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
681 new VerifyIdIsBoundTo<Type>("x", 2)));
682}
683
684TEST(Has, MatchesChildrenOfTypes) {
685 EXPECT_TRUE(matches("int i;",
686 varDecl(hasName("i"), has(isInteger()))));
687 EXPECT_TRUE(notMatches("int** i;",
688 varDecl(hasName("i"), has(isInteger()))));
689 EXPECT_TRUE(matchAndVerifyResultTrue(
690 "int (*f)(float, int);",
691 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
692 new VerifyIdIsBoundTo<QualType>("x", 2)));
693}
694
695TEST(Has, MatchesChildTypes) {
696 EXPECT_TRUE(matches(
697 "int* i;",
698 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
699 EXPECT_TRUE(notMatches(
700 "int* i;",
701 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
702}
703
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000704TEST(Enum, DoesNotMatchClasses) {
705 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
706}
707
708TEST(Enum, MatchesEnums) {
709 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
710}
711
712TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000713 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000714 EXPECT_TRUE(matches("enum X{ A };", Matcher));
715 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
716 EXPECT_TRUE(notMatches("enum X {};", Matcher));
717}
718
Manuel Klimek4da21662012-07-06 05:48:52 +0000719TEST(StatementMatcher, Has) {
720 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000721 expr(hasType(pointsTo(recordDecl(hasName("X")))),
722 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000723
724 EXPECT_TRUE(matches(
725 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
726 EXPECT_TRUE(notMatches(
727 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
728}
729
730TEST(StatementMatcher, HasDescendant) {
731 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000732 expr(hasType(pointsTo(recordDecl(hasName("X")))),
733 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000734
735 EXPECT_TRUE(matches(
736 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
737 HasDescendantVariableI));
738 EXPECT_TRUE(notMatches(
739 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
740 HasDescendantVariableI));
741}
742
743TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000744 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000745
746 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
747 EXPECT_TRUE(notMatches("class A {};", TypeA));
748
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000749 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000750
751 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
752 TypeDerivedFromA));
753 EXPECT_TRUE(notMatches("class A {};", TypeA));
754
755 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000757
758 EXPECT_TRUE(
759 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
760}
761
Manuel Klimek4da21662012-07-06 05:48:52 +0000762TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000763 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000764
765 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000766 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000767
768 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000769 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000770
771 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000772 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000773
774 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
775 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000776 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000777
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 StatementMatcher MethodX =
779 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000780
781 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
782 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000783 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000784}
785
786TEST(Matcher, BindTheSameNameInAlternatives) {
787 StatementMatcher matcher = anyOf(
788 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000790 hasRHS(integerLiteral(equals(0)))),
791 binaryOperator(hasOperatorName("+"),
792 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000793 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000794
795 EXPECT_TRUE(matchAndVerifyResultTrue(
796 // The first branch of the matcher binds x to 0 but then fails.
797 // The second branch binds x to f() and succeeds.
798 "int f() { return 0 + f(); }",
799 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000800 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000801}
802
Manuel Klimek66341c52012-08-30 19:41:06 +0000803TEST(Matcher, BindsIDForMemoizedResults) {
804 // Using the same matcher in two match expressions will make memoization
805 // kick in.
806 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
807 EXPECT_TRUE(matchAndVerifyResultTrue(
808 "class A { class B { class X {}; }; };",
809 DeclarationMatcher(anyOf(
810 recordDecl(hasName("A"), hasDescendant(ClassX)),
811 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000812 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000813}
814
Daniel Jasper189f2e42012-12-03 15:43:25 +0000815TEST(HasDeclaration, HasDeclarationOfEnumType) {
816 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
817 expr(hasType(pointsTo(
818 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
819}
820
Edwin Vaneb45083d2013-02-25 14:32:42 +0000821TEST(HasDeclaration, HasGetDeclTraitTest) {
822 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
823 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
824 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
825}
826
Edwin Vane52380602013-02-19 17:14:34 +0000827TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
828 EXPECT_TRUE(matches("typedef int X; X a;",
829 varDecl(hasName("a"),
830 hasType(typedefType(hasDeclaration(decl()))))));
831
832 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
833}
834
Manuel Klimek4da21662012-07-06 05:48:52 +0000835TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000836 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000837 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000838 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000839 EXPECT_TRUE(
840 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000841 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000842 EXPECT_TRUE(
843 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000844 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000845}
846
847TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000848 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000849 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000850 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000851 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000852 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000853 EXPECT_TRUE(
854 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000855 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000856}
857
858TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000859 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000860 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000861 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000862 EXPECT_TRUE(
863 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000864 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000865}
866
867TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000868 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000869 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000870 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000871 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000872 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000873}
874
875TEST(Matcher, Call) {
876 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000877 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000878 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000879
880 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
881 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
882
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000883 StatementMatcher MethodOnY =
884 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000885
886 EXPECT_TRUE(
887 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
888 MethodOnY));
889 EXPECT_TRUE(
890 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
891 MethodOnY));
892 EXPECT_TRUE(
893 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
894 MethodOnY));
895 EXPECT_TRUE(
896 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
897 MethodOnY));
898 EXPECT_TRUE(
899 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
900 MethodOnY));
901
902 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000903 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000904
905 EXPECT_TRUE(
906 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
907 MethodOnYPointer));
908 EXPECT_TRUE(
909 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
910 MethodOnYPointer));
911 EXPECT_TRUE(
912 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
913 MethodOnYPointer));
914 EXPECT_TRUE(
915 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
916 MethodOnYPointer));
917 EXPECT_TRUE(
918 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
919 MethodOnYPointer));
920}
921
Daniel Jasper31f7c082012-10-01 13:40:41 +0000922TEST(Matcher, Lambda) {
923 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
924 lambdaExpr()));
925}
926
927TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000928 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
929 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000930 forRangeStmt()));
931 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
932 forRangeStmt()));
933}
934
935TEST(Matcher, UserDefinedLiteral) {
936 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
937 " return i + 1;"
938 "}"
939 "char c = 'a'_inc;",
940 userDefinedLiteral()));
941}
942
Daniel Jasperb54b7642012-09-20 14:12:57 +0000943TEST(Matcher, FlowControl) {
944 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
945 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
946 continueStmt()));
947 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
948 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
949 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
950}
951
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000952TEST(HasType, MatchesAsString) {
953 EXPECT_TRUE(
954 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000955 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000956 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000957 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000958 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000959 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000960 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000961 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000962}
963
Manuel Klimek4da21662012-07-06 05:48:52 +0000964TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000965 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000966 // Unary operator
967 EXPECT_TRUE(matches("class Y { }; "
968 "bool operator!(Y x) { return false; }; "
969 "Y y; bool c = !y;", OpCall));
970 // No match -- special operators like "new", "delete"
971 // FIXME: operator new takes size_t, for which we need stddef.h, for which
972 // we need to figure out include paths in the test.
973 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
974 // "class Y { }; "
975 // "void *operator new(size_t size) { return 0; } "
976 // "Y *y = new Y;", OpCall));
977 EXPECT_TRUE(notMatches("class Y { }; "
978 "void operator delete(void *p) { } "
979 "void a() {Y *y = new Y; delete y;}", OpCall));
980 // Binary operator
981 EXPECT_TRUE(matches("class Y { }; "
982 "bool operator&&(Y x, Y y) { return true; }; "
983 "Y a; Y b; bool c = a && b;",
984 OpCall));
985 // No match -- normal operator, not an overloaded one.
986 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
987 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
988}
989
990TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
991 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000992 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000993 EXPECT_TRUE(matches("class Y { }; "
994 "bool operator&&(Y x, Y y) { return true; }; "
995 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
996 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000997 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000998 EXPECT_TRUE(notMatches("class Y { }; "
999 "bool operator&&(Y x, Y y) { return true; }; "
1000 "Y a; Y b; bool c = a && b;",
1001 OpCallLessLess));
1002}
1003
Daniel Jasper278057f2012-11-15 03:29:05 +00001004TEST(Matcher, NestedOverloadedOperatorCalls) {
1005 EXPECT_TRUE(matchAndVerifyResultTrue(
1006 "class Y { }; "
1007 "Y& operator&&(Y& x, Y& y) { return x; }; "
1008 "Y a; Y b; Y c; Y d = a && b && c;",
1009 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1010 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1011 EXPECT_TRUE(matches(
1012 "class Y { }; "
1013 "Y& operator&&(Y& x, Y& y) { return x; }; "
1014 "Y a; Y b; Y c; Y d = a && b && c;",
1015 operatorCallExpr(hasParent(operatorCallExpr()))));
1016 EXPECT_TRUE(matches(
1017 "class Y { }; "
1018 "Y& operator&&(Y& x, Y& y) { return x; }; "
1019 "Y a; Y b; Y c; Y d = a && b && c;",
1020 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1021}
1022
Manuel Klimek4da21662012-07-06 05:48:52 +00001023TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001024 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001025 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001026
1027 EXPECT_TRUE(
1028 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1029 MethodOnY));
1030 EXPECT_TRUE(
1031 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1032 MethodOnY));
1033 EXPECT_TRUE(
1034 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1035 MethodOnY));
1036 EXPECT_TRUE(
1037 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1038 MethodOnY));
1039 EXPECT_TRUE(
1040 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1041 MethodOnY));
1042
1043 EXPECT_TRUE(matches(
1044 "class Y {"
1045 " public: virtual void x();"
1046 "};"
1047 "class X : public Y {"
1048 " public: virtual void x();"
1049 "};"
1050 "void z() { X *x; x->Y::x(); }", MethodOnY));
1051}
1052
1053TEST(Matcher, VariableUsage) {
1054 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001055 declRefExpr(to(
1056 varDecl(hasInitializer(
1057 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001058
1059 EXPECT_TRUE(matches(
1060 "class Y {"
1061 " public:"
1062 " bool x() const;"
1063 "};"
1064 "void z(const Y &y) {"
1065 " bool b = y.x();"
1066 " if (b) {}"
1067 "}", Reference));
1068
1069 EXPECT_TRUE(notMatches(
1070 "class Y {"
1071 " public:"
1072 " bool x() const;"
1073 "};"
1074 "void z(const Y &y) {"
1075 " bool b = y.x();"
1076 "}", Reference));
1077}
1078
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001079TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001080 EXPECT_TRUE(matches(
1081 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001082 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001083}
1084
Manuel Klimek4da21662012-07-06 05:48:52 +00001085TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001086 StatementMatcher CallOnVariableY =
1087 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001088
1089 EXPECT_TRUE(matches(
1090 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1091 EXPECT_TRUE(matches(
1092 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1093 EXPECT_TRUE(matches(
1094 "class Y { public: void x(); };"
1095 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1096 EXPECT_TRUE(matches(
1097 "class Y { public: void x(); };"
1098 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1099 EXPECT_TRUE(notMatches(
1100 "class Y { public: void x(); };"
1101 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1102 CallOnVariableY));
1103}
1104
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001105TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1106 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1107 unaryExprOrTypeTraitExpr()));
1108 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1109 alignOfExpr(anything())));
1110 // FIXME: Uncomment once alignof is enabled.
1111 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1112 // unaryExprOrTypeTraitExpr()));
1113 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1114 // sizeOfExpr()));
1115}
1116
1117TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1118 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1119 hasArgumentOfType(asString("int")))));
1120 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1121 hasArgumentOfType(asString("float")))));
1122 EXPECT_TRUE(matches(
1123 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001124 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001125 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001126 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001127}
1128
Manuel Klimek4da21662012-07-06 05:48:52 +00001129TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001130 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001131}
1132
1133TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001134 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001135}
1136
1137TEST(MemberExpression, MatchesVariable) {
1138 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001139 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001140 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001141 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001142 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001143 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001144}
1145
1146TEST(MemberExpression, MatchesStaticVariable) {
1147 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001148 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001149 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001150 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001151 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001152 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001153}
1154
Daniel Jasper6a124492012-07-12 08:50:38 +00001155TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001156 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1157 EXPECT_TRUE(matches(
1158 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1159 callExpr(hasArgument(0, declRefExpr(
1160 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001161}
1162
1163TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001164 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001165 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001166 callExpr(hasArgument(0, declRefExpr(
1167 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001168}
1169
Manuel Klimek4da21662012-07-06 05:48:52 +00001170TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1171 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001172 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001173 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001174 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001175 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001176 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001177}
1178
1179TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1180 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001181 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001182 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001183 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001184 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001185 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001186}
1187
1188TEST(IsArrow, MatchesMemberCallsViaArrow) {
1189 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001190 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001191 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001192 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001193 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001194 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001195}
1196
1197TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001198 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001199
1200 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1201 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1202}
1203
1204TEST(Callee, MatchesMemberExpressions) {
1205 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001206 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001207 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001208 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001209}
1210
1211TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001212 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001213
1214 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1215 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1216
Manuel Klimeke265c872012-07-10 14:21:30 +00001217#if !defined(_MSC_VER)
1218 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001219 // Dependent contexts, but a non-dependent call.
1220 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1221 CallFunctionF));
1222 EXPECT_TRUE(
1223 matches("void f(); template <int N> struct S { void g() { f(); } };",
1224 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001225#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001226
1227 // Depedent calls don't match.
1228 EXPECT_TRUE(
1229 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1230 CallFunctionF));
1231 EXPECT_TRUE(
1232 notMatches("void f(int);"
1233 "template <typename T> struct S { void g(T t) { f(t); } };",
1234 CallFunctionF));
1235}
1236
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001237TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1238 EXPECT_TRUE(
1239 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001240 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001241}
1242
1243TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1244 EXPECT_TRUE(
1245 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001246 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001247}
1248
1249TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1250 EXPECT_TRUE(
1251 notMatches("void g(); template <typename T> void f(T t) {}"
1252 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001253 functionTemplateDecl(hasName("f"),
1254 hasDescendant(declRefExpr(to(
1255 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001256}
1257
Manuel Klimek4da21662012-07-06 05:48:52 +00001258TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001259 StatementMatcher CallArgumentY = callExpr(
1260 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001261
1262 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1263 EXPECT_TRUE(
1264 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1265 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1266
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001267 StatementMatcher WrongIndex = callExpr(
1268 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001269 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1270}
1271
1272TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001273 StatementMatcher CallArgumentY = callExpr(
1274 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001275 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1276 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1277 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1278}
1279
1280TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001281 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001282
1283 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1284 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1285 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1286}
1287
Daniel Jasper36e29d62012-12-04 11:54:27 +00001288TEST(Matcher, ParameterCount) {
1289 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1290 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1291 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1292 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1293 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1294}
1295
Manuel Klimek4da21662012-07-06 05:48:52 +00001296TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001297 DeclarationMatcher ReferenceClassX = varDecl(
1298 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001299 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1300 ReferenceClassX));
1301 EXPECT_TRUE(
1302 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1303 EXPECT_TRUE(
1304 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1305 EXPECT_TRUE(
1306 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1307}
1308
1309TEST(HasParameter, CallsInnerMatcher) {
1310 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001311 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001312 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001313 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001314}
1315
1316TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1317 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001318 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001319}
1320
1321TEST(HasType, MatchesParameterVariableTypesStrictly) {
1322 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001323 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001324 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001325 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001326 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001327 methodDecl(hasParameter(0,
1328 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001329 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001330 methodDecl(hasParameter(0,
1331 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001332}
1333
1334TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1335 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001336 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001337 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001338 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001339}
1340
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001341TEST(Returns, MatchesReturnTypes) {
1342 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001343 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001344 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001345 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001346 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001347 functionDecl(returns(hasDeclaration(
1348 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001349}
1350
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001351TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001352 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1353 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1354 functionDecl(isExternC())));
1355 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001356}
1357
Manuel Klimek4da21662012-07-06 05:48:52 +00001358TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1359 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001360 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001361}
1362
1363TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1364 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001365 methodDecl(hasAnyParameter(hasType(pointsTo(
1366 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001367}
1368
1369TEST(HasName, MatchesParameterVariableDeclartions) {
1370 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001371 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001372 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001373 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001374}
1375
Daniel Jasper371f9392012-08-01 08:40:24 +00001376TEST(Matcher, MatchesClassTemplateSpecialization) {
1377 EXPECT_TRUE(matches("template<typename T> struct A {};"
1378 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001379 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001380 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001381 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001382 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001383 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001384}
1385
1386TEST(Matcher, MatchesTypeTemplateArgument) {
1387 EXPECT_TRUE(matches(
1388 "template<typename T> struct B {};"
1389 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001390 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001391 asString("int"))))));
1392}
1393
1394TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1395 EXPECT_TRUE(matches(
1396 "struct B { int next; };"
1397 "template<int(B::*next_ptr)> struct A {};"
1398 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001399 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1400 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001401
1402 EXPECT_TRUE(notMatches(
1403 "template <typename T> struct A {};"
1404 "A<int> a;",
1405 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1406 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001407}
1408
1409TEST(Matcher, MatchesSpecificArgument) {
1410 EXPECT_TRUE(matches(
1411 "template<typename T, typename U> class A {};"
1412 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001414 1, refersToType(asString("int"))))));
1415 EXPECT_TRUE(notMatches(
1416 "template<typename T, typename U> class A {};"
1417 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001418 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001419 1, refersToType(asString("int"))))));
1420}
1421
Daniel Jasperf3197e92013-02-25 12:02:08 +00001422TEST(Matcher, MatchesAccessSpecDecls) {
1423 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1424 EXPECT_TRUE(
1425 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1426 EXPECT_TRUE(
1427 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1428 EXPECT_TRUE(
1429 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1430
1431 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1432}
1433
Manuel Klimek4da21662012-07-06 05:48:52 +00001434TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001435 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001436
1437 EXPECT_TRUE(
1438 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1439 EXPECT_TRUE(
1440 matches("class X { public: X(); }; void x() { X x = X(); }",
1441 Constructor));
1442 EXPECT_TRUE(
1443 matches("class X { public: X(int); }; void x() { X x = 0; }",
1444 Constructor));
1445 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1446}
1447
1448TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001449 StatementMatcher Constructor = constructExpr(
1450 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001451
1452 EXPECT_TRUE(
1453 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1454 Constructor));
1455 EXPECT_TRUE(
1456 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1457 Constructor));
1458 EXPECT_TRUE(
1459 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1460 Constructor));
1461 EXPECT_TRUE(
1462 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1463 Constructor));
1464
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001465 StatementMatcher WrongIndex = constructExpr(
1466 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001467 EXPECT_TRUE(
1468 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1469 WrongIndex));
1470}
1471
1472TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001473 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001474
1475 EXPECT_TRUE(
1476 matches("class X { public: X(int); }; void x() { X x(0); }",
1477 Constructor1Arg));
1478 EXPECT_TRUE(
1479 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1480 Constructor1Arg));
1481 EXPECT_TRUE(
1482 matches("class X { public: X(int); }; void x() { X x = 0; }",
1483 Constructor1Arg));
1484 EXPECT_TRUE(
1485 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1486 Constructor1Arg));
1487}
1488
Manuel Klimek70b9db92012-10-23 10:40:50 +00001489TEST(Matcher,ThisExpr) {
1490 EXPECT_TRUE(
1491 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1492 EXPECT_TRUE(
1493 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1494}
1495
Manuel Klimek4da21662012-07-06 05:48:52 +00001496TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001497 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001498
1499 std::string ClassString = "class string { public: string(); ~string(); }; ";
1500
1501 EXPECT_TRUE(
1502 matches(ClassString +
1503 "string GetStringByValue();"
1504 "void FunctionTakesString(string s);"
1505 "void run() { FunctionTakesString(GetStringByValue()); }",
1506 TempExpression));
1507
1508 EXPECT_TRUE(
1509 notMatches(ClassString +
1510 "string* GetStringPointer(); "
1511 "void FunctionTakesStringPtr(string* s);"
1512 "void run() {"
1513 " string* s = GetStringPointer();"
1514 " FunctionTakesStringPtr(GetStringPointer());"
1515 " FunctionTakesStringPtr(s);"
1516 "}",
1517 TempExpression));
1518
1519 EXPECT_TRUE(
1520 notMatches("class no_dtor {};"
1521 "no_dtor GetObjByValue();"
1522 "void ConsumeObj(no_dtor param);"
1523 "void run() { ConsumeObj(GetObjByValue()); }",
1524 TempExpression));
1525}
1526
Sam Panzere16acd32012-08-24 22:04:44 +00001527TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1528 std::string ClassString =
1529 "class string { public: string(); int length(); }; ";
1530
1531 EXPECT_TRUE(
1532 matches(ClassString +
1533 "string GetStringByValue();"
1534 "void FunctionTakesString(string s);"
1535 "void run() { FunctionTakesString(GetStringByValue()); }",
1536 materializeTemporaryExpr()));
1537
1538 EXPECT_TRUE(
1539 notMatches(ClassString +
1540 "string* GetStringPointer(); "
1541 "void FunctionTakesStringPtr(string* s);"
1542 "void run() {"
1543 " string* s = GetStringPointer();"
1544 " FunctionTakesStringPtr(GetStringPointer());"
1545 " FunctionTakesStringPtr(s);"
1546 "}",
1547 materializeTemporaryExpr()));
1548
1549 EXPECT_TRUE(
1550 notMatches(ClassString +
1551 "string GetStringByValue();"
1552 "void run() { int k = GetStringByValue().length(); }",
1553 materializeTemporaryExpr()));
1554
1555 EXPECT_TRUE(
1556 notMatches(ClassString +
1557 "string GetStringByValue();"
1558 "void run() { GetStringByValue(); }",
1559 materializeTemporaryExpr()));
1560}
1561
Manuel Klimek4da21662012-07-06 05:48:52 +00001562TEST(ConstructorDeclaration, SimpleCase) {
1563 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001564 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001565 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001566 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001567}
1568
1569TEST(ConstructorDeclaration, IsImplicit) {
1570 // This one doesn't match because the constructor is not added by the
1571 // compiler (it is not needed).
1572 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001573 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001574 // The compiler added the implicit default constructor.
1575 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001576 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001577 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001578 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001579}
1580
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001581TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1582 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001583 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001584}
1585
1586TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001587 EXPECT_TRUE(notMatches("class Foo {};",
1588 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001589}
1590
Manuel Klimek4da21662012-07-06 05:48:52 +00001591TEST(HasAnyConstructorInitializer, SimpleCase) {
1592 EXPECT_TRUE(notMatches(
1593 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001594 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001595 EXPECT_TRUE(matches(
1596 "class Foo {"
1597 " Foo() : foo_() { }"
1598 " int foo_;"
1599 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001600 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001601}
1602
1603TEST(HasAnyConstructorInitializer, ForField) {
1604 static const char Code[] =
1605 "class Baz { };"
1606 "class Foo {"
1607 " Foo() : foo_() { }"
1608 " Baz foo_;"
1609 " Baz bar_;"
1610 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001611 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1612 forField(hasType(recordDecl(hasName("Baz"))))))));
1613 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001614 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001615 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1616 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001617}
1618
1619TEST(HasAnyConstructorInitializer, WithInitializer) {
1620 static const char Code[] =
1621 "class Foo {"
1622 " Foo() : foo_(0) { }"
1623 " int foo_;"
1624 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001625 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001626 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001627 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001628 withInitializer(integerLiteral(equals(1)))))));
1629}
1630
1631TEST(HasAnyConstructorInitializer, IsWritten) {
1632 static const char Code[] =
1633 "struct Bar { Bar(){} };"
1634 "class Foo {"
1635 " Foo() : foo_() { }"
1636 " Bar foo_;"
1637 " Bar bar_;"
1638 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001639 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001640 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001641 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001642 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001643 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001644 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1645}
1646
1647TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001648 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001649
1650 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1651 EXPECT_TRUE(
1652 matches("class X { public: X(); }; void x() { new X(); }", New));
1653 EXPECT_TRUE(
1654 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1655 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1656}
1657
1658TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001659 StatementMatcher New = constructExpr(
1660 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001661
1662 EXPECT_TRUE(
1663 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1664 New));
1665 EXPECT_TRUE(
1666 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1667 New));
1668 EXPECT_TRUE(
1669 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1670 New));
1671
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001672 StatementMatcher WrongIndex = constructExpr(
1673 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001674 EXPECT_TRUE(
1675 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1676 WrongIndex));
1677}
1678
1679TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001680 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001681
1682 EXPECT_TRUE(
1683 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1684 EXPECT_TRUE(
1685 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1686 New));
1687}
1688
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001689TEST(Matcher, DeleteExpression) {
1690 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001691 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001692}
1693
Manuel Klimek4da21662012-07-06 05:48:52 +00001694TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001695 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001696
1697 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1698 EXPECT_TRUE(
1699 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1700 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1701}
1702
1703TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001704 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001705 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1706 // wide string
1707 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1708 // with escaped characters
1709 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1710 // no matching -- though the data type is the same, there is no string literal
1711 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1712}
1713
1714TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001715 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001716 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1717 // wide character
1718 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1719 // wide character, Hex encoded, NOT MATCHED!
1720 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1721 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1722}
1723
1724TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001725 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001726 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1727 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1728 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1729 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1730
1731 // Non-matching cases (character literals, float and double)
1732 EXPECT_TRUE(notMatches("int i = L'a';",
1733 HasIntLiteral)); // this is actually a character
1734 // literal cast to int
1735 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1736 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1737 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1738}
1739
Daniel Jasper31f7c082012-10-01 13:40:41 +00001740TEST(Matcher, NullPtrLiteral) {
1741 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1742}
1743
Daniel Jasperb54b7642012-09-20 14:12:57 +00001744TEST(Matcher, AsmStatement) {
1745 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1746}
1747
Manuel Klimek4da21662012-07-06 05:48:52 +00001748TEST(Matcher, Conditions) {
1749 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1750
1751 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1752 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1753 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1754 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1755 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1756}
1757
1758TEST(MatchBinaryOperator, HasOperatorName) {
1759 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1760
1761 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1762 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1763}
1764
1765TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1766 StatementMatcher OperatorTrueFalse =
1767 binaryOperator(hasLHS(boolLiteral(equals(true))),
1768 hasRHS(boolLiteral(equals(false))));
1769
1770 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1771 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1772 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1773}
1774
1775TEST(MatchBinaryOperator, HasEitherOperand) {
1776 StatementMatcher HasOperand =
1777 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1778
1779 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1780 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1781 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1782}
1783
1784TEST(Matcher, BinaryOperatorTypes) {
1785 // Integration test that verifies the AST provides all binary operators in
1786 // a way we expect.
1787 // FIXME: Operator ','
1788 EXPECT_TRUE(
1789 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1790 EXPECT_TRUE(
1791 matches("bool b; bool c = (b = true);",
1792 binaryOperator(hasOperatorName("="))));
1793 EXPECT_TRUE(
1794 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1795 EXPECT_TRUE(
1796 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1797 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1798 EXPECT_TRUE(
1799 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1800 EXPECT_TRUE(
1801 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1802 EXPECT_TRUE(
1803 matches("int i = 1; int j = (i <<= 2);",
1804 binaryOperator(hasOperatorName("<<="))));
1805 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1806 EXPECT_TRUE(
1807 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1808 EXPECT_TRUE(
1809 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1810 EXPECT_TRUE(
1811 matches("int i = 1; int j = (i >>= 2);",
1812 binaryOperator(hasOperatorName(">>="))));
1813 EXPECT_TRUE(
1814 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1815 EXPECT_TRUE(
1816 matches("int i = 42; int j = (i ^= 42);",
1817 binaryOperator(hasOperatorName("^="))));
1818 EXPECT_TRUE(
1819 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1820 EXPECT_TRUE(
1821 matches("int i = 42; int j = (i %= 42);",
1822 binaryOperator(hasOperatorName("%="))));
1823 EXPECT_TRUE(
1824 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1825 EXPECT_TRUE(
1826 matches("bool b = true && false;",
1827 binaryOperator(hasOperatorName("&&"))));
1828 EXPECT_TRUE(
1829 matches("bool b = true; bool c = (b &= false);",
1830 binaryOperator(hasOperatorName("&="))));
1831 EXPECT_TRUE(
1832 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1833 EXPECT_TRUE(
1834 matches("bool b = true || false;",
1835 binaryOperator(hasOperatorName("||"))));
1836 EXPECT_TRUE(
1837 matches("bool b = true; bool c = (b |= false);",
1838 binaryOperator(hasOperatorName("|="))));
1839 EXPECT_TRUE(
1840 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1841 EXPECT_TRUE(
1842 matches("int i = 42; int j = (i *= 23);",
1843 binaryOperator(hasOperatorName("*="))));
1844 EXPECT_TRUE(
1845 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1846 EXPECT_TRUE(
1847 matches("int i = 42; int j = (i /= 23);",
1848 binaryOperator(hasOperatorName("/="))));
1849 EXPECT_TRUE(
1850 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1851 EXPECT_TRUE(
1852 matches("int i = 42; int j = (i += 23);",
1853 binaryOperator(hasOperatorName("+="))));
1854 EXPECT_TRUE(
1855 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1856 EXPECT_TRUE(
1857 matches("int i = 42; int j = (i -= 23);",
1858 binaryOperator(hasOperatorName("-="))));
1859 EXPECT_TRUE(
1860 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1861 binaryOperator(hasOperatorName("->*"))));
1862 EXPECT_TRUE(
1863 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1864 binaryOperator(hasOperatorName(".*"))));
1865
1866 // Member expressions as operators are not supported in matches.
1867 EXPECT_TRUE(
1868 notMatches("struct A { void x(A *a) { a->x(this); } };",
1869 binaryOperator(hasOperatorName("->"))));
1870
1871 // Initializer assignments are not represented as operator equals.
1872 EXPECT_TRUE(
1873 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1874
1875 // Array indexing is not represented as operator.
1876 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1877
1878 // Overloaded operators do not match at all.
1879 EXPECT_TRUE(notMatches(
1880 "struct A { bool operator&&(const A &a) const { return false; } };"
1881 "void x() { A a, b; a && b; }",
1882 binaryOperator()));
1883}
1884
1885TEST(MatchUnaryOperator, HasOperatorName) {
1886 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1887
1888 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1889 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1890}
1891
1892TEST(MatchUnaryOperator, HasUnaryOperand) {
1893 StatementMatcher OperatorOnFalse =
1894 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1895
1896 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1897 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1898}
1899
1900TEST(Matcher, UnaryOperatorTypes) {
1901 // Integration test that verifies the AST provides all unary operators in
1902 // a way we expect.
1903 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1904 EXPECT_TRUE(
1905 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1906 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1907 EXPECT_TRUE(
1908 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1909 EXPECT_TRUE(
1910 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1911 EXPECT_TRUE(
1912 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1913 EXPECT_TRUE(
1914 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1915 EXPECT_TRUE(
1916 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1917 EXPECT_TRUE(
1918 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1919 EXPECT_TRUE(
1920 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1921
1922 // We don't match conversion operators.
1923 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1924
1925 // Function calls are not represented as operator.
1926 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1927
1928 // Overloaded operators do not match at all.
1929 // FIXME: We probably want to add that.
1930 EXPECT_TRUE(notMatches(
1931 "struct A { bool operator!() const { return false; } };"
1932 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1933}
1934
1935TEST(Matcher, ConditionalOperator) {
1936 StatementMatcher Conditional = conditionalOperator(
1937 hasCondition(boolLiteral(equals(true))),
1938 hasTrueExpression(boolLiteral(equals(false))));
1939
1940 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1941 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1942 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1943
1944 StatementMatcher ConditionalFalse = conditionalOperator(
1945 hasFalseExpression(boolLiteral(equals(false))));
1946
1947 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1948 EXPECT_TRUE(
1949 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1950}
1951
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001952TEST(ArraySubscriptMatchers, ArraySubscripts) {
1953 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
1954 arraySubscriptExpr()));
1955 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
1956 arraySubscriptExpr()));
1957}
1958
1959TEST(ArraySubscriptMatchers, ArrayIndex) {
1960 EXPECT_TRUE(matches(
1961 "int i[2]; void f() { i[1] = 1; }",
1962 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1963 EXPECT_TRUE(matches(
1964 "int i[2]; void f() { 1[i] = 1; }",
1965 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
1966 EXPECT_TRUE(notMatches(
1967 "int i[2]; void f() { i[1] = 1; }",
1968 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
1969}
1970
1971TEST(ArraySubscriptMatchers, MatchesArrayBase) {
1972 EXPECT_TRUE(matches(
1973 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001974 arraySubscriptExpr(hasBase(implicitCastExpr(
1975 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001976}
1977
Manuel Klimek4da21662012-07-06 05:48:52 +00001978TEST(Matcher, HasNameSupportsNamespaces) {
1979 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001980 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001981 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001982 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001983 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001984 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001985 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001986 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001987 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001988 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001989 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001990 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001991 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001992 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001993 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001994 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001995 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001996 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001997 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001998 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001999 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002000 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002001 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002002 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002003}
2004
2005TEST(Matcher, HasNameSupportsOuterClasses) {
2006 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002007 matches("class A { class B { class C; }; };",
2008 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002009 EXPECT_TRUE(
2010 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002011 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002012 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002013 matches("class A { class B { class C; }; };",
2014 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002015 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002016 matches("class A { class B { class C; }; };",
2017 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002018 EXPECT_TRUE(
2019 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002020 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002021 EXPECT_TRUE(
2022 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002023 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002024 EXPECT_TRUE(
2025 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002026 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002027 EXPECT_TRUE(
2028 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002029 recordDecl(hasName("::C"))));
2030 EXPECT_TRUE(
2031 notMatches("class A { class B { class C; }; };",
2032 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002033 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002034 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002035 EXPECT_TRUE(
2036 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002037 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002038}
2039
2040TEST(Matcher, IsDefinition) {
2041 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002042 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002043 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2044 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2045
2046 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002047 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002048 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2049 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2050
2051 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002052 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002053 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2054 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2055}
2056
2057TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002058 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002059 ofClass(hasName("X")))));
2060
2061 EXPECT_TRUE(
2062 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2063 EXPECT_TRUE(
2064 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2065 Constructor));
2066 EXPECT_TRUE(
2067 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2068 Constructor));
2069}
2070
2071TEST(Matcher, VisitsTemplateInstantiations) {
2072 EXPECT_TRUE(matches(
2073 "class A { public: void x(); };"
2074 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002075 "void f() { B<A> b; b.y(); }",
2076 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002077
2078 EXPECT_TRUE(matches(
2079 "class A { public: void x(); };"
2080 "class C {"
2081 " public:"
2082 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2083 "};"
2084 "void f() {"
2085 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002086 "}",
2087 recordDecl(hasName("C"),
2088 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002089}
2090
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002091TEST(Matcher, HandlesNullQualTypes) {
2092 // FIXME: Add a Type matcher so we can replace uses of this
2093 // variable with Type(True())
2094 const TypeMatcher AnyType = anything();
2095
2096 // We don't really care whether this matcher succeeds; we're testing that
2097 // it completes without crashing.
2098 EXPECT_TRUE(matches(
2099 "struct A { };"
2100 "template <typename T>"
2101 "void f(T t) {"
2102 " T local_t(t /* this becomes a null QualType in the AST */);"
2103 "}"
2104 "void g() {"
2105 " f(0);"
2106 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002107 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002108 anyOf(
2109 TypeMatcher(hasDeclaration(anything())),
2110 pointsTo(AnyType),
2111 references(AnyType)
2112 // Other QualType matchers should go here.
2113 ))))));
2114}
2115
Manuel Klimek4da21662012-07-06 05:48:52 +00002116// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002117AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002118 // Make sure all special variables are used: node, match_finder,
2119 // bound_nodes_builder, and the parameter named 'AMatcher'.
2120 return AMatcher.matches(Node, Finder, Builder);
2121}
2122
2123TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002124 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002125
2126 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002127 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002128
2129 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002130 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002131
2132 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002133 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002134}
2135
2136AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002137 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2138 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2139 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002140 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002141 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002142 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002143 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2144 ASTMatchFinder::BK_First);
2145}
2146
2147TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002148 DeclarationMatcher HasClassB =
2149 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002150
2151 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002152 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002153
2154 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002155 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002156
2157 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002158 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002159
2160 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002161 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002162
2163 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2164}
2165
2166TEST(For, FindsForLoops) {
2167 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2168 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002169 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2170 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002171 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002172}
2173
Daniel Jasper6a124492012-07-12 08:50:38 +00002174TEST(For, ForLoopInternals) {
2175 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2176 forStmt(hasCondition(anything()))));
2177 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2178 forStmt(hasLoopInit(anything()))));
2179}
2180
2181TEST(For, NegativeForLoopInternals) {
2182 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002183 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002184 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2185 forStmt(hasLoopInit(anything()))));
2186}
2187
Manuel Klimek4da21662012-07-06 05:48:52 +00002188TEST(For, ReportsNoFalsePositives) {
2189 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2190 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2191}
2192
2193TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002194 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2195 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2196 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002197}
2198
2199TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2200 // It's not a compound statement just because there's "{}" in the source
2201 // text. This is an AST search, not grep.
2202 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002203 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002204 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002205 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002206}
2207
Daniel Jasper6a124492012-07-12 08:50:38 +00002208TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002209 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002210 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002212 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002213 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002214 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002215 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002216 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002217}
2218
2219TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2220 // The simplest case: every compound statement is in a function
2221 // definition, and the function body itself must be a compound
2222 // statement.
2223 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002224 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002225}
2226
2227TEST(HasAnySubstatement, IsNotRecursive) {
2228 // It's really "has any immediate substatement".
2229 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002230 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002231}
2232
2233TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2234 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002235 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002236}
2237
2238TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2239 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002240 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002241}
2242
2243TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2244 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002245 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002246 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002247 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002248}
2249
2250TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2251 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002252 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002253 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002254 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002255 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002256 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002257}
2258
2259TEST(StatementCountIs, WorksWithMultipleStatements) {
2260 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002261 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002262}
2263
2264TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2265 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002266 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002267 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002268 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002269 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002270 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002271 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002272 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002273}
2274
2275TEST(Member, WorksInSimplestCase) {
2276 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002277 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002278}
2279
2280TEST(Member, DoesNotMatchTheBaseExpression) {
2281 // Don't pick out the wrong part of the member expression, this should
2282 // be checking the member (name) only.
2283 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002284 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002285}
2286
2287TEST(Member, MatchesInMemberFunctionCall) {
2288 EXPECT_TRUE(matches("void f() {"
2289 " struct { void first() {}; } s;"
2290 " s.first();"
2291 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002292 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002293}
2294
Daniel Jasperc711af22012-10-23 15:46:39 +00002295TEST(Member, MatchesMember) {
2296 EXPECT_TRUE(matches(
2297 "struct A { int i; }; void f() { A a; a.i = 2; }",
2298 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2299 EXPECT_TRUE(notMatches(
2300 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2301 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2302}
2303
Daniel Jasperf3197e92013-02-25 12:02:08 +00002304TEST(Member, UnderstandsAccess) {
2305 EXPECT_TRUE(matches(
2306 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2307 EXPECT_TRUE(notMatches(
2308 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2309 EXPECT_TRUE(notMatches(
2310 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2311
2312 EXPECT_TRUE(notMatches(
2313 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2314 EXPECT_TRUE(notMatches(
2315 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2316 EXPECT_TRUE(matches(
2317 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2318
2319 EXPECT_TRUE(notMatches(
2320 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2321 EXPECT_TRUE(matches("class A { protected: int i; };",
2322 fieldDecl(isProtected(), hasName("i"))));
2323 EXPECT_TRUE(notMatches(
2324 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2325
2326 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2327 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2328 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2329 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2330}
2331
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002332TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002333 // Fails in C++11 mode
2334 EXPECT_TRUE(matchesConditionally(
2335 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2336 "class X { void *operator new(std::size_t); };",
2337 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002338
2339 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002340 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002341
Daniel Jasper31f7c082012-10-01 13:40:41 +00002342 // Fails in C++11 mode
2343 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002344 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2345 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002346 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002347}
2348
Manuel Klimek4da21662012-07-06 05:48:52 +00002349TEST(HasObjectExpression, DoesNotMatchMember) {
2350 EXPECT_TRUE(notMatches(
2351 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002352 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002353}
2354
2355TEST(HasObjectExpression, MatchesBaseOfVariable) {
2356 EXPECT_TRUE(matches(
2357 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002358 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002359 EXPECT_TRUE(matches(
2360 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002361 memberExpr(hasObjectExpression(
2362 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002363}
2364
2365TEST(HasObjectExpression,
2366 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2367 EXPECT_TRUE(matches(
2368 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002369 memberExpr(hasObjectExpression(
2370 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002371 EXPECT_TRUE(matches(
2372 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002373 memberExpr(hasObjectExpression(
2374 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002375}
2376
2377TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002378 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2379 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2380 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2381 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002382}
2383
2384TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002385 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002386}
2387
2388TEST(IsConstQualified, MatchesConstInt) {
2389 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002390 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002391}
2392
2393TEST(IsConstQualified, MatchesConstPointer) {
2394 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002395 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002396}
2397
2398TEST(IsConstQualified, MatchesThroughTypedef) {
2399 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002400 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002401 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002402 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002403}
2404
2405TEST(IsConstQualified, DoesNotMatchInappropriately) {
2406 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002407 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002408 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002409 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002410}
2411
Sam Panzer089e5b32012-08-16 16:58:10 +00002412TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002413 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2414 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2415 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2416 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002417}
2418TEST(CastExpression, MatchesImplicitCasts) {
2419 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002420 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002421 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002422 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002423}
2424
2425TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002426 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2427 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2428 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2429 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002430}
2431
Manuel Klimek4da21662012-07-06 05:48:52 +00002432TEST(ReinterpretCast, MatchesSimpleCase) {
2433 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002434 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002435}
2436
2437TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002438 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002439 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002440 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002441 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002442 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002443 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2444 "B b;"
2445 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002446 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002447}
2448
2449TEST(FunctionalCast, MatchesSimpleCase) {
2450 std::string foo_class = "class Foo { public: Foo(char*); };";
2451 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002452 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002453}
2454
2455TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2456 std::string FooClass = "class Foo { public: Foo(char*); };";
2457 EXPECT_TRUE(
2458 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002459 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002460 EXPECT_TRUE(
2461 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002462 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002463}
2464
2465TEST(DynamicCast, MatchesSimpleCase) {
2466 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2467 "B b;"
2468 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002469 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002470}
2471
2472TEST(StaticCast, MatchesSimpleCase) {
2473 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002474 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002475}
2476
2477TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002478 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002479 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002480 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002481 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002482 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002483 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2484 "B b;"
2485 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002486 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002487}
2488
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002489TEST(CStyleCast, MatchesSimpleCase) {
2490 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2491}
2492
2493TEST(CStyleCast, DoesNotMatchOtherCasts) {
2494 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2495 "char q, *r = const_cast<char*>(&q);"
2496 "void* s = reinterpret_cast<char*>(&s);"
2497 "struct B { virtual ~B() {} }; struct D : B {};"
2498 "B b;"
2499 "D* t = dynamic_cast<D*>(&b);",
2500 cStyleCastExpr()));
2501}
2502
Manuel Klimek4da21662012-07-06 05:48:52 +00002503TEST(HasDestinationType, MatchesSimpleCase) {
2504 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002505 staticCastExpr(hasDestinationType(
2506 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002507}
2508
Sam Panzer089e5b32012-08-16 16:58:10 +00002509TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2510 // This test creates an implicit const cast.
2511 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002512 implicitCastExpr(
2513 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002514 // This test creates an implicit array-to-pointer cast.
2515 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002516 implicitCastExpr(hasImplicitDestinationType(
2517 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002518}
2519
2520TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2521 // This test creates an implicit cast from int to char.
2522 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002523 implicitCastExpr(hasImplicitDestinationType(
2524 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002525 // This test creates an implicit array-to-pointer cast.
2526 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002527 implicitCastExpr(hasImplicitDestinationType(
2528 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002529}
2530
2531TEST(ImplicitCast, MatchesSimpleCase) {
2532 // This test creates an implicit const cast.
2533 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002534 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002535 // This test creates an implicit cast from int to char.
2536 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002537 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002538 // This test creates an implicit array-to-pointer cast.
2539 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002540 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002541}
2542
2543TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002544 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002545 // are present, and that it ignores explicit and paren casts.
2546
2547 // These two test cases have no casts.
2548 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002549 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002550 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002551 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002552
2553 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002554 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002555 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002556 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002557
2558 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002559 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002560}
2561
2562TEST(IgnoringImpCasts, MatchesImpCasts) {
2563 // This test checks that ignoringImpCasts matches when implicit casts are
2564 // present and its inner matcher alone does not match.
2565 // Note that this test creates an implicit const cast.
2566 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002567 varDecl(hasInitializer(ignoringImpCasts(
2568 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002569 // This test creates an implict cast from int to char.
2570 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002571 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002572 integerLiteral(equals(0)))))));
2573}
2574
2575TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2576 // These tests verify that ignoringImpCasts does not match if the inner
2577 // matcher does not match.
2578 // Note that the first test creates an implicit const cast.
2579 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002580 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002581 unless(anything()))))));
2582 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002583 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002584 unless(anything()))))));
2585
2586 // These tests verify that ignoringImplictCasts does not look through explicit
2587 // casts or parentheses.
2588 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002589 varDecl(hasInitializer(ignoringImpCasts(
2590 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002591 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002592 varDecl(hasInitializer(ignoringImpCasts(
2593 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002594 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002595 varDecl(hasInitializer(ignoringImpCasts(
2596 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002597 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002598 varDecl(hasInitializer(ignoringImpCasts(
2599 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002600}
2601
2602TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2603 // This test verifies that expressions that do not have implicit casts
2604 // still match the inner matcher.
2605 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002606 varDecl(hasInitializer(ignoringImpCasts(
2607 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002608}
2609
2610TEST(IgnoringParenCasts, MatchesParenCasts) {
2611 // This test checks that ignoringParenCasts matches when parentheses and/or
2612 // casts are present and its inner matcher alone does not match.
2613 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002614 varDecl(hasInitializer(ignoringParenCasts(
2615 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002616 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002617 varDecl(hasInitializer(ignoringParenCasts(
2618 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002619
2620 // This test creates an implict cast from int to char in addition to the
2621 // parentheses.
2622 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002623 varDecl(hasInitializer(ignoringParenCasts(
2624 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002625
2626 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002627 varDecl(hasInitializer(ignoringParenCasts(
2628 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002629 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002630 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002631 integerLiteral(equals(0)))))));
2632}
2633
2634TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2635 // This test verifies that expressions that do not have any casts still match.
2636 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002637 varDecl(hasInitializer(ignoringParenCasts(
2638 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002639}
2640
2641TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2642 // These tests verify that ignoringImpCasts does not match if the inner
2643 // matcher does not match.
2644 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002645 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002646 unless(anything()))))));
2647
2648 // This test creates an implicit cast from int to char in addition to the
2649 // parentheses.
2650 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002651 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002652 unless(anything()))))));
2653
2654 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002655 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002656 unless(anything()))))));
2657}
2658
2659TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2660 // This test checks that ignoringParenAndImpCasts matches when
2661 // parentheses and/or implicit casts are present and its inner matcher alone
2662 // does not match.
2663 // Note that this test creates an implicit const cast.
2664 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002665 varDecl(hasInitializer(ignoringParenImpCasts(
2666 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002667 // This test creates an implicit cast from int to char.
2668 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002669 varDecl(hasInitializer(ignoringParenImpCasts(
2670 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002671}
2672
2673TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2674 // This test verifies that expressions that do not have parentheses or
2675 // implicit casts still match.
2676 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002677 varDecl(hasInitializer(ignoringParenImpCasts(
2678 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002679 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002680 varDecl(hasInitializer(ignoringParenImpCasts(
2681 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002682}
2683
2684TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2685 // These tests verify that ignoringParenImpCasts does not match if
2686 // the inner matcher does not match.
2687 // This test creates an implicit cast.
2688 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002689 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002690 unless(anything()))))));
2691 // These tests verify that ignoringParenAndImplictCasts does not look
2692 // through explicit casts.
2693 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002694 varDecl(hasInitializer(ignoringParenImpCasts(
2695 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002696 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002697 varDecl(hasInitializer(ignoringParenImpCasts(
2698 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002699 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002700 varDecl(hasInitializer(ignoringParenImpCasts(
2701 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002702}
2703
Manuel Klimek715c9562012-07-25 10:02:02 +00002704TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002705 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2706 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002707 implicitCastExpr(
2708 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002709}
2710
Manuel Klimek715c9562012-07-25 10:02:02 +00002711TEST(HasSourceExpression, MatchesExplicitCasts) {
2712 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002713 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002714 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002715 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002716}
2717
Manuel Klimek4da21662012-07-06 05:48:52 +00002718TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002719 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002720}
2721
2722TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002723 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002724}
2725
2726TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002727 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002728}
2729
2730TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002731 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002732}
2733
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002734TEST(InitListExpression, MatchesInitListExpression) {
2735 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2736 initListExpr(hasType(asString("int [2]")))));
2737 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002738 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002739}
2740
2741TEST(UsingDeclaration, MatchesUsingDeclarations) {
2742 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2743 usingDecl()));
2744}
2745
2746TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2747 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2748 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2749}
2750
2751TEST(UsingDeclaration, MatchesSpecificTarget) {
2752 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2753 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002754 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002755 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2756 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002757 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002758}
2759
2760TEST(UsingDeclaration, ThroughUsingDeclaration) {
2761 EXPECT_TRUE(matches(
2762 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002763 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002764 EXPECT_TRUE(notMatches(
2765 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002766 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002767}
2768
Sam Panzer425f41b2012-08-16 17:20:59 +00002769TEST(SingleDecl, IsSingleDecl) {
2770 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002771 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002772 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2773 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2774 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2775 SingleDeclStmt));
2776}
2777
2778TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002779 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002780
2781 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002782 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002783 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002784 declStmt(containsDeclaration(0, MatchesInit),
2785 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002786 unsigned WrongIndex = 42;
2787 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002788 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002789 MatchesInit))));
2790}
2791
2792TEST(DeclCount, DeclCountIsCorrect) {
2793 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002794 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002795 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002796 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002797 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002798 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002799}
2800
Manuel Klimek4da21662012-07-06 05:48:52 +00002801TEST(While, MatchesWhileLoops) {
2802 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2803 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2804 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2805}
2806
2807TEST(Do, MatchesDoLoops) {
2808 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2809 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2810}
2811
2812TEST(Do, DoesNotMatchWhileLoops) {
2813 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2814}
2815
2816TEST(SwitchCase, MatchesCase) {
2817 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2818 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2819 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2820 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2821}
2822
Daniel Jasperb54b7642012-09-20 14:12:57 +00002823TEST(SwitchCase, MatchesSwitch) {
2824 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2825 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2826 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2827 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2828}
2829
2830TEST(ExceptionHandling, SimpleCases) {
2831 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2832 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2833 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2834 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2835 throwExpr()));
2836 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2837 throwExpr()));
2838}
2839
Manuel Klimek4da21662012-07-06 05:48:52 +00002840TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2841 EXPECT_TRUE(notMatches(
2842 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002843 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002844 EXPECT_TRUE(notMatches(
2845 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002846 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002847}
2848
2849TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2850 EXPECT_TRUE(matches(
2851 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002852 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002853}
2854
2855TEST(ForEach, BindsOneNode) {
2856 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002857 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002858 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002859}
2860
2861TEST(ForEach, BindsMultipleNodes) {
2862 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002863 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002864 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002865}
2866
2867TEST(ForEach, BindsRecursiveCombinations) {
2868 EXPECT_TRUE(matchAndVerifyResultTrue(
2869 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002870 recordDecl(hasName("C"),
2871 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002872 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002873}
2874
2875TEST(ForEachDescendant, BindsOneNode) {
2876 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002877 recordDecl(hasName("C"),
2878 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002879 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002880}
2881
Daniel Jasper5f684e92012-11-16 18:39:22 +00002882TEST(ForEachDescendant, NestedForEachDescendant) {
2883 DeclarationMatcher m = recordDecl(
2884 isDefinition(), decl().bind("x"), hasName("C"));
2885 EXPECT_TRUE(matchAndVerifyResultTrue(
2886 "class A { class B { class C {}; }; };",
2887 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2888 new VerifyIdIsBoundTo<Decl>("x", "C")));
2889
2890 // FIXME: This is not really a useful matcher, but the result is still
2891 // surprising (currently binds "A").
2892 //EXPECT_TRUE(matchAndVerifyResultTrue(
2893 // "class A { class B { class C {}; }; };",
2894 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2895 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2896}
2897
Manuel Klimek4da21662012-07-06 05:48:52 +00002898TEST(ForEachDescendant, BindsMultipleNodes) {
2899 EXPECT_TRUE(matchAndVerifyResultTrue(
2900 "class C { class D { int x; int y; }; "
2901 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002902 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002903 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002904}
2905
2906TEST(ForEachDescendant, BindsRecursiveCombinations) {
2907 EXPECT_TRUE(matchAndVerifyResultTrue(
2908 "class C { class D { "
2909 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002910 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2911 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002912 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002913}
2914
Daniel Jasper11c98772012-11-11 22:14:55 +00002915TEST(ForEachDescendant, BindsCorrectNodes) {
2916 EXPECT_TRUE(matchAndVerifyResultTrue(
2917 "class C { void f(); int i; };",
2918 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2919 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2920 EXPECT_TRUE(matchAndVerifyResultTrue(
2921 "class C { void f() {} int i; };",
2922 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2923 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2924}
2925
Manuel Klimek152ea0e2013-02-04 10:59:20 +00002926TEST(FindAll, BindsNodeOnMatch) {
2927 EXPECT_TRUE(matchAndVerifyResultTrue(
2928 "class A {};",
2929 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
2930 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
2931}
2932
2933TEST(FindAll, BindsDescendantNodeOnMatch) {
2934 EXPECT_TRUE(matchAndVerifyResultTrue(
2935 "class A { int a; int b; };",
2936 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
2937 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2938}
2939
2940TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
2941 EXPECT_TRUE(matchAndVerifyResultTrue(
2942 "class A { int a; int b; };",
2943 recordDecl(hasName("::A"),
2944 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
2945 fieldDecl().bind("v"))))),
2946 new VerifyIdIsBoundTo<Decl>("v", 3)));
2947
2948 EXPECT_TRUE(matchAndVerifyResultTrue(
2949 "class A { class B {}; class C {}; };",
2950 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
2951 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
2952}
2953
Manuel Klimek73876732013-02-04 09:42:38 +00002954TEST(EachOf, TriggersForEachMatch) {
2955 EXPECT_TRUE(matchAndVerifyResultTrue(
2956 "class A { int a; int b; };",
2957 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2958 has(fieldDecl(hasName("b")).bind("v")))),
2959 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2960}
2961
2962TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
2963 EXPECT_TRUE(matchAndVerifyResultTrue(
2964 "class A { int a; int c; };",
2965 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2966 has(fieldDecl(hasName("b")).bind("v")))),
2967 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
2968 EXPECT_TRUE(matchAndVerifyResultTrue(
2969 "class A { int c; int b; };",
2970 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2971 has(fieldDecl(hasName("b")).bind("v")))),
2972 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
2973 EXPECT_TRUE(notMatches(
2974 "class A { int c; int d; };",
2975 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
2976 has(fieldDecl(hasName("b")).bind("v"))))));
2977}
Manuel Klimek4da21662012-07-06 05:48:52 +00002978
2979TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
2980 // Make sure that we can both match the class by name (::X) and by the type
2981 // the template was instantiated with (via a field).
2982
2983 EXPECT_TRUE(matches(
2984 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002985 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00002986
2987 EXPECT_TRUE(matches(
2988 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002989 recordDecl(isTemplateInstantiation(), hasDescendant(
2990 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002991}
2992
2993TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
2994 EXPECT_TRUE(matches(
2995 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002996 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00002997 isTemplateInstantiation())));
2998}
2999
3000TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3001 EXPECT_TRUE(matches(
3002 "template <typename T> class X { T t; }; class A {};"
3003 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003004 recordDecl(isTemplateInstantiation(), hasDescendant(
3005 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003006}
3007
3008TEST(IsTemplateInstantiation,
3009 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3010 EXPECT_TRUE(matches(
3011 "template <typename T> class X {};"
3012 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003013 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003014}
3015
3016TEST(IsTemplateInstantiation,
3017 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3018 EXPECT_TRUE(matches(
3019 "class A {};"
3020 "class X {"
3021 " template <typename U> class Y { U u; };"
3022 " Y<A> y;"
3023 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003024 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003025}
3026
3027TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3028 // FIXME: Figure out whether this makes sense. It doesn't affect the
3029 // normal use case as long as the uppermost instantiation always is marked
3030 // as template instantiation, but it might be confusing as a predicate.
3031 EXPECT_TRUE(matches(
3032 "class A {};"
3033 "template <typename T> class X {"
3034 " template <typename U> class Y { U u; };"
3035 " Y<T> y;"
3036 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003037 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003038}
3039
3040TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3041 EXPECT_TRUE(notMatches(
3042 "template <typename T> class X {}; class A {};"
3043 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003044 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003045}
3046
3047TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3048 EXPECT_TRUE(notMatches(
3049 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003050 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003051}
3052
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003053TEST(IsExplicitTemplateSpecialization,
3054 DoesNotMatchPrimaryTemplate) {
3055 EXPECT_TRUE(notMatches(
3056 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003057 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003058 EXPECT_TRUE(notMatches(
3059 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003060 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003061}
3062
3063TEST(IsExplicitTemplateSpecialization,
3064 DoesNotMatchExplicitTemplateInstantiations) {
3065 EXPECT_TRUE(notMatches(
3066 "template <typename T> class X {};"
3067 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003068 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003069 EXPECT_TRUE(notMatches(
3070 "template <typename T> void f(T t) {}"
3071 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003072 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003073}
3074
3075TEST(IsExplicitTemplateSpecialization,
3076 DoesNotMatchImplicitTemplateInstantiations) {
3077 EXPECT_TRUE(notMatches(
3078 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003079 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003080 EXPECT_TRUE(notMatches(
3081 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003082 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003083}
3084
3085TEST(IsExplicitTemplateSpecialization,
3086 MatchesExplicitTemplateSpecializations) {
3087 EXPECT_TRUE(matches(
3088 "template <typename T> class X {};"
3089 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003090 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003091 EXPECT_TRUE(matches(
3092 "template <typename T> void f(T t) {}"
3093 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003094 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003095}
3096
Manuel Klimek579b1202012-09-07 09:26:10 +00003097TEST(HasAncenstor, MatchesDeclarationAncestors) {
3098 EXPECT_TRUE(matches(
3099 "class A { class B { class C {}; }; };",
3100 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3101}
3102
3103TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3104 EXPECT_TRUE(notMatches(
3105 "class A { class B { class C {}; }; };",
3106 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3107}
3108
3109TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3110 EXPECT_TRUE(matches(
3111 "class A { class B { void f() { C c; } class C {}; }; };",
3112 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3113 hasAncestor(recordDecl(hasName("A"))))))));
3114}
3115
3116TEST(HasAncenstor, MatchesStatementAncestors) {
3117 EXPECT_TRUE(matches(
3118 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003119 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003120}
3121
3122TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3123 EXPECT_TRUE(matches(
3124 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003125 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003126}
3127
3128TEST(HasAncestor, BindsRecursiveCombinations) {
3129 EXPECT_TRUE(matchAndVerifyResultTrue(
3130 "class C { class D { class E { class F { int y; }; }; }; };",
3131 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003132 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003133}
3134
3135TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3136 EXPECT_TRUE(matchAndVerifyResultTrue(
3137 "class C { class D { class E { class F { int y; }; }; }; };",
3138 fieldDecl(hasAncestor(
3139 decl(
3140 hasDescendant(recordDecl(isDefinition(),
3141 hasAncestor(recordDecl())))
3142 ).bind("d")
3143 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003144 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003145}
3146
3147TEST(HasAncestor, MatchesInTemplateInstantiations) {
3148 EXPECT_TRUE(matches(
3149 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3150 "A<int>::B::C a;",
3151 fieldDecl(hasType(asString("int")),
3152 hasAncestor(recordDecl(hasName("A"))))));
3153}
3154
3155TEST(HasAncestor, MatchesInImplicitCode) {
3156 EXPECT_TRUE(matches(
3157 "struct X {}; struct A { A() {} X x; };",
3158 constructorDecl(
3159 hasAnyConstructorInitializer(withInitializer(expr(
3160 hasAncestor(recordDecl(hasName("A")))))))));
3161}
3162
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003163TEST(HasParent, MatchesOnlyParent) {
3164 EXPECT_TRUE(matches(
3165 "void f() { if (true) { int x = 42; } }",
3166 compoundStmt(hasParent(ifStmt()))));
3167 EXPECT_TRUE(notMatches(
3168 "void f() { for (;;) { int x = 42; } }",
3169 compoundStmt(hasParent(ifStmt()))));
3170 EXPECT_TRUE(notMatches(
3171 "void f() { if (true) for (;;) { int x = 42; } }",
3172 compoundStmt(hasParent(ifStmt()))));
3173}
3174
Manuel Klimek30ace372012-12-06 14:42:48 +00003175TEST(HasAncestor, MatchesAllAncestors) {
3176 EXPECT_TRUE(matches(
3177 "template <typename T> struct C { static void f() { 42; } };"
3178 "void t() { C<int>::f(); }",
3179 integerLiteral(
3180 equals(42),
3181 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3182 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3183}
3184
3185TEST(HasParent, MatchesAllParents) {
3186 EXPECT_TRUE(matches(
3187 "template <typename T> struct C { static void f() { 42; } };"
3188 "void t() { C<int>::f(); }",
3189 integerLiteral(
3190 equals(42),
3191 hasParent(compoundStmt(hasParent(functionDecl(
3192 hasParent(recordDecl(isTemplateInstantiation())))))))));
3193 EXPECT_TRUE(matches(
3194 "template <typename T> struct C { static void f() { 42; } };"
3195 "void t() { C<int>::f(); }",
3196 integerLiteral(
3197 equals(42),
3198 hasParent(compoundStmt(hasParent(functionDecl(
3199 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3200 EXPECT_TRUE(matches(
3201 "template <typename T> struct C { static void f() { 42; } };"
3202 "void t() { C<int>::f(); }",
3203 integerLiteral(equals(42),
3204 hasParent(compoundStmt(allOf(
3205 hasParent(functionDecl(
3206 hasParent(recordDecl(isTemplateInstantiation())))),
3207 hasParent(functionDecl(hasParent(recordDecl(
3208 unless(isTemplateInstantiation())))))))))));
3209}
3210
Daniel Jasperce620072012-10-17 08:52:59 +00003211TEST(TypeMatching, MatchesTypes) {
3212 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3213}
3214
3215TEST(TypeMatching, MatchesArrayTypes) {
3216 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3217 EXPECT_TRUE(matches("int a[42];", arrayType()));
3218 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3219
3220 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3221 arrayType(hasElementType(builtinType()))));
3222
3223 EXPECT_TRUE(matches(
3224 "int const a[] = { 2, 3 };",
3225 qualType(arrayType(hasElementType(builtinType())))));
3226 EXPECT_TRUE(matches(
3227 "int const a[] = { 2, 3 };",
3228 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3229 EXPECT_TRUE(matches(
3230 "typedef const int T; T x[] = { 1, 2 };",
3231 qualType(isConstQualified(), arrayType())));
3232
3233 EXPECT_TRUE(notMatches(
3234 "int a[] = { 2, 3 };",
3235 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3236 EXPECT_TRUE(notMatches(
3237 "int a[] = { 2, 3 };",
3238 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3239 EXPECT_TRUE(notMatches(
3240 "int const a[] = { 2, 3 };",
3241 qualType(arrayType(hasElementType(builtinType())),
3242 unless(isConstQualified()))));
3243
3244 EXPECT_TRUE(matches("int a[2];",
3245 constantArrayType(hasElementType(builtinType()))));
3246 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3247}
3248
3249TEST(TypeMatching, MatchesComplexTypes) {
3250 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3251 EXPECT_TRUE(matches(
3252 "_Complex float f;",
3253 complexType(hasElementType(builtinType()))));
3254 EXPECT_TRUE(notMatches(
3255 "_Complex float f;",
3256 complexType(hasElementType(isInteger()))));
3257}
3258
3259TEST(TypeMatching, MatchesConstantArrayTypes) {
3260 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3261 EXPECT_TRUE(notMatches(
3262 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3263 constantArrayType(hasElementType(builtinType()))));
3264
3265 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3266 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3267 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3268}
3269
3270TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3271 EXPECT_TRUE(matches(
3272 "template <typename T, int Size> class array { T data[Size]; };",
3273 dependentSizedArrayType()));
3274 EXPECT_TRUE(notMatches(
3275 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3276 dependentSizedArrayType()));
3277}
3278
3279TEST(TypeMatching, MatchesIncompleteArrayType) {
3280 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3281 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3282
3283 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3284 incompleteArrayType()));
3285}
3286
3287TEST(TypeMatching, MatchesVariableArrayType) {
3288 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3289 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3290
3291 EXPECT_TRUE(matches(
3292 "void f(int b) { int a[b]; }",
3293 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3294 varDecl(hasName("b")))))))));
3295}
3296
3297TEST(TypeMatching, MatchesAtomicTypes) {
3298 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3299
3300 EXPECT_TRUE(matches("_Atomic(int) i;",
3301 atomicType(hasValueType(isInteger()))));
3302 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3303 atomicType(hasValueType(isInteger()))));
3304}
3305
3306TEST(TypeMatching, MatchesAutoTypes) {
3307 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3308 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3309 autoType()));
3310
3311 EXPECT_TRUE(matches("auto a = 1;",
3312 autoType(hasDeducedType(isInteger()))));
3313 EXPECT_TRUE(notMatches("auto b = 2.0;",
3314 autoType(hasDeducedType(isInteger()))));
3315}
3316
Daniel Jaspera267cf62012-10-29 10:14:44 +00003317TEST(TypeMatching, MatchesFunctionTypes) {
3318 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3319 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3320}
3321
Daniel Jasperce620072012-10-17 08:52:59 +00003322TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003323 // FIXME: Reactive when these tests can be more specific (not matching
3324 // implicit code on certain platforms), likely when we have hasDescendant for
3325 // Types/TypeLocs.
3326 //EXPECT_TRUE(matchAndVerifyResultTrue(
3327 // "int* a;",
3328 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3329 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3330 //EXPECT_TRUE(matchAndVerifyResultTrue(
3331 // "int* a;",
3332 // pointerTypeLoc().bind("loc"),
3333 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003334 EXPECT_TRUE(matches(
3335 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003336 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003337 EXPECT_TRUE(matches(
3338 "int** a;",
3339 loc(pointerType(pointee(pointerType())))));
3340 EXPECT_TRUE(matches(
3341 "int* b; int* * const a = &b;",
3342 loc(qualType(isConstQualified(), pointerType()))));
3343
3344 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003345 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3346 hasType(blockPointerType()))));
3347 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3348 hasType(memberPointerType()))));
3349 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3350 hasType(pointerType()))));
3351 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3352 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003353
Daniel Jasper1802daf2012-10-17 13:35:36 +00003354 Fragment = "int *ptr;";
3355 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3356 hasType(blockPointerType()))));
3357 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3358 hasType(memberPointerType()))));
3359 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3360 hasType(pointerType()))));
3361 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3362 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003363
Daniel Jasper1802daf2012-10-17 13:35:36 +00003364 Fragment = "int a; int &ref = a;";
3365 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3366 hasType(blockPointerType()))));
3367 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3368 hasType(memberPointerType()))));
3369 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3370 hasType(pointerType()))));
3371 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3372 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003373}
3374
3375TEST(TypeMatching, PointeeTypes) {
3376 EXPECT_TRUE(matches("int b; int &a = b;",
3377 referenceType(pointee(builtinType()))));
3378 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3379
3380 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003381 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003382
3383 EXPECT_TRUE(matches(
3384 "int const *A;",
3385 pointerType(pointee(isConstQualified(), builtinType()))));
3386 EXPECT_TRUE(notMatches(
3387 "int *A;",
3388 pointerType(pointee(isConstQualified(), builtinType()))));
3389}
3390
3391TEST(TypeMatching, MatchesPointersToConstTypes) {
3392 EXPECT_TRUE(matches("int b; int * const a = &b;",
3393 loc(pointerType())));
3394 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003395 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003396 EXPECT_TRUE(matches(
3397 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003398 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003399 EXPECT_TRUE(matches(
3400 "int b; const int * a = &b;",
3401 pointerType(pointee(builtinType()))));
3402}
3403
3404TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003405 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3406 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003407}
3408
Daniel Jaspera7564432012-09-13 13:11:25 +00003409TEST(NNS, MatchesNestedNameSpecifiers) {
3410 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3411 nestedNameSpecifier()));
3412 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3413 nestedNameSpecifier()));
3414 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3415 nestedNameSpecifier()));
3416
3417 EXPECT_TRUE(matches(
3418 "struct A { static void f() {} }; void g() { A::f(); }",
3419 nestedNameSpecifier()));
3420 EXPECT_TRUE(notMatches(
3421 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3422 nestedNameSpecifier()));
3423}
3424
Daniel Jasperb54b7642012-09-20 14:12:57 +00003425TEST(NullStatement, SimpleCases) {
3426 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3427 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3428}
3429
Daniel Jaspera7564432012-09-13 13:11:25 +00003430TEST(NNS, MatchesTypes) {
3431 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3432 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3433 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3434 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3435 Matcher));
3436 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3437}
3438
3439TEST(NNS, MatchesNamespaceDecls) {
3440 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3441 specifiesNamespace(hasName("ns")));
3442 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3443 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3444 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3445}
3446
3447TEST(NNS, BindsNestedNameSpecifiers) {
3448 EXPECT_TRUE(matchAndVerifyResultTrue(
3449 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3450 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3451 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3452}
3453
3454TEST(NNS, BindsNestedNameSpecifierLocs) {
3455 EXPECT_TRUE(matchAndVerifyResultTrue(
3456 "namespace ns { struct B {}; } ns::B b;",
3457 loc(nestedNameSpecifier()).bind("loc"),
3458 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3459}
3460
3461TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3462 EXPECT_TRUE(matches(
3463 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3464 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3465 EXPECT_TRUE(matches(
3466 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003467 nestedNameSpecifierLoc(hasPrefix(
3468 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003469}
3470
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003471TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3472 std::string Fragment =
3473 "namespace a { struct A { struct B { struct C {}; }; }; };"
3474 "void f() { a::A::B::C c; }";
3475 EXPECT_TRUE(matches(
3476 Fragment,
3477 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3478 hasDescendant(nestedNameSpecifier(
3479 specifiesNamespace(hasName("a")))))));
3480 EXPECT_TRUE(notMatches(
3481 Fragment,
3482 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3483 has(nestedNameSpecifier(
3484 specifiesNamespace(hasName("a")))))));
3485 EXPECT_TRUE(matches(
3486 Fragment,
3487 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3488 has(nestedNameSpecifier(
3489 specifiesNamespace(hasName("a")))))));
3490
3491 // Not really useful because a NestedNameSpecifier can af at most one child,
3492 // but to complete the interface.
3493 EXPECT_TRUE(matchAndVerifyResultTrue(
3494 Fragment,
3495 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3496 forEach(nestedNameSpecifier().bind("x"))),
3497 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3498}
3499
3500TEST(NNS, NestedNameSpecifiersAsDescendants) {
3501 std::string Fragment =
3502 "namespace a { struct A { struct B { struct C {}; }; }; };"
3503 "void f() { a::A::B::C c; }";
3504 EXPECT_TRUE(matches(
3505 Fragment,
3506 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3507 asString("struct a::A")))))));
3508 EXPECT_TRUE(matchAndVerifyResultTrue(
3509 Fragment,
3510 functionDecl(hasName("f"),
3511 forEachDescendant(nestedNameSpecifier().bind("x"))),
3512 // Nested names: a, a::A and a::A::B.
3513 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3514}
3515
3516TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3517 std::string Fragment =
3518 "namespace a { struct A { struct B { struct C {}; }; }; };"
3519 "void f() { a::A::B::C c; }";
3520 EXPECT_TRUE(matches(
3521 Fragment,
3522 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3523 hasDescendant(loc(nestedNameSpecifier(
3524 specifiesNamespace(hasName("a"))))))));
3525 EXPECT_TRUE(notMatches(
3526 Fragment,
3527 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3528 has(loc(nestedNameSpecifier(
3529 specifiesNamespace(hasName("a"))))))));
3530 EXPECT_TRUE(matches(
3531 Fragment,
3532 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3533 has(loc(nestedNameSpecifier(
3534 specifiesNamespace(hasName("a"))))))));
3535
3536 EXPECT_TRUE(matchAndVerifyResultTrue(
3537 Fragment,
3538 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3539 forEach(nestedNameSpecifierLoc().bind("x"))),
3540 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3541}
3542
3543TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3544 std::string Fragment =
3545 "namespace a { struct A { struct B { struct C {}; }; }; };"
3546 "void f() { a::A::B::C c; }";
3547 EXPECT_TRUE(matches(
3548 Fragment,
3549 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3550 asString("struct a::A"))))))));
3551 EXPECT_TRUE(matchAndVerifyResultTrue(
3552 Fragment,
3553 functionDecl(hasName("f"),
3554 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3555 // Nested names: a, a::A and a::A::B.
3556 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3557}
3558
Manuel Klimek60969f52013-02-01 13:41:35 +00003559template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003560public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003561 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3562 StringRef InnerId)
3563 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003564 }
3565
Manuel Klimek60969f52013-02-01 13:41:35 +00003566 virtual bool run(const BoundNodes *Nodes) { return false; }
3567
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003568 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3569 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003570 return selectFirst<const T>(InnerId,
3571 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003572 }
3573private:
3574 std::string Id;
3575 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003576 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003577};
3578
3579TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003580 EXPECT_TRUE(matchAndVerifyResultTrue(
3581 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3582 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003583 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
3584 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003585 EXPECT_TRUE(matchAndVerifyResultFalse(
3586 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3587 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003588 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
3589 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003590}
3591
3592TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003593 EXPECT_TRUE(matchAndVerifyResultTrue(
3594 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003595 new VerifyMatchOnNode<clang::Stmt>(
3596 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003597 EXPECT_TRUE(matchAndVerifyResultFalse(
3598 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003599 new VerifyMatchOnNode<clang::Stmt>(
3600 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003601}
3602
3603TEST(MatchFinder, CanMatchSingleNodesRecursively) {
3604 EXPECT_TRUE(matchAndVerifyResultTrue(
3605 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3606 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003607 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003608 EXPECT_TRUE(matchAndVerifyResultFalse(
3609 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3610 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003611 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003612}
3613
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00003614template <typename T>
3615class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
3616public:
3617 virtual bool run(const BoundNodes *Nodes) { return false; }
3618
3619 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3620 const T *Node = Nodes->getNodeAs<T>("");
3621 return verify(*Nodes, *Context, Node);
3622 }
3623
3624 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
3625 return selectFirst<const T>(
3626 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
3627 *Node, Context)) != NULL;
3628 }
3629 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
3630 return selectFirst<const T>(
3631 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
3632 *Node, Context)) != NULL;
3633 }
3634};
3635
3636TEST(IsEqualTo, MatchesNodesByIdentity) {
3637 EXPECT_TRUE(matchAndVerifyResultTrue(
3638 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
3639 new VerifyAncestorHasChildIsEqual<Decl>()));
3640 EXPECT_TRUE(
3641 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
3642 new VerifyAncestorHasChildIsEqual<Stmt>()));
3643}
3644
Manuel Klimeke5793282012-11-02 01:31:03 +00003645class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3646public:
3647 VerifyStartOfTranslationUnit() : Called(false) {}
3648 virtual void run(const MatchFinder::MatchResult &Result) {
3649 EXPECT_TRUE(Called);
3650 }
3651 virtual void onStartOfTranslationUnit() {
3652 Called = true;
3653 }
3654 bool Called;
3655};
3656
3657TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3658 MatchFinder Finder;
3659 VerifyStartOfTranslationUnit VerifyCallback;
3660 Finder.addMatcher(decl(), &VerifyCallback);
3661 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3662 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3663 EXPECT_TRUE(VerifyCallback.Called);
3664}
3665
Manuel Klimek4da21662012-07-06 05:48:52 +00003666} // end namespace ast_matchers
3667} // end namespace clang