blob: aa3c795b57d0dc3055eb0d012abdb515a5916be3 [file] [log] [blame]
Manuel Klimek04616e42012-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 Kramere5015e52012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek04616e42012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Manuel Klimek04616e42012-07-06 05:48:52 +000014#include "clang/Tooling/Tooling.h"
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +000015#include "llvm/ADT/Triple.h"
16#include "llvm/Support/Host.h"
Manuel Klimek04616e42012-07-06 05:48:52 +000017#include "gtest/gtest.h"
18
19namespace clang {
20namespace ast_matchers {
21
Benjamin Kramer60d7f5a2012-07-10 17:30:44 +000022#if GTEST_HAS_DEATH_TEST
Manuel Klimek04616e42012-07-06 05:48:52 +000023TEST(HasNameDeathTest, DiesOnEmptyName) {
24 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000025 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek04616e42012-07-06 05:48:52 +000026 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
27 }, "");
28}
29
Daniel Jasper1dad1832012-07-10 20:20:19 +000030TEST(HasNameDeathTest, DiesOnEmptyPattern) {
31 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000032 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jasper1dad1832012-07-10 20:20:19 +000033 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
34 }, "");
35}
36
Manuel Klimek04616e42012-07-06 05:48:52 +000037TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
38 ASSERT_DEBUG_DEATH({
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000039 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek04616e42012-07-06 05:48:52 +000040 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
41 }, "");
42}
Benjamin Kramer60d7f5a2012-07-10 17:30:44 +000043#endif
Manuel Klimek04616e42012-07-06 05:48:52 +000044
Peter Collingbourne2b9471302013-11-07 22:30:32 +000045TEST(Finder, DynamicOnlyAcceptsSomeMatchers) {
46 MatchFinder Finder;
47 EXPECT_TRUE(Finder.addDynamicMatcher(decl(), NULL));
48 EXPECT_TRUE(Finder.addDynamicMatcher(callExpr(), NULL));
49 EXPECT_TRUE(Finder.addDynamicMatcher(constantArrayType(hasSize(42)), NULL));
50
51 // Do not accept non-toplevel matchers.
52 EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), NULL));
53 EXPECT_FALSE(Finder.addDynamicMatcher(hasSize(2), NULL));
54 EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), NULL));
55}
56
Manuel Klimeke9235692012-07-25 10:02:02 +000057TEST(Decl, MatchesDeclarations) {
58 EXPECT_TRUE(notMatches("", decl(usingDecl())));
59 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
60 decl(usingDecl())));
61}
62
Manuel Klimek04616e42012-07-06 05:48:52 +000063TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000064 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +000065 EXPECT_TRUE(matches("typedef int X;", NamedX));
66 EXPECT_TRUE(matches("int X;", NamedX));
67 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
68 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
69 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
70 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jasper1dad1832012-07-10 20:20:19 +000071 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek04616e42012-07-06 05:48:52 +000072
73 EXPECT_TRUE(notMatches("#define X 1", NamedX));
74}
75
Daniel Jasper1dad1832012-07-10 20:20:19 +000076TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000077 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000078 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
79 EXPECT_TRUE(matches("int Xb;", NamedX));
80 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
81 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
82 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
83 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
84 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
85
86 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
87
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000088 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000089 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
90 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
91
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000092 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jasper1dad1832012-07-10 20:20:19 +000093 EXPECT_TRUE(matches("int abc;", Abc));
94 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
95 EXPECT_TRUE(notMatches("int cab;", Abc));
96 EXPECT_TRUE(matches("int cabc;", Abc));
Manuel Klimeke792efd2012-12-10 07:08:53 +000097
98 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
99 EXPECT_TRUE(matches("int k;", StartsWithK));
100 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
101 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
102 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
103 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000104}
105
Manuel Klimek04616e42012-07-06 05:48:52 +0000106TEST(DeclarationMatcher, MatchClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000107 DeclarationMatcher ClassMatcher(recordDecl());
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +0000108 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
109 llvm::Triple::Win32)
110 EXPECT_FALSE(matches("", ClassMatcher));
111 else
112 // Matches class type_info.
113 EXPECT_TRUE(matches("", ClassMatcher));
Manuel Klimek04616e42012-07-06 05:48:52 +0000114
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000115 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000116 EXPECT_TRUE(matches("class X;", ClassX));
117 EXPECT_TRUE(matches("class X {};", ClassX));
118 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
119 EXPECT_TRUE(notMatches("", ClassX));
120}
121
122TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000123 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000124
125 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasperf49d1e02012-09-07 12:48:17 +0000126 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
127 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek04616e42012-07-06 05:48:52 +0000128 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
129 EXPECT_TRUE(notMatches("", IsDerivedFromX));
130
Daniel Jasperd6b82cb2012-09-12 21:14:15 +0000131 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasperf49d1e02012-09-07 12:48:17 +0000132
133 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
134 EXPECT_TRUE(matches("class X {};", IsAX));
135 EXPECT_TRUE(matches("class X;", IsAX));
136 EXPECT_TRUE(notMatches("class Y;", IsAX));
137 EXPECT_TRUE(notMatches("", IsAX));
138
Manuel Klimek04616e42012-07-06 05:48:52 +0000139 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000140 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000141 EXPECT_TRUE(
142 matches("class X {}; class Y : public X {}; class Z : public Y {};",
143 ZIsDerivedFromX));
144 EXPECT_TRUE(
145 matches("class X {};"
146 "template<class T> class Y : public X {};"
147 "class Z : public Y<int> {};", ZIsDerivedFromX));
148 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
149 ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template<class T> class X {}; "
152 "template<class T> class Z : public X<T> {};",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 matches("template<class T, class U=T> class X {}; "
156 "template<class T> class Z : public X<T> {};",
157 ZIsDerivedFromX));
158 EXPECT_TRUE(
159 notMatches("template<class X> class A { class Z : public X {}; };",
160 ZIsDerivedFromX));
161 EXPECT_TRUE(
162 matches("template<class X> class A { public: class Z : public X {}; }; "
163 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
164 EXPECT_TRUE(
165 matches("template <class T> class X {}; "
166 "template<class Y> class A { class Z : public X<Y> {}; };",
167 ZIsDerivedFromX));
168 EXPECT_TRUE(
169 notMatches("template<template<class T> class X> class A { "
170 " class Z : public X<int> {}; };", ZIsDerivedFromX));
171 EXPECT_TRUE(
172 matches("template<template<class T> class X> class A { "
173 " public: class Z : public X<int> {}; }; "
174 "template<class T> class X {}; void y() { A<X>::Z z; }",
175 ZIsDerivedFromX));
176 EXPECT_TRUE(
177 notMatches("template<class X> class A { class Z : public X::D {}; };",
178 ZIsDerivedFromX));
179 EXPECT_TRUE(
180 matches("template<class X> class A { public: "
181 " class Z : public X::D {}; }; "
182 "class Y { public: class X {}; typedef X D; }; "
183 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
184 EXPECT_TRUE(
185 matches("class X {}; typedef X Y; class Z : public Y {};",
186 ZIsDerivedFromX));
187 EXPECT_TRUE(
188 matches("template<class T> class Y { typedef typename T::U X; "
189 " class Z : public X {}; };", ZIsDerivedFromX));
190 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
191 ZIsDerivedFromX));
192 EXPECT_TRUE(
193 notMatches("template<class T> class X {}; "
194 "template<class T> class A { class Z : public X<T>::D {}; };",
195 ZIsDerivedFromX));
196 EXPECT_TRUE(
197 matches("template<class T> class X { public: typedef X<T> D; }; "
198 "template<class T> class A { public: "
199 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
200 ZIsDerivedFromX));
201 EXPECT_TRUE(
202 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
203 ZIsDerivedFromX));
204 EXPECT_TRUE(
205 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
206 ZIsDerivedFromX));
207 EXPECT_TRUE(
208 matches("class X {}; class Y : public X {}; "
209 "typedef Y V; typedef V W; class Z : public W {};",
210 ZIsDerivedFromX));
211 EXPECT_TRUE(
212 matches("template<class T, class U> class X {}; "
213 "template<class T> class A { class Z : public X<T, int> {}; };",
214 ZIsDerivedFromX));
215 EXPECT_TRUE(
216 notMatches("template<class X> class D { typedef X A; typedef A B; "
217 " typedef B C; class Z : public C {}; };",
218 ZIsDerivedFromX));
219 EXPECT_TRUE(
220 matches("class X {}; typedef X A; typedef A B; "
221 "class Z : public B {};", ZIsDerivedFromX));
222 EXPECT_TRUE(
223 matches("class X {}; typedef X A; typedef A B; typedef B C; "
224 "class Z : public C {};", ZIsDerivedFromX));
225 EXPECT_TRUE(
226 matches("class U {}; typedef U X; typedef X V; "
227 "class Z : public V {};", ZIsDerivedFromX));
228 EXPECT_TRUE(
229 matches("class Base {}; typedef Base X; "
230 "class Z : public Base {};", ZIsDerivedFromX));
231 EXPECT_TRUE(
232 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
233 "class Z : public Base {};", ZIsDerivedFromX));
234 EXPECT_TRUE(
235 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
236 "class Z : public Base {};", ZIsDerivedFromX));
237 EXPECT_TRUE(
238 matches("class A {}; typedef A X; typedef A Y; "
239 "class Z : public Y {};", ZIsDerivedFromX));
240 EXPECT_TRUE(
241 notMatches("template <typename T> class Z;"
242 "template <> class Z<void> {};"
243 "template <typename T> class Z : public Z<void> {};",
244 IsDerivedFromX));
245 EXPECT_TRUE(
246 matches("template <typename T> class X;"
247 "template <> class X<void> {};"
248 "template <typename T> class X : public X<void> {};",
249 IsDerivedFromX));
250 EXPECT_TRUE(matches(
251 "class X {};"
252 "template <typename T> class Z;"
253 "template <> class Z<void> {};"
254 "template <typename T> class Z : public Z<void>, public X {};",
255 ZIsDerivedFromX));
Manuel Klimek5472a522012-12-04 13:40:29 +0000256 EXPECT_TRUE(
257 notMatches("template<int> struct X;"
258 "template<int i> struct X : public X<i-1> {};",
259 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
260 EXPECT_TRUE(matches(
261 "struct A {};"
262 "template<int> struct X;"
263 "template<int i> struct X : public X<i-1> {};"
264 "template<> struct X<0> : public A {};"
265 "struct B : public X<42> {};",
266 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000267
268 // FIXME: Once we have better matchers for template type matching,
269 // get rid of the Variable(...) matching and match the right template
270 // declarations directly.
271 const char *RecursiveTemplateOneParameter =
272 "class Base1 {}; class Base2 {};"
273 "template <typename T> class Z;"
274 "template <> class Z<void> : public Base1 {};"
275 "template <> class Z<int> : public Base2 {};"
276 "template <> class Z<float> : public Z<void> {};"
277 "template <> class Z<double> : public Z<int> {};"
278 "template <typename T> class Z : public Z<float>, public Z<double> {};"
279 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
280 EXPECT_TRUE(matches(
281 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000282 varDecl(hasName("z_float"),
283 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000284 EXPECT_TRUE(notMatches(
285 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000286 varDecl(hasName("z_float"),
287 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000288 EXPECT_TRUE(matches(
289 RecursiveTemplateOneParameter,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000290 varDecl(hasName("z_char"),
291 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
292 isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000293
294 const char *RecursiveTemplateTwoParameters =
295 "class Base1 {}; class Base2 {};"
296 "template <typename T1, typename T2> class Z;"
297 "template <typename T> class Z<void, T> : public Base1 {};"
298 "template <typename T> class Z<int, T> : public Base2 {};"
299 "template <typename T> class Z<float, T> : public Z<void, T> {};"
300 "template <typename T> class Z<double, T> : public Z<int, T> {};"
301 "template <typename T1, typename T2> class Z : "
302 " public Z<float, T2>, public Z<double, T2> {};"
303 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
304 " Z<char, void> z_char; }";
305 EXPECT_TRUE(matches(
306 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000307 varDecl(hasName("z_float"),
308 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000309 EXPECT_TRUE(notMatches(
310 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000311 varDecl(hasName("z_float"),
312 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000313 EXPECT_TRUE(matches(
314 RecursiveTemplateTwoParameters,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000315 varDecl(hasName("z_char"),
316 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
317 isDerivedFrom("Base2")))))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000318 EXPECT_TRUE(matches(
319 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000320 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000321 EXPECT_TRUE(notMatches(
322 "class X {}; class Y : public X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000323 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper2b3c7d42012-07-17 07:39:27 +0000324
325 EXPECT_TRUE(matches(
326 "class X {}; class Y : public X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000327 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek1863e502013-08-02 21:24:09 +0000328
329 EXPECT_TRUE(matches(
330 "template<typename T> class X {};"
331 "template<typename T> using Z = X<T>;"
332 "template <typename T> class Y : Z<T> {};",
333 recordDecl(isDerivedFrom(namedDecl(hasName("X"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000334}
335
Edwin Vane0a4836e2013-03-06 17:02:57 +0000336TEST(DeclarationMatcher, hasMethod) {
337 EXPECT_TRUE(matches("class A { void func(); };",
338 recordDecl(hasMethod(hasName("func")))));
339 EXPECT_TRUE(notMatches("class A { void func(); };",
340 recordDecl(hasMethod(isPublic()))));
341}
342
Daniel Jasper83dafaf2012-09-18 14:17:42 +0000343TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
344 EXPECT_TRUE(matches(
345 "template <typename T> struct A {"
346 " template <typename T2> struct F {};"
347 "};"
348 "template <typename T> struct B : A<T>::template F<T> {};"
349 "B<int> b;",
350 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
351}
352
Edwin Vaneb6eae142013-02-25 20:43:32 +0000353TEST(DeclarationMatcher, hasDeclContext) {
354 EXPECT_TRUE(matches(
355 "namespace N {"
356 " namespace M {"
357 " class D {};"
358 " }"
359 "}",
Daniel Jasper9fcdc462013-04-08 16:44:05 +0000360 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +0000361 EXPECT_TRUE(notMatches(
362 "namespace N {"
363 " namespace M {"
364 " class D {};"
365 " }"
366 "}",
Daniel Jasper9fcdc462013-04-08 16:44:05 +0000367 recordDecl(hasDeclContext(namespaceDecl(hasName("N"))))));
368
369 EXPECT_TRUE(matches("namespace {"
370 " namespace M {"
371 " class D {};"
372 " }"
373 "}",
374 recordDecl(hasDeclContext(namespaceDecl(
375 hasName("M"), hasDeclContext(namespaceDecl()))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +0000376}
377
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000378TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000379 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000380 EXPECT_TRUE(notMatches("class X;", ClassX));
381 EXPECT_TRUE(notMatches("class X {};", ClassX));
382}
383
384TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000385 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000386 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
387 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
388}
389
390TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
391 EXPECT_TRUE(notMatches("template<typename T> class X { };"
392 "template<> class X<int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000393 classTemplateDecl(hasName("X"),
394 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000395}
396
397TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
398 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
399 "template<typename T> class X<T, int> { int a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000400 classTemplateDecl(hasName("X"),
401 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +0000402}
403
Daniel Jasper4e566c42012-07-12 08:50:38 +0000404TEST(AllOf, AllOverloadsWork) {
405 const char Program[] =
Edwin Vanee9dd3602013-02-12 13:55:40 +0000406 "struct T { };"
407 "int f(int, T*, int, int);"
408 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper4e566c42012-07-12 08:50:38 +0000409 EXPECT_TRUE(matches(Program,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000410 callExpr(allOf(callee(functionDecl(hasName("f"))),
411 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +0000412 EXPECT_TRUE(matches(Program,
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000413 callExpr(allOf(callee(functionDecl(hasName("f"))),
414 hasArgument(0, declRefExpr(to(varDecl()))),
415 hasArgument(1, hasType(pointsTo(
416 recordDecl(hasName("T")))))))));
Edwin Vanee9dd3602013-02-12 13:55:40 +0000417 EXPECT_TRUE(matches(Program,
418 callExpr(allOf(callee(functionDecl(hasName("f"))),
419 hasArgument(0, declRefExpr(to(varDecl()))),
420 hasArgument(1, hasType(pointsTo(
421 recordDecl(hasName("T"))))),
422 hasArgument(2, integerLiteral(equals(3)))))));
423 EXPECT_TRUE(matches(Program,
424 callExpr(allOf(callee(functionDecl(hasName("f"))),
425 hasArgument(0, declRefExpr(to(varDecl()))),
426 hasArgument(1, hasType(pointsTo(
427 recordDecl(hasName("T"))))),
428 hasArgument(2, integerLiteral(equals(3))),
429 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +0000430}
431
Manuel Klimek04616e42012-07-06 05:48:52 +0000432TEST(DeclarationMatcher, MatchAnyOf) {
433 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000434 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000435 EXPECT_TRUE(
436 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
437 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
438 EXPECT_TRUE(
439 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
440 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
441
Daniel Jasper84c763e2012-07-15 19:57:12 +0000442 DeclarationMatcher XOrYOrZOrU =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000443 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasper84c763e2012-07-15 19:57:12 +0000444 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
445 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
446
Manuel Klimek04616e42012-07-06 05:48:52 +0000447 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000448 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
449 hasName("V")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000450 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
451 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
452 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
453 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
454 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
455 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
456}
457
458TEST(DeclarationMatcher, MatchHas) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000459 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000460 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
461 EXPECT_TRUE(matches("class X {};", HasClassX));
462
463 DeclarationMatcher YHasClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000464 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000465 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
466 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
467 EXPECT_TRUE(
468 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
469}
470
471TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
472 DeclarationMatcher Recursive =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000473 recordDecl(
474 has(recordDecl(
475 has(recordDecl(hasName("X"))),
476 has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000477 hasName("Z"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000478 has(recordDecl(
479 has(recordDecl(hasName("A"))),
480 has(recordDecl(hasName("B"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000481 hasName("C"))),
482 hasName("F"));
483
484 EXPECT_TRUE(matches(
485 "class F {"
486 " class Z {"
487 " class X {};"
488 " class Y {};"
489 " };"
490 " class C {"
491 " class A {};"
492 " class B {};"
493 " };"
494 "};", Recursive));
495
496 EXPECT_TRUE(matches(
497 "class F {"
498 " class Z {"
499 " class A {};"
500 " class X {};"
501 " class Y {};"
502 " };"
503 " class C {"
504 " class X {};"
505 " class A {};"
506 " class B {};"
507 " };"
508 "};", Recursive));
509
510 EXPECT_TRUE(matches(
511 "class O1 {"
512 " class O2 {"
513 " class F {"
514 " class Z {"
515 " class A {};"
516 " class X {};"
517 " class Y {};"
518 " };"
519 " class C {"
520 " class X {};"
521 " class A {};"
522 " class B {};"
523 " };"
524 " };"
525 " };"
526 "};", Recursive));
527}
528
529TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
530 DeclarationMatcher Recursive =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000531 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000532 anyOf(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000533 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000534 anyOf(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000535 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000536 hasName("X"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000537 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000538 hasName("Y"))),
539 hasName("Z")))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000540 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000541 anyOf(
542 hasName("C"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000543 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000544 hasName("A"))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000545 has(recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000546 hasName("B")))))),
547 hasName("F")));
548
549 EXPECT_TRUE(matches("class F {};", Recursive));
550 EXPECT_TRUE(matches("class Z {};", Recursive));
551 EXPECT_TRUE(matches("class C {};", Recursive));
552 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
553 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
554 EXPECT_TRUE(
555 matches("class O1 { class O2 {"
556 " class M { class N { class B {}; }; }; "
557 "}; };", Recursive));
558}
559
560TEST(DeclarationMatcher, MatchNot) {
561 DeclarationMatcher NotClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000562 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000563 isDerivedFrom("Y"),
Manuel Klimek04616e42012-07-06 05:48:52 +0000564 unless(hasName("X")));
565 EXPECT_TRUE(notMatches("", NotClassX));
566 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
567 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
568 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
569 EXPECT_TRUE(
570 notMatches("class Y {}; class Z {}; class X : public Y {};",
571 NotClassX));
572
573 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000574 recordDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +0000575 hasName("X"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000576 has(recordDecl(hasName("Z"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000577 unless(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000578 has(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000579 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
580 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
581 ClassXHasNotClassY));
582}
583
584TEST(DeclarationMatcher, HasDescendant) {
585 DeclarationMatcher ZDescendantClassX =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000586 recordDecl(
587 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000588 hasName("Z"));
589 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
590 EXPECT_TRUE(
591 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
592 EXPECT_TRUE(
593 matches("class Z { class A { class Y { class X {}; }; }; };",
594 ZDescendantClassX));
595 EXPECT_TRUE(
596 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
597 ZDescendantClassX));
598 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
599
600 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000601 recordDecl(
602 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000603 hasName("X"))),
604 hasName("Z"));
605 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
606 ZDescendantClassXHasClassY));
607 EXPECT_TRUE(
608 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
609 ZDescendantClassXHasClassY));
610 EXPECT_TRUE(notMatches(
611 "class Z {"
612 " class A {"
613 " class B {"
614 " class X {"
615 " class C {"
616 " class Y {};"
617 " };"
618 " };"
619 " }; "
620 " };"
621 "};", ZDescendantClassXHasClassY));
622
623 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000624 recordDecl(
625 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
626 hasName("X"))),
Manuel Klimek04616e42012-07-06 05:48:52 +0000627 hasName("Z"));
628 EXPECT_TRUE(
629 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
630 ZDescendantClassXDescendantClassY));
631 EXPECT_TRUE(matches(
632 "class Z {"
633 " class A {"
634 " class X {"
635 " class B {"
636 " class Y {};"
637 " };"
638 " class Y {};"
639 " };"
640 " };"
641 "};", ZDescendantClassXDescendantClassY));
642}
643
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000644// Implements a run method that returns whether BoundNodes contains a
645// Decl bound to Id that can be dynamically cast to T.
646// Optionally checks that the check succeeded a specific number of times.
647template <typename T>
648class VerifyIdIsBoundTo : public BoundNodesCallback {
649public:
650 // Create an object that checks that a node of type \c T was bound to \c Id.
651 // Does not check for a certain number of matches.
652 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
653 : Id(Id), ExpectedCount(-1), Count(0) {}
654
655 // Create an object that checks that a node of type \c T was bound to \c Id.
656 // Checks that there were exactly \c ExpectedCount matches.
657 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
658 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
659
660 // Create an object that checks that a node of type \c T was bound to \c Id.
661 // Checks that there was exactly one match with the name \c ExpectedName.
662 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimekb64d6b72013-03-14 16:33:21 +0000663 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
664 int ExpectedCount = 1)
665 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
666 ExpectedName(ExpectedName) {}
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000667
Peter Collingbourne2b9471302013-11-07 22:30:32 +0000668 void onEndOfTranslationUnit() LLVM_OVERRIDE {
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000669 if (ExpectedCount != -1)
670 EXPECT_EQ(ExpectedCount, Count);
671 if (!ExpectedName.empty())
672 EXPECT_EQ(ExpectedName, Name);
Peter Collingbourne2b9471302013-11-07 22:30:32 +0000673 Count = 0;
674 Name.clear();
675 }
676
677 ~VerifyIdIsBoundTo() {
678 EXPECT_EQ(0, Count);
679 EXPECT_EQ("", Name);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000680 }
681
682 virtual bool run(const BoundNodes *Nodes) {
Peter Collingbourne093a7292013-11-06 00:27:07 +0000683 const BoundNodes::IDToNodeMap &M = Nodes->getMap();
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000684 if (Nodes->getNodeAs<T>(Id)) {
685 ++Count;
686 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
687 Name = Named->getNameAsString();
688 } else if (const NestedNameSpecifier *NNS =
689 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
690 llvm::raw_string_ostream OS(Name);
691 NNS->print(OS, PrintingPolicy(LangOptions()));
692 }
Peter Collingbourne093a7292013-11-06 00:27:07 +0000693 BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
694 EXPECT_NE(M.end(), I);
695 if (I != M.end())
696 EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000697 return true;
698 }
Peter Collingbourne093a7292013-11-06 00:27:07 +0000699 EXPECT_TRUE(M.count(Id) == 0 || M.find(Id)->second.template get<T>() == 0);
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000700 return false;
701 }
702
Daniel Jaspere9aa6872012-10-29 10:48:25 +0000703 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
704 return run(Nodes);
705 }
706
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000707private:
708 const std::string Id;
709 const int ExpectedCount;
710 int Count;
711 const std::string ExpectedName;
712 std::string Name;
713};
714
715TEST(HasDescendant, MatchesDescendantTypes) {
716 EXPECT_TRUE(matches("void f() { int i = 3; }",
717 decl(hasDescendant(loc(builtinType())))));
718 EXPECT_TRUE(matches("void f() { int i = 3; }",
719 stmt(hasDescendant(builtinType()))));
720
721 EXPECT_TRUE(matches("void f() { int i = 3; }",
722 stmt(hasDescendant(loc(builtinType())))));
723 EXPECT_TRUE(matches("void f() { int i = 3; }",
724 stmt(hasDescendant(qualType(builtinType())))));
725
726 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
727 stmt(hasDescendant(isInteger()))));
728
729 EXPECT_TRUE(matchAndVerifyResultTrue(
730 "void f() { int a; float c; int d; int e; }",
731 functionDecl(forEachDescendant(
732 varDecl(hasDescendant(isInteger())).bind("x"))),
733 new VerifyIdIsBoundTo<Decl>("x", 3)));
734}
735
736TEST(HasDescendant, MatchesDescendantsOfTypes) {
737 EXPECT_TRUE(matches("void f() { int*** i; }",
738 qualType(hasDescendant(builtinType()))));
739 EXPECT_TRUE(matches("void f() { int*** i; }",
740 qualType(hasDescendant(
741 pointerType(pointee(builtinType()))))));
742 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikieb61d0872013-02-18 19:04:16 +0000743 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jasperd29d5fa2012-10-29 10:14:44 +0000744
745 EXPECT_TRUE(matchAndVerifyResultTrue(
746 "void f() { int*** i; }",
747 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
748 new VerifyIdIsBoundTo<Type>("x", 2)));
749}
750
751TEST(Has, MatchesChildrenOfTypes) {
752 EXPECT_TRUE(matches("int i;",
753 varDecl(hasName("i"), has(isInteger()))));
754 EXPECT_TRUE(notMatches("int** i;",
755 varDecl(hasName("i"), has(isInteger()))));
756 EXPECT_TRUE(matchAndVerifyResultTrue(
757 "int (*f)(float, int);",
758 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
759 new VerifyIdIsBoundTo<QualType>("x", 2)));
760}
761
762TEST(Has, MatchesChildTypes) {
763 EXPECT_TRUE(matches(
764 "int* i;",
765 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
766 EXPECT_TRUE(notMatches(
767 "int* i;",
768 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
769}
770
Daniel Jasper1dad1832012-07-10 20:20:19 +0000771TEST(Enum, DoesNotMatchClasses) {
772 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
773}
774
775TEST(Enum, MatchesEnums) {
776 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
777}
778
779TEST(EnumConstant, Matches) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000780 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000781 EXPECT_TRUE(matches("enum X{ A };", Matcher));
782 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
783 EXPECT_TRUE(notMatches("enum X {};", Matcher));
784}
785
Manuel Klimek04616e42012-07-06 05:48:52 +0000786TEST(StatementMatcher, Has) {
787 StatementMatcher HasVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000788 expr(hasType(pointsTo(recordDecl(hasName("X")))),
789 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000790
791 EXPECT_TRUE(matches(
792 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
793 EXPECT_TRUE(notMatches(
794 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
795}
796
797TEST(StatementMatcher, HasDescendant) {
798 StatementMatcher HasDescendantVariableI =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000799 expr(hasType(pointsTo(recordDecl(hasName("X")))),
800 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000801
802 EXPECT_TRUE(matches(
803 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
804 HasDescendantVariableI));
805 EXPECT_TRUE(notMatches(
806 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
807 HasDescendantVariableI));
808}
809
810TEST(TypeMatcher, MatchesClassType) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000811 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000812
813 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
814 EXPECT_TRUE(notMatches("class A {};", TypeA));
815
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000816 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000817
818 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
819 TypeDerivedFromA));
820 EXPECT_TRUE(notMatches("class A {};", TypeA));
821
822 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000823 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000824
825 EXPECT_TRUE(
826 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
827}
828
Manuel Klimek04616e42012-07-06 05:48:52 +0000829TEST(Matcher, BindMatchedNodes) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000830 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000831
832 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000833 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000834
835 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000836 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000837
838 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000839 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000840
841 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
842 TypeAHasClassB,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000843 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000844
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000845 StatementMatcher MethodX =
846 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek04616e42012-07-06 05:48:52 +0000847
848 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
849 MethodX,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000850 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000851}
852
853TEST(Matcher, BindTheSameNameInAlternatives) {
854 StatementMatcher matcher = anyOf(
855 binaryOperator(hasOperatorName("+"),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000856 hasLHS(expr().bind("x")),
Daniel Jasper1dad1832012-07-10 20:20:19 +0000857 hasRHS(integerLiteral(equals(0)))),
858 binaryOperator(hasOperatorName("+"),
859 hasLHS(integerLiteral(equals(0))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000860 hasRHS(expr().bind("x"))));
Daniel Jasper1dad1832012-07-10 20:20:19 +0000861
862 EXPECT_TRUE(matchAndVerifyResultTrue(
863 // The first branch of the matcher binds x to 0 but then fails.
864 // The second branch binds x to f() and succeeds.
865 "int f() { return 0 + f(); }",
866 matcher,
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000867 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000868}
869
Manuel Klimekfdf98762012-08-30 19:41:06 +0000870TEST(Matcher, BindsIDForMemoizedResults) {
871 // Using the same matcher in two match expressions will make memoization
872 // kick in.
873 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
874 EXPECT_TRUE(matchAndVerifyResultTrue(
875 "class A { class B { class X {}; }; };",
876 DeclarationMatcher(anyOf(
877 recordDecl(hasName("A"), hasDescendant(ClassX)),
878 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +0000879 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimekfdf98762012-08-30 19:41:06 +0000880}
881
Daniel Jasper856194d02012-12-03 15:43:25 +0000882TEST(HasDeclaration, HasDeclarationOfEnumType) {
883 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
884 expr(hasType(pointsTo(
885 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
886}
887
Edwin Vaneed936452013-02-25 14:32:42 +0000888TEST(HasDeclaration, HasGetDeclTraitTest) {
889 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
890 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
891 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
892}
893
Edwin Vane2c197e02013-02-19 17:14:34 +0000894TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
895 EXPECT_TRUE(matches("typedef int X; X a;",
896 varDecl(hasName("a"),
897 hasType(typedefType(hasDeclaration(decl()))))));
898
899 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
900}
901
Edwin Vanef901b712013-02-25 14:49:29 +0000902TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
903 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
904 varDecl(hasType(templateSpecializationType(
905 hasDeclaration(namedDecl(hasName("A"))))))));
906}
907
Manuel Klimek04616e42012-07-06 05:48:52 +0000908TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000909 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000910 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000911 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000912 EXPECT_TRUE(
913 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000914 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000915 EXPECT_TRUE(
916 matches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000917 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000918}
919
920TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000921 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek04616e42012-07-06 05:48:52 +0000922 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000923 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000924 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000925 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000926 EXPECT_TRUE(
927 matches("class X {}; void y() { X *x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000928 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000929}
930
931TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000932 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000933 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000934 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000935 EXPECT_TRUE(
936 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000937 expr(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000938}
939
940TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000941 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek04616e42012-07-06 05:48:52 +0000942 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000943 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000944 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000945 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000946}
947
Manuel Klimekc16c6522013-06-20 13:08:29 +0000948TEST(HasTypeLoc, MatchesDeclaratorDecls) {
949 EXPECT_TRUE(matches("int x;",
950 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
951
952 // Make sure we don't crash on implicit constructors.
953 EXPECT_TRUE(notMatches("class X {}; X x;",
954 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
955}
956
Manuel Klimek04616e42012-07-06 05:48:52 +0000957TEST(Matcher, Call) {
958 // FIXME: Do we want to overload Call() to directly take
Daniel Jasper1dad1832012-07-10 20:20:19 +0000959 // Matcher<Decl>, too?
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000960 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000961
962 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
963 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
964
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000965 StatementMatcher MethodOnY =
966 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000967
968 EXPECT_TRUE(
969 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
970 MethodOnY));
971 EXPECT_TRUE(
972 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
973 MethodOnY));
974 EXPECT_TRUE(
975 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
976 MethodOnY));
977 EXPECT_TRUE(
978 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
979 MethodOnY));
980 EXPECT_TRUE(
981 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
982 MethodOnY));
983
984 StatementMatcher MethodOnYPointer =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +0000985 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +0000986
987 EXPECT_TRUE(
988 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
989 MethodOnYPointer));
990 EXPECT_TRUE(
991 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
992 MethodOnYPointer));
993 EXPECT_TRUE(
994 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
995 MethodOnYPointer));
996 EXPECT_TRUE(
997 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
998 MethodOnYPointer));
999 EXPECT_TRUE(
1000 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1001 MethodOnYPointer));
1002}
1003
Daniel Jasper5901e472012-10-01 13:40:41 +00001004TEST(Matcher, Lambda) {
Richard Smith3d584b02014-02-06 21:49:08 +00001005 EXPECT_TRUE(matches("auto f = [] (int i) { return i; };",
Daniel Jasper5901e472012-10-01 13:40:41 +00001006 lambdaExpr()));
1007}
1008
1009TEST(Matcher, ForRange) {
Daniel Jasper6f595392012-10-01 15:05:34 +00001010 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
1011 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00001012 forRangeStmt()));
1013 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
1014 forRangeStmt()));
1015}
1016
1017TEST(Matcher, UserDefinedLiteral) {
1018 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
1019 " return i + 1;"
1020 "}"
1021 "char c = 'a'_inc;",
1022 userDefinedLiteral()));
1023}
1024
Daniel Jasper87c3d362012-09-20 14:12:57 +00001025TEST(Matcher, FlowControl) {
1026 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
1027 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
1028 continueStmt()));
1029 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
1030 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
1031 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
1032}
1033
Daniel Jasper1dad1832012-07-10 20:20:19 +00001034TEST(HasType, MatchesAsString) {
1035 EXPECT_TRUE(
1036 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001037 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001038 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001039 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001040 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001041 fieldDecl(hasType(asString("ns::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001042 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
David Blaikie8972f4e2014-02-14 22:12:54 +00001043 fieldDecl(hasType(asString("struct <anonymous namespace>::A")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001044}
1045
Manuel Klimek04616e42012-07-06 05:48:52 +00001046TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001047 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001048 // Unary operator
1049 EXPECT_TRUE(matches("class Y { }; "
1050 "bool operator!(Y x) { return false; }; "
1051 "Y y; bool c = !y;", OpCall));
1052 // No match -- special operators like "new", "delete"
1053 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1054 // we need to figure out include paths in the test.
1055 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1056 // "class Y { }; "
1057 // "void *operator new(size_t size) { return 0; } "
1058 // "Y *y = new Y;", OpCall));
1059 EXPECT_TRUE(notMatches("class Y { }; "
1060 "void operator delete(void *p) { } "
1061 "void a() {Y *y = new Y; delete y;}", OpCall));
1062 // Binary operator
1063 EXPECT_TRUE(matches("class Y { }; "
1064 "bool operator&&(Y x, Y y) { return true; }; "
1065 "Y a; Y b; bool c = a && b;",
1066 OpCall));
1067 // No match -- normal operator, not an overloaded one.
1068 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1069 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1070}
1071
1072TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1073 StatementMatcher OpCallAndAnd =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001074 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek04616e42012-07-06 05:48:52 +00001075 EXPECT_TRUE(matches("class Y { }; "
1076 "bool operator&&(Y x, Y y) { return true; }; "
1077 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1078 StatementMatcher OpCallLessLess =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001079 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek04616e42012-07-06 05:48:52 +00001080 EXPECT_TRUE(notMatches("class Y { }; "
1081 "bool operator&&(Y x, Y y) { return true; }; "
1082 "Y a; Y b; bool c = a && b;",
1083 OpCallLessLess));
Edwin Vane0a4836e2013-03-06 17:02:57 +00001084 DeclarationMatcher ClassWithOpStar =
1085 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1086 EXPECT_TRUE(matches("class Y { int operator*(); };",
1087 ClassWithOpStar));
1088 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1089 ClassWithOpStar)) ;
Manuel Klimek04616e42012-07-06 05:48:52 +00001090}
1091
Daniel Jasper0f9f0192012-11-15 03:29:05 +00001092TEST(Matcher, NestedOverloadedOperatorCalls) {
1093 EXPECT_TRUE(matchAndVerifyResultTrue(
1094 "class Y { }; "
1095 "Y& operator&&(Y& x, Y& y) { return x; }; "
1096 "Y a; Y b; Y c; Y d = a && b && c;",
1097 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1098 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1099 EXPECT_TRUE(matches(
1100 "class Y { }; "
1101 "Y& operator&&(Y& x, Y& y) { return x; }; "
1102 "Y a; Y b; Y c; Y d = a && b && c;",
1103 operatorCallExpr(hasParent(operatorCallExpr()))));
1104 EXPECT_TRUE(matches(
1105 "class Y { }; "
1106 "Y& operator&&(Y& x, Y& y) { return x; }; "
1107 "Y a; Y b; Y c; Y d = a && b && c;",
1108 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1109}
1110
Manuel Klimek04616e42012-07-06 05:48:52 +00001111TEST(Matcher, ThisPointerType) {
Manuel Klimek86f8bbc2012-07-24 13:37:29 +00001112 StatementMatcher MethodOnY =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001113 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001114
1115 EXPECT_TRUE(
1116 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1117 MethodOnY));
1118 EXPECT_TRUE(
1119 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1120 MethodOnY));
1121 EXPECT_TRUE(
1122 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1123 MethodOnY));
1124 EXPECT_TRUE(
1125 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1126 MethodOnY));
1127 EXPECT_TRUE(
1128 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1129 MethodOnY));
1130
1131 EXPECT_TRUE(matches(
1132 "class Y {"
1133 " public: virtual void x();"
1134 "};"
1135 "class X : public Y {"
1136 " public: virtual void x();"
1137 "};"
1138 "void z() { X *x; x->Y::x(); }", MethodOnY));
1139}
1140
1141TEST(Matcher, VariableUsage) {
1142 StatementMatcher Reference =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001143 declRefExpr(to(
1144 varDecl(hasInitializer(
1145 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001146
1147 EXPECT_TRUE(matches(
1148 "class Y {"
1149 " public:"
1150 " bool x() const;"
1151 "};"
1152 "void z(const Y &y) {"
1153 " bool b = y.x();"
1154 " if (b) {}"
1155 "}", Reference));
1156
1157 EXPECT_TRUE(notMatches(
1158 "class Y {"
1159 " public:"
1160 " bool x() const;"
1161 "};"
1162 "void z(const Y &y) {"
1163 " bool b = y.x();"
1164 "}", Reference));
1165}
1166
Manuel Klimek61379422012-12-04 14:42:08 +00001167TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001168 EXPECT_TRUE(matches(
1169 "void f(int i) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001170 varDecl(hasName("i"))));
Daniel Jasper3cb72b42012-07-30 05:03:25 +00001171}
1172
Manuel Klimek04616e42012-07-06 05:48:52 +00001173TEST(Matcher, CalledVariable) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001174 StatementMatcher CallOnVariableY =
1175 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001176
1177 EXPECT_TRUE(matches(
1178 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1179 EXPECT_TRUE(matches(
1180 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1181 EXPECT_TRUE(matches(
1182 "class Y { public: void x(); };"
1183 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1184 EXPECT_TRUE(matches(
1185 "class Y { public: void x(); };"
1186 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1187 EXPECT_TRUE(notMatches(
1188 "class Y { public: void x(); };"
1189 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1190 CallOnVariableY));
1191}
1192
Daniel Jasper1dad1832012-07-10 20:20:19 +00001193TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1194 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1195 unaryExprOrTypeTraitExpr()));
1196 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1197 alignOfExpr(anything())));
1198 // FIXME: Uncomment once alignof is enabled.
1199 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1200 // unaryExprOrTypeTraitExpr()));
1201 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1202 // sizeOfExpr()));
1203}
1204
1205TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1206 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1207 hasArgumentOfType(asString("int")))));
1208 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1209 hasArgumentOfType(asString("float")))));
1210 EXPECT_TRUE(matches(
1211 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001212 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001213 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001214 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001215}
1216
Manuel Klimek04616e42012-07-06 05:48:52 +00001217TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001218 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001219}
1220
1221TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001222 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001223}
1224
1225TEST(MemberExpression, MatchesVariable) {
1226 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001227 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001228 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001229 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001230 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001231 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001232}
1233
1234TEST(MemberExpression, MatchesStaticVariable) {
1235 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001236 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001237 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001238 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001239 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001240 memberExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00001241}
1242
Daniel Jasper4e566c42012-07-12 08:50:38 +00001243TEST(IsInteger, MatchesIntegers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001244 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1245 EXPECT_TRUE(matches(
1246 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1247 callExpr(hasArgument(0, declRefExpr(
1248 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001249}
1250
1251TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001252 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001253 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001254 callExpr(hasArgument(0, declRefExpr(
1255 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00001256}
1257
Manuel Klimek04616e42012-07-06 05:48:52 +00001258TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1259 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001260 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001261 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001262 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001263 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001264 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001265}
1266
1267TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1268 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001269 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001270 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001271 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001272 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001273 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001274}
1275
1276TEST(IsArrow, MatchesMemberCallsViaArrow) {
1277 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001278 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001279 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001280 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001281 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001282 memberExpr(isArrow())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001283}
1284
1285TEST(Callee, MatchesDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001286 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001287
1288 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1289 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1290}
1291
1292TEST(Callee, MatchesMemberExpressions) {
1293 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001294 callExpr(callee(memberExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001295 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001296 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001297}
1298
1299TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001300 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001301
1302 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1303 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1304
NAKAMURA Takumi7d2da0b2014-02-16 10:16:09 +00001305 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
1306 llvm::Triple::Win32) {
1307 // FIXME: Make this work for MSVC.
1308 // Dependent contexts, but a non-dependent call.
1309 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1310 CallFunctionF));
1311 EXPECT_TRUE(
1312 matches("void f(); template <int N> struct S { void g() { f(); } };",
1313 CallFunctionF));
1314 }
Manuel Klimek04616e42012-07-06 05:48:52 +00001315
1316 // Depedent calls don't match.
1317 EXPECT_TRUE(
1318 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1319 CallFunctionF));
1320 EXPECT_TRUE(
1321 notMatches("void f(int);"
1322 "template <typename T> struct S { void g(T t) { f(t); } };",
1323 CallFunctionF));
1324}
1325
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001326TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1327 EXPECT_TRUE(
1328 matches("template <typename T> void f(T t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001329 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001330}
1331
1332TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1333 EXPECT_TRUE(
1334 notMatches("void f(double d); void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001335 functionTemplateDecl(hasName("f"))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001336}
1337
1338TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1339 EXPECT_TRUE(
1340 notMatches("void g(); template <typename T> void f(T t) {}"
1341 "template <> void f(int t) { g(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001342 functionTemplateDecl(hasName("f"),
1343 hasDescendant(declRefExpr(to(
1344 functionDecl(hasName("g"))))))));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00001345}
1346
Manuel Klimek04616e42012-07-06 05:48:52 +00001347TEST(Matcher, Argument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001348 StatementMatcher CallArgumentY = callExpr(
1349 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001350
1351 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1352 EXPECT_TRUE(
1353 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1354 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1355
Daniel Jasper848cbe12012-09-18 13:09:13 +00001356 StatementMatcher WrongIndex = callExpr(
1357 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001358 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1359}
1360
1361TEST(Matcher, AnyArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001362 StatementMatcher CallArgumentY = callExpr(
1363 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001364 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1365 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1366 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1367}
1368
1369TEST(Matcher, ArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001370 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001371
1372 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1373 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1374 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1375}
1376
Daniel Jasper9f501292012-12-04 11:54:27 +00001377TEST(Matcher, ParameterCount) {
1378 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1379 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1380 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1381 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1382 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1383}
1384
Manuel Klimek04616e42012-07-06 05:48:52 +00001385TEST(Matcher, References) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001386 DeclarationMatcher ReferenceClassX = varDecl(
1387 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001388 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1389 ReferenceClassX));
1390 EXPECT_TRUE(
1391 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
Michael Hanc90d12d2013-09-11 15:53:29 +00001392 // The match here is on the implicit copy constructor code for
1393 // class X, not on code 'X x = y'.
Manuel Klimek04616e42012-07-06 05:48:52 +00001394 EXPECT_TRUE(
Michael Hanc90d12d2013-09-11 15:53:29 +00001395 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1396 EXPECT_TRUE(
1397 notMatches("class X {}; extern X x;", ReferenceClassX));
Manuel Klimek04616e42012-07-06 05:48:52 +00001398 EXPECT_TRUE(
1399 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1400}
1401
Edwin Vane0a4836e2013-03-06 17:02:57 +00001402TEST(QualType, hasCanonicalType) {
1403 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1404 "int a;"
1405 "int_ref b = a;",
1406 varDecl(hasType(qualType(referenceType())))));
1407 EXPECT_TRUE(
1408 matches("typedef int &int_ref;"
1409 "int a;"
1410 "int_ref b = a;",
1411 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1412}
1413
Edwin Vane119d3df2013-04-02 18:15:55 +00001414TEST(QualType, hasLocalQualifiers) {
1415 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1416 varDecl(hasType(hasLocalQualifiers()))));
1417 EXPECT_TRUE(matches("int *const j = nullptr;",
1418 varDecl(hasType(hasLocalQualifiers()))));
1419 EXPECT_TRUE(matches("int *volatile k;",
1420 varDecl(hasType(hasLocalQualifiers()))));
1421 EXPECT_TRUE(notMatches("int m;",
1422 varDecl(hasType(hasLocalQualifiers()))));
1423}
1424
Manuel Klimek04616e42012-07-06 05:48:52 +00001425TEST(HasParameter, CallsInnerMatcher) {
1426 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001427 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001428 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001429 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001430}
1431
1432TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1433 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001434 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001435}
1436
1437TEST(HasType, MatchesParameterVariableTypesStrictly) {
1438 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001439 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001440 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001441 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001442 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001443 methodDecl(hasParameter(0,
1444 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001445 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001446 methodDecl(hasParameter(0,
1447 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001448}
1449
1450TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1451 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001452 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001453 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001454 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001455}
1456
Daniel Jasper1dad1832012-07-10 20:20:19 +00001457TEST(Returns, MatchesReturnTypes) {
1458 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001459 functionDecl(returns(asString("int")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001460 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001461 functionDecl(returns(asString("float")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001462 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001463 functionDecl(returns(hasDeclaration(
1464 recordDecl(hasName("Y")))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001465}
1466
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001467TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001468 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1469 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1470 functionDecl(isExternC())));
1471 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasperfaaffe32012-08-15 18:52:19 +00001472}
1473
Manuel Klimek04616e42012-07-06 05:48:52 +00001474TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1475 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001476 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001477}
1478
1479TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1480 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001481 methodDecl(hasAnyParameter(hasType(pointsTo(
1482 recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001483}
1484
Alp Toker8db6e7a2014-01-05 06:38:57 +00001485TEST(HasName, MatchesParameterVariableDeclarations) {
Manuel Klimek04616e42012-07-06 05:48:52 +00001486 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001487 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001488 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001489 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001490}
1491
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001492TEST(Matcher, MatchesClassTemplateSpecialization) {
1493 EXPECT_TRUE(matches("template<typename T> struct A {};"
1494 "template<> struct A<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001495 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001496 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001497 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001498 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001499 classTemplateSpecializationDecl()));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001500}
1501
Manuel Klimekc16c6522013-06-20 13:08:29 +00001502TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1503 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1504 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1505}
1506
1507TEST(ParmVarDecl, MatchesParmVars) {
1508 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1509 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1510}
1511
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001512TEST(Matcher, MatchesTypeTemplateArgument) {
1513 EXPECT_TRUE(matches(
1514 "template<typename T> struct B {};"
1515 "B<int> b;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001516 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001517 asString("int"))))));
1518}
1519
1520TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1521 EXPECT_TRUE(matches(
1522 "struct B { int next; };"
1523 "template<int(B::*next_ptr)> struct A {};"
1524 "A<&B::next> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001525 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1526 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasper0c303372012-09-29 15:55:18 +00001527
1528 EXPECT_TRUE(notMatches(
1529 "template <typename T> struct A {};"
1530 "A<int> a;",
1531 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1532 refersToDeclaration(decl())))));
Peter Collingbourne564597f2014-02-20 19:18:03 +00001533
1534 EXPECT_TRUE(matches(
1535 "struct B { int next; };"
1536 "template<int(B::*next_ptr)> struct A {};"
1537 "A<&B::next> a;",
1538 templateSpecializationType(hasAnyTemplateArgument(isExpr(
1539 hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))));
1540
1541 EXPECT_TRUE(notMatches(
1542 "template <typename T> struct A {};"
1543 "A<int> a;",
1544 templateSpecializationType(hasAnyTemplateArgument(
1545 refersToDeclaration(decl())))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001546}
1547
1548TEST(Matcher, MatchesSpecificArgument) {
1549 EXPECT_TRUE(matches(
1550 "template<typename T, typename U> class A {};"
1551 "A<bool, int> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001552 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001553 1, refersToType(asString("int"))))));
1554 EXPECT_TRUE(notMatches(
1555 "template<typename T, typename U> class A {};"
1556 "A<int, bool> a;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001557 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001558 1, refersToType(asString("int"))))));
Peter Collingbourne564597f2014-02-20 19:18:03 +00001559
1560 EXPECT_TRUE(matches(
1561 "template<typename T, typename U> class A {};"
1562 "A<bool, int> a;",
1563 templateSpecializationType(hasTemplateArgument(
1564 1, refersToType(asString("int"))))));
1565 EXPECT_TRUE(notMatches(
1566 "template<typename T, typename U> class A {};"
1567 "A<int, bool> a;",
1568 templateSpecializationType(hasTemplateArgument(
1569 1, refersToType(asString("int"))))));
Daniel Jasper8bd14aa2012-08-01 08:40:24 +00001570}
1571
Daniel Jasper639522c2013-02-25 12:02:08 +00001572TEST(Matcher, MatchesAccessSpecDecls) {
1573 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1574 EXPECT_TRUE(
1575 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1576 EXPECT_TRUE(
1577 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1578 EXPECT_TRUE(
1579 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1580
1581 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1582}
1583
Edwin Vane37ee1d72013-04-09 20:46:36 +00001584TEST(Matcher, MatchesVirtualMethod) {
1585 EXPECT_TRUE(matches("class X { virtual int f(); };",
1586 methodDecl(isVirtual(), hasName("::X::f"))));
1587 EXPECT_TRUE(notMatches("class X { int f(); };",
1588 methodDecl(isVirtual())));
1589}
1590
Dmitri Gribenko51c1b552014-02-24 09:27:46 +00001591TEST(Matcher, MatchesPureMethod) {
1592 EXPECT_TRUE(matches("class X { virtual int f() = 0; };",
1593 methodDecl(isPure(), hasName("::X::f"))));
1594 EXPECT_TRUE(notMatches("class X { int f(); };",
1595 methodDecl(isPure())));
1596}
1597
Edwin Vanefc4f7dc2013-05-09 17:00:17 +00001598TEST(Matcher, MatchesConstMethod) {
1599 EXPECT_TRUE(matches("struct A { void foo() const; };",
1600 methodDecl(isConst())));
1601 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1602 methodDecl(isConst())));
1603}
1604
Edwin Vane37ee1d72013-04-09 20:46:36 +00001605TEST(Matcher, MatchesOverridingMethod) {
1606 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1607 "class Y : public X { int f(); };",
1608 methodDecl(isOverride(), hasName("::Y::f"))));
1609 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1610 "class Y : public X { int f(); };",
1611 methodDecl(isOverride(), hasName("::X::f"))));
1612 EXPECT_TRUE(notMatches("class X { int f(); }; "
1613 "class Y : public X { int f(); };",
1614 methodDecl(isOverride())));
1615 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1616 methodDecl(isOverride())));
1617}
1618
Manuel Klimek04616e42012-07-06 05:48:52 +00001619TEST(Matcher, ConstructorCall) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001620 StatementMatcher Constructor = constructExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001621
1622 EXPECT_TRUE(
1623 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1624 EXPECT_TRUE(
1625 matches("class X { public: X(); }; void x() { X x = X(); }",
1626 Constructor));
1627 EXPECT_TRUE(
1628 matches("class X { public: X(int); }; void x() { X x = 0; }",
1629 Constructor));
1630 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1631}
1632
1633TEST(Matcher, ConstructorArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001634 StatementMatcher Constructor = constructExpr(
1635 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001636
1637 EXPECT_TRUE(
1638 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1639 Constructor));
1640 EXPECT_TRUE(
1641 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1642 Constructor));
1643 EXPECT_TRUE(
1644 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1645 Constructor));
1646 EXPECT_TRUE(
1647 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1648 Constructor));
1649
Daniel Jasper848cbe12012-09-18 13:09:13 +00001650 StatementMatcher WrongIndex = constructExpr(
1651 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001652 EXPECT_TRUE(
1653 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1654 WrongIndex));
1655}
1656
1657TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001658 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001659
1660 EXPECT_TRUE(
1661 matches("class X { public: X(int); }; void x() { X x(0); }",
1662 Constructor1Arg));
1663 EXPECT_TRUE(
1664 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1665 Constructor1Arg));
1666 EXPECT_TRUE(
1667 matches("class X { public: X(int); }; void x() { X x = 0; }",
1668 Constructor1Arg));
1669 EXPECT_TRUE(
1670 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1671 Constructor1Arg));
1672}
1673
Peter Collingbourne1fec3df2014-02-06 21:52:24 +00001674TEST(Matcher, ConstructorListInitialization) {
1675 StatementMatcher ConstructorListInit = constructExpr(isListInitialization());
1676
1677 EXPECT_TRUE(
1678 matches("class X { public: X(int); }; void x() { X x{0}; }",
1679 ConstructorListInit));
1680 EXPECT_FALSE(
1681 matches("class X { public: X(int); }; void x() { X x(0); }",
1682 ConstructorListInit));
1683}
1684
Manuel Klimek7fca93b2012-10-23 10:40:50 +00001685TEST(Matcher,ThisExpr) {
1686 EXPECT_TRUE(
1687 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1688 EXPECT_TRUE(
1689 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1690}
1691
Manuel Klimek04616e42012-07-06 05:48:52 +00001692TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001693 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001694
1695 std::string ClassString = "class string { public: string(); ~string(); }; ";
1696
1697 EXPECT_TRUE(
1698 matches(ClassString +
1699 "string GetStringByValue();"
1700 "void FunctionTakesString(string s);"
1701 "void run() { FunctionTakesString(GetStringByValue()); }",
1702 TempExpression));
1703
1704 EXPECT_TRUE(
1705 notMatches(ClassString +
1706 "string* GetStringPointer(); "
1707 "void FunctionTakesStringPtr(string* s);"
1708 "void run() {"
1709 " string* s = GetStringPointer();"
1710 " FunctionTakesStringPtr(GetStringPointer());"
1711 " FunctionTakesStringPtr(s);"
1712 "}",
1713 TempExpression));
1714
1715 EXPECT_TRUE(
1716 notMatches("class no_dtor {};"
1717 "no_dtor GetObjByValue();"
1718 "void ConsumeObj(no_dtor param);"
1719 "void run() { ConsumeObj(GetObjByValue()); }",
1720 TempExpression));
1721}
1722
Sam Panzer68a35af2012-08-24 22:04:44 +00001723TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1724 std::string ClassString =
1725 "class string { public: string(); int length(); }; ";
1726
1727 EXPECT_TRUE(
1728 matches(ClassString +
1729 "string GetStringByValue();"
1730 "void FunctionTakesString(string s);"
1731 "void run() { FunctionTakesString(GetStringByValue()); }",
1732 materializeTemporaryExpr()));
1733
1734 EXPECT_TRUE(
1735 notMatches(ClassString +
1736 "string* GetStringPointer(); "
1737 "void FunctionTakesStringPtr(string* s);"
1738 "void run() {"
1739 " string* s = GetStringPointer();"
1740 " FunctionTakesStringPtr(GetStringPointer());"
1741 " FunctionTakesStringPtr(s);"
1742 "}",
1743 materializeTemporaryExpr()));
1744
1745 EXPECT_TRUE(
1746 notMatches(ClassString +
1747 "string GetStringByValue();"
1748 "void run() { int k = GetStringByValue().length(); }",
1749 materializeTemporaryExpr()));
1750
1751 EXPECT_TRUE(
1752 notMatches(ClassString +
1753 "string GetStringByValue();"
1754 "void run() { GetStringByValue(); }",
1755 materializeTemporaryExpr()));
1756}
1757
Manuel Klimek04616e42012-07-06 05:48:52 +00001758TEST(ConstructorDeclaration, SimpleCase) {
1759 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001760 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001761 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001762 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001763}
1764
1765TEST(ConstructorDeclaration, IsImplicit) {
1766 // This one doesn't match because the constructor is not added by the
1767 // compiler (it is not needed).
1768 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001769 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001770 // The compiler added the implicit default constructor.
1771 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001772 constructorDecl(isImplicit())));
Manuel Klimek04616e42012-07-06 05:48:52 +00001773 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001774 constructorDecl(unless(isImplicit()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001775}
1776
Daniel Jasper1dad1832012-07-10 20:20:19 +00001777TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1778 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001779 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001780}
1781
1782TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001783 EXPECT_TRUE(notMatches("class Foo {};",
1784 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001785}
1786
Manuel Klimek04616e42012-07-06 05:48:52 +00001787TEST(HasAnyConstructorInitializer, SimpleCase) {
1788 EXPECT_TRUE(notMatches(
1789 "class Foo { Foo() { } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001790 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001791 EXPECT_TRUE(matches(
1792 "class Foo {"
1793 " Foo() : foo_() { }"
1794 " int foo_;"
1795 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001796 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001797}
1798
1799TEST(HasAnyConstructorInitializer, ForField) {
1800 static const char Code[] =
1801 "class Baz { };"
1802 "class Foo {"
1803 " Foo() : foo_() { }"
1804 " Baz foo_;"
1805 " Baz bar_;"
1806 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001807 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1808 forField(hasType(recordDecl(hasName("Baz"))))))));
1809 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001810 forField(hasName("foo_"))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001811 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1812 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001813}
1814
1815TEST(HasAnyConstructorInitializer, WithInitializer) {
1816 static const char Code[] =
1817 "class Foo {"
1818 " Foo() : foo_(0) { }"
1819 " int foo_;"
1820 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001821 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001822 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001823 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001824 withInitializer(integerLiteral(equals(1)))))));
1825}
1826
1827TEST(HasAnyConstructorInitializer, IsWritten) {
1828 static const char Code[] =
1829 "struct Bar { Bar(){} };"
1830 "class Foo {"
1831 " Foo() : foo_() { }"
1832 " Bar foo_;"
1833 " Bar bar_;"
1834 "};";
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001835 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001836 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001837 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001838 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001839 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek04616e42012-07-06 05:48:52 +00001840 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1841}
1842
1843TEST(Matcher, NewExpression) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001844 StatementMatcher New = newExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001845
1846 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1847 EXPECT_TRUE(
1848 matches("class X { public: X(); }; void x() { new X(); }", New));
1849 EXPECT_TRUE(
1850 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1851 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1852}
1853
1854TEST(Matcher, NewExpressionArgument) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001855 StatementMatcher New = constructExpr(
1856 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001857
1858 EXPECT_TRUE(
1859 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1860 New));
1861 EXPECT_TRUE(
1862 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1863 New));
1864 EXPECT_TRUE(
1865 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1866 New));
1867
Daniel Jasper848cbe12012-09-18 13:09:13 +00001868 StatementMatcher WrongIndex = constructExpr(
1869 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00001870 EXPECT_TRUE(
1871 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1872 WrongIndex));
1873}
1874
1875TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001876 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek04616e42012-07-06 05:48:52 +00001877
1878 EXPECT_TRUE(
1879 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1880 EXPECT_TRUE(
1881 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1882 New));
1883}
1884
Daniel Jasper1dad1832012-07-10 20:20:19 +00001885TEST(Matcher, DeleteExpression) {
1886 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001887 deleteExpr()));
Daniel Jasper1dad1832012-07-10 20:20:19 +00001888}
1889
Manuel Klimek04616e42012-07-06 05:48:52 +00001890TEST(Matcher, DefaultArgument) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00001891 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek04616e42012-07-06 05:48:52 +00001892
1893 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1894 EXPECT_TRUE(
1895 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1896 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1897}
1898
1899TEST(Matcher, StringLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001900 StatementMatcher Literal = stringLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001901 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1902 // wide string
1903 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1904 // with escaped characters
1905 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1906 // no matching -- though the data type is the same, there is no string literal
1907 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1908}
1909
1910TEST(Matcher, CharacterLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001911 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001912 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1913 // wide character
1914 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1915 // wide character, Hex encoded, NOT MATCHED!
1916 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1917 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1918}
1919
1920TEST(Matcher, IntegerLiterals) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00001921 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek04616e42012-07-06 05:48:52 +00001922 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1923 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1924 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1925 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1926
1927 // Non-matching cases (character literals, float and double)
1928 EXPECT_TRUE(notMatches("int i = L'a';",
1929 HasIntLiteral)); // this is actually a character
1930 // literal cast to int
1931 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1932 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1933 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1934}
1935
Daniel Jasper91f1c8c2013-07-26 18:52:58 +00001936TEST(Matcher, FloatLiterals) {
1937 StatementMatcher HasFloatLiteral = floatLiteral();
1938 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1939 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1940 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1941 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1942 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1943
1944 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1945}
1946
Daniel Jasper5901e472012-10-01 13:40:41 +00001947TEST(Matcher, NullPtrLiteral) {
1948 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1949}
1950
Daniel Jasper87c3d362012-09-20 14:12:57 +00001951TEST(Matcher, AsmStatement) {
1952 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1953}
1954
Manuel Klimek04616e42012-07-06 05:48:52 +00001955TEST(Matcher, Conditions) {
1956 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1957
1958 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1959 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1960 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1961 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1962 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1963}
1964
1965TEST(MatchBinaryOperator, HasOperatorName) {
1966 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1967
1968 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1969 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1970}
1971
1972TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1973 StatementMatcher OperatorTrueFalse =
1974 binaryOperator(hasLHS(boolLiteral(equals(true))),
1975 hasRHS(boolLiteral(equals(false))));
1976
1977 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1978 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1979 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1980}
1981
1982TEST(MatchBinaryOperator, HasEitherOperand) {
1983 StatementMatcher HasOperand =
1984 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1985
1986 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1987 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1988 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1989}
1990
1991TEST(Matcher, BinaryOperatorTypes) {
1992 // Integration test that verifies the AST provides all binary operators in
1993 // a way we expect.
1994 // FIXME: Operator ','
1995 EXPECT_TRUE(
1996 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1997 EXPECT_TRUE(
1998 matches("bool b; bool c = (b = true);",
1999 binaryOperator(hasOperatorName("="))));
2000 EXPECT_TRUE(
2001 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
2002 EXPECT_TRUE(
2003 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
2004 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
2005 EXPECT_TRUE(
2006 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
2007 EXPECT_TRUE(
2008 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
2009 EXPECT_TRUE(
2010 matches("int i = 1; int j = (i <<= 2);",
2011 binaryOperator(hasOperatorName("<<="))));
2012 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
2013 EXPECT_TRUE(
2014 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
2015 EXPECT_TRUE(
2016 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
2017 EXPECT_TRUE(
2018 matches("int i = 1; int j = (i >>= 2);",
2019 binaryOperator(hasOperatorName(">>="))));
2020 EXPECT_TRUE(
2021 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
2022 EXPECT_TRUE(
2023 matches("int i = 42; int j = (i ^= 42);",
2024 binaryOperator(hasOperatorName("^="))));
2025 EXPECT_TRUE(
2026 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
2027 EXPECT_TRUE(
2028 matches("int i = 42; int j = (i %= 42);",
2029 binaryOperator(hasOperatorName("%="))));
2030 EXPECT_TRUE(
2031 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
2032 EXPECT_TRUE(
2033 matches("bool b = true && false;",
2034 binaryOperator(hasOperatorName("&&"))));
2035 EXPECT_TRUE(
2036 matches("bool b = true; bool c = (b &= false);",
2037 binaryOperator(hasOperatorName("&="))));
2038 EXPECT_TRUE(
2039 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
2040 EXPECT_TRUE(
2041 matches("bool b = true || false;",
2042 binaryOperator(hasOperatorName("||"))));
2043 EXPECT_TRUE(
2044 matches("bool b = true; bool c = (b |= false);",
2045 binaryOperator(hasOperatorName("|="))));
2046 EXPECT_TRUE(
2047 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2048 EXPECT_TRUE(
2049 matches("int i = 42; int j = (i *= 23);",
2050 binaryOperator(hasOperatorName("*="))));
2051 EXPECT_TRUE(
2052 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
2053 EXPECT_TRUE(
2054 matches("int i = 42; int j = (i /= 23);",
2055 binaryOperator(hasOperatorName("/="))));
2056 EXPECT_TRUE(
2057 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
2058 EXPECT_TRUE(
2059 matches("int i = 42; int j = (i += 23);",
2060 binaryOperator(hasOperatorName("+="))));
2061 EXPECT_TRUE(
2062 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
2063 EXPECT_TRUE(
2064 matches("int i = 42; int j = (i -= 23);",
2065 binaryOperator(hasOperatorName("-="))));
2066 EXPECT_TRUE(
2067 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2068 binaryOperator(hasOperatorName("->*"))));
2069 EXPECT_TRUE(
2070 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2071 binaryOperator(hasOperatorName(".*"))));
2072
2073 // Member expressions as operators are not supported in matches.
2074 EXPECT_TRUE(
2075 notMatches("struct A { void x(A *a) { a->x(this); } };",
2076 binaryOperator(hasOperatorName("->"))));
2077
2078 // Initializer assignments are not represented as operator equals.
2079 EXPECT_TRUE(
2080 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2081
2082 // Array indexing is not represented as operator.
2083 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2084
2085 // Overloaded operators do not match at all.
2086 EXPECT_TRUE(notMatches(
2087 "struct A { bool operator&&(const A &a) const { return false; } };"
2088 "void x() { A a, b; a && b; }",
2089 binaryOperator()));
2090}
2091
2092TEST(MatchUnaryOperator, HasOperatorName) {
2093 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2094
2095 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2096 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2097}
2098
2099TEST(MatchUnaryOperator, HasUnaryOperand) {
2100 StatementMatcher OperatorOnFalse =
2101 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2102
2103 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2104 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2105}
2106
2107TEST(Matcher, UnaryOperatorTypes) {
2108 // Integration test that verifies the AST provides all unary operators in
2109 // a way we expect.
2110 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2111 EXPECT_TRUE(
2112 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2113 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2114 EXPECT_TRUE(
2115 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2116 EXPECT_TRUE(
2117 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2118 EXPECT_TRUE(
2119 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2120 EXPECT_TRUE(
2121 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2122 EXPECT_TRUE(
2123 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2124 EXPECT_TRUE(
2125 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2126 EXPECT_TRUE(
2127 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2128
2129 // We don't match conversion operators.
2130 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2131
2132 // Function calls are not represented as operator.
2133 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2134
2135 // Overloaded operators do not match at all.
2136 // FIXME: We probably want to add that.
2137 EXPECT_TRUE(notMatches(
2138 "struct A { bool operator!() const { return false; } };"
2139 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2140}
2141
2142TEST(Matcher, ConditionalOperator) {
2143 StatementMatcher Conditional = conditionalOperator(
2144 hasCondition(boolLiteral(equals(true))),
2145 hasTrueExpression(boolLiteral(equals(false))));
2146
2147 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2148 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2149 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2150
2151 StatementMatcher ConditionalFalse = conditionalOperator(
2152 hasFalseExpression(boolLiteral(equals(false))));
2153
2154 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2155 EXPECT_TRUE(
2156 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2157}
2158
Daniel Jasper1dad1832012-07-10 20:20:19 +00002159TEST(ArraySubscriptMatchers, ArraySubscripts) {
2160 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2161 arraySubscriptExpr()));
2162 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2163 arraySubscriptExpr()));
2164}
2165
2166TEST(ArraySubscriptMatchers, ArrayIndex) {
2167 EXPECT_TRUE(matches(
2168 "int i[2]; void f() { i[1] = 1; }",
2169 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2170 EXPECT_TRUE(matches(
2171 "int i[2]; void f() { 1[i] = 1; }",
2172 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2173 EXPECT_TRUE(notMatches(
2174 "int i[2]; void f() { i[1] = 1; }",
2175 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2176}
2177
2178TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2179 EXPECT_TRUE(matches(
2180 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002181 arraySubscriptExpr(hasBase(implicitCastExpr(
2182 hasSourceExpression(declRefExpr()))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002183}
2184
Manuel Klimek04616e42012-07-06 05:48:52 +00002185TEST(Matcher, HasNameSupportsNamespaces) {
2186 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002187 recordDecl(hasName("a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002188 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002189 recordDecl(hasName("::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002190 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002191 recordDecl(hasName("b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002192 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002193 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002194 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002195 recordDecl(hasName("c::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002196 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002197 recordDecl(hasName("a::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002198 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002199 recordDecl(hasName("a::b::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002200 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002201 recordDecl(hasName("::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002202 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002203 recordDecl(hasName("::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002204 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002205 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002206 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002207 recordDecl(hasName("a+b::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002208 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002209 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002210}
2211
2212TEST(Matcher, HasNameSupportsOuterClasses) {
2213 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002214 matches("class A { class B { class C; }; };",
2215 recordDecl(hasName("A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002216 EXPECT_TRUE(
2217 matches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002218 recordDecl(hasName("::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002219 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002220 matches("class A { class B { class C; }; };",
2221 recordDecl(hasName("B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002222 EXPECT_TRUE(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002223 matches("class A { class B { class C; }; };",
2224 recordDecl(hasName("C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002225 EXPECT_TRUE(
2226 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002227 recordDecl(hasName("c::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002228 EXPECT_TRUE(
2229 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002230 recordDecl(hasName("A::c::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002231 EXPECT_TRUE(
2232 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002233 recordDecl(hasName("A::B::A"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002234 EXPECT_TRUE(
2235 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002236 recordDecl(hasName("::C"))));
2237 EXPECT_TRUE(
2238 notMatches("class A { class B { class C; }; };",
2239 recordDecl(hasName("::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002240 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002241 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002242 EXPECT_TRUE(
2243 notMatches("class A { class B { class C; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002244 recordDecl(hasName("A+B::C"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002245}
2246
2247TEST(Matcher, IsDefinition) {
2248 DeclarationMatcher DefinitionOfClassA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002249 recordDecl(hasName("A"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002250 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2251 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2252
2253 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002254 varDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002255 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2256 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2257
2258 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002259 methodDecl(hasName("a"), isDefinition());
Manuel Klimek04616e42012-07-06 05:48:52 +00002260 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2261 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2262}
2263
2264TEST(Matcher, OfClass) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002265 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek04616e42012-07-06 05:48:52 +00002266 ofClass(hasName("X")))));
2267
2268 EXPECT_TRUE(
2269 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2270 EXPECT_TRUE(
2271 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2272 Constructor));
2273 EXPECT_TRUE(
2274 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2275 Constructor));
2276}
2277
2278TEST(Matcher, VisitsTemplateInstantiations) {
2279 EXPECT_TRUE(matches(
2280 "class A { public: void x(); };"
2281 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002282 "void f() { B<A> b; b.y(); }",
2283 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002284
2285 EXPECT_TRUE(matches(
2286 "class A { public: void x(); };"
2287 "class C {"
2288 " public:"
2289 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2290 "};"
2291 "void f() {"
2292 " C::B<A> b; b.y();"
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002293 "}",
2294 recordDecl(hasName("C"),
2295 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002296}
2297
Daniel Jasper1dad1832012-07-10 20:20:19 +00002298TEST(Matcher, HandlesNullQualTypes) {
2299 // FIXME: Add a Type matcher so we can replace uses of this
2300 // variable with Type(True())
2301 const TypeMatcher AnyType = anything();
2302
2303 // We don't really care whether this matcher succeeds; we're testing that
2304 // it completes without crashing.
2305 EXPECT_TRUE(matches(
2306 "struct A { };"
2307 "template <typename T>"
2308 "void f(T t) {"
2309 " T local_t(t /* this becomes a null QualType in the AST */);"
2310 "}"
2311 "void g() {"
2312 " f(0);"
2313 "}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002314 expr(hasType(TypeMatcher(
Daniel Jasper1dad1832012-07-10 20:20:19 +00002315 anyOf(
2316 TypeMatcher(hasDeclaration(anything())),
2317 pointsTo(AnyType),
2318 references(AnyType)
2319 // Other QualType matchers should go here.
2320 ))))));
2321}
2322
Manuel Klimek04616e42012-07-06 05:48:52 +00002323// For testing AST_MATCHER_P().
Daniel Jasper1dad1832012-07-10 20:20:19 +00002324AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002325 // Make sure all special variables are used: node, match_finder,
2326 // bound_nodes_builder, and the parameter named 'AMatcher'.
2327 return AMatcher.matches(Node, Finder, Builder);
2328}
2329
2330TEST(AstMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002331 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002332
2333 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002334 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002335
2336 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002337 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002338
2339 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002340 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002341}
2342
2343AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenc6f2c9b2013-06-21 15:51:31 +00002344 polymorphicHas,
2345 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2346 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002347 return Finder->matchesChildOf(
Manuel Klimekeb958de2012-09-05 12:12:07 +00002348 Node, AMatcher, Builder,
Manuel Klimek04616e42012-07-06 05:48:52 +00002349 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2350 ASTMatchFinder::BK_First);
2351}
2352
2353TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002354 DeclarationMatcher HasClassB =
2355 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek04616e42012-07-06 05:48:52 +00002356
2357 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002358 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002359
2360 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002361 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002362
2363 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00002364 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002365
2366 StatementMatcher StatementHasClassB =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002367 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek04616e42012-07-06 05:48:52 +00002368
2369 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2370}
2371
2372TEST(For, FindsForLoops) {
2373 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2374 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper6f595392012-10-01 15:05:34 +00002375 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2376 "void f() { for (auto &a : as); }",
Daniel Jasper5901e472012-10-01 13:40:41 +00002377 forStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002378}
2379
Daniel Jasper4e566c42012-07-12 08:50:38 +00002380TEST(For, ForLoopInternals) {
2381 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2382 forStmt(hasCondition(anything()))));
2383 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2384 forStmt(hasLoopInit(anything()))));
2385}
2386
Alexander Kornienko9b539e12014-02-05 16:35:08 +00002387TEST(For, ForRangeLoopInternals) {
2388 EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2389 forRangeStmt(hasLoopVariable(anything()))));
2390}
2391
Daniel Jasper4e566c42012-07-12 08:50:38 +00002392TEST(For, NegativeForLoopInternals) {
2393 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002394 forStmt(hasCondition(expr()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002395 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2396 forStmt(hasLoopInit(anything()))));
2397}
2398
Manuel Klimek04616e42012-07-06 05:48:52 +00002399TEST(For, ReportsNoFalsePositives) {
2400 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2401 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2402}
2403
2404TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002405 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2406 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2407 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002408}
2409
2410TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2411 // It's not a compound statement just because there's "{}" in the source
2412 // text. This is an AST search, not grep.
2413 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002414 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002415 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002416 compoundStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002417}
2418
Daniel Jasper4e566c42012-07-12 08:50:38 +00002419TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002420 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002421 forStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002422 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002423 forStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002424 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002425 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper4e566c42012-07-12 08:50:38 +00002426 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002427 doStmt(hasBody(compoundStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002428}
2429
2430TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2431 // The simplest case: every compound statement is in a function
2432 // definition, and the function body itself must be a compound
2433 // statement.
2434 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002435 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002436}
2437
2438TEST(HasAnySubstatement, IsNotRecursive) {
2439 // It's really "has any immediate substatement".
2440 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002441 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002442}
2443
2444TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2445 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002446 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002447}
2448
2449TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2450 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002451 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002452}
2453
2454TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2455 EXPECT_TRUE(matches("void f() { }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002456 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002457 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002458 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002459}
2460
2461TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2462 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002463 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002464 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002465 compoundStmt(statementCountIs(0))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002466 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002467 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002468}
2469
2470TEST(StatementCountIs, WorksWithMultipleStatements) {
2471 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002472 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002473}
2474
2475TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2476 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002477 compoundStmt(statementCountIs(1))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002478 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002479 compoundStmt(statementCountIs(2))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002480 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002481 compoundStmt(statementCountIs(3))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002482 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002483 compoundStmt(statementCountIs(4))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002484}
2485
2486TEST(Member, WorksInSimplestCase) {
2487 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002488 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002489}
2490
2491TEST(Member, DoesNotMatchTheBaseExpression) {
2492 // Don't pick out the wrong part of the member expression, this should
2493 // be checking the member (name) only.
2494 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002495 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002496}
2497
2498TEST(Member, MatchesInMemberFunctionCall) {
2499 EXPECT_TRUE(matches("void f() {"
2500 " struct { void first() {}; } s;"
2501 " s.first();"
2502 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002503 memberExpr(member(hasName("first")))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002504}
2505
Daniel Jasperb0c7b612012-10-23 15:46:39 +00002506TEST(Member, MatchesMember) {
2507 EXPECT_TRUE(matches(
2508 "struct A { int i; }; void f() { A a; a.i = 2; }",
2509 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2510 EXPECT_TRUE(notMatches(
2511 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2512 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2513}
2514
Daniel Jasper639522c2013-02-25 12:02:08 +00002515TEST(Member, UnderstandsAccess) {
2516 EXPECT_TRUE(matches(
2517 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2518 EXPECT_TRUE(notMatches(
2519 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2520 EXPECT_TRUE(notMatches(
2521 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2522
2523 EXPECT_TRUE(notMatches(
2524 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2525 EXPECT_TRUE(notMatches(
2526 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2527 EXPECT_TRUE(matches(
2528 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2529
2530 EXPECT_TRUE(notMatches(
2531 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2532 EXPECT_TRUE(matches("class A { protected: int i; };",
2533 fieldDecl(isProtected(), hasName("i"))));
2534 EXPECT_TRUE(notMatches(
2535 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2536
2537 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2538 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2539 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2540 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2541}
2542
Dmitri Gribenko06963042012-08-18 00:29:27 +00002543TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper5901e472012-10-01 13:40:41 +00002544 // Fails in C++11 mode
2545 EXPECT_TRUE(matchesConditionally(
2546 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2547 "class X { void *operator new(std::size_t); };",
2548 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002549
2550 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002551 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002552
Daniel Jasper5901e472012-10-01 13:40:41 +00002553 // Fails in C++11 mode
2554 EXPECT_TRUE(matchesConditionally(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002555 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2556 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper5901e472012-10-01 13:40:41 +00002557 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko06963042012-08-18 00:29:27 +00002558}
2559
Manuel Klimek04616e42012-07-06 05:48:52 +00002560TEST(HasObjectExpression, DoesNotMatchMember) {
2561 EXPECT_TRUE(notMatches(
2562 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002563 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002564}
2565
2566TEST(HasObjectExpression, MatchesBaseOfVariable) {
2567 EXPECT_TRUE(matches(
2568 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002569 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002570 EXPECT_TRUE(matches(
2571 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002572 memberExpr(hasObjectExpression(
2573 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002574}
2575
2576TEST(HasObjectExpression,
2577 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2578 EXPECT_TRUE(matches(
2579 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002580 memberExpr(hasObjectExpression(
2581 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002582 EXPECT_TRUE(matches(
2583 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002584 memberExpr(hasObjectExpression(
2585 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002586}
2587
2588TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002589 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2590 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2591 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2592 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002593}
2594
2595TEST(Field, MatchesField) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002596 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002597}
2598
2599TEST(IsConstQualified, MatchesConstInt) {
2600 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002601 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002602}
2603
2604TEST(IsConstQualified, MatchesConstPointer) {
2605 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002606 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002607}
2608
2609TEST(IsConstQualified, MatchesThroughTypedef) {
2610 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002611 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002612 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002613 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002614}
2615
2616TEST(IsConstQualified, DoesNotMatchInappropriately) {
2617 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002618 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002619 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002620 varDecl(hasType(isConstQualified()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002621}
2622
Sam Panzer80c13772012-08-16 16:58:10 +00002623TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002624 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2625 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2626 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2627 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002628}
2629TEST(CastExpression, MatchesImplicitCasts) {
2630 // This test creates an implicit cast from int to char.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002631 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002632 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper848cbe12012-09-18 13:09:13 +00002633 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002634}
2635
2636TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002637 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2638 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2639 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2640 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer80c13772012-08-16 16:58:10 +00002641}
2642
Manuel Klimek04616e42012-07-06 05:48:52 +00002643TEST(ReinterpretCast, MatchesSimpleCase) {
2644 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002645 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002646}
2647
2648TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002649 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002650 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002651 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002652 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002653 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002654 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2655 "B b;"
2656 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002657 reinterpretCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002658}
2659
2660TEST(FunctionalCast, MatchesSimpleCase) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002661 std::string foo_class = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002662 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002663 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002664}
2665
2666TEST(FunctionalCast, DoesNotMatchOtherCasts) {
Ismail Pazarbasi1121de32014-01-17 21:08:52 +00002667 std::string FooClass = "class Foo { public: Foo(const char*); };";
Manuel Klimek04616e42012-07-06 05:48:52 +00002668 EXPECT_TRUE(
2669 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002670 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002671 EXPECT_TRUE(
2672 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002673 functionalCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002674}
2675
2676TEST(DynamicCast, MatchesSimpleCase) {
2677 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2678 "B b;"
2679 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002680 dynamicCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002681}
2682
2683TEST(StaticCast, MatchesSimpleCase) {
2684 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002685 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002686}
2687
2688TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper848cbe12012-09-18 13:09:13 +00002689 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002690 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002691 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002692 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002693 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002694 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2695 "B b;"
2696 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002697 staticCastExpr()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002698}
2699
Daniel Jasper417f7762012-09-18 13:36:17 +00002700TEST(CStyleCast, MatchesSimpleCase) {
2701 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2702}
2703
2704TEST(CStyleCast, DoesNotMatchOtherCasts) {
2705 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2706 "char q, *r = const_cast<char*>(&q);"
2707 "void* s = reinterpret_cast<char*>(&s);"
2708 "struct B { virtual ~B() {} }; struct D : B {};"
2709 "B b;"
2710 "D* t = dynamic_cast<D*>(&b);",
2711 cStyleCastExpr()));
2712}
2713
Manuel Klimek04616e42012-07-06 05:48:52 +00002714TEST(HasDestinationType, MatchesSimpleCase) {
2715 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002716 staticCastExpr(hasDestinationType(
2717 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002718}
2719
Sam Panzer80c13772012-08-16 16:58:10 +00002720TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2721 // This test creates an implicit const cast.
2722 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002723 implicitCastExpr(
2724 hasImplicitDestinationType(isInteger()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002725 // This test creates an implicit array-to-pointer cast.
2726 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002727 implicitCastExpr(hasImplicitDestinationType(
2728 pointsTo(TypeMatcher(anything()))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002729}
2730
2731TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2732 // This test creates an implicit cast from int to char.
2733 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002734 implicitCastExpr(hasImplicitDestinationType(
2735 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002736 // This test creates an implicit array-to-pointer cast.
2737 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002738 implicitCastExpr(hasImplicitDestinationType(
2739 unless(anything())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002740}
2741
2742TEST(ImplicitCast, MatchesSimpleCase) {
2743 // This test creates an implicit const cast.
2744 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002745 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002746 // This test creates an implicit cast from int to char.
2747 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002748 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002749 // This test creates an implicit array-to-pointer cast.
2750 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002751 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002752}
2753
2754TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002755 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer80c13772012-08-16 16:58:10 +00002756 // are present, and that it ignores explicit and paren casts.
2757
2758 // These two test cases have no casts.
2759 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002760 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002761 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002762 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002763
2764 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002765 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002766 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002767 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002768
2769 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002770 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer80c13772012-08-16 16:58:10 +00002771}
2772
2773TEST(IgnoringImpCasts, MatchesImpCasts) {
2774 // This test checks that ignoringImpCasts matches when implicit casts are
2775 // present and its inner matcher alone does not match.
2776 // Note that this test creates an implicit const cast.
2777 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002778 varDecl(hasInitializer(ignoringImpCasts(
2779 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002780 // This test creates an implict cast from int to char.
2781 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002782 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002783 integerLiteral(equals(0)))))));
2784}
2785
2786TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2787 // These tests verify that ignoringImpCasts does not match if the inner
2788 // matcher does not match.
2789 // Note that the first test creates an implicit const cast.
2790 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002791 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002792 unless(anything()))))));
2793 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002794 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002795 unless(anything()))))));
2796
2797 // These tests verify that ignoringImplictCasts does not look through explicit
2798 // casts or parentheses.
2799 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002800 varDecl(hasInitializer(ignoringImpCasts(
2801 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002802 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002803 varDecl(hasInitializer(ignoringImpCasts(
2804 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002805 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002806 varDecl(hasInitializer(ignoringImpCasts(
2807 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002808 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002809 varDecl(hasInitializer(ignoringImpCasts(
2810 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002811}
2812
2813TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2814 // This test verifies that expressions that do not have implicit casts
2815 // still match the inner matcher.
2816 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002817 varDecl(hasInitializer(ignoringImpCasts(
2818 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002819}
2820
2821TEST(IgnoringParenCasts, MatchesParenCasts) {
2822 // This test checks that ignoringParenCasts matches when parentheses and/or
2823 // casts are present and its inner matcher alone does not match.
2824 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002825 varDecl(hasInitializer(ignoringParenCasts(
2826 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002827 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002828 varDecl(hasInitializer(ignoringParenCasts(
2829 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002830
2831 // This test creates an implict cast from int to char in addition to the
2832 // parentheses.
2833 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002834 varDecl(hasInitializer(ignoringParenCasts(
2835 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002836
2837 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002838 varDecl(hasInitializer(ignoringParenCasts(
2839 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002840 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002841 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002842 integerLiteral(equals(0)))))));
2843}
2844
2845TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2846 // This test verifies that expressions that do not have any casts still match.
2847 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002848 varDecl(hasInitializer(ignoringParenCasts(
2849 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002850}
2851
2852TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2853 // These tests verify that ignoringImpCasts does not match if the inner
2854 // matcher does not match.
2855 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002856 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002857 unless(anything()))))));
2858
2859 // This test creates an implicit cast from int to char in addition to the
2860 // parentheses.
2861 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002862 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002863 unless(anything()))))));
2864
2865 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002866 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002867 unless(anything()))))));
2868}
2869
2870TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2871 // This test checks that ignoringParenAndImpCasts matches when
2872 // parentheses and/or implicit casts are present and its inner matcher alone
2873 // does not match.
2874 // Note that this test creates an implicit const cast.
2875 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002876 varDecl(hasInitializer(ignoringParenImpCasts(
2877 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002878 // This test creates an implicit cast from int to char.
2879 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002880 varDecl(hasInitializer(ignoringParenImpCasts(
2881 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002882}
2883
2884TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2885 // This test verifies that expressions that do not have parentheses or
2886 // implicit casts still match.
2887 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002888 varDecl(hasInitializer(ignoringParenImpCasts(
2889 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002890 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002891 varDecl(hasInitializer(ignoringParenImpCasts(
2892 integerLiteral(equals(0)))))));
Sam Panzer80c13772012-08-16 16:58:10 +00002893}
2894
2895TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2896 // These tests verify that ignoringParenImpCasts does not match if
2897 // the inner matcher does not match.
2898 // This test creates an implicit cast.
2899 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002900 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer80c13772012-08-16 16:58:10 +00002901 unless(anything()))))));
2902 // These tests verify that ignoringParenAndImplictCasts does not look
2903 // through explicit casts.
2904 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002905 varDecl(hasInitializer(ignoringParenImpCasts(
2906 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002907 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002908 varDecl(hasInitializer(ignoringParenImpCasts(
2909 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002910 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002911 varDecl(hasInitializer(ignoringParenImpCasts(
2912 integerLiteral())))));
Sam Panzer80c13772012-08-16 16:58:10 +00002913}
2914
Manuel Klimeke9235692012-07-25 10:02:02 +00002915TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek04616e42012-07-06 05:48:52 +00002916 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2917 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002918 implicitCastExpr(
2919 hasSourceExpression(constructExpr()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00002920}
2921
Manuel Klimeke9235692012-07-25 10:02:02 +00002922TEST(HasSourceExpression, MatchesExplicitCasts) {
2923 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper848cbe12012-09-18 13:09:13 +00002924 explicitCastExpr(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002925 hasSourceExpression(hasDescendant(
Daniel Jasper848cbe12012-09-18 13:09:13 +00002926 expr(integerLiteral()))))));
Manuel Klimeke9235692012-07-25 10:02:02 +00002927}
2928
Manuel Klimek04616e42012-07-06 05:48:52 +00002929TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002930 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002931}
2932
2933TEST(Statement, MatchesCompoundStatments) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002934 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002935}
2936
2937TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002938 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002939}
2940
2941TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002942 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek04616e42012-07-06 05:48:52 +00002943}
2944
Daniel Jasper1dad1832012-07-10 20:20:19 +00002945TEST(InitListExpression, MatchesInitListExpression) {
2946 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2947 initListExpr(hasType(asString("int [2]")))));
2948 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002949 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002950}
2951
2952TEST(UsingDeclaration, MatchesUsingDeclarations) {
2953 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2954 usingDecl()));
2955}
2956
2957TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2958 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2959 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2960}
2961
2962TEST(UsingDeclaration, MatchesSpecificTarget) {
2963 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2964 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002965 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002966 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2967 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002968 hasTargetDecl(functionDecl())))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002969}
2970
2971TEST(UsingDeclaration, ThroughUsingDeclaration) {
2972 EXPECT_TRUE(matches(
2973 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002974 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002975 EXPECT_TRUE(notMatches(
2976 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002977 declRefExpr(throughUsingDecl(anything()))));
Daniel Jasper1dad1832012-07-10 20:20:19 +00002978}
2979
Sam Panzerd624bfb2012-08-16 17:20:59 +00002980TEST(SingleDecl, IsSingleDecl) {
2981 StatementMatcher SingleDeclStmt =
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002982 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002983 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2984 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2985 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2986 SingleDeclStmt));
2987}
2988
2989TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002990 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002991
2992 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002993 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002994 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002995 declStmt(containsDeclaration(0, MatchesInit),
2996 containsDeclaration(1, MatchesInit))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00002997 unsigned WrongIndex = 42;
2998 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00002999 declStmt(containsDeclaration(WrongIndex,
Sam Panzerd624bfb2012-08-16 17:20:59 +00003000 MatchesInit))));
3001}
3002
3003TEST(DeclCount, DeclCountIsCorrect) {
3004 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003005 declStmt(declCountIs(2))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003006 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003007 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003008 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003009 declStmt(declCountIs(3))));
Sam Panzerd624bfb2012-08-16 17:20:59 +00003010}
3011
Manuel Klimek04616e42012-07-06 05:48:52 +00003012TEST(While, MatchesWhileLoops) {
3013 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
3014 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
3015 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
3016}
3017
3018TEST(Do, MatchesDoLoops) {
3019 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
3020 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
3021}
3022
3023TEST(Do, DoesNotMatchWhileLoops) {
3024 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
3025}
3026
3027TEST(SwitchCase, MatchesCase) {
3028 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
3029 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
3030 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
3031 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
3032}
3033
Daniel Jasper87c3d362012-09-20 14:12:57 +00003034TEST(SwitchCase, MatchesSwitch) {
3035 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
3036 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
3037 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
3038 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
3039}
3040
Peter Collingbourne3154a102013-05-10 11:52:02 +00003041TEST(SwitchCase, MatchesEachCase) {
3042 EXPECT_TRUE(notMatches("void x() { switch(42); }",
3043 switchStmt(forEachSwitchCase(caseStmt()))));
3044 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
3045 switchStmt(forEachSwitchCase(caseStmt()))));
3046 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
3047 switchStmt(forEachSwitchCase(caseStmt()))));
3048 EXPECT_TRUE(notMatches(
3049 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3050 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3051 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3052 switchStmt(forEachSwitchCase(
3053 caseStmt(hasCaseConstant(integerLiteral()))))));
3054 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3055 switchStmt(forEachSwitchCase(
3056 caseStmt(hasCaseConstant(integerLiteral()))))));
3057 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3058 switchStmt(forEachSwitchCase(
3059 caseStmt(hasCaseConstant(integerLiteral()))))));
3060 EXPECT_TRUE(matchAndVerifyResultTrue(
3061 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3062 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3063 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3064}
3065
Manuel Klimekba46fc02013-07-19 11:50:54 +00003066TEST(ForEachConstructorInitializer, MatchesInitializers) {
3067 EXPECT_TRUE(matches(
3068 "struct X { X() : i(42), j(42) {} int i, j; };",
3069 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3070}
3071
Daniel Jasper87c3d362012-09-20 14:12:57 +00003072TEST(ExceptionHandling, SimpleCases) {
3073 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3074 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3075 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3076 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3077 throwExpr()));
3078 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3079 throwExpr()));
3080}
3081
Manuel Klimek04616e42012-07-06 05:48:52 +00003082TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3083 EXPECT_TRUE(notMatches(
3084 "void x() { if(true) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003085 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003086 EXPECT_TRUE(notMatches(
3087 "void x() { int x; if((x = 42)) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003088 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003089}
3090
3091TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3092 EXPECT_TRUE(matches(
3093 "void x() { if(int* a = 0) {} }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003094 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003095}
3096
3097TEST(ForEach, BindsOneNode) {
3098 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003099 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003100 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003101}
3102
3103TEST(ForEach, BindsMultipleNodes) {
3104 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003105 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003106 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003107}
3108
3109TEST(ForEach, BindsRecursiveCombinations) {
3110 EXPECT_TRUE(matchAndVerifyResultTrue(
3111 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003112 recordDecl(hasName("C"),
3113 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003114 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003115}
3116
3117TEST(ForEachDescendant, BindsOneNode) {
3118 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003119 recordDecl(hasName("C"),
3120 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003121 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003122}
3123
Daniel Jasper94a56852012-11-16 18:39:22 +00003124TEST(ForEachDescendant, NestedForEachDescendant) {
3125 DeclarationMatcher m = recordDecl(
3126 isDefinition(), decl().bind("x"), hasName("C"));
3127 EXPECT_TRUE(matchAndVerifyResultTrue(
3128 "class A { class B { class C {}; }; };",
3129 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3130 new VerifyIdIsBoundTo<Decl>("x", "C")));
3131
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003132 // Check that a partial match of 'm' that binds 'x' in the
3133 // first part of anyOf(m, anything()) will not overwrite the
3134 // binding created by the earlier binding in the hasDescendant.
3135 EXPECT_TRUE(matchAndVerifyResultTrue(
3136 "class A { class B { class C {}; }; };",
3137 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3138 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper94a56852012-11-16 18:39:22 +00003139}
3140
Manuel Klimek04616e42012-07-06 05:48:52 +00003141TEST(ForEachDescendant, BindsMultipleNodes) {
3142 EXPECT_TRUE(matchAndVerifyResultTrue(
3143 "class C { class D { int x; int y; }; "
3144 " class E { class F { int y; int z; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003145 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003146 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003147}
3148
3149TEST(ForEachDescendant, BindsRecursiveCombinations) {
3150 EXPECT_TRUE(matchAndVerifyResultTrue(
3151 "class C { class D { "
3152 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003153 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3154 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003155 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek04616e42012-07-06 05:48:52 +00003156}
3157
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003158TEST(ForEachDescendant, BindsCombinations) {
3159 EXPECT_TRUE(matchAndVerifyResultTrue(
3160 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3161 "(true) {} }",
3162 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3163 forEachDescendant(whileStmt().bind("while"))),
3164 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3165}
3166
3167TEST(Has, DoesNotDeleteBindings) {
3168 EXPECT_TRUE(matchAndVerifyResultTrue(
3169 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3170 new VerifyIdIsBoundTo<Decl>("x", 1)));
3171}
3172
3173TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3174 // Those matchers cover all the cases where an inner matcher is called
3175 // and there is not a 1:1 relationship between the match of the outer
3176 // matcher and the match of the inner matcher.
3177 // The pattern to look for is:
3178 // ... return InnerMatcher.matches(...); ...
3179 // In which case no special handling is needed.
3180 //
3181 // On the other hand, if there are multiple alternative matches
3182 // (for example forEach*) or matches might be discarded (for example has*)
3183 // the implementation must make sure that the discarded matches do not
3184 // affect the bindings.
3185 // When new such matchers are added, add a test here that:
3186 // - matches a simple node, and binds it as the first thing in the matcher:
3187 // recordDecl(decl().bind("x"), hasName("X")))
3188 // - uses the matcher under test afterwards in a way that not the first
3189 // alternative is matched; for anyOf, that means the first branch
3190 // would need to return false; for hasAncestor, it means that not
3191 // the direct parent matches the inner matcher.
3192
3193 EXPECT_TRUE(matchAndVerifyResultTrue(
3194 "class X { int y; };",
3195 recordDecl(
3196 recordDecl().bind("x"), hasName("::X"),
3197 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3198 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3199 EXPECT_TRUE(matchAndVerifyResultTrue(
3200 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3201 anyOf(unless(anything()), anything())),
3202 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3203 EXPECT_TRUE(matchAndVerifyResultTrue(
3204 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3205 classTemplateSpecializationDecl(
3206 decl().bind("x"),
3207 hasAnyTemplateArgument(refersToType(asString("int")))),
3208 new VerifyIdIsBoundTo<Decl>("x", 1)));
3209 EXPECT_TRUE(matchAndVerifyResultTrue(
3210 "class X { void f(); void g(); };",
3211 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3212 new VerifyIdIsBoundTo<Decl>("x", 1)));
3213 EXPECT_TRUE(matchAndVerifyResultTrue(
3214 "class X { X() : a(1), b(2) {} double a; int b; };",
3215 recordDecl(decl().bind("x"),
3216 has(constructorDecl(
3217 hasAnyConstructorInitializer(forField(hasName("b")))))),
3218 new VerifyIdIsBoundTo<Decl>("x", 1)));
3219 EXPECT_TRUE(matchAndVerifyResultTrue(
3220 "void x(int, int) { x(0, 42); }",
3221 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3222 new VerifyIdIsBoundTo<Expr>("x", 1)));
3223 EXPECT_TRUE(matchAndVerifyResultTrue(
3224 "void x(int, int y) {}",
3225 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3226 new VerifyIdIsBoundTo<Decl>("x", 1)));
3227 EXPECT_TRUE(matchAndVerifyResultTrue(
3228 "void x() { return; if (true) {} }",
3229 functionDecl(decl().bind("x"),
3230 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3231 new VerifyIdIsBoundTo<Decl>("x", 1)));
3232 EXPECT_TRUE(matchAndVerifyResultTrue(
3233 "namespace X { void b(int); void b(); }"
3234 "using X::b;",
3235 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3236 functionDecl(parameterCountIs(1))))),
3237 new VerifyIdIsBoundTo<Decl>("x", 1)));
3238 EXPECT_TRUE(matchAndVerifyResultTrue(
3239 "class A{}; class B{}; class C : B, A {};",
3240 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3241 new VerifyIdIsBoundTo<Decl>("x", 1)));
3242 EXPECT_TRUE(matchAndVerifyResultTrue(
3243 "class A{}; typedef A B; typedef A C; typedef A D;"
3244 "class E : A {};",
3245 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3246 new VerifyIdIsBoundTo<Decl>("x", 1)));
3247 EXPECT_TRUE(matchAndVerifyResultTrue(
3248 "class A { class B { void f() {} }; };",
3249 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3250 new VerifyIdIsBoundTo<Decl>("x", 1)));
3251 EXPECT_TRUE(matchAndVerifyResultTrue(
3252 "template <typename T> struct A { struct B {"
3253 " void f() { if(true) {} }"
3254 "}; };"
3255 "void t() { A<int>::B b; b.f(); }",
3256 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3257 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3258 EXPECT_TRUE(matchAndVerifyResultTrue(
3259 "class A {};",
3260 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3261 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimekba46fc02013-07-19 11:50:54 +00003262 EXPECT_TRUE(matchAndVerifyResultTrue(
3263 "class A { A() : s(), i(42) {} const char *s; int i; };",
3264 constructorDecl(hasName("::A::A"), decl().bind("x"),
3265 forEachConstructorInitializer(forField(hasName("i")))),
3266 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimeka0c025f2013-06-19 15:42:45 +00003267}
3268
Daniel Jasper33806cd2012-11-11 22:14:55 +00003269TEST(ForEachDescendant, BindsCorrectNodes) {
3270 EXPECT_TRUE(matchAndVerifyResultTrue(
3271 "class C { void f(); int i; };",
3272 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3273 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3274 EXPECT_TRUE(matchAndVerifyResultTrue(
3275 "class C { void f() {} int i; };",
3276 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3277 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3278}
3279
Manuel Klimekabf43712013-02-04 10:59:20 +00003280TEST(FindAll, BindsNodeOnMatch) {
3281 EXPECT_TRUE(matchAndVerifyResultTrue(
3282 "class A {};",
3283 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3284 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3285}
3286
3287TEST(FindAll, BindsDescendantNodeOnMatch) {
3288 EXPECT_TRUE(matchAndVerifyResultTrue(
3289 "class A { int a; int b; };",
3290 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3291 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3292}
3293
3294TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3295 EXPECT_TRUE(matchAndVerifyResultTrue(
3296 "class A { int a; int b; };",
3297 recordDecl(hasName("::A"),
3298 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3299 fieldDecl().bind("v"))))),
3300 new VerifyIdIsBoundTo<Decl>("v", 3)));
3301
3302 EXPECT_TRUE(matchAndVerifyResultTrue(
3303 "class A { class B {}; class C {}; };",
3304 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3305 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3306}
3307
Manuel Klimek88b95872013-02-04 09:42:38 +00003308TEST(EachOf, TriggersForEachMatch) {
3309 EXPECT_TRUE(matchAndVerifyResultTrue(
3310 "class A { int a; int b; };",
3311 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3312 has(fieldDecl(hasName("b")).bind("v")))),
3313 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3314}
3315
3316TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3317 EXPECT_TRUE(matchAndVerifyResultTrue(
3318 "class A { int a; int c; };",
3319 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3320 has(fieldDecl(hasName("b")).bind("v")))),
3321 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3322 EXPECT_TRUE(matchAndVerifyResultTrue(
3323 "class A { int c; int b; };",
3324 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3325 has(fieldDecl(hasName("b")).bind("v")))),
3326 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3327 EXPECT_TRUE(notMatches(
3328 "class A { int c; int d; };",
3329 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3330 has(fieldDecl(hasName("b")).bind("v"))))));
3331}
Manuel Klimek04616e42012-07-06 05:48:52 +00003332
3333TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3334 // Make sure that we can both match the class by name (::X) and by the type
3335 // the template was instantiated with (via a field).
3336
3337 EXPECT_TRUE(matches(
3338 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003339 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003340
3341 EXPECT_TRUE(matches(
3342 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003343 recordDecl(isTemplateInstantiation(), hasDescendant(
3344 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003345}
3346
3347TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3348 EXPECT_TRUE(matches(
3349 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003350 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek04616e42012-07-06 05:48:52 +00003351 isTemplateInstantiation())));
3352}
3353
3354TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3355 EXPECT_TRUE(matches(
3356 "template <typename T> class X { T t; }; class A {};"
3357 "template class X<A>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003358 recordDecl(isTemplateInstantiation(), hasDescendant(
3359 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003360}
3361
3362TEST(IsTemplateInstantiation,
3363 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3364 EXPECT_TRUE(matches(
3365 "template <typename T> class X {};"
3366 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003367 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003368}
3369
3370TEST(IsTemplateInstantiation,
3371 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3372 EXPECT_TRUE(matches(
3373 "class A {};"
3374 "class X {"
3375 " template <typename U> class Y { U u; };"
3376 " Y<A> y;"
3377 "};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003378 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003379}
3380
3381TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3382 // FIXME: Figure out whether this makes sense. It doesn't affect the
3383 // normal use case as long as the uppermost instantiation always is marked
3384 // as template instantiation, but it might be confusing as a predicate.
3385 EXPECT_TRUE(matches(
3386 "class A {};"
3387 "template <typename T> class X {"
3388 " template <typename U> class Y { U u; };"
3389 " Y<T> y;"
3390 "}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003391 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek04616e42012-07-06 05:48:52 +00003392}
3393
3394TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3395 EXPECT_TRUE(notMatches(
3396 "template <typename T> class X {}; class A {};"
3397 "template <> class X<A> {}; X<A> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003398 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003399}
3400
3401TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3402 EXPECT_TRUE(notMatches(
3403 "class A {}; class Y { A a; };",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003404 recordDecl(isTemplateInstantiation())));
Manuel Klimek04616e42012-07-06 05:48:52 +00003405}
3406
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003407TEST(IsExplicitTemplateSpecialization,
3408 DoesNotMatchPrimaryTemplate) {
3409 EXPECT_TRUE(notMatches(
3410 "template <typename T> class X {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003411 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003412 EXPECT_TRUE(notMatches(
3413 "template <typename T> void f(T t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003414 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003415}
3416
3417TEST(IsExplicitTemplateSpecialization,
3418 DoesNotMatchExplicitTemplateInstantiations) {
3419 EXPECT_TRUE(notMatches(
3420 "template <typename T> class X {};"
3421 "template class X<int>; extern template class X<long>;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003422 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003423 EXPECT_TRUE(notMatches(
3424 "template <typename T> void f(T t) {}"
3425 "template void f(int t); extern template void f(long t);",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003426 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003427}
3428
3429TEST(IsExplicitTemplateSpecialization,
3430 DoesNotMatchImplicitTemplateInstantiations) {
3431 EXPECT_TRUE(notMatches(
3432 "template <typename T> class X {}; X<int> x;",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003433 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003434 EXPECT_TRUE(notMatches(
3435 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003436 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003437}
3438
3439TEST(IsExplicitTemplateSpecialization,
3440 MatchesExplicitTemplateSpecializations) {
3441 EXPECT_TRUE(matches(
3442 "template <typename T> class X {};"
3443 "template<> class X<int> {};",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003444 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003445 EXPECT_TRUE(matches(
3446 "template <typename T> void f(T t) {}"
3447 "template<> void f(int t) {}",
Daniel Jasperbd3d76d2012-08-24 05:12:34 +00003448 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenkod394c8a2012-08-17 18:42:47 +00003449}
3450
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003451TEST(HasAncenstor, MatchesDeclarationAncestors) {
3452 EXPECT_TRUE(matches(
3453 "class A { class B { class C {}; }; };",
3454 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3455}
3456
3457TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3458 EXPECT_TRUE(notMatches(
3459 "class A { class B { class C {}; }; };",
3460 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3461}
3462
3463TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3464 EXPECT_TRUE(matches(
3465 "class A { class B { void f() { C c; } class C {}; }; };",
3466 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3467 hasAncestor(recordDecl(hasName("A"))))))));
3468}
3469
3470TEST(HasAncenstor, MatchesStatementAncestors) {
3471 EXPECT_TRUE(matches(
3472 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003473 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003474}
3475
3476TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3477 EXPECT_TRUE(matches(
3478 "void f() { if (true) { int x = 42; } }",
Daniel Jasper848cbe12012-09-18 13:09:13 +00003479 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003480}
3481
3482TEST(HasAncestor, BindsRecursiveCombinations) {
3483 EXPECT_TRUE(matchAndVerifyResultTrue(
3484 "class C { class D { class E { class F { int y; }; }; }; };",
3485 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003486 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003487}
3488
3489TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3490 EXPECT_TRUE(matchAndVerifyResultTrue(
3491 "class C { class D { class E { class F { int y; }; }; }; };",
3492 fieldDecl(hasAncestor(
3493 decl(
3494 hasDescendant(recordDecl(isDefinition(),
3495 hasAncestor(recordDecl())))
3496 ).bind("d")
3497 )),
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003498 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003499}
3500
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003501TEST(HasAncestor, MatchesClosestAncestor) {
3502 EXPECT_TRUE(matchAndVerifyResultTrue(
3503 "template <typename T> struct C {"
3504 " void f(int) {"
3505 " struct I { void g(T) { int x; } } i; i.g(42);"
3506 " }"
3507 "};"
3508 "template struct C<int>;",
3509 varDecl(hasName("x"),
3510 hasAncestor(functionDecl(hasParameter(
3511 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3512 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3513}
3514
Manuel Klimek3ca12c52012-09-07 09:26:10 +00003515TEST(HasAncestor, MatchesInTemplateInstantiations) {
3516 EXPECT_TRUE(matches(
3517 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3518 "A<int>::B::C a;",
3519 fieldDecl(hasType(asString("int")),
3520 hasAncestor(recordDecl(hasName("A"))))));
3521}
3522
3523TEST(HasAncestor, MatchesInImplicitCode) {
3524 EXPECT_TRUE(matches(
3525 "struct X {}; struct A { A() {} X x; };",
3526 constructorDecl(
3527 hasAnyConstructorInitializer(withInitializer(expr(
3528 hasAncestor(recordDecl(hasName("A")))))))));
3529}
3530
Daniel Jasper632aea92012-10-22 16:26:51 +00003531TEST(HasParent, MatchesOnlyParent) {
3532 EXPECT_TRUE(matches(
3533 "void f() { if (true) { int x = 42; } }",
3534 compoundStmt(hasParent(ifStmt()))));
3535 EXPECT_TRUE(notMatches(
3536 "void f() { for (;;) { int x = 42; } }",
3537 compoundStmt(hasParent(ifStmt()))));
3538 EXPECT_TRUE(notMatches(
3539 "void f() { if (true) for (;;) { int x = 42; } }",
3540 compoundStmt(hasParent(ifStmt()))));
3541}
3542
Manuel Klimekc844a462012-12-06 14:42:48 +00003543TEST(HasAncestor, MatchesAllAncestors) {
3544 EXPECT_TRUE(matches(
3545 "template <typename T> struct C { static void f() { 42; } };"
3546 "void t() { C<int>::f(); }",
3547 integerLiteral(
3548 equals(42),
3549 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3550 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3551}
3552
3553TEST(HasParent, MatchesAllParents) {
3554 EXPECT_TRUE(matches(
3555 "template <typename T> struct C { static void f() { 42; } };"
3556 "void t() { C<int>::f(); }",
3557 integerLiteral(
3558 equals(42),
3559 hasParent(compoundStmt(hasParent(functionDecl(
3560 hasParent(recordDecl(isTemplateInstantiation())))))))));
3561 EXPECT_TRUE(matches(
3562 "template <typename T> struct C { static void f() { 42; } };"
3563 "void t() { C<int>::f(); }",
3564 integerLiteral(
3565 equals(42),
3566 hasParent(compoundStmt(hasParent(functionDecl(
3567 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3568 EXPECT_TRUE(matches(
3569 "template <typename T> struct C { static void f() { 42; } };"
3570 "void t() { C<int>::f(); }",
3571 integerLiteral(equals(42),
3572 hasParent(compoundStmt(allOf(
3573 hasParent(functionDecl(
3574 hasParent(recordDecl(isTemplateInstantiation())))),
3575 hasParent(functionDecl(hasParent(recordDecl(
3576 unless(isTemplateInstantiation())))))))))));
Manuel Klimekb64d6b72013-03-14 16:33:21 +00003577 EXPECT_TRUE(
3578 notMatches("template <typename T> struct C { static void f() {} };"
3579 "void t() { C<int>::f(); }",
3580 compoundStmt(hasParent(recordDecl()))));
Manuel Klimekc844a462012-12-06 14:42:48 +00003581}
3582
Daniel Jasper516b02e2012-10-17 08:52:59 +00003583TEST(TypeMatching, MatchesTypes) {
3584 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3585}
3586
3587TEST(TypeMatching, MatchesArrayTypes) {
3588 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3589 EXPECT_TRUE(matches("int a[42];", arrayType()));
3590 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3591
3592 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3593 arrayType(hasElementType(builtinType()))));
3594
3595 EXPECT_TRUE(matches(
3596 "int const a[] = { 2, 3 };",
3597 qualType(arrayType(hasElementType(builtinType())))));
3598 EXPECT_TRUE(matches(
3599 "int const a[] = { 2, 3 };",
3600 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3601 EXPECT_TRUE(matches(
3602 "typedef const int T; T x[] = { 1, 2 };",
3603 qualType(isConstQualified(), arrayType())));
3604
3605 EXPECT_TRUE(notMatches(
3606 "int a[] = { 2, 3 };",
3607 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3608 EXPECT_TRUE(notMatches(
3609 "int a[] = { 2, 3 };",
3610 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3611 EXPECT_TRUE(notMatches(
3612 "int const a[] = { 2, 3 };",
3613 qualType(arrayType(hasElementType(builtinType())),
3614 unless(isConstQualified()))));
3615
3616 EXPECT_TRUE(matches("int a[2];",
3617 constantArrayType(hasElementType(builtinType()))));
3618 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3619}
3620
3621TEST(TypeMatching, MatchesComplexTypes) {
3622 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3623 EXPECT_TRUE(matches(
3624 "_Complex float f;",
3625 complexType(hasElementType(builtinType()))));
3626 EXPECT_TRUE(notMatches(
3627 "_Complex float f;",
3628 complexType(hasElementType(isInteger()))));
3629}
3630
3631TEST(TypeMatching, MatchesConstantArrayTypes) {
3632 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3633 EXPECT_TRUE(notMatches(
3634 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3635 constantArrayType(hasElementType(builtinType()))));
3636
3637 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3638 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3639 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3640}
3641
3642TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3643 EXPECT_TRUE(matches(
3644 "template <typename T, int Size> class array { T data[Size]; };",
3645 dependentSizedArrayType()));
3646 EXPECT_TRUE(notMatches(
3647 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3648 dependentSizedArrayType()));
3649}
3650
3651TEST(TypeMatching, MatchesIncompleteArrayType) {
3652 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3653 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3654
3655 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3656 incompleteArrayType()));
3657}
3658
3659TEST(TypeMatching, MatchesVariableArrayType) {
3660 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3661 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3662
3663 EXPECT_TRUE(matches(
3664 "void f(int b) { int a[b]; }",
3665 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3666 varDecl(hasName("b")))))))));
3667}
3668
3669TEST(TypeMatching, MatchesAtomicTypes) {
3670 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3671
3672 EXPECT_TRUE(matches("_Atomic(int) i;",
3673 atomicType(hasValueType(isInteger()))));
3674 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3675 atomicType(hasValueType(isInteger()))));
3676}
3677
3678TEST(TypeMatching, MatchesAutoTypes) {
3679 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3680 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3681 autoType()));
3682
Richard Smith061f1e22013-04-30 21:23:01 +00003683 // FIXME: Matching against the type-as-written can't work here, because the
3684 // type as written was not deduced.
3685 //EXPECT_TRUE(matches("auto a = 1;",
3686 // autoType(hasDeducedType(isInteger()))));
3687 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3688 // autoType(hasDeducedType(isInteger()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003689}
3690
Daniel Jasperd29d5fa2012-10-29 10:14:44 +00003691TEST(TypeMatching, MatchesFunctionTypes) {
3692 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3693 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3694}
3695
Edwin Vaneec074802013-04-01 18:33:34 +00003696TEST(TypeMatching, MatchesParenType) {
3697 EXPECT_TRUE(
3698 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3699 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3700
3701 EXPECT_TRUE(matches(
3702 "int (*ptr_to_func)(int);",
3703 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3704 EXPECT_TRUE(notMatches(
3705 "int (*ptr_to_array)[4];",
3706 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3707}
3708
Daniel Jasper516b02e2012-10-17 08:52:59 +00003709TEST(TypeMatching, PointerTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003710 // FIXME: Reactive when these tests can be more specific (not matching
3711 // implicit code on certain platforms), likely when we have hasDescendant for
3712 // Types/TypeLocs.
3713 //EXPECT_TRUE(matchAndVerifyResultTrue(
3714 // "int* a;",
3715 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3716 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3717 //EXPECT_TRUE(matchAndVerifyResultTrue(
3718 // "int* a;",
3719 // pointerTypeLoc().bind("loc"),
3720 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003721 EXPECT_TRUE(matches(
3722 "int** a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003723 loc(pointerType(pointee(qualType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003724 EXPECT_TRUE(matches(
3725 "int** a;",
3726 loc(pointerType(pointee(pointerType())))));
3727 EXPECT_TRUE(matches(
3728 "int* b; int* * const a = &b;",
3729 loc(qualType(isConstQualified(), pointerType()))));
3730
3731 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper7943eb52012-10-17 13:35:36 +00003732 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3733 hasType(blockPointerType()))));
3734 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3735 hasType(memberPointerType()))));
3736 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3737 hasType(pointerType()))));
3738 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3739 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003740 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3741 hasType(lValueReferenceType()))));
3742 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3743 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003744
Daniel Jasper7943eb52012-10-17 13:35:36 +00003745 Fragment = "int *ptr;";
3746 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3747 hasType(blockPointerType()))));
3748 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3749 hasType(memberPointerType()))));
3750 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3751 hasType(pointerType()))));
3752 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3753 hasType(referenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003754
Daniel Jasper7943eb52012-10-17 13:35:36 +00003755 Fragment = "int a; int &ref = a;";
3756 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3757 hasType(blockPointerType()))));
3758 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3759 hasType(memberPointerType()))));
3760 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3761 hasType(pointerType()))));
3762 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3763 hasType(referenceType()))));
Edwin Vane2a760d02013-03-07 15:44:40 +00003764 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3765 hasType(lValueReferenceType()))));
3766 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3767 hasType(rValueReferenceType()))));
3768
3769 Fragment = "int &&ref = 2;";
3770 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3771 hasType(blockPointerType()))));
3772 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3773 hasType(memberPointerType()))));
3774 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3775 hasType(pointerType()))));
3776 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3777 hasType(referenceType()))));
3778 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3779 hasType(lValueReferenceType()))));
3780 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3781 hasType(rValueReferenceType()))));
3782}
3783
3784TEST(TypeMatching, AutoRefTypes) {
3785 std::string Fragment = "auto a = 1;"
3786 "auto b = a;"
3787 "auto &c = a;"
3788 "auto &&d = c;"
3789 "auto &&e = 2;";
3790 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3791 hasType(referenceType()))));
3792 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3793 hasType(referenceType()))));
3794 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3795 hasType(referenceType()))));
3796 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3797 hasType(lValueReferenceType()))));
3798 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3799 hasType(rValueReferenceType()))));
3800 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3801 hasType(referenceType()))));
3802 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3803 hasType(lValueReferenceType()))));
3804 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3805 hasType(rValueReferenceType()))));
3806 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3807 hasType(referenceType()))));
3808 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3809 hasType(lValueReferenceType()))));
3810 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3811 hasType(rValueReferenceType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003812}
3813
3814TEST(TypeMatching, PointeeTypes) {
3815 EXPECT_TRUE(matches("int b; int &a = b;",
3816 referenceType(pointee(builtinType()))));
3817 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3818
3819 EXPECT_TRUE(matches("int *a;",
David Blaikieb61d0872013-02-18 19:04:16 +00003820 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003821
3822 EXPECT_TRUE(matches(
3823 "int const *A;",
3824 pointerType(pointee(isConstQualified(), builtinType()))));
3825 EXPECT_TRUE(notMatches(
3826 "int *A;",
3827 pointerType(pointee(isConstQualified(), builtinType()))));
3828}
3829
3830TEST(TypeMatching, MatchesPointersToConstTypes) {
3831 EXPECT_TRUE(matches("int b; int * const a = &b;",
3832 loc(pointerType())));
3833 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003834 loc(pointerType())));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003835 EXPECT_TRUE(matches(
3836 "int b; const int * a = &b;",
David Blaikieb61d0872013-02-18 19:04:16 +00003837 loc(pointerType(pointee(builtinType())))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003838 EXPECT_TRUE(matches(
3839 "int b; const int * a = &b;",
3840 pointerType(pointee(builtinType()))));
3841}
3842
3843TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper7943eb52012-10-17 13:35:36 +00003844 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3845 hasType(typedefType()))));
Daniel Jasper516b02e2012-10-17 08:52:59 +00003846}
3847
Edwin Vanef901b712013-02-25 14:49:29 +00003848TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vaneb6eae142013-02-25 20:43:32 +00003849 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vanef901b712013-02-25 14:49:29 +00003850 templateSpecializationType()));
3851}
3852
Edwin Vaneb6eae142013-02-25 20:43:32 +00003853TEST(TypeMatching, MatchesRecordType) {
3854 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek59b0af62013-02-27 11:56:58 +00003855 EXPECT_TRUE(matches("struct S{}; S s;",
3856 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3857 EXPECT_TRUE(notMatches("int i;",
3858 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003859}
3860
3861TEST(TypeMatching, MatchesElaboratedType) {
3862 EXPECT_TRUE(matches(
3863 "namespace N {"
3864 " namespace M {"
3865 " class D {};"
3866 " }"
3867 "}"
3868 "N::M::D d;", elaboratedType()));
3869 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3870 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3871}
3872
3873TEST(ElaboratedTypeNarrowing, hasQualifier) {
3874 EXPECT_TRUE(matches(
3875 "namespace N {"
3876 " namespace M {"
3877 " class D {};"
3878 " }"
3879 "}"
3880 "N::M::D d;",
3881 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3882 EXPECT_TRUE(notMatches(
3883 "namespace M {"
3884 " class D {};"
3885 "}"
3886 "M::D d;",
3887 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vane6972f6d2013-03-04 17:51:00 +00003888 EXPECT_TRUE(notMatches(
3889 "struct D {"
3890 "} d;",
3891 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vaneb6eae142013-02-25 20:43:32 +00003892}
3893
3894TEST(ElaboratedTypeNarrowing, namesType) {
3895 EXPECT_TRUE(matches(
3896 "namespace N {"
3897 " namespace M {"
3898 " class D {};"
3899 " }"
3900 "}"
3901 "N::M::D d;",
3902 elaboratedType(elaboratedType(namesType(recordType(
3903 hasDeclaration(namedDecl(hasName("D")))))))));
3904 EXPECT_TRUE(notMatches(
3905 "namespace M {"
3906 " class D {};"
3907 "}"
3908 "M::D d;",
3909 elaboratedType(elaboratedType(namesType(typedefType())))));
3910}
3911
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003912TEST(NNS, MatchesNestedNameSpecifiers) {
3913 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3914 nestedNameSpecifier()));
3915 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3916 nestedNameSpecifier()));
3917 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3918 nestedNameSpecifier()));
3919
3920 EXPECT_TRUE(matches(
3921 "struct A { static void f() {} }; void g() { A::f(); }",
3922 nestedNameSpecifier()));
3923 EXPECT_TRUE(notMatches(
3924 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3925 nestedNameSpecifier()));
3926}
3927
Daniel Jasper87c3d362012-09-20 14:12:57 +00003928TEST(NullStatement, SimpleCases) {
3929 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3930 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3931}
3932
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003933TEST(NNS, MatchesTypes) {
3934 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3935 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3936 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3937 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3938 Matcher));
3939 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3940}
3941
3942TEST(NNS, MatchesNamespaceDecls) {
3943 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3944 specifiesNamespace(hasName("ns")));
3945 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3946 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3947 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3948}
3949
3950TEST(NNS, BindsNestedNameSpecifiers) {
3951 EXPECT_TRUE(matchAndVerifyResultTrue(
3952 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3953 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3954 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3955}
3956
3957TEST(NNS, BindsNestedNameSpecifierLocs) {
3958 EXPECT_TRUE(matchAndVerifyResultTrue(
3959 "namespace ns { struct B {}; } ns::B b;",
3960 loc(nestedNameSpecifier()).bind("loc"),
3961 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3962}
3963
3964TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3965 EXPECT_TRUE(matches(
3966 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3967 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3968 EXPECT_TRUE(matches(
3969 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasper516b02e2012-10-17 08:52:59 +00003970 nestedNameSpecifierLoc(hasPrefix(
3971 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera6bc1f62012-09-13 13:11:25 +00003972}
3973
Daniel Jasper6fc34332012-10-30 15:42:00 +00003974TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3975 std::string Fragment =
3976 "namespace a { struct A { struct B { struct C {}; }; }; };"
3977 "void f() { a::A::B::C c; }";
3978 EXPECT_TRUE(matches(
3979 Fragment,
3980 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3981 hasDescendant(nestedNameSpecifier(
3982 specifiesNamespace(hasName("a")))))));
3983 EXPECT_TRUE(notMatches(
3984 Fragment,
3985 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3986 has(nestedNameSpecifier(
3987 specifiesNamespace(hasName("a")))))));
3988 EXPECT_TRUE(matches(
3989 Fragment,
3990 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3991 has(nestedNameSpecifier(
3992 specifiesNamespace(hasName("a")))))));
3993
3994 // Not really useful because a NestedNameSpecifier can af at most one child,
3995 // but to complete the interface.
3996 EXPECT_TRUE(matchAndVerifyResultTrue(
3997 Fragment,
3998 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3999 forEach(nestedNameSpecifier().bind("x"))),
4000 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
4001}
4002
4003TEST(NNS, NestedNameSpecifiersAsDescendants) {
4004 std::string Fragment =
4005 "namespace a { struct A { struct B { struct C {}; }; }; };"
4006 "void f() { a::A::B::C c; }";
4007 EXPECT_TRUE(matches(
4008 Fragment,
4009 decl(hasDescendant(nestedNameSpecifier(specifiesType(
4010 asString("struct a::A")))))));
4011 EXPECT_TRUE(matchAndVerifyResultTrue(
4012 Fragment,
4013 functionDecl(hasName("f"),
4014 forEachDescendant(nestedNameSpecifier().bind("x"))),
4015 // Nested names: a, a::A and a::A::B.
4016 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
4017}
4018
4019TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
4020 std::string Fragment =
4021 "namespace a { struct A { struct B { struct C {}; }; }; };"
4022 "void f() { a::A::B::C c; }";
4023 EXPECT_TRUE(matches(
4024 Fragment,
4025 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4026 hasDescendant(loc(nestedNameSpecifier(
4027 specifiesNamespace(hasName("a"))))))));
4028 EXPECT_TRUE(notMatches(
4029 Fragment,
4030 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4031 has(loc(nestedNameSpecifier(
4032 specifiesNamespace(hasName("a"))))))));
4033 EXPECT_TRUE(matches(
4034 Fragment,
4035 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
4036 has(loc(nestedNameSpecifier(
4037 specifiesNamespace(hasName("a"))))))));
4038
4039 EXPECT_TRUE(matchAndVerifyResultTrue(
4040 Fragment,
4041 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4042 forEach(nestedNameSpecifierLoc().bind("x"))),
4043 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
4044}
4045
4046TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
4047 std::string Fragment =
4048 "namespace a { struct A { struct B { struct C {}; }; }; };"
4049 "void f() { a::A::B::C c; }";
4050 EXPECT_TRUE(matches(
4051 Fragment,
4052 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4053 asString("struct a::A"))))))));
4054 EXPECT_TRUE(matchAndVerifyResultTrue(
4055 Fragment,
4056 functionDecl(hasName("f"),
4057 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4058 // Nested names: a, a::A and a::A::B.
4059 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4060}
4061
Manuel Klimek191c0932013-02-01 13:41:35 +00004062template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimekc2687452012-10-24 14:47:44 +00004063public:
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004064 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4065 StringRef InnerId)
4066 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jaspere9aa6872012-10-29 10:48:25 +00004067 }
4068
Manuel Klimek191c0932013-02-01 13:41:35 +00004069 virtual bool run(const BoundNodes *Nodes) { return false; }
4070
Manuel Klimekc2687452012-10-24 14:47:44 +00004071 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4072 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004073 return selectFirst<const T>(InnerId,
4074 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimekc2687452012-10-24 14:47:44 +00004075 }
4076private:
4077 std::string Id;
4078 internal::Matcher<T> InnerMatcher;
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004079 std::string InnerId;
Manuel Klimekc2687452012-10-24 14:47:44 +00004080};
4081
4082TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004083 EXPECT_TRUE(matchAndVerifyResultTrue(
4084 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4085 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004086 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4087 "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004088 EXPECT_TRUE(matchAndVerifyResultFalse(
4089 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4090 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004091 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4092 "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004093}
4094
4095TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek191c0932013-02-01 13:41:35 +00004096 EXPECT_TRUE(matchAndVerifyResultTrue(
4097 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004098 new VerifyMatchOnNode<clang::Stmt>(
4099 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004100 EXPECT_TRUE(matchAndVerifyResultFalse(
4101 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004102 new VerifyMatchOnNode<clang::Stmt>(
4103 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004104}
4105
4106TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4107 EXPECT_TRUE(matchAndVerifyResultTrue(
4108 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4109 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004110 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek191c0932013-02-01 13:41:35 +00004111 EXPECT_TRUE(matchAndVerifyResultFalse(
4112 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4113 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimek2cff49e2013-02-06 10:33:21 +00004114 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimekc2687452012-10-24 14:47:44 +00004115}
4116
Manuel Klimekbee08572013-02-07 12:42:10 +00004117template <typename T>
4118class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4119public:
4120 virtual bool run(const BoundNodes *Nodes) { return false; }
4121
4122 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4123 const T *Node = Nodes->getNodeAs<T>("");
4124 return verify(*Nodes, *Context, Node);
4125 }
4126
4127 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4128 return selectFirst<const T>(
4129 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4130 *Node, Context)) != NULL;
4131 }
4132 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4133 return selectFirst<const T>(
4134 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4135 *Node, Context)) != NULL;
4136 }
4137};
4138
4139TEST(IsEqualTo, MatchesNodesByIdentity) {
4140 EXPECT_TRUE(matchAndVerifyResultTrue(
4141 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4142 new VerifyAncestorHasChildIsEqual<Decl>()));
4143 EXPECT_TRUE(
4144 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4145 new VerifyAncestorHasChildIsEqual<Stmt>()));
4146}
4147
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004148class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4149public:
4150 VerifyStartOfTranslationUnit() : Called(false) {}
4151 virtual void run(const MatchFinder::MatchResult &Result) {
4152 EXPECT_TRUE(Called);
4153 }
4154 virtual void onStartOfTranslationUnit() {
4155 Called = true;
4156 }
4157 bool Called;
4158};
4159
4160TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4161 MatchFinder Finder;
4162 VerifyStartOfTranslationUnit VerifyCallback;
4163 Finder.addMatcher(decl(), &VerifyCallback);
4164 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4165 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4166 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004167
4168 VerifyCallback.Called = false;
4169 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4170 ASSERT_TRUE(AST.get());
4171 Finder.matchAST(AST->getASTContext());
4172 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimekbd0e2b72012-11-02 01:31:03 +00004173}
4174
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004175class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4176public:
4177 VerifyEndOfTranslationUnit() : Called(false) {}
4178 virtual void run(const MatchFinder::MatchResult &Result) {
4179 EXPECT_FALSE(Called);
4180 }
4181 virtual void onEndOfTranslationUnit() {
4182 Called = true;
4183 }
4184 bool Called;
4185};
4186
4187TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4188 MatchFinder Finder;
4189 VerifyEndOfTranslationUnit VerifyCallback;
4190 Finder.addMatcher(decl(), &VerifyCallback);
4191 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4192 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4193 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbournea2334162013-11-07 22:30:36 +00004194
4195 VerifyCallback.Called = false;
4196 OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
4197 ASSERT_TRUE(AST.get());
4198 Finder.matchAST(AST->getASTContext());
4199 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne6a55bb22013-05-28 19:21:51 +00004200}
4201
Manuel Klimekbbb75852013-06-20 14:06:32 +00004202TEST(EqualsBoundNodeMatcher, QualType) {
4203 EXPECT_TRUE(matches(
4204 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4205 hasInitializer(ignoringParenImpCasts(
4206 hasType(qualType(equalsBoundNode("type"))))))));
4207 EXPECT_TRUE(notMatches("int i = 1.f;",
4208 varDecl(hasType(qualType().bind("type")),
4209 hasInitializer(ignoringParenImpCasts(hasType(
4210 qualType(equalsBoundNode("type"))))))));
4211}
4212
4213TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4214 EXPECT_TRUE(notMatches(
4215 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4216 hasInitializer(ignoringParenImpCasts(
4217 hasType(qualType(equalsBoundNode("type"))))))));
4218}
4219
4220TEST(EqualsBoundNodeMatcher, Stmt) {
4221 EXPECT_TRUE(
4222 matches("void f() { if(true) {} }",
4223 stmt(allOf(ifStmt().bind("if"),
4224 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4225
4226 EXPECT_TRUE(notMatches(
4227 "void f() { if(true) { if (true) {} } }",
4228 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4229}
4230
4231TEST(EqualsBoundNodeMatcher, Decl) {
4232 EXPECT_TRUE(matches(
4233 "class X { class Y {}; };",
4234 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4235 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4236
4237 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4238 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4239 has(decl(equalsBoundNode("record")))))));
4240}
4241
4242TEST(EqualsBoundNodeMatcher, Type) {
4243 EXPECT_TRUE(matches(
4244 "class X { int a; int b; };",
4245 recordDecl(
4246 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4247 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4248
4249 EXPECT_TRUE(notMatches(
4250 "class X { int a; double b; };",
4251 recordDecl(
4252 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4253 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4254}
4255
4256TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4257
4258 EXPECT_TRUE(matchAndVerifyResultTrue(
4259 "int f() {"
4260 " if (1) {"
4261 " int i = 9;"
4262 " }"
4263 " int j = 10;"
4264 " {"
4265 " float k = 9.0;"
4266 " }"
4267 " return 0;"
4268 "}",
4269 // Look for variable declarations within functions whose type is the same
4270 // as the function return type.
4271 functionDecl(returns(qualType().bind("type")),
4272 forEachDescendant(varDecl(hasType(
4273 qualType(equalsBoundNode("type")))).bind("decl"))),
4274 // Only i and j should match, not k.
4275 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4276}
4277
4278TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4279 EXPECT_TRUE(matchAndVerifyResultTrue(
4280 "void f() {"
4281 " int x;"
4282 " double d;"
4283 " x = d + x - d + x;"
4284 "}",
4285 functionDecl(
4286 hasName("f"), forEachDescendant(varDecl().bind("d")),
4287 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4288 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4289}
4290
Manuel Klimek04616e42012-07-06 05:48:52 +00004291} // end namespace ast_matchers
4292} // end namespace clang