blob: 318d09c9bfde792bc10c89f3ab9713a03328ab50 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
Benjamin Kramer8b9ed712012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000014#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace ast_matchers {
19
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000020#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000021TEST(HasNameDeathTest, DiesOnEmptyName) {
22 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000023 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000024 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
25 }, "");
26}
27
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000028TEST(HasNameDeathTest, DiesOnEmptyPattern) {
29 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000030 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000031 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
32 }, "");
33}
34
Manuel Klimek4da21662012-07-06 05:48:52 +000035TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
36 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000037 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000038 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
39 }, "");
40}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000041#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000042
Manuel Klimek715c9562012-07-25 10:02:02 +000043TEST(Decl, MatchesDeclarations) {
44 EXPECT_TRUE(notMatches("", decl(usingDecl())));
45 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
46 decl(usingDecl())));
47}
48
Manuel Klimek4da21662012-07-06 05:48:52 +000049TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000050 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000051 EXPECT_TRUE(matches("typedef int X;", NamedX));
52 EXPECT_TRUE(matches("int X;", NamedX));
53 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
54 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
55 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
56 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000057 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000058
59 EXPECT_TRUE(notMatches("#define X 1", NamedX));
60}
61
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000062TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000063 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000064 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
65 EXPECT_TRUE(matches("int Xb;", NamedX));
66 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
67 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
68 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
69 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
70 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
71
72 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
73
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000074 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000075 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
76 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
77
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000078 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000079 EXPECT_TRUE(matches("int abc;", Abc));
80 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
81 EXPECT_TRUE(notMatches("int cab;", Abc));
82 EXPECT_TRUE(matches("int cabc;", Abc));
Manuel Klimekec33b6f2012-12-10 07:08:53 +000083
84 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
85 EXPECT_TRUE(matches("int k;", StartsWithK));
86 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
87 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
88 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
89 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000090}
91
Manuel Klimek4da21662012-07-06 05:48:52 +000092TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000094#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000095 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000096#else
97 // Matches class type_info.
98 EXPECT_TRUE(matches("", ClassMatcher));
99#endif
Manuel Klimek4da21662012-07-06 05:48:52 +0000100
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102 EXPECT_TRUE(matches("class X;", ClassX));
103 EXPECT_TRUE(matches("class X {};", ClassX));
104 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
105 EXPECT_TRUE(notMatches("", ClassX));
106}
107
108TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000109 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000112 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
113 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000114 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
115 EXPECT_TRUE(notMatches("", IsDerivedFromX));
116
Daniel Jasper63d88722012-09-12 21:14:15 +0000117 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000118
119 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
120 EXPECT_TRUE(matches("class X {};", IsAX));
121 EXPECT_TRUE(matches("class X;", IsAX));
122 EXPECT_TRUE(notMatches("class Y;", IsAX));
123 EXPECT_TRUE(notMatches("", IsAX));
124
Manuel Klimek4da21662012-07-06 05:48:52 +0000125 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000126 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000127 EXPECT_TRUE(
128 matches("class X {}; class Y : public X {}; class Z : public Y {};",
129 ZIsDerivedFromX));
130 EXPECT_TRUE(
131 matches("class X {};"
132 "template<class T> class Y : public X {};"
133 "class Z : public Y<int> {};", ZIsDerivedFromX));
134 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 matches("template<class T> class X {}; "
138 "template<class T> class Z : public X<T> {};",
139 ZIsDerivedFromX));
140 EXPECT_TRUE(
141 matches("template<class T, class U=T> class X {}; "
142 "template<class T> class Z : public X<T> {};",
143 ZIsDerivedFromX));
144 EXPECT_TRUE(
145 notMatches("template<class X> class A { class Z : public X {}; };",
146 ZIsDerivedFromX));
147 EXPECT_TRUE(
148 matches("template<class X> class A { public: class Z : public X {}; }; "
149 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template <class T> class X {}; "
152 "template<class Y> class A { class Z : public X<Y> {}; };",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<template<class T> class X> class A { "
156 " class Z : public X<int> {}; };", ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<template<class T> class X> class A { "
159 " public: class Z : public X<int> {}; }; "
160 "template<class T> class X {}; void y() { A<X>::Z z; }",
161 ZIsDerivedFromX));
162 EXPECT_TRUE(
163 notMatches("template<class X> class A { class Z : public X::D {}; };",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class X> class A { public: "
167 " class Z : public X::D {}; }; "
168 "class Y { public: class X {}; typedef X D; }; "
169 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
170 EXPECT_TRUE(
171 matches("class X {}; typedef X Y; class Z : public Y {};",
172 ZIsDerivedFromX));
173 EXPECT_TRUE(
174 matches("template<class T> class Y { typedef typename T::U X; "
175 " class Z : public X {}; };", ZIsDerivedFromX));
176 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
177 ZIsDerivedFromX));
178 EXPECT_TRUE(
179 notMatches("template<class T> class X {}; "
180 "template<class T> class A { class Z : public X<T>::D {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("template<class T> class X { public: typedef X<T> D; }; "
184 "template<class T> class A { public: "
185 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
186 ZIsDerivedFromX));
187 EXPECT_TRUE(
188 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 matches("class X {}; class Y : public X {}; "
195 "typedef Y V; typedef V W; class Z : public W {};",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("template<class T, class U> class X {}; "
199 "template<class T> class A { class Z : public X<T, int> {}; };",
200 ZIsDerivedFromX));
201 EXPECT_TRUE(
202 notMatches("template<class X> class D { typedef X A; typedef A B; "
203 " typedef B C; class Z : public C {}; };",
204 ZIsDerivedFromX));
205 EXPECT_TRUE(
206 matches("class X {}; typedef X A; typedef A B; "
207 "class Z : public B {};", ZIsDerivedFromX));
208 EXPECT_TRUE(
209 matches("class X {}; typedef X A; typedef A B; typedef B C; "
210 "class Z : public C {};", ZIsDerivedFromX));
211 EXPECT_TRUE(
212 matches("class U {}; typedef U X; typedef X V; "
213 "class Z : public V {};", ZIsDerivedFromX));
214 EXPECT_TRUE(
215 matches("class Base {}; typedef Base X; "
216 "class Z : public Base {};", ZIsDerivedFromX));
217 EXPECT_TRUE(
218 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
219 "class Z : public Base {};", ZIsDerivedFromX));
220 EXPECT_TRUE(
221 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
222 "class Z : public Base {};", ZIsDerivedFromX));
223 EXPECT_TRUE(
224 matches("class A {}; typedef A X; typedef A Y; "
225 "class Z : public Y {};", ZIsDerivedFromX));
226 EXPECT_TRUE(
227 notMatches("template <typename T> class Z;"
228 "template <> class Z<void> {};"
229 "template <typename T> class Z : public Z<void> {};",
230 IsDerivedFromX));
231 EXPECT_TRUE(
232 matches("template <typename T> class X;"
233 "template <> class X<void> {};"
234 "template <typename T> class X : public X<void> {};",
235 IsDerivedFromX));
236 EXPECT_TRUE(matches(
237 "class X {};"
238 "template <typename T> class Z;"
239 "template <> class Z<void> {};"
240 "template <typename T> class Z : public Z<void>, public X {};",
241 ZIsDerivedFromX));
Manuel Klimek987c2f52012-12-04 13:40:29 +0000242 EXPECT_TRUE(
243 notMatches("template<int> struct X;"
244 "template<int i> struct X : public X<i-1> {};",
245 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
246 EXPECT_TRUE(matches(
247 "struct A {};"
248 "template<int> struct X;"
249 "template<int i> struct X : public X<i-1> {};"
250 "template<> struct X<0> : public A {};"
251 "struct B : public X<42> {};",
252 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000253
254 // FIXME: Once we have better matchers for template type matching,
255 // get rid of the Variable(...) matching and match the right template
256 // declarations directly.
257 const char *RecursiveTemplateOneParameter =
258 "class Base1 {}; class Base2 {};"
259 "template <typename T> class Z;"
260 "template <> class Z<void> : public Base1 {};"
261 "template <> class Z<int> : public Base2 {};"
262 "template <> class Z<float> : public Z<void> {};"
263 "template <> class Z<double> : public Z<int> {};"
264 "template <typename T> class Z : public Z<float>, public Z<double> {};"
265 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
266 EXPECT_TRUE(matches(
267 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000268 varDecl(hasName("z_float"),
269 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000270 EXPECT_TRUE(notMatches(
271 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000272 varDecl(hasName("z_float"),
273 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000274 EXPECT_TRUE(matches(
275 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000276 varDecl(hasName("z_char"),
277 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
278 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000279
280 const char *RecursiveTemplateTwoParameters =
281 "class Base1 {}; class Base2 {};"
282 "template <typename T1, typename T2> class Z;"
283 "template <typename T> class Z<void, T> : public Base1 {};"
284 "template <typename T> class Z<int, T> : public Base2 {};"
285 "template <typename T> class Z<float, T> : public Z<void, T> {};"
286 "template <typename T> class Z<double, T> : public Z<int, T> {};"
287 "template <typename T1, typename T2> class Z : "
288 " public Z<float, T2>, public Z<double, T2> {};"
289 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
290 " Z<char, void> z_char; }";
291 EXPECT_TRUE(matches(
292 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000293 varDecl(hasName("z_float"),
294 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295 EXPECT_TRUE(notMatches(
296 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000297 varDecl(hasName("z_float"),
298 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000299 EXPECT_TRUE(matches(
300 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000301 varDecl(hasName("z_char"),
302 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
303 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000304 EXPECT_TRUE(matches(
305 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000306 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000307 EXPECT_TRUE(notMatches(
308 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000309 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000310
311 EXPECT_TRUE(matches(
312 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000313 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000314}
315
Edwin Vane6a19a972013-03-06 17:02:57 +0000316TEST(DeclarationMatcher, hasMethod) {
317 EXPECT_TRUE(matches("class A { void func(); };",
318 recordDecl(hasMethod(hasName("func")))));
319 EXPECT_TRUE(notMatches("class A { void func(); };",
320 recordDecl(hasMethod(isPublic()))));
321}
322
Daniel Jasper08f0c532012-09-18 14:17:42 +0000323TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
324 EXPECT_TRUE(matches(
325 "template <typename T> struct A {"
326 " template <typename T2> struct F {};"
327 "};"
328 "template <typename T> struct B : A<T>::template F<T> {};"
329 "B<int> b;",
330 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
331}
332
Edwin Vane742d9e72013-02-25 20:43:32 +0000333TEST(DeclarationMatcher, hasDeclContext) {
334 EXPECT_TRUE(matches(
335 "namespace N {"
336 " namespace M {"
337 " class D {};"
338 " }"
339 "}",
340 recordDecl(hasDeclContext(namedDecl(hasName("M"))))));
341 EXPECT_TRUE(notMatches(
342 "namespace N {"
343 " namespace M {"
344 " class D {};"
345 " }"
346 "}",
347 recordDecl(hasDeclContext(namedDecl(hasName("N"))))));
348}
349
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000350TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000351 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000352 EXPECT_TRUE(notMatches("class X;", ClassX));
353 EXPECT_TRUE(notMatches("class X {};", ClassX));
354}
355
356TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000357 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000358 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
359 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
360}
361
362TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
363 EXPECT_TRUE(notMatches("template<typename T> class X { };"
364 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000365 classTemplateDecl(hasName("X"),
366 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000367}
368
369TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
370 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
371 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000372 classTemplateDecl(hasName("X"),
373 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000374}
375
Daniel Jasper6a124492012-07-12 08:50:38 +0000376TEST(AllOf, AllOverloadsWork) {
377 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000378 "struct T { };"
379 "int f(int, T*, int, int);"
380 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000381 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000382 callExpr(allOf(callee(functionDecl(hasName("f"))),
383 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000384 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000385 callExpr(allOf(callee(functionDecl(hasName("f"))),
386 hasArgument(0, declRefExpr(to(varDecl()))),
387 hasArgument(1, hasType(pointsTo(
388 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000389 EXPECT_TRUE(matches(Program,
390 callExpr(allOf(callee(functionDecl(hasName("f"))),
391 hasArgument(0, declRefExpr(to(varDecl()))),
392 hasArgument(1, hasType(pointsTo(
393 recordDecl(hasName("T"))))),
394 hasArgument(2, integerLiteral(equals(3)))))));
395 EXPECT_TRUE(matches(Program,
396 callExpr(allOf(callee(functionDecl(hasName("f"))),
397 hasArgument(0, declRefExpr(to(varDecl()))),
398 hasArgument(1, hasType(pointsTo(
399 recordDecl(hasName("T"))))),
400 hasArgument(2, integerLiteral(equals(3))),
401 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000402}
403
Manuel Klimek4da21662012-07-06 05:48:52 +0000404TEST(DeclarationMatcher, MatchAnyOf) {
405 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000406 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000407 EXPECT_TRUE(
408 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
409 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
410 EXPECT_TRUE(
411 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
412 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
413
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000414 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000415 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000416 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
417 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
418
Manuel Klimek4da21662012-07-06 05:48:52 +0000419 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000420 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
421 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000422 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
423 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
424 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
425 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
426 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
427 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
428}
429
430TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000431 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000432 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
433 EXPECT_TRUE(matches("class X {};", HasClassX));
434
435 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000436 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000437 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
438 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
439 EXPECT_TRUE(
440 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
441}
442
443TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
444 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000445 recordDecl(
446 has(recordDecl(
447 has(recordDecl(hasName("X"))),
448 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000449 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000450 has(recordDecl(
451 has(recordDecl(hasName("A"))),
452 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000453 hasName("C"))),
454 hasName("F"));
455
456 EXPECT_TRUE(matches(
457 "class F {"
458 " class Z {"
459 " class X {};"
460 " class Y {};"
461 " };"
462 " class C {"
463 " class A {};"
464 " class B {};"
465 " };"
466 "};", Recursive));
467
468 EXPECT_TRUE(matches(
469 "class F {"
470 " class Z {"
471 " class A {};"
472 " class X {};"
473 " class Y {};"
474 " };"
475 " class C {"
476 " class X {};"
477 " class A {};"
478 " class B {};"
479 " };"
480 "};", Recursive));
481
482 EXPECT_TRUE(matches(
483 "class O1 {"
484 " class O2 {"
485 " class F {"
486 " class Z {"
487 " class A {};"
488 " class X {};"
489 " class Y {};"
490 " };"
491 " class C {"
492 " class X {};"
493 " class A {};"
494 " class B {};"
495 " };"
496 " };"
497 " };"
498 "};", Recursive));
499}
500
501TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
502 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000503 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000504 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000505 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000506 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000507 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000508 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000509 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000510 hasName("Y"))),
511 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000512 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000513 anyOf(
514 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000516 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000517 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000518 hasName("B")))))),
519 hasName("F")));
520
521 EXPECT_TRUE(matches("class F {};", Recursive));
522 EXPECT_TRUE(matches("class Z {};", Recursive));
523 EXPECT_TRUE(matches("class C {};", Recursive));
524 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
525 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
526 EXPECT_TRUE(
527 matches("class O1 { class O2 {"
528 " class M { class N { class B {}; }; }; "
529 "}; };", Recursive));
530}
531
532TEST(DeclarationMatcher, MatchNot) {
533 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000534 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000535 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000536 unless(hasName("X")));
537 EXPECT_TRUE(notMatches("", NotClassX));
538 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
539 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
540 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
541 EXPECT_TRUE(
542 notMatches("class Y {}; class Z {}; class X : public Y {};",
543 NotClassX));
544
545 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000546 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000547 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000548 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000549 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000550 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000551 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
552 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
553 ClassXHasNotClassY));
554}
555
556TEST(DeclarationMatcher, HasDescendant) {
557 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000558 recordDecl(
559 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000560 hasName("Z"));
561 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
562 EXPECT_TRUE(
563 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
564 EXPECT_TRUE(
565 matches("class Z { class A { class Y { class X {}; }; }; };",
566 ZDescendantClassX));
567 EXPECT_TRUE(
568 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
569 ZDescendantClassX));
570 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
571
572 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000573 recordDecl(
574 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000575 hasName("X"))),
576 hasName("Z"));
577 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
578 ZDescendantClassXHasClassY));
579 EXPECT_TRUE(
580 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
581 ZDescendantClassXHasClassY));
582 EXPECT_TRUE(notMatches(
583 "class Z {"
584 " class A {"
585 " class B {"
586 " class X {"
587 " class C {"
588 " class Y {};"
589 " };"
590 " };"
591 " }; "
592 " };"
593 "};", ZDescendantClassXHasClassY));
594
595 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000596 recordDecl(
597 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
598 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000599 hasName("Z"));
600 EXPECT_TRUE(
601 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
602 ZDescendantClassXDescendantClassY));
603 EXPECT_TRUE(matches(
604 "class Z {"
605 " class A {"
606 " class X {"
607 " class B {"
608 " class Y {};"
609 " };"
610 " class Y {};"
611 " };"
612 " };"
613 "};", ZDescendantClassXDescendantClassY));
614}
615
Daniel Jaspera267cf62012-10-29 10:14:44 +0000616// Implements a run method that returns whether BoundNodes contains a
617// Decl bound to Id that can be dynamically cast to T.
618// Optionally checks that the check succeeded a specific number of times.
619template <typename T>
620class VerifyIdIsBoundTo : public BoundNodesCallback {
621public:
622 // Create an object that checks that a node of type \c T was bound to \c Id.
623 // Does not check for a certain number of matches.
624 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
625 : Id(Id), ExpectedCount(-1), Count(0) {}
626
627 // Create an object that checks that a node of type \c T was bound to \c Id.
628 // Checks that there were exactly \c ExpectedCount matches.
629 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
630 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
631
632 // Create an object that checks that a node of type \c T was bound to \c Id.
633 // Checks that there was exactly one match with the name \c ExpectedName.
634 // Note that \c T must be a NamedDecl for this to work.
635 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName)
636 : Id(Id), ExpectedCount(1), Count(0), ExpectedName(ExpectedName) {}
637
638 ~VerifyIdIsBoundTo() {
639 if (ExpectedCount != -1)
640 EXPECT_EQ(ExpectedCount, Count);
641 if (!ExpectedName.empty())
642 EXPECT_EQ(ExpectedName, Name);
643 }
644
645 virtual bool run(const BoundNodes *Nodes) {
646 if (Nodes->getNodeAs<T>(Id)) {
647 ++Count;
648 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
649 Name = Named->getNameAsString();
650 } else if (const NestedNameSpecifier *NNS =
651 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
652 llvm::raw_string_ostream OS(Name);
653 NNS->print(OS, PrintingPolicy(LangOptions()));
654 }
655 return true;
656 }
657 return false;
658 }
659
Daniel Jasper452abbc2012-10-29 10:48:25 +0000660 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
661 return run(Nodes);
662 }
663
Daniel Jaspera267cf62012-10-29 10:14:44 +0000664private:
665 const std::string Id;
666 const int ExpectedCount;
667 int Count;
668 const std::string ExpectedName;
669 std::string Name;
670};
671
672TEST(HasDescendant, MatchesDescendantTypes) {
673 EXPECT_TRUE(matches("void f() { int i = 3; }",
674 decl(hasDescendant(loc(builtinType())))));
675 EXPECT_TRUE(matches("void f() { int i = 3; }",
676 stmt(hasDescendant(builtinType()))));
677
678 EXPECT_TRUE(matches("void f() { int i = 3; }",
679 stmt(hasDescendant(loc(builtinType())))));
680 EXPECT_TRUE(matches("void f() { int i = 3; }",
681 stmt(hasDescendant(qualType(builtinType())))));
682
683 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
684 stmt(hasDescendant(isInteger()))));
685
686 EXPECT_TRUE(matchAndVerifyResultTrue(
687 "void f() { int a; float c; int d; int e; }",
688 functionDecl(forEachDescendant(
689 varDecl(hasDescendant(isInteger())).bind("x"))),
690 new VerifyIdIsBoundTo<Decl>("x", 3)));
691}
692
693TEST(HasDescendant, MatchesDescendantsOfTypes) {
694 EXPECT_TRUE(matches("void f() { int*** i; }",
695 qualType(hasDescendant(builtinType()))));
696 EXPECT_TRUE(matches("void f() { int*** i; }",
697 qualType(hasDescendant(
698 pointerType(pointee(builtinType()))))));
699 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000700 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000701
702 EXPECT_TRUE(matchAndVerifyResultTrue(
703 "void f() { int*** i; }",
704 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
705 new VerifyIdIsBoundTo<Type>("x", 2)));
706}
707
708TEST(Has, MatchesChildrenOfTypes) {
709 EXPECT_TRUE(matches("int i;",
710 varDecl(hasName("i"), has(isInteger()))));
711 EXPECT_TRUE(notMatches("int** i;",
712 varDecl(hasName("i"), has(isInteger()))));
713 EXPECT_TRUE(matchAndVerifyResultTrue(
714 "int (*f)(float, int);",
715 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
716 new VerifyIdIsBoundTo<QualType>("x", 2)));
717}
718
719TEST(Has, MatchesChildTypes) {
720 EXPECT_TRUE(matches(
721 "int* i;",
722 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
723 EXPECT_TRUE(notMatches(
724 "int* i;",
725 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
726}
727
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000728TEST(Enum, DoesNotMatchClasses) {
729 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
730}
731
732TEST(Enum, MatchesEnums) {
733 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
734}
735
736TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000737 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000738 EXPECT_TRUE(matches("enum X{ A };", Matcher));
739 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
740 EXPECT_TRUE(notMatches("enum X {};", Matcher));
741}
742
Manuel Klimek4da21662012-07-06 05:48:52 +0000743TEST(StatementMatcher, Has) {
744 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000745 expr(hasType(pointsTo(recordDecl(hasName("X")))),
746 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000747
748 EXPECT_TRUE(matches(
749 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
750 EXPECT_TRUE(notMatches(
751 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
752}
753
754TEST(StatementMatcher, HasDescendant) {
755 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000756 expr(hasType(pointsTo(recordDecl(hasName("X")))),
757 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000758
759 EXPECT_TRUE(matches(
760 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
761 HasDescendantVariableI));
762 EXPECT_TRUE(notMatches(
763 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
764 HasDescendantVariableI));
765}
766
767TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000768 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000769
770 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
771 EXPECT_TRUE(notMatches("class A {};", TypeA));
772
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000773 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000774
775 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
776 TypeDerivedFromA));
777 EXPECT_TRUE(notMatches("class A {};", TypeA));
778
779 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000780 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000781
782 EXPECT_TRUE(
783 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
784}
785
Manuel Klimek4da21662012-07-06 05:48:52 +0000786TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000787 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000788
789 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000790 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000791
792 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000793 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000794
795 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000796 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000797
798 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
799 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000800 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000801
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000802 StatementMatcher MethodX =
803 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000804
805 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
806 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000807 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000808}
809
810TEST(Matcher, BindTheSameNameInAlternatives) {
811 StatementMatcher matcher = anyOf(
812 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000813 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000814 hasRHS(integerLiteral(equals(0)))),
815 binaryOperator(hasOperatorName("+"),
816 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000817 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000818
819 EXPECT_TRUE(matchAndVerifyResultTrue(
820 // The first branch of the matcher binds x to 0 but then fails.
821 // The second branch binds x to f() and succeeds.
822 "int f() { return 0 + f(); }",
823 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000824 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000825}
826
Manuel Klimek66341c52012-08-30 19:41:06 +0000827TEST(Matcher, BindsIDForMemoizedResults) {
828 // Using the same matcher in two match expressions will make memoization
829 // kick in.
830 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
831 EXPECT_TRUE(matchAndVerifyResultTrue(
832 "class A { class B { class X {}; }; };",
833 DeclarationMatcher(anyOf(
834 recordDecl(hasName("A"), hasDescendant(ClassX)),
835 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000836 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000837}
838
Daniel Jasper189f2e42012-12-03 15:43:25 +0000839TEST(HasDeclaration, HasDeclarationOfEnumType) {
840 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
841 expr(hasType(pointsTo(
842 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
843}
844
Edwin Vaneb45083d2013-02-25 14:32:42 +0000845TEST(HasDeclaration, HasGetDeclTraitTest) {
846 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
847 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
848 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
849}
850
Edwin Vane52380602013-02-19 17:14:34 +0000851TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
852 EXPECT_TRUE(matches("typedef int X; X a;",
853 varDecl(hasName("a"),
854 hasType(typedefType(hasDeclaration(decl()))))));
855
856 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
857}
858
Edwin Vane3abf7782013-02-25 14:49:29 +0000859TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
860 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
861 varDecl(hasType(templateSpecializationType(
862 hasDeclaration(namedDecl(hasName("A"))))))));
863}
864
Manuel Klimek4da21662012-07-06 05:48:52 +0000865TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000866 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000867 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000868 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000869 EXPECT_TRUE(
870 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000871 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000872 EXPECT_TRUE(
873 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000874 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000875}
876
877TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000878 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000879 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000880 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000881 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000882 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000883 EXPECT_TRUE(
884 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000885 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000886}
887
888TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000889 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000890 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000891 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000892 EXPECT_TRUE(
893 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000894 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000895}
896
897TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000898 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000899 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000900 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000901 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000902 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000903}
904
905TEST(Matcher, Call) {
906 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000907 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000908 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000909
910 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
911 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
912
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000913 StatementMatcher MethodOnY =
914 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000915
916 EXPECT_TRUE(
917 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
918 MethodOnY));
919 EXPECT_TRUE(
920 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
921 MethodOnY));
922 EXPECT_TRUE(
923 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
924 MethodOnY));
925 EXPECT_TRUE(
926 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
927 MethodOnY));
928 EXPECT_TRUE(
929 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
930 MethodOnY));
931
932 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000933 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000934
935 EXPECT_TRUE(
936 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
937 MethodOnYPointer));
938 EXPECT_TRUE(
939 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
940 MethodOnYPointer));
941 EXPECT_TRUE(
942 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
943 MethodOnYPointer));
944 EXPECT_TRUE(
945 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
946 MethodOnYPointer));
947 EXPECT_TRUE(
948 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
949 MethodOnYPointer));
950}
951
Daniel Jasper31f7c082012-10-01 13:40:41 +0000952TEST(Matcher, Lambda) {
953 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
954 lambdaExpr()));
955}
956
957TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000958 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
959 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000960 forRangeStmt()));
961 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
962 forRangeStmt()));
963}
964
965TEST(Matcher, UserDefinedLiteral) {
966 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
967 " return i + 1;"
968 "}"
969 "char c = 'a'_inc;",
970 userDefinedLiteral()));
971}
972
Daniel Jasperb54b7642012-09-20 14:12:57 +0000973TEST(Matcher, FlowControl) {
974 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
975 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
976 continueStmt()));
977 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
978 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
979 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
980}
981
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000982TEST(HasType, MatchesAsString) {
983 EXPECT_TRUE(
984 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000985 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000986 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000987 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000988 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000989 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000990 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000991 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000992}
993
Manuel Klimek4da21662012-07-06 05:48:52 +0000994TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000995 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +0000996 // Unary operator
997 EXPECT_TRUE(matches("class Y { }; "
998 "bool operator!(Y x) { return false; }; "
999 "Y y; bool c = !y;", OpCall));
1000 // No match -- special operators like "new", "delete"
1001 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1002 // we need to figure out include paths in the test.
1003 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1004 // "class Y { }; "
1005 // "void *operator new(size_t size) { return 0; } "
1006 // "Y *y = new Y;", OpCall));
1007 EXPECT_TRUE(notMatches("class Y { }; "
1008 "void operator delete(void *p) { } "
1009 "void a() {Y *y = new Y; delete y;}", OpCall));
1010 // Binary operator
1011 EXPECT_TRUE(matches("class Y { }; "
1012 "bool operator&&(Y x, Y y) { return true; }; "
1013 "Y a; Y b; bool c = a && b;",
1014 OpCall));
1015 // No match -- normal operator, not an overloaded one.
1016 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1017 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1018}
1019
1020TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1021 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001022 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001023 EXPECT_TRUE(matches("class Y { }; "
1024 "bool operator&&(Y x, Y y) { return true; }; "
1025 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1026 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001027 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001028 EXPECT_TRUE(notMatches("class Y { }; "
1029 "bool operator&&(Y x, Y y) { return true; }; "
1030 "Y a; Y b; bool c = a && b;",
1031 OpCallLessLess));
Edwin Vane6a19a972013-03-06 17:02:57 +00001032 DeclarationMatcher ClassWithOpStar =
1033 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1034 EXPECT_TRUE(matches("class Y { int operator*(); };",
1035 ClassWithOpStar));
1036 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1037 ClassWithOpStar)) ;
Manuel Klimek4da21662012-07-06 05:48:52 +00001038}
1039
Daniel Jasper278057f2012-11-15 03:29:05 +00001040TEST(Matcher, NestedOverloadedOperatorCalls) {
1041 EXPECT_TRUE(matchAndVerifyResultTrue(
1042 "class Y { }; "
1043 "Y& operator&&(Y& x, Y& y) { return x; }; "
1044 "Y a; Y b; Y c; Y d = a && b && c;",
1045 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1046 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1047 EXPECT_TRUE(matches(
1048 "class Y { }; "
1049 "Y& operator&&(Y& x, Y& y) { return x; }; "
1050 "Y a; Y b; Y c; Y d = a && b && c;",
1051 operatorCallExpr(hasParent(operatorCallExpr()))));
1052 EXPECT_TRUE(matches(
1053 "class Y { }; "
1054 "Y& operator&&(Y& x, Y& y) { return x; }; "
1055 "Y a; Y b; Y c; Y d = a && b && c;",
1056 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1057}
1058
Manuel Klimek4da21662012-07-06 05:48:52 +00001059TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001060 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001061 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001062
1063 EXPECT_TRUE(
1064 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1065 MethodOnY));
1066 EXPECT_TRUE(
1067 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1068 MethodOnY));
1069 EXPECT_TRUE(
1070 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1071 MethodOnY));
1072 EXPECT_TRUE(
1073 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1074 MethodOnY));
1075 EXPECT_TRUE(
1076 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1077 MethodOnY));
1078
1079 EXPECT_TRUE(matches(
1080 "class Y {"
1081 " public: virtual void x();"
1082 "};"
1083 "class X : public Y {"
1084 " public: virtual void x();"
1085 "};"
1086 "void z() { X *x; x->Y::x(); }", MethodOnY));
1087}
1088
1089TEST(Matcher, VariableUsage) {
1090 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001091 declRefExpr(to(
1092 varDecl(hasInitializer(
1093 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001094
1095 EXPECT_TRUE(matches(
1096 "class Y {"
1097 " public:"
1098 " bool x() const;"
1099 "};"
1100 "void z(const Y &y) {"
1101 " bool b = y.x();"
1102 " if (b) {}"
1103 "}", Reference));
1104
1105 EXPECT_TRUE(notMatches(
1106 "class Y {"
1107 " public:"
1108 " bool x() const;"
1109 "};"
1110 "void z(const Y &y) {"
1111 " bool b = y.x();"
1112 "}", Reference));
1113}
1114
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001115TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001116 EXPECT_TRUE(matches(
1117 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001118 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001119}
1120
Manuel Klimek4da21662012-07-06 05:48:52 +00001121TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001122 StatementMatcher CallOnVariableY =
1123 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001124
1125 EXPECT_TRUE(matches(
1126 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1127 EXPECT_TRUE(matches(
1128 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1129 EXPECT_TRUE(matches(
1130 "class Y { public: void x(); };"
1131 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1132 EXPECT_TRUE(matches(
1133 "class Y { public: void x(); };"
1134 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1135 EXPECT_TRUE(notMatches(
1136 "class Y { public: void x(); };"
1137 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1138 CallOnVariableY));
1139}
1140
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001141TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1142 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1143 unaryExprOrTypeTraitExpr()));
1144 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1145 alignOfExpr(anything())));
1146 // FIXME: Uncomment once alignof is enabled.
1147 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1148 // unaryExprOrTypeTraitExpr()));
1149 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1150 // sizeOfExpr()));
1151}
1152
1153TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1154 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1155 hasArgumentOfType(asString("int")))));
1156 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1157 hasArgumentOfType(asString("float")))));
1158 EXPECT_TRUE(matches(
1159 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001160 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001161 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001162 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001163}
1164
Manuel Klimek4da21662012-07-06 05:48:52 +00001165TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001166 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001167}
1168
1169TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001170 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001171}
1172
1173TEST(MemberExpression, MatchesVariable) {
1174 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001175 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001176 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001177 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001178 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001179 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001180}
1181
1182TEST(MemberExpression, MatchesStaticVariable) {
1183 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001184 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001185 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001186 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001187 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001188 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001189}
1190
Daniel Jasper6a124492012-07-12 08:50:38 +00001191TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001192 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1193 EXPECT_TRUE(matches(
1194 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1195 callExpr(hasArgument(0, declRefExpr(
1196 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001197}
1198
1199TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001200 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001201 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001202 callExpr(hasArgument(0, declRefExpr(
1203 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001204}
1205
Manuel Klimek4da21662012-07-06 05:48:52 +00001206TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1207 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001208 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001209 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001210 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001211 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001212 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001213}
1214
1215TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1216 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001217 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001218 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001219 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001220 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001221 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001222}
1223
1224TEST(IsArrow, MatchesMemberCallsViaArrow) {
1225 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001226 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001227 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001228 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001229 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001230 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001231}
1232
1233TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001234 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001235
1236 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1237 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1238}
1239
1240TEST(Callee, MatchesMemberExpressions) {
1241 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001242 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001243 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001244 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001245}
1246
1247TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001248 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001249
1250 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1251 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1252
Manuel Klimeke265c872012-07-10 14:21:30 +00001253#if !defined(_MSC_VER)
1254 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001255 // Dependent contexts, but a non-dependent call.
1256 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1257 CallFunctionF));
1258 EXPECT_TRUE(
1259 matches("void f(); template <int N> struct S { void g() { f(); } };",
1260 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001261#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001262
1263 // Depedent calls don't match.
1264 EXPECT_TRUE(
1265 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1266 CallFunctionF));
1267 EXPECT_TRUE(
1268 notMatches("void f(int);"
1269 "template <typename T> struct S { void g(T t) { f(t); } };",
1270 CallFunctionF));
1271}
1272
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001273TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1274 EXPECT_TRUE(
1275 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001276 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001277}
1278
1279TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1280 EXPECT_TRUE(
1281 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001282 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001283}
1284
1285TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1286 EXPECT_TRUE(
1287 notMatches("void g(); template <typename T> void f(T t) {}"
1288 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001289 functionTemplateDecl(hasName("f"),
1290 hasDescendant(declRefExpr(to(
1291 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001292}
1293
Manuel Klimek4da21662012-07-06 05:48:52 +00001294TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001295 StatementMatcher CallArgumentY = callExpr(
1296 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001297
1298 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1299 EXPECT_TRUE(
1300 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1301 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1302
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001303 StatementMatcher WrongIndex = callExpr(
1304 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001305 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1306}
1307
1308TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001309 StatementMatcher CallArgumentY = callExpr(
1310 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001311 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1312 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1313 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1314}
1315
1316TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001317 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001318
1319 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1320 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1321 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1322}
1323
Daniel Jasper36e29d62012-12-04 11:54:27 +00001324TEST(Matcher, ParameterCount) {
1325 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1326 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1327 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1328 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1329 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1330}
1331
Manuel Klimek4da21662012-07-06 05:48:52 +00001332TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001333 DeclarationMatcher ReferenceClassX = varDecl(
1334 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001335 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1336 ReferenceClassX));
1337 EXPECT_TRUE(
1338 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1339 EXPECT_TRUE(
1340 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1341 EXPECT_TRUE(
1342 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1343}
1344
Edwin Vane6a19a972013-03-06 17:02:57 +00001345TEST(QualType, hasCanonicalType) {
1346 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1347 "int a;"
1348 "int_ref b = a;",
1349 varDecl(hasType(qualType(referenceType())))));
1350 EXPECT_TRUE(
1351 matches("typedef int &int_ref;"
1352 "int a;"
1353 "int_ref b = a;",
1354 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1355}
1356
Manuel Klimek4da21662012-07-06 05:48:52 +00001357TEST(HasParameter, CallsInnerMatcher) {
1358 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001359 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001360 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001361 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001362}
1363
1364TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1365 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001366 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001367}
1368
1369TEST(HasType, MatchesParameterVariableTypesStrictly) {
1370 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001371 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001372 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001373 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001374 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001375 methodDecl(hasParameter(0,
1376 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001377 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001378 methodDecl(hasParameter(0,
1379 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001380}
1381
1382TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1383 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001384 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001385 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001386 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001387}
1388
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001389TEST(Returns, MatchesReturnTypes) {
1390 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001391 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001392 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001393 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001394 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001395 functionDecl(returns(hasDeclaration(
1396 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001397}
1398
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001399TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001400 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1401 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1402 functionDecl(isExternC())));
1403 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001404}
1405
Manuel Klimek4da21662012-07-06 05:48:52 +00001406TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1407 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001408 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001409}
1410
1411TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1412 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001413 methodDecl(hasAnyParameter(hasType(pointsTo(
1414 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001415}
1416
1417TEST(HasName, MatchesParameterVariableDeclartions) {
1418 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001419 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001420 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001421 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001422}
1423
Daniel Jasper371f9392012-08-01 08:40:24 +00001424TEST(Matcher, MatchesClassTemplateSpecialization) {
1425 EXPECT_TRUE(matches("template<typename T> struct A {};"
1426 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001427 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001428 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001429 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001430 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001431 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001432}
1433
1434TEST(Matcher, MatchesTypeTemplateArgument) {
1435 EXPECT_TRUE(matches(
1436 "template<typename T> struct B {};"
1437 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001438 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001439 asString("int"))))));
1440}
1441
1442TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1443 EXPECT_TRUE(matches(
1444 "struct B { int next; };"
1445 "template<int(B::*next_ptr)> struct A {};"
1446 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001447 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1448 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001449
1450 EXPECT_TRUE(notMatches(
1451 "template <typename T> struct A {};"
1452 "A<int> a;",
1453 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1454 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001455}
1456
1457TEST(Matcher, MatchesSpecificArgument) {
1458 EXPECT_TRUE(matches(
1459 "template<typename T, typename U> class A {};"
1460 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001461 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001462 1, refersToType(asString("int"))))));
1463 EXPECT_TRUE(notMatches(
1464 "template<typename T, typename U> class A {};"
1465 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001466 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001467 1, refersToType(asString("int"))))));
1468}
1469
Daniel Jasperf3197e92013-02-25 12:02:08 +00001470TEST(Matcher, MatchesAccessSpecDecls) {
1471 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1472 EXPECT_TRUE(
1473 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1474 EXPECT_TRUE(
1475 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1476 EXPECT_TRUE(
1477 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1478
1479 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1480}
1481
Manuel Klimek4da21662012-07-06 05:48:52 +00001482TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001483 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001484
1485 EXPECT_TRUE(
1486 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1487 EXPECT_TRUE(
1488 matches("class X { public: X(); }; void x() { X x = X(); }",
1489 Constructor));
1490 EXPECT_TRUE(
1491 matches("class X { public: X(int); }; void x() { X x = 0; }",
1492 Constructor));
1493 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1494}
1495
1496TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001497 StatementMatcher Constructor = constructExpr(
1498 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001499
1500 EXPECT_TRUE(
1501 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1502 Constructor));
1503 EXPECT_TRUE(
1504 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1505 Constructor));
1506 EXPECT_TRUE(
1507 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1508 Constructor));
1509 EXPECT_TRUE(
1510 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1511 Constructor));
1512
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001513 StatementMatcher WrongIndex = constructExpr(
1514 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001515 EXPECT_TRUE(
1516 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1517 WrongIndex));
1518}
1519
1520TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001521 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001522
1523 EXPECT_TRUE(
1524 matches("class X { public: X(int); }; void x() { X x(0); }",
1525 Constructor1Arg));
1526 EXPECT_TRUE(
1527 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1528 Constructor1Arg));
1529 EXPECT_TRUE(
1530 matches("class X { public: X(int); }; void x() { X x = 0; }",
1531 Constructor1Arg));
1532 EXPECT_TRUE(
1533 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1534 Constructor1Arg));
1535}
1536
Manuel Klimek70b9db92012-10-23 10:40:50 +00001537TEST(Matcher,ThisExpr) {
1538 EXPECT_TRUE(
1539 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1540 EXPECT_TRUE(
1541 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1542}
1543
Manuel Klimek4da21662012-07-06 05:48:52 +00001544TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001545 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001546
1547 std::string ClassString = "class string { public: string(); ~string(); }; ";
1548
1549 EXPECT_TRUE(
1550 matches(ClassString +
1551 "string GetStringByValue();"
1552 "void FunctionTakesString(string s);"
1553 "void run() { FunctionTakesString(GetStringByValue()); }",
1554 TempExpression));
1555
1556 EXPECT_TRUE(
1557 notMatches(ClassString +
1558 "string* GetStringPointer(); "
1559 "void FunctionTakesStringPtr(string* s);"
1560 "void run() {"
1561 " string* s = GetStringPointer();"
1562 " FunctionTakesStringPtr(GetStringPointer());"
1563 " FunctionTakesStringPtr(s);"
1564 "}",
1565 TempExpression));
1566
1567 EXPECT_TRUE(
1568 notMatches("class no_dtor {};"
1569 "no_dtor GetObjByValue();"
1570 "void ConsumeObj(no_dtor param);"
1571 "void run() { ConsumeObj(GetObjByValue()); }",
1572 TempExpression));
1573}
1574
Sam Panzere16acd32012-08-24 22:04:44 +00001575TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1576 std::string ClassString =
1577 "class string { public: string(); int length(); }; ";
1578
1579 EXPECT_TRUE(
1580 matches(ClassString +
1581 "string GetStringByValue();"
1582 "void FunctionTakesString(string s);"
1583 "void run() { FunctionTakesString(GetStringByValue()); }",
1584 materializeTemporaryExpr()));
1585
1586 EXPECT_TRUE(
1587 notMatches(ClassString +
1588 "string* GetStringPointer(); "
1589 "void FunctionTakesStringPtr(string* s);"
1590 "void run() {"
1591 " string* s = GetStringPointer();"
1592 " FunctionTakesStringPtr(GetStringPointer());"
1593 " FunctionTakesStringPtr(s);"
1594 "}",
1595 materializeTemporaryExpr()));
1596
1597 EXPECT_TRUE(
1598 notMatches(ClassString +
1599 "string GetStringByValue();"
1600 "void run() { int k = GetStringByValue().length(); }",
1601 materializeTemporaryExpr()));
1602
1603 EXPECT_TRUE(
1604 notMatches(ClassString +
1605 "string GetStringByValue();"
1606 "void run() { GetStringByValue(); }",
1607 materializeTemporaryExpr()));
1608}
1609
Manuel Klimek4da21662012-07-06 05:48:52 +00001610TEST(ConstructorDeclaration, SimpleCase) {
1611 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001612 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001613 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001614 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001615}
1616
1617TEST(ConstructorDeclaration, IsImplicit) {
1618 // This one doesn't match because the constructor is not added by the
1619 // compiler (it is not needed).
1620 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001621 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001622 // The compiler added the implicit default constructor.
1623 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001624 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001625 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001626 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001627}
1628
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001629TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1630 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001631 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001632}
1633
1634TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001635 EXPECT_TRUE(notMatches("class Foo {};",
1636 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001637}
1638
Manuel Klimek4da21662012-07-06 05:48:52 +00001639TEST(HasAnyConstructorInitializer, SimpleCase) {
1640 EXPECT_TRUE(notMatches(
1641 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001642 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001643 EXPECT_TRUE(matches(
1644 "class Foo {"
1645 " Foo() : foo_() { }"
1646 " int foo_;"
1647 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001648 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001649}
1650
1651TEST(HasAnyConstructorInitializer, ForField) {
1652 static const char Code[] =
1653 "class Baz { };"
1654 "class Foo {"
1655 " Foo() : foo_() { }"
1656 " Baz foo_;"
1657 " Baz bar_;"
1658 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001659 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1660 forField(hasType(recordDecl(hasName("Baz"))))))));
1661 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001662 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001663 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1664 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001665}
1666
1667TEST(HasAnyConstructorInitializer, WithInitializer) {
1668 static const char Code[] =
1669 "class Foo {"
1670 " Foo() : foo_(0) { }"
1671 " int foo_;"
1672 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001673 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001674 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001675 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001676 withInitializer(integerLiteral(equals(1)))))));
1677}
1678
1679TEST(HasAnyConstructorInitializer, IsWritten) {
1680 static const char Code[] =
1681 "struct Bar { Bar(){} };"
1682 "class Foo {"
1683 " Foo() : foo_() { }"
1684 " Bar foo_;"
1685 " Bar bar_;"
1686 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001687 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001688 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001689 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001690 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001691 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001692 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1693}
1694
1695TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001696 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001697
1698 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1699 EXPECT_TRUE(
1700 matches("class X { public: X(); }; void x() { new X(); }", New));
1701 EXPECT_TRUE(
1702 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1703 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1704}
1705
1706TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001707 StatementMatcher New = constructExpr(
1708 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001709
1710 EXPECT_TRUE(
1711 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1712 New));
1713 EXPECT_TRUE(
1714 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1715 New));
1716 EXPECT_TRUE(
1717 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1718 New));
1719
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001720 StatementMatcher WrongIndex = constructExpr(
1721 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001722 EXPECT_TRUE(
1723 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1724 WrongIndex));
1725}
1726
1727TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001728 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001729
1730 EXPECT_TRUE(
1731 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1732 EXPECT_TRUE(
1733 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1734 New));
1735}
1736
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001737TEST(Matcher, DeleteExpression) {
1738 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001739 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001740}
1741
Manuel Klimek4da21662012-07-06 05:48:52 +00001742TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001743 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001744
1745 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1746 EXPECT_TRUE(
1747 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1748 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1749}
1750
1751TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001752 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001753 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1754 // wide string
1755 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1756 // with escaped characters
1757 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1758 // no matching -- though the data type is the same, there is no string literal
1759 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1760}
1761
1762TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001763 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001764 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1765 // wide character
1766 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1767 // wide character, Hex encoded, NOT MATCHED!
1768 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1769 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1770}
1771
1772TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001773 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001774 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1775 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1776 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1777 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1778
1779 // Non-matching cases (character literals, float and double)
1780 EXPECT_TRUE(notMatches("int i = L'a';",
1781 HasIntLiteral)); // this is actually a character
1782 // literal cast to int
1783 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1784 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1785 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1786}
1787
Daniel Jasper31f7c082012-10-01 13:40:41 +00001788TEST(Matcher, NullPtrLiteral) {
1789 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1790}
1791
Daniel Jasperb54b7642012-09-20 14:12:57 +00001792TEST(Matcher, AsmStatement) {
1793 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1794}
1795
Manuel Klimek4da21662012-07-06 05:48:52 +00001796TEST(Matcher, Conditions) {
1797 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1798
1799 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1800 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1801 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1802 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1803 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1804}
1805
1806TEST(MatchBinaryOperator, HasOperatorName) {
1807 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1808
1809 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1810 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1811}
1812
1813TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1814 StatementMatcher OperatorTrueFalse =
1815 binaryOperator(hasLHS(boolLiteral(equals(true))),
1816 hasRHS(boolLiteral(equals(false))));
1817
1818 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1819 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1820 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1821}
1822
1823TEST(MatchBinaryOperator, HasEitherOperand) {
1824 StatementMatcher HasOperand =
1825 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1826
1827 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1828 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1829 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1830}
1831
1832TEST(Matcher, BinaryOperatorTypes) {
1833 // Integration test that verifies the AST provides all binary operators in
1834 // a way we expect.
1835 // FIXME: Operator ','
1836 EXPECT_TRUE(
1837 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1838 EXPECT_TRUE(
1839 matches("bool b; bool c = (b = true);",
1840 binaryOperator(hasOperatorName("="))));
1841 EXPECT_TRUE(
1842 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1843 EXPECT_TRUE(
1844 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1845 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1846 EXPECT_TRUE(
1847 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1848 EXPECT_TRUE(
1849 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1850 EXPECT_TRUE(
1851 matches("int i = 1; int j = (i <<= 2);",
1852 binaryOperator(hasOperatorName("<<="))));
1853 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1854 EXPECT_TRUE(
1855 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1856 EXPECT_TRUE(
1857 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1858 EXPECT_TRUE(
1859 matches("int i = 1; int j = (i >>= 2);",
1860 binaryOperator(hasOperatorName(">>="))));
1861 EXPECT_TRUE(
1862 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1863 EXPECT_TRUE(
1864 matches("int i = 42; int j = (i ^= 42);",
1865 binaryOperator(hasOperatorName("^="))));
1866 EXPECT_TRUE(
1867 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1868 EXPECT_TRUE(
1869 matches("int i = 42; int j = (i %= 42);",
1870 binaryOperator(hasOperatorName("%="))));
1871 EXPECT_TRUE(
1872 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1873 EXPECT_TRUE(
1874 matches("bool b = true && false;",
1875 binaryOperator(hasOperatorName("&&"))));
1876 EXPECT_TRUE(
1877 matches("bool b = true; bool c = (b &= false);",
1878 binaryOperator(hasOperatorName("&="))));
1879 EXPECT_TRUE(
1880 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1881 EXPECT_TRUE(
1882 matches("bool b = true || false;",
1883 binaryOperator(hasOperatorName("||"))));
1884 EXPECT_TRUE(
1885 matches("bool b = true; bool c = (b |= false);",
1886 binaryOperator(hasOperatorName("|="))));
1887 EXPECT_TRUE(
1888 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1889 EXPECT_TRUE(
1890 matches("int i = 42; int j = (i *= 23);",
1891 binaryOperator(hasOperatorName("*="))));
1892 EXPECT_TRUE(
1893 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1894 EXPECT_TRUE(
1895 matches("int i = 42; int j = (i /= 23);",
1896 binaryOperator(hasOperatorName("/="))));
1897 EXPECT_TRUE(
1898 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1899 EXPECT_TRUE(
1900 matches("int i = 42; int j = (i += 23);",
1901 binaryOperator(hasOperatorName("+="))));
1902 EXPECT_TRUE(
1903 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1904 EXPECT_TRUE(
1905 matches("int i = 42; int j = (i -= 23);",
1906 binaryOperator(hasOperatorName("-="))));
1907 EXPECT_TRUE(
1908 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1909 binaryOperator(hasOperatorName("->*"))));
1910 EXPECT_TRUE(
1911 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1912 binaryOperator(hasOperatorName(".*"))));
1913
1914 // Member expressions as operators are not supported in matches.
1915 EXPECT_TRUE(
1916 notMatches("struct A { void x(A *a) { a->x(this); } };",
1917 binaryOperator(hasOperatorName("->"))));
1918
1919 // Initializer assignments are not represented as operator equals.
1920 EXPECT_TRUE(
1921 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
1922
1923 // Array indexing is not represented as operator.
1924 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
1925
1926 // Overloaded operators do not match at all.
1927 EXPECT_TRUE(notMatches(
1928 "struct A { bool operator&&(const A &a) const { return false; } };"
1929 "void x() { A a, b; a && b; }",
1930 binaryOperator()));
1931}
1932
1933TEST(MatchUnaryOperator, HasOperatorName) {
1934 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
1935
1936 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
1937 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
1938}
1939
1940TEST(MatchUnaryOperator, HasUnaryOperand) {
1941 StatementMatcher OperatorOnFalse =
1942 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
1943
1944 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
1945 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
1946}
1947
1948TEST(Matcher, UnaryOperatorTypes) {
1949 // Integration test that verifies the AST provides all unary operators in
1950 // a way we expect.
1951 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
1952 EXPECT_TRUE(
1953 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
1954 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
1955 EXPECT_TRUE(
1956 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
1957 EXPECT_TRUE(
1958 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
1959 EXPECT_TRUE(
1960 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
1961 EXPECT_TRUE(
1962 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
1963 EXPECT_TRUE(
1964 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
1965 EXPECT_TRUE(
1966 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
1967 EXPECT_TRUE(
1968 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
1969
1970 // We don't match conversion operators.
1971 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
1972
1973 // Function calls are not represented as operator.
1974 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
1975
1976 // Overloaded operators do not match at all.
1977 // FIXME: We probably want to add that.
1978 EXPECT_TRUE(notMatches(
1979 "struct A { bool operator!() const { return false; } };"
1980 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
1981}
1982
1983TEST(Matcher, ConditionalOperator) {
1984 StatementMatcher Conditional = conditionalOperator(
1985 hasCondition(boolLiteral(equals(true))),
1986 hasTrueExpression(boolLiteral(equals(false))));
1987
1988 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
1989 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
1990 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
1991
1992 StatementMatcher ConditionalFalse = conditionalOperator(
1993 hasFalseExpression(boolLiteral(equals(false))));
1994
1995 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
1996 EXPECT_TRUE(
1997 notMatches("void x() { true ? false : true; }", ConditionalFalse));
1998}
1999
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002000TEST(ArraySubscriptMatchers, ArraySubscripts) {
2001 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2002 arraySubscriptExpr()));
2003 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2004 arraySubscriptExpr()));
2005}
2006
2007TEST(ArraySubscriptMatchers, ArrayIndex) {
2008 EXPECT_TRUE(matches(
2009 "int i[2]; void f() { i[1] = 1; }",
2010 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2011 EXPECT_TRUE(matches(
2012 "int i[2]; void f() { 1[i] = 1; }",
2013 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2014 EXPECT_TRUE(notMatches(
2015 "int i[2]; void f() { i[1] = 1; }",
2016 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2017}
2018
2019TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2020 EXPECT_TRUE(matches(
2021 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002022 arraySubscriptExpr(hasBase(implicitCastExpr(
2023 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002024}
2025
Manuel Klimek4da21662012-07-06 05:48:52 +00002026TEST(Matcher, HasNameSupportsNamespaces) {
2027 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002028 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002029 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002030 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002031 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002032 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002033 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002034 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002035 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002036 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002037 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002038 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002039 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002040 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002041 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002042 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002043 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002044 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002045 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002046 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002047 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002048 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002049 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002050 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002051}
2052
2053TEST(Matcher, HasNameSupportsOuterClasses) {
2054 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002055 matches("class A { class B { class C; }; };",
2056 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002057 EXPECT_TRUE(
2058 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002059 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002060 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002061 matches("class A { class B { class C; }; };",
2062 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002063 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002064 matches("class A { class B { class C; }; };",
2065 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002066 EXPECT_TRUE(
2067 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002068 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002069 EXPECT_TRUE(
2070 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002071 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002072 EXPECT_TRUE(
2073 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002074 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002075 EXPECT_TRUE(
2076 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002077 recordDecl(hasName("::C"))));
2078 EXPECT_TRUE(
2079 notMatches("class A { class B { class C; }; };",
2080 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002081 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002082 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002083 EXPECT_TRUE(
2084 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002085 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002086}
2087
2088TEST(Matcher, IsDefinition) {
2089 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002090 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002091 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2092 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2093
2094 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002095 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002096 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2097 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2098
2099 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002100 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002101 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2102 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2103}
2104
2105TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002106 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002107 ofClass(hasName("X")))));
2108
2109 EXPECT_TRUE(
2110 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2111 EXPECT_TRUE(
2112 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2113 Constructor));
2114 EXPECT_TRUE(
2115 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2116 Constructor));
2117}
2118
2119TEST(Matcher, VisitsTemplateInstantiations) {
2120 EXPECT_TRUE(matches(
2121 "class A { public: void x(); };"
2122 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002123 "void f() { B<A> b; b.y(); }",
2124 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002125
2126 EXPECT_TRUE(matches(
2127 "class A { public: void x(); };"
2128 "class C {"
2129 " public:"
2130 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2131 "};"
2132 "void f() {"
2133 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002134 "}",
2135 recordDecl(hasName("C"),
2136 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002137}
2138
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002139TEST(Matcher, HandlesNullQualTypes) {
2140 // FIXME: Add a Type matcher so we can replace uses of this
2141 // variable with Type(True())
2142 const TypeMatcher AnyType = anything();
2143
2144 // We don't really care whether this matcher succeeds; we're testing that
2145 // it completes without crashing.
2146 EXPECT_TRUE(matches(
2147 "struct A { };"
2148 "template <typename T>"
2149 "void f(T t) {"
2150 " T local_t(t /* this becomes a null QualType in the AST */);"
2151 "}"
2152 "void g() {"
2153 " f(0);"
2154 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002155 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002156 anyOf(
2157 TypeMatcher(hasDeclaration(anything())),
2158 pointsTo(AnyType),
2159 references(AnyType)
2160 // Other QualType matchers should go here.
2161 ))))));
2162}
2163
Manuel Klimek4da21662012-07-06 05:48:52 +00002164// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002165AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002166 // Make sure all special variables are used: node, match_finder,
2167 // bound_nodes_builder, and the parameter named 'AMatcher'.
2168 return AMatcher.matches(Node, Finder, Builder);
2169}
2170
2171TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002172 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002173
2174 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002175 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002176
2177 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002178 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002179
2180 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002181 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002182}
2183
2184AST_POLYMORPHIC_MATCHER_P(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002185 polymorphicHas, internal::Matcher<Decl>, AMatcher) {
2186 TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) ||
2187 (llvm::is_same<NodeType, Stmt>::value),
Manuel Klimek4da21662012-07-06 05:48:52 +00002188 assert_node_type_is_accessible);
Manuel Klimek4da21662012-07-06 05:48:52 +00002189 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002190 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002191 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2192 ASTMatchFinder::BK_First);
2193}
2194
2195TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002196 DeclarationMatcher HasClassB =
2197 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002198
2199 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002200 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002201
2202 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002203 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002204
2205 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002206 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002207
2208 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002209 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002210
2211 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2212}
2213
2214TEST(For, FindsForLoops) {
2215 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2216 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002217 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2218 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002219 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220}
2221
Daniel Jasper6a124492012-07-12 08:50:38 +00002222TEST(For, ForLoopInternals) {
2223 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2224 forStmt(hasCondition(anything()))));
2225 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2226 forStmt(hasLoopInit(anything()))));
2227}
2228
2229TEST(For, NegativeForLoopInternals) {
2230 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002231 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002232 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2233 forStmt(hasLoopInit(anything()))));
2234}
2235
Manuel Klimek4da21662012-07-06 05:48:52 +00002236TEST(For, ReportsNoFalsePositives) {
2237 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2238 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2239}
2240
2241TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002242 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2243 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2244 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002245}
2246
2247TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2248 // It's not a compound statement just because there's "{}" in the source
2249 // text. This is an AST search, not grep.
2250 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002251 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002252 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002253 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002254}
2255
Daniel Jasper6a124492012-07-12 08:50:38 +00002256TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002257 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002258 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002259 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002260 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002261 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002262 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002263 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002264 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002265}
2266
2267TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2268 // The simplest case: every compound statement is in a function
2269 // definition, and the function body itself must be a compound
2270 // statement.
2271 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002272 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002273}
2274
2275TEST(HasAnySubstatement, IsNotRecursive) {
2276 // It's really "has any immediate substatement".
2277 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002278 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002279}
2280
2281TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2282 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002283 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002284}
2285
2286TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2287 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002288 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002289}
2290
2291TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2292 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002293 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002294 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002295 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002296}
2297
2298TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2299 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002300 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002301 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002302 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002303 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002304 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002305}
2306
2307TEST(StatementCountIs, WorksWithMultipleStatements) {
2308 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002309 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002310}
2311
2312TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2313 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002314 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002315 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002316 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002317 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002318 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002319 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002320 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002321}
2322
2323TEST(Member, WorksInSimplestCase) {
2324 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002325 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002326}
2327
2328TEST(Member, DoesNotMatchTheBaseExpression) {
2329 // Don't pick out the wrong part of the member expression, this should
2330 // be checking the member (name) only.
2331 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002332 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002333}
2334
2335TEST(Member, MatchesInMemberFunctionCall) {
2336 EXPECT_TRUE(matches("void f() {"
2337 " struct { void first() {}; } s;"
2338 " s.first();"
2339 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002340 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002341}
2342
Daniel Jasperc711af22012-10-23 15:46:39 +00002343TEST(Member, MatchesMember) {
2344 EXPECT_TRUE(matches(
2345 "struct A { int i; }; void f() { A a; a.i = 2; }",
2346 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2347 EXPECT_TRUE(notMatches(
2348 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2349 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2350}
2351
Daniel Jasperf3197e92013-02-25 12:02:08 +00002352TEST(Member, UnderstandsAccess) {
2353 EXPECT_TRUE(matches(
2354 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2355 EXPECT_TRUE(notMatches(
2356 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2357 EXPECT_TRUE(notMatches(
2358 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2359
2360 EXPECT_TRUE(notMatches(
2361 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2362 EXPECT_TRUE(notMatches(
2363 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2364 EXPECT_TRUE(matches(
2365 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2366
2367 EXPECT_TRUE(notMatches(
2368 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2369 EXPECT_TRUE(matches("class A { protected: int i; };",
2370 fieldDecl(isProtected(), hasName("i"))));
2371 EXPECT_TRUE(notMatches(
2372 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2373
2374 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2375 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2376 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2377 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2378}
2379
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002380TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002381 // Fails in C++11 mode
2382 EXPECT_TRUE(matchesConditionally(
2383 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2384 "class X { void *operator new(std::size_t); };",
2385 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002386
2387 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002388 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002389
Daniel Jasper31f7c082012-10-01 13:40:41 +00002390 // Fails in C++11 mode
2391 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002392 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2393 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002394 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002395}
2396
Manuel Klimek4da21662012-07-06 05:48:52 +00002397TEST(HasObjectExpression, DoesNotMatchMember) {
2398 EXPECT_TRUE(notMatches(
2399 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002400 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002401}
2402
2403TEST(HasObjectExpression, MatchesBaseOfVariable) {
2404 EXPECT_TRUE(matches(
2405 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002406 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002407 EXPECT_TRUE(matches(
2408 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002409 memberExpr(hasObjectExpression(
2410 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002411}
2412
2413TEST(HasObjectExpression,
2414 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2415 EXPECT_TRUE(matches(
2416 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002417 memberExpr(hasObjectExpression(
2418 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002419 EXPECT_TRUE(matches(
2420 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002421 memberExpr(hasObjectExpression(
2422 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002423}
2424
2425TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002426 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2427 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2428 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2429 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002430}
2431
2432TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002433 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002434}
2435
2436TEST(IsConstQualified, MatchesConstInt) {
2437 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002438 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002439}
2440
2441TEST(IsConstQualified, MatchesConstPointer) {
2442 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002443 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002444}
2445
2446TEST(IsConstQualified, MatchesThroughTypedef) {
2447 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002448 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002449 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002450 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002451}
2452
2453TEST(IsConstQualified, DoesNotMatchInappropriately) {
2454 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002455 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002456 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002457 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002458}
2459
Sam Panzer089e5b32012-08-16 16:58:10 +00002460TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002461 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2462 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2463 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2464 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002465}
2466TEST(CastExpression, MatchesImplicitCasts) {
2467 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002468 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002469 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002470 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002471}
2472
2473TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002474 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2475 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2476 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2477 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002478}
2479
Manuel Klimek4da21662012-07-06 05:48:52 +00002480TEST(ReinterpretCast, MatchesSimpleCase) {
2481 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002482 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002483}
2484
2485TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002486 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002487 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002488 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002489 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002490 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002491 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2492 "B b;"
2493 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002494 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002495}
2496
2497TEST(FunctionalCast, MatchesSimpleCase) {
2498 std::string foo_class = "class Foo { public: Foo(char*); };";
2499 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002500 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002501}
2502
2503TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2504 std::string FooClass = "class Foo { public: Foo(char*); };";
2505 EXPECT_TRUE(
2506 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002507 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002508 EXPECT_TRUE(
2509 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002510 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002511}
2512
2513TEST(DynamicCast, MatchesSimpleCase) {
2514 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2515 "B b;"
2516 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002517 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002518}
2519
2520TEST(StaticCast, MatchesSimpleCase) {
2521 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002522 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002523}
2524
2525TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002526 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002527 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002528 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002529 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002530 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002531 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2532 "B b;"
2533 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002534 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002535}
2536
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002537TEST(CStyleCast, MatchesSimpleCase) {
2538 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2539}
2540
2541TEST(CStyleCast, DoesNotMatchOtherCasts) {
2542 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2543 "char q, *r = const_cast<char*>(&q);"
2544 "void* s = reinterpret_cast<char*>(&s);"
2545 "struct B { virtual ~B() {} }; struct D : B {};"
2546 "B b;"
2547 "D* t = dynamic_cast<D*>(&b);",
2548 cStyleCastExpr()));
2549}
2550
Manuel Klimek4da21662012-07-06 05:48:52 +00002551TEST(HasDestinationType, MatchesSimpleCase) {
2552 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002553 staticCastExpr(hasDestinationType(
2554 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002555}
2556
Sam Panzer089e5b32012-08-16 16:58:10 +00002557TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2558 // This test creates an implicit const cast.
2559 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002560 implicitCastExpr(
2561 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002562 // This test creates an implicit array-to-pointer cast.
2563 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002564 implicitCastExpr(hasImplicitDestinationType(
2565 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002566}
2567
2568TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2569 // This test creates an implicit cast from int to char.
2570 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002571 implicitCastExpr(hasImplicitDestinationType(
2572 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002573 // This test creates an implicit array-to-pointer cast.
2574 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002575 implicitCastExpr(hasImplicitDestinationType(
2576 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002577}
2578
2579TEST(ImplicitCast, MatchesSimpleCase) {
2580 // This test creates an implicit const cast.
2581 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002582 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002583 // This test creates an implicit cast from int to char.
2584 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002585 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002586 // This test creates an implicit array-to-pointer cast.
2587 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002588 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002589}
2590
2591TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002592 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002593 // are present, and that it ignores explicit and paren casts.
2594
2595 // These two test cases have no casts.
2596 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002597 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002598 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002599 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002600
2601 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002602 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002603 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002604 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002605
2606 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002607 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002608}
2609
2610TEST(IgnoringImpCasts, MatchesImpCasts) {
2611 // This test checks that ignoringImpCasts matches when implicit casts are
2612 // present and its inner matcher alone does not match.
2613 // Note that this test creates an implicit const cast.
2614 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002615 varDecl(hasInitializer(ignoringImpCasts(
2616 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002617 // This test creates an implict cast from int to char.
2618 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002619 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002620 integerLiteral(equals(0)))))));
2621}
2622
2623TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2624 // These tests verify that ignoringImpCasts does not match if the inner
2625 // matcher does not match.
2626 // Note that the first test creates an implicit const cast.
2627 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002628 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002629 unless(anything()))))));
2630 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002631 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002632 unless(anything()))))));
2633
2634 // These tests verify that ignoringImplictCasts does not look through explicit
2635 // casts or parentheses.
2636 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002637 varDecl(hasInitializer(ignoringImpCasts(
2638 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002639 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002640 varDecl(hasInitializer(ignoringImpCasts(
2641 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002642 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002643 varDecl(hasInitializer(ignoringImpCasts(
2644 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002645 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002646 varDecl(hasInitializer(ignoringImpCasts(
2647 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002648}
2649
2650TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2651 // This test verifies that expressions that do not have implicit casts
2652 // still match the inner matcher.
2653 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002654 varDecl(hasInitializer(ignoringImpCasts(
2655 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002656}
2657
2658TEST(IgnoringParenCasts, MatchesParenCasts) {
2659 // This test checks that ignoringParenCasts matches when parentheses and/or
2660 // casts are present and its inner matcher alone does not match.
2661 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002662 varDecl(hasInitializer(ignoringParenCasts(
2663 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002664 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002665 varDecl(hasInitializer(ignoringParenCasts(
2666 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002667
2668 // This test creates an implict cast from int to char in addition to the
2669 // parentheses.
2670 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002671 varDecl(hasInitializer(ignoringParenCasts(
2672 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002673
2674 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002675 varDecl(hasInitializer(ignoringParenCasts(
2676 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002677 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002678 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002679 integerLiteral(equals(0)))))));
2680}
2681
2682TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2683 // This test verifies that expressions that do not have any casts still match.
2684 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002685 varDecl(hasInitializer(ignoringParenCasts(
2686 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002687}
2688
2689TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2690 // These tests verify that ignoringImpCasts does not match if the inner
2691 // matcher does not match.
2692 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002693 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002694 unless(anything()))))));
2695
2696 // This test creates an implicit cast from int to char in addition to the
2697 // parentheses.
2698 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002699 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002700 unless(anything()))))));
2701
2702 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002703 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002704 unless(anything()))))));
2705}
2706
2707TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2708 // This test checks that ignoringParenAndImpCasts matches when
2709 // parentheses and/or implicit casts are present and its inner matcher alone
2710 // does not match.
2711 // Note that this test creates an implicit const cast.
2712 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002713 varDecl(hasInitializer(ignoringParenImpCasts(
2714 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002715 // This test creates an implicit cast from int to char.
2716 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002717 varDecl(hasInitializer(ignoringParenImpCasts(
2718 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002719}
2720
2721TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2722 // This test verifies that expressions that do not have parentheses or
2723 // implicit casts still match.
2724 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002725 varDecl(hasInitializer(ignoringParenImpCasts(
2726 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002727 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002728 varDecl(hasInitializer(ignoringParenImpCasts(
2729 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002730}
2731
2732TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2733 // These tests verify that ignoringParenImpCasts does not match if
2734 // the inner matcher does not match.
2735 // This test creates an implicit cast.
2736 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002737 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002738 unless(anything()))))));
2739 // These tests verify that ignoringParenAndImplictCasts does not look
2740 // through explicit casts.
2741 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002742 varDecl(hasInitializer(ignoringParenImpCasts(
2743 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002744 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002745 varDecl(hasInitializer(ignoringParenImpCasts(
2746 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002747 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002748 varDecl(hasInitializer(ignoringParenImpCasts(
2749 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002750}
2751
Manuel Klimek715c9562012-07-25 10:02:02 +00002752TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002753 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2754 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002755 implicitCastExpr(
2756 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002757}
2758
Manuel Klimek715c9562012-07-25 10:02:02 +00002759TEST(HasSourceExpression, MatchesExplicitCasts) {
2760 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002761 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002762 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002763 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002764}
2765
Manuel Klimek4da21662012-07-06 05:48:52 +00002766TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002767 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002768}
2769
2770TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002771 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002772}
2773
2774TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002775 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002776}
2777
2778TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002779 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002780}
2781
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002782TEST(InitListExpression, MatchesInitListExpression) {
2783 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2784 initListExpr(hasType(asString("int [2]")))));
2785 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002786 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002787}
2788
2789TEST(UsingDeclaration, MatchesUsingDeclarations) {
2790 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2791 usingDecl()));
2792}
2793
2794TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2795 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2796 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2797}
2798
2799TEST(UsingDeclaration, MatchesSpecificTarget) {
2800 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2801 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002802 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002803 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2804 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002805 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002806}
2807
2808TEST(UsingDeclaration, ThroughUsingDeclaration) {
2809 EXPECT_TRUE(matches(
2810 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002811 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002812 EXPECT_TRUE(notMatches(
2813 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002814 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002815}
2816
Sam Panzer425f41b2012-08-16 17:20:59 +00002817TEST(SingleDecl, IsSingleDecl) {
2818 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002819 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002820 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2821 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2822 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2823 SingleDeclStmt));
2824}
2825
2826TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002827 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002828
2829 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002830 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002831 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002832 declStmt(containsDeclaration(0, MatchesInit),
2833 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002834 unsigned WrongIndex = 42;
2835 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002836 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002837 MatchesInit))));
2838}
2839
2840TEST(DeclCount, DeclCountIsCorrect) {
2841 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002842 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002843 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002844 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002845 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002846 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002847}
2848
Manuel Klimek4da21662012-07-06 05:48:52 +00002849TEST(While, MatchesWhileLoops) {
2850 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2851 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2852 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2853}
2854
2855TEST(Do, MatchesDoLoops) {
2856 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2857 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2858}
2859
2860TEST(Do, DoesNotMatchWhileLoops) {
2861 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2862}
2863
2864TEST(SwitchCase, MatchesCase) {
2865 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2866 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2867 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2868 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2869}
2870
Daniel Jasperb54b7642012-09-20 14:12:57 +00002871TEST(SwitchCase, MatchesSwitch) {
2872 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2873 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2874 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2875 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2876}
2877
2878TEST(ExceptionHandling, SimpleCases) {
2879 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2880 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2881 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2882 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2883 throwExpr()));
2884 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2885 throwExpr()));
2886}
2887
Manuel Klimek4da21662012-07-06 05:48:52 +00002888TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2889 EXPECT_TRUE(notMatches(
2890 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002891 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002892 EXPECT_TRUE(notMatches(
2893 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002894 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002895}
2896
2897TEST(HasConditionVariableStatement, MatchesConditionVariables) {
2898 EXPECT_TRUE(matches(
2899 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002900 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002901}
2902
2903TEST(ForEach, BindsOneNode) {
2904 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002905 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002906 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002907}
2908
2909TEST(ForEach, BindsMultipleNodes) {
2910 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002911 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002912 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002913}
2914
2915TEST(ForEach, BindsRecursiveCombinations) {
2916 EXPECT_TRUE(matchAndVerifyResultTrue(
2917 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002918 recordDecl(hasName("C"),
2919 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002920 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002921}
2922
2923TEST(ForEachDescendant, BindsOneNode) {
2924 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002925 recordDecl(hasName("C"),
2926 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002927 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002928}
2929
Daniel Jasper5f684e92012-11-16 18:39:22 +00002930TEST(ForEachDescendant, NestedForEachDescendant) {
2931 DeclarationMatcher m = recordDecl(
2932 isDefinition(), decl().bind("x"), hasName("C"));
2933 EXPECT_TRUE(matchAndVerifyResultTrue(
2934 "class A { class B { class C {}; }; };",
2935 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
2936 new VerifyIdIsBoundTo<Decl>("x", "C")));
2937
2938 // FIXME: This is not really a useful matcher, but the result is still
2939 // surprising (currently binds "A").
2940 //EXPECT_TRUE(matchAndVerifyResultTrue(
2941 // "class A { class B { class C {}; }; };",
2942 // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
2943 // new VerifyIdIsBoundTo<Decl>("x", "C")));
2944}
2945
Manuel Klimek4da21662012-07-06 05:48:52 +00002946TEST(ForEachDescendant, BindsMultipleNodes) {
2947 EXPECT_TRUE(matchAndVerifyResultTrue(
2948 "class C { class D { int x; int y; }; "
2949 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002950 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002951 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002952}
2953
2954TEST(ForEachDescendant, BindsRecursiveCombinations) {
2955 EXPECT_TRUE(matchAndVerifyResultTrue(
2956 "class C { class D { "
2957 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002958 recordDecl(hasName("C"), forEachDescendant(recordDecl(
2959 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00002960 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00002961}
2962
Daniel Jasper11c98772012-11-11 22:14:55 +00002963TEST(ForEachDescendant, BindsCorrectNodes) {
2964 EXPECT_TRUE(matchAndVerifyResultTrue(
2965 "class C { void f(); int i; };",
2966 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2967 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
2968 EXPECT_TRUE(matchAndVerifyResultTrue(
2969 "class C { void f() {} int i; };",
2970 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
2971 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
2972}
2973
Manuel Klimek152ea0e2013-02-04 10:59:20 +00002974TEST(FindAll, BindsNodeOnMatch) {
2975 EXPECT_TRUE(matchAndVerifyResultTrue(
2976 "class A {};",
2977 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
2978 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
2979}
2980
2981TEST(FindAll, BindsDescendantNodeOnMatch) {
2982 EXPECT_TRUE(matchAndVerifyResultTrue(
2983 "class A { int a; int b; };",
2984 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
2985 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
2986}
2987
2988TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
2989 EXPECT_TRUE(matchAndVerifyResultTrue(
2990 "class A { int a; int b; };",
2991 recordDecl(hasName("::A"),
2992 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
2993 fieldDecl().bind("v"))))),
2994 new VerifyIdIsBoundTo<Decl>("v", 3)));
2995
2996 EXPECT_TRUE(matchAndVerifyResultTrue(
2997 "class A { class B {}; class C {}; };",
2998 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
2999 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3000}
3001
Manuel Klimek73876732013-02-04 09:42:38 +00003002TEST(EachOf, TriggersForEachMatch) {
3003 EXPECT_TRUE(matchAndVerifyResultTrue(
3004 "class A { int a; int b; };",
3005 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3006 has(fieldDecl(hasName("b")).bind("v")))),
3007 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3008}
3009
3010TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3011 EXPECT_TRUE(matchAndVerifyResultTrue(
3012 "class A { int a; int c; };",
3013 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3014 has(fieldDecl(hasName("b")).bind("v")))),
3015 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3016 EXPECT_TRUE(matchAndVerifyResultTrue(
3017 "class A { int c; int b; };",
3018 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3019 has(fieldDecl(hasName("b")).bind("v")))),
3020 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3021 EXPECT_TRUE(notMatches(
3022 "class A { int c; int d; };",
3023 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3024 has(fieldDecl(hasName("b")).bind("v"))))));
3025}
Manuel Klimek4da21662012-07-06 05:48:52 +00003026
3027TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3028 // Make sure that we can both match the class by name (::X) and by the type
3029 // the template was instantiated with (via a field).
3030
3031 EXPECT_TRUE(matches(
3032 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003033 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003034
3035 EXPECT_TRUE(matches(
3036 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003037 recordDecl(isTemplateInstantiation(), hasDescendant(
3038 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003039}
3040
3041TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3042 EXPECT_TRUE(matches(
3043 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003044 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003045 isTemplateInstantiation())));
3046}
3047
3048TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3049 EXPECT_TRUE(matches(
3050 "template <typename T> class X { T t; }; class A {};"
3051 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003052 recordDecl(isTemplateInstantiation(), hasDescendant(
3053 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003054}
3055
3056TEST(IsTemplateInstantiation,
3057 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3058 EXPECT_TRUE(matches(
3059 "template <typename T> class X {};"
3060 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003061 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003062}
3063
3064TEST(IsTemplateInstantiation,
3065 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3066 EXPECT_TRUE(matches(
3067 "class A {};"
3068 "class X {"
3069 " template <typename U> class Y { U u; };"
3070 " Y<A> y;"
3071 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003072 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003073}
3074
3075TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3076 // FIXME: Figure out whether this makes sense. It doesn't affect the
3077 // normal use case as long as the uppermost instantiation always is marked
3078 // as template instantiation, but it might be confusing as a predicate.
3079 EXPECT_TRUE(matches(
3080 "class A {};"
3081 "template <typename T> class X {"
3082 " template <typename U> class Y { U u; };"
3083 " Y<T> y;"
3084 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003085 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003086}
3087
3088TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3089 EXPECT_TRUE(notMatches(
3090 "template <typename T> class X {}; class A {};"
3091 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003092 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003093}
3094
3095TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3096 EXPECT_TRUE(notMatches(
3097 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003098 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003099}
3100
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003101TEST(IsExplicitTemplateSpecialization,
3102 DoesNotMatchPrimaryTemplate) {
3103 EXPECT_TRUE(notMatches(
3104 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003105 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003106 EXPECT_TRUE(notMatches(
3107 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003108 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003109}
3110
3111TEST(IsExplicitTemplateSpecialization,
3112 DoesNotMatchExplicitTemplateInstantiations) {
3113 EXPECT_TRUE(notMatches(
3114 "template <typename T> class X {};"
3115 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003116 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003117 EXPECT_TRUE(notMatches(
3118 "template <typename T> void f(T t) {}"
3119 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003120 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003121}
3122
3123TEST(IsExplicitTemplateSpecialization,
3124 DoesNotMatchImplicitTemplateInstantiations) {
3125 EXPECT_TRUE(notMatches(
3126 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003127 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003128 EXPECT_TRUE(notMatches(
3129 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003130 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003131}
3132
3133TEST(IsExplicitTemplateSpecialization,
3134 MatchesExplicitTemplateSpecializations) {
3135 EXPECT_TRUE(matches(
3136 "template <typename T> class X {};"
3137 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003138 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003139 EXPECT_TRUE(matches(
3140 "template <typename T> void f(T t) {}"
3141 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003142 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003143}
3144
Manuel Klimek579b1202012-09-07 09:26:10 +00003145TEST(HasAncenstor, MatchesDeclarationAncestors) {
3146 EXPECT_TRUE(matches(
3147 "class A { class B { class C {}; }; };",
3148 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3149}
3150
3151TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3152 EXPECT_TRUE(notMatches(
3153 "class A { class B { class C {}; }; };",
3154 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3155}
3156
3157TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3158 EXPECT_TRUE(matches(
3159 "class A { class B { void f() { C c; } class C {}; }; };",
3160 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3161 hasAncestor(recordDecl(hasName("A"))))))));
3162}
3163
3164TEST(HasAncenstor, MatchesStatementAncestors) {
3165 EXPECT_TRUE(matches(
3166 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003167 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003168}
3169
3170TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3171 EXPECT_TRUE(matches(
3172 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003173 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003174}
3175
3176TEST(HasAncestor, BindsRecursiveCombinations) {
3177 EXPECT_TRUE(matchAndVerifyResultTrue(
3178 "class C { class D { class E { class F { int y; }; }; }; };",
3179 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003180 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003181}
3182
3183TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3184 EXPECT_TRUE(matchAndVerifyResultTrue(
3185 "class C { class D { class E { class F { int y; }; }; }; };",
3186 fieldDecl(hasAncestor(
3187 decl(
3188 hasDescendant(recordDecl(isDefinition(),
3189 hasAncestor(recordDecl())))
3190 ).bind("d")
3191 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003192 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003193}
3194
3195TEST(HasAncestor, MatchesInTemplateInstantiations) {
3196 EXPECT_TRUE(matches(
3197 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3198 "A<int>::B::C a;",
3199 fieldDecl(hasType(asString("int")),
3200 hasAncestor(recordDecl(hasName("A"))))));
3201}
3202
3203TEST(HasAncestor, MatchesInImplicitCode) {
3204 EXPECT_TRUE(matches(
3205 "struct X {}; struct A { A() {} X x; };",
3206 constructorDecl(
3207 hasAnyConstructorInitializer(withInitializer(expr(
3208 hasAncestor(recordDecl(hasName("A")))))))));
3209}
3210
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003211TEST(HasParent, MatchesOnlyParent) {
3212 EXPECT_TRUE(matches(
3213 "void f() { if (true) { int x = 42; } }",
3214 compoundStmt(hasParent(ifStmt()))));
3215 EXPECT_TRUE(notMatches(
3216 "void f() { for (;;) { int x = 42; } }",
3217 compoundStmt(hasParent(ifStmt()))));
3218 EXPECT_TRUE(notMatches(
3219 "void f() { if (true) for (;;) { int x = 42; } }",
3220 compoundStmt(hasParent(ifStmt()))));
3221}
3222
Manuel Klimek30ace372012-12-06 14:42:48 +00003223TEST(HasAncestor, MatchesAllAncestors) {
3224 EXPECT_TRUE(matches(
3225 "template <typename T> struct C { static void f() { 42; } };"
3226 "void t() { C<int>::f(); }",
3227 integerLiteral(
3228 equals(42),
3229 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3230 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3231}
3232
3233TEST(HasParent, MatchesAllParents) {
3234 EXPECT_TRUE(matches(
3235 "template <typename T> struct C { static void f() { 42; } };"
3236 "void t() { C<int>::f(); }",
3237 integerLiteral(
3238 equals(42),
3239 hasParent(compoundStmt(hasParent(functionDecl(
3240 hasParent(recordDecl(isTemplateInstantiation())))))))));
3241 EXPECT_TRUE(matches(
3242 "template <typename T> struct C { static void f() { 42; } };"
3243 "void t() { C<int>::f(); }",
3244 integerLiteral(
3245 equals(42),
3246 hasParent(compoundStmt(hasParent(functionDecl(
3247 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3248 EXPECT_TRUE(matches(
3249 "template <typename T> struct C { static void f() { 42; } };"
3250 "void t() { C<int>::f(); }",
3251 integerLiteral(equals(42),
3252 hasParent(compoundStmt(allOf(
3253 hasParent(functionDecl(
3254 hasParent(recordDecl(isTemplateInstantiation())))),
3255 hasParent(functionDecl(hasParent(recordDecl(
3256 unless(isTemplateInstantiation())))))))))));
3257}
3258
Daniel Jasperce620072012-10-17 08:52:59 +00003259TEST(TypeMatching, MatchesTypes) {
3260 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3261}
3262
3263TEST(TypeMatching, MatchesArrayTypes) {
3264 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3265 EXPECT_TRUE(matches("int a[42];", arrayType()));
3266 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3267
3268 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3269 arrayType(hasElementType(builtinType()))));
3270
3271 EXPECT_TRUE(matches(
3272 "int const a[] = { 2, 3 };",
3273 qualType(arrayType(hasElementType(builtinType())))));
3274 EXPECT_TRUE(matches(
3275 "int const a[] = { 2, 3 };",
3276 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3277 EXPECT_TRUE(matches(
3278 "typedef const int T; T x[] = { 1, 2 };",
3279 qualType(isConstQualified(), arrayType())));
3280
3281 EXPECT_TRUE(notMatches(
3282 "int a[] = { 2, 3 };",
3283 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3284 EXPECT_TRUE(notMatches(
3285 "int a[] = { 2, 3 };",
3286 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3287 EXPECT_TRUE(notMatches(
3288 "int const a[] = { 2, 3 };",
3289 qualType(arrayType(hasElementType(builtinType())),
3290 unless(isConstQualified()))));
3291
3292 EXPECT_TRUE(matches("int a[2];",
3293 constantArrayType(hasElementType(builtinType()))));
3294 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3295}
3296
3297TEST(TypeMatching, MatchesComplexTypes) {
3298 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3299 EXPECT_TRUE(matches(
3300 "_Complex float f;",
3301 complexType(hasElementType(builtinType()))));
3302 EXPECT_TRUE(notMatches(
3303 "_Complex float f;",
3304 complexType(hasElementType(isInteger()))));
3305}
3306
3307TEST(TypeMatching, MatchesConstantArrayTypes) {
3308 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3309 EXPECT_TRUE(notMatches(
3310 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3311 constantArrayType(hasElementType(builtinType()))));
3312
3313 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3314 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3315 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3316}
3317
3318TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3319 EXPECT_TRUE(matches(
3320 "template <typename T, int Size> class array { T data[Size]; };",
3321 dependentSizedArrayType()));
3322 EXPECT_TRUE(notMatches(
3323 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3324 dependentSizedArrayType()));
3325}
3326
3327TEST(TypeMatching, MatchesIncompleteArrayType) {
3328 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3329 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3330
3331 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3332 incompleteArrayType()));
3333}
3334
3335TEST(TypeMatching, MatchesVariableArrayType) {
3336 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3337 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3338
3339 EXPECT_TRUE(matches(
3340 "void f(int b) { int a[b]; }",
3341 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3342 varDecl(hasName("b")))))))));
3343}
3344
3345TEST(TypeMatching, MatchesAtomicTypes) {
3346 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3347
3348 EXPECT_TRUE(matches("_Atomic(int) i;",
3349 atomicType(hasValueType(isInteger()))));
3350 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3351 atomicType(hasValueType(isInteger()))));
3352}
3353
3354TEST(TypeMatching, MatchesAutoTypes) {
3355 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3356 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3357 autoType()));
3358
3359 EXPECT_TRUE(matches("auto a = 1;",
3360 autoType(hasDeducedType(isInteger()))));
3361 EXPECT_TRUE(notMatches("auto b = 2.0;",
3362 autoType(hasDeducedType(isInteger()))));
3363}
3364
Daniel Jaspera267cf62012-10-29 10:14:44 +00003365TEST(TypeMatching, MatchesFunctionTypes) {
3366 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3367 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3368}
3369
Daniel Jasperce620072012-10-17 08:52:59 +00003370TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003371 // FIXME: Reactive when these tests can be more specific (not matching
3372 // implicit code on certain platforms), likely when we have hasDescendant for
3373 // Types/TypeLocs.
3374 //EXPECT_TRUE(matchAndVerifyResultTrue(
3375 // "int* a;",
3376 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3377 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3378 //EXPECT_TRUE(matchAndVerifyResultTrue(
3379 // "int* a;",
3380 // pointerTypeLoc().bind("loc"),
3381 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003382 EXPECT_TRUE(matches(
3383 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003384 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003385 EXPECT_TRUE(matches(
3386 "int** a;",
3387 loc(pointerType(pointee(pointerType())))));
3388 EXPECT_TRUE(matches(
3389 "int* b; int* * const a = &b;",
3390 loc(qualType(isConstQualified(), pointerType()))));
3391
3392 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003393 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3394 hasType(blockPointerType()))));
3395 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3396 hasType(memberPointerType()))));
3397 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3398 hasType(pointerType()))));
3399 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3400 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003401
Daniel Jasper1802daf2012-10-17 13:35:36 +00003402 Fragment = "int *ptr;";
3403 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3404 hasType(blockPointerType()))));
3405 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3406 hasType(memberPointerType()))));
3407 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3408 hasType(pointerType()))));
3409 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3410 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003411
Daniel Jasper1802daf2012-10-17 13:35:36 +00003412 Fragment = "int a; int &ref = a;";
3413 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3414 hasType(blockPointerType()))));
3415 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3416 hasType(memberPointerType()))));
3417 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3418 hasType(pointerType()))));
3419 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3420 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003421}
3422
3423TEST(TypeMatching, PointeeTypes) {
3424 EXPECT_TRUE(matches("int b; int &a = b;",
3425 referenceType(pointee(builtinType()))));
3426 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3427
3428 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003429 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003430
3431 EXPECT_TRUE(matches(
3432 "int const *A;",
3433 pointerType(pointee(isConstQualified(), builtinType()))));
3434 EXPECT_TRUE(notMatches(
3435 "int *A;",
3436 pointerType(pointee(isConstQualified(), builtinType()))));
3437}
3438
3439TEST(TypeMatching, MatchesPointersToConstTypes) {
3440 EXPECT_TRUE(matches("int b; int * const a = &b;",
3441 loc(pointerType())));
3442 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003443 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003444 EXPECT_TRUE(matches(
3445 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003446 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003447 EXPECT_TRUE(matches(
3448 "int b; const int * a = &b;",
3449 pointerType(pointee(builtinType()))));
3450}
3451
3452TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003453 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3454 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003455}
3456
Edwin Vane3abf7782013-02-25 14:49:29 +00003457TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003458 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003459 templateSpecializationType()));
3460}
3461
Edwin Vane742d9e72013-02-25 20:43:32 +00003462TEST(TypeMatching, MatchesRecordType) {
3463 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003464 EXPECT_TRUE(matches("struct S{}; S s;",
3465 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3466 EXPECT_TRUE(notMatches("int i;",
3467 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003468}
3469
3470TEST(TypeMatching, MatchesElaboratedType) {
3471 EXPECT_TRUE(matches(
3472 "namespace N {"
3473 " namespace M {"
3474 " class D {};"
3475 " }"
3476 "}"
3477 "N::M::D d;", elaboratedType()));
3478 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3479 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3480}
3481
3482TEST(ElaboratedTypeNarrowing, hasQualifier) {
3483 EXPECT_TRUE(matches(
3484 "namespace N {"
3485 " namespace M {"
3486 " class D {};"
3487 " }"
3488 "}"
3489 "N::M::D d;",
3490 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3491 EXPECT_TRUE(notMatches(
3492 "namespace M {"
3493 " class D {};"
3494 "}"
3495 "M::D d;",
3496 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003497 EXPECT_TRUE(notMatches(
3498 "struct D {"
3499 "} d;",
3500 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003501}
3502
3503TEST(ElaboratedTypeNarrowing, namesType) {
3504 EXPECT_TRUE(matches(
3505 "namespace N {"
3506 " namespace M {"
3507 " class D {};"
3508 " }"
3509 "}"
3510 "N::M::D d;",
3511 elaboratedType(elaboratedType(namesType(recordType(
3512 hasDeclaration(namedDecl(hasName("D")))))))));
3513 EXPECT_TRUE(notMatches(
3514 "namespace M {"
3515 " class D {};"
3516 "}"
3517 "M::D d;",
3518 elaboratedType(elaboratedType(namesType(typedefType())))));
3519}
3520
Daniel Jaspera7564432012-09-13 13:11:25 +00003521TEST(NNS, MatchesNestedNameSpecifiers) {
3522 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3523 nestedNameSpecifier()));
3524 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3525 nestedNameSpecifier()));
3526 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3527 nestedNameSpecifier()));
3528
3529 EXPECT_TRUE(matches(
3530 "struct A { static void f() {} }; void g() { A::f(); }",
3531 nestedNameSpecifier()));
3532 EXPECT_TRUE(notMatches(
3533 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3534 nestedNameSpecifier()));
3535}
3536
Daniel Jasperb54b7642012-09-20 14:12:57 +00003537TEST(NullStatement, SimpleCases) {
3538 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3539 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3540}
3541
Daniel Jaspera7564432012-09-13 13:11:25 +00003542TEST(NNS, MatchesTypes) {
3543 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3544 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3545 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3546 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3547 Matcher));
3548 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3549}
3550
3551TEST(NNS, MatchesNamespaceDecls) {
3552 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3553 specifiesNamespace(hasName("ns")));
3554 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3555 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3556 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3557}
3558
3559TEST(NNS, BindsNestedNameSpecifiers) {
3560 EXPECT_TRUE(matchAndVerifyResultTrue(
3561 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3562 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3563 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3564}
3565
3566TEST(NNS, BindsNestedNameSpecifierLocs) {
3567 EXPECT_TRUE(matchAndVerifyResultTrue(
3568 "namespace ns { struct B {}; } ns::B b;",
3569 loc(nestedNameSpecifier()).bind("loc"),
3570 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3571}
3572
3573TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3574 EXPECT_TRUE(matches(
3575 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3576 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3577 EXPECT_TRUE(matches(
3578 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003579 nestedNameSpecifierLoc(hasPrefix(
3580 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003581}
3582
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003583TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3584 std::string Fragment =
3585 "namespace a { struct A { struct B { struct C {}; }; }; };"
3586 "void f() { a::A::B::C c; }";
3587 EXPECT_TRUE(matches(
3588 Fragment,
3589 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3590 hasDescendant(nestedNameSpecifier(
3591 specifiesNamespace(hasName("a")))))));
3592 EXPECT_TRUE(notMatches(
3593 Fragment,
3594 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3595 has(nestedNameSpecifier(
3596 specifiesNamespace(hasName("a")))))));
3597 EXPECT_TRUE(matches(
3598 Fragment,
3599 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3600 has(nestedNameSpecifier(
3601 specifiesNamespace(hasName("a")))))));
3602
3603 // Not really useful because a NestedNameSpecifier can af at most one child,
3604 // but to complete the interface.
3605 EXPECT_TRUE(matchAndVerifyResultTrue(
3606 Fragment,
3607 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3608 forEach(nestedNameSpecifier().bind("x"))),
3609 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3610}
3611
3612TEST(NNS, NestedNameSpecifiersAsDescendants) {
3613 std::string Fragment =
3614 "namespace a { struct A { struct B { struct C {}; }; }; };"
3615 "void f() { a::A::B::C c; }";
3616 EXPECT_TRUE(matches(
3617 Fragment,
3618 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3619 asString("struct a::A")))))));
3620 EXPECT_TRUE(matchAndVerifyResultTrue(
3621 Fragment,
3622 functionDecl(hasName("f"),
3623 forEachDescendant(nestedNameSpecifier().bind("x"))),
3624 // Nested names: a, a::A and a::A::B.
3625 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3626}
3627
3628TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3629 std::string Fragment =
3630 "namespace a { struct A { struct B { struct C {}; }; }; };"
3631 "void f() { a::A::B::C c; }";
3632 EXPECT_TRUE(matches(
3633 Fragment,
3634 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3635 hasDescendant(loc(nestedNameSpecifier(
3636 specifiesNamespace(hasName("a"))))))));
3637 EXPECT_TRUE(notMatches(
3638 Fragment,
3639 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3640 has(loc(nestedNameSpecifier(
3641 specifiesNamespace(hasName("a"))))))));
3642 EXPECT_TRUE(matches(
3643 Fragment,
3644 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3645 has(loc(nestedNameSpecifier(
3646 specifiesNamespace(hasName("a"))))))));
3647
3648 EXPECT_TRUE(matchAndVerifyResultTrue(
3649 Fragment,
3650 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3651 forEach(nestedNameSpecifierLoc().bind("x"))),
3652 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3653}
3654
3655TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3656 std::string Fragment =
3657 "namespace a { struct A { struct B { struct C {}; }; }; };"
3658 "void f() { a::A::B::C c; }";
3659 EXPECT_TRUE(matches(
3660 Fragment,
3661 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3662 asString("struct a::A"))))))));
3663 EXPECT_TRUE(matchAndVerifyResultTrue(
3664 Fragment,
3665 functionDecl(hasName("f"),
3666 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3667 // Nested names: a, a::A and a::A::B.
3668 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3669}
3670
Manuel Klimek60969f52013-02-01 13:41:35 +00003671template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003672public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003673 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3674 StringRef InnerId)
3675 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003676 }
3677
Manuel Klimek60969f52013-02-01 13:41:35 +00003678 virtual bool run(const BoundNodes *Nodes) { return false; }
3679
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003680 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3681 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003682 return selectFirst<const T>(InnerId,
3683 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003684 }
3685private:
3686 std::string Id;
3687 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003688 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003689};
3690
3691TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003692 EXPECT_TRUE(matchAndVerifyResultTrue(
3693 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3694 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003695 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
3696 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003697 EXPECT_TRUE(matchAndVerifyResultFalse(
3698 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3699 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003700 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
3701 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003702}
3703
3704TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003705 EXPECT_TRUE(matchAndVerifyResultTrue(
3706 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003707 new VerifyMatchOnNode<clang::Stmt>(
3708 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003709 EXPECT_TRUE(matchAndVerifyResultFalse(
3710 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003711 new VerifyMatchOnNode<clang::Stmt>(
3712 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003713}
3714
3715TEST(MatchFinder, CanMatchSingleNodesRecursively) {
3716 EXPECT_TRUE(matchAndVerifyResultTrue(
3717 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3718 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003719 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00003720 EXPECT_TRUE(matchAndVerifyResultFalse(
3721 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
3722 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003723 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003724}
3725
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00003726template <typename T>
3727class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
3728public:
3729 virtual bool run(const BoundNodes *Nodes) { return false; }
3730
3731 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3732 const T *Node = Nodes->getNodeAs<T>("");
3733 return verify(*Nodes, *Context, Node);
3734 }
3735
3736 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
3737 return selectFirst<const T>(
3738 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
3739 *Node, Context)) != NULL;
3740 }
3741 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
3742 return selectFirst<const T>(
3743 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
3744 *Node, Context)) != NULL;
3745 }
3746};
3747
3748TEST(IsEqualTo, MatchesNodesByIdentity) {
3749 EXPECT_TRUE(matchAndVerifyResultTrue(
3750 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
3751 new VerifyAncestorHasChildIsEqual<Decl>()));
3752 EXPECT_TRUE(
3753 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
3754 new VerifyAncestorHasChildIsEqual<Stmt>()));
3755}
3756
Manuel Klimeke5793282012-11-02 01:31:03 +00003757class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
3758public:
3759 VerifyStartOfTranslationUnit() : Called(false) {}
3760 virtual void run(const MatchFinder::MatchResult &Result) {
3761 EXPECT_TRUE(Called);
3762 }
3763 virtual void onStartOfTranslationUnit() {
3764 Called = true;
3765 }
3766 bool Called;
3767};
3768
3769TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
3770 MatchFinder Finder;
3771 VerifyStartOfTranslationUnit VerifyCallback;
3772 Finder.addMatcher(decl(), &VerifyCallback);
3773 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
3774 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
3775 EXPECT_TRUE(VerifyCallback.Called);
3776}
3777
Manuel Klimek4da21662012-07-06 05:48:52 +00003778} // end namespace ast_matchers
3779} // end namespace clang