blob: 5d09700e6e0ec34e33f6af6fe0eff2c32fad0cc9 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
Benjamin Kramer8b9ed712012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000014#include "clang/Tooling/Tooling.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070015#include "llvm/ADT/Triple.h"
16#include "llvm/Support/Host.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000017#include "gtest/gtest.h"
18
19namespace clang {
20namespace ast_matchers {
21
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000022#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000023TEST(HasNameDeathTest, DiesOnEmptyName) {
24 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000025 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000026 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
27 }, "");
28}
29
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000030TEST(HasNameDeathTest, DiesOnEmptyPattern) {
31 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000032 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000033 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
34 }, "");
35}
36
Manuel Klimek4da21662012-07-06 05:48:52 +000037TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
38 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000039 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000040 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
41 }, "");
42}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000043#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000044
Peter Collingbourned2bd5892013-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 Klimek715c9562012-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 Klimek4da21662012-07-06 05:48:52 +000063TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000064 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-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 Jaspere0e6b9e2012-07-10 20:20:19 +000071 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000072
73 EXPECT_TRUE(notMatches("#define X 1", NamedX));
74}
75
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000076TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000077 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-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 Jasper2dc75ed2012-08-24 05:12:34 +000088 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-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 Jasper2dc75ed2012-08-24 05:12:34 +000092 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-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 Klimekec33b6f2012-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 Jaspere0e6b9e2012-07-10 20:20:19 +0000104}
105
Manuel Klimek4da21662012-07-06 05:48:52 +0000106TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000107 DeclarationMatcher ClassMatcher(recordDecl());
Stephen Hines651f13c2014-04-23 16:59:28 -0700108 llvm::Triple Triple(llvm::sys::getDefaultTargetTriple());
109 if (Triple.getOS() != llvm::Triple::Win32 ||
110 Triple.getEnvironment() != llvm::Triple::MSVC)
111 EXPECT_FALSE(matches("", ClassMatcher));
112 else
113 // Matches class type_info.
114 EXPECT_TRUE(matches("", ClassMatcher));
Manuel Klimek4da21662012-07-06 05:48:52 +0000115
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000116 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000117 EXPECT_TRUE(matches("class X;", ClassX));
118 EXPECT_TRUE(matches("class X {};", ClassX));
119 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
120 EXPECT_TRUE(notMatches("", ClassX));
121}
122
123TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000124 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000125
126 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000127 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
128 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000129 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
130 EXPECT_TRUE(notMatches("", IsDerivedFromX));
131
Daniel Jasper63d88722012-09-12 21:14:15 +0000132 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000133
134 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
135 EXPECT_TRUE(matches("class X {};", IsAX));
136 EXPECT_TRUE(matches("class X;", IsAX));
137 EXPECT_TRUE(notMatches("class Y;", IsAX));
138 EXPECT_TRUE(notMatches("", IsAX));
139
Manuel Klimek4da21662012-07-06 05:48:52 +0000140 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000141 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000142 EXPECT_TRUE(
143 matches("class X {}; class Y : public X {}; class Z : public Y {};",
144 ZIsDerivedFromX));
145 EXPECT_TRUE(
146 matches("class X {};"
147 "template<class T> class Y : public X {};"
148 "class Z : public Y<int> {};", ZIsDerivedFromX));
149 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
150 ZIsDerivedFromX));
151 EXPECT_TRUE(
152 matches("template<class T> class X {}; "
153 "template<class T> class Z : public X<T> {};",
154 ZIsDerivedFromX));
155 EXPECT_TRUE(
156 matches("template<class T, class U=T> class X {}; "
157 "template<class T> class Z : public X<T> {};",
158 ZIsDerivedFromX));
159 EXPECT_TRUE(
160 notMatches("template<class X> class A { class Z : public X {}; };",
161 ZIsDerivedFromX));
162 EXPECT_TRUE(
163 matches("template<class X> class A { public: class Z : public X {}; }; "
164 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template <class T> class X {}; "
167 "template<class Y> class A { class Z : public X<Y> {}; };",
168 ZIsDerivedFromX));
169 EXPECT_TRUE(
170 notMatches("template<template<class T> class X> class A { "
171 " class Z : public X<int> {}; };", ZIsDerivedFromX));
172 EXPECT_TRUE(
173 matches("template<template<class T> class X> class A { "
174 " public: class Z : public X<int> {}; }; "
175 "template<class T> class X {}; void y() { A<X>::Z z; }",
176 ZIsDerivedFromX));
177 EXPECT_TRUE(
178 notMatches("template<class X> class A { class Z : public X::D {}; };",
179 ZIsDerivedFromX));
180 EXPECT_TRUE(
181 matches("template<class X> class A { public: "
182 " class Z : public X::D {}; }; "
183 "class Y { public: class X {}; typedef X D; }; "
184 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
185 EXPECT_TRUE(
186 matches("class X {}; typedef X Y; class Z : public Y {};",
187 ZIsDerivedFromX));
188 EXPECT_TRUE(
189 matches("template<class T> class Y { typedef typename T::U X; "
190 " class Z : public X {}; };", ZIsDerivedFromX));
191 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 notMatches("template<class T> class X {}; "
195 "template<class T> class A { class Z : public X<T>::D {}; };",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("template<class T> class X { public: typedef X<T> D; }; "
199 "template<class T> class A { public: "
200 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
201 ZIsDerivedFromX));
202 EXPECT_TRUE(
203 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
204 ZIsDerivedFromX));
205 EXPECT_TRUE(
206 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
207 ZIsDerivedFromX));
208 EXPECT_TRUE(
209 matches("class X {}; class Y : public X {}; "
210 "typedef Y V; typedef V W; class Z : public W {};",
211 ZIsDerivedFromX));
212 EXPECT_TRUE(
213 matches("template<class T, class U> class X {}; "
214 "template<class T> class A { class Z : public X<T, int> {}; };",
215 ZIsDerivedFromX));
216 EXPECT_TRUE(
217 notMatches("template<class X> class D { typedef X A; typedef A B; "
218 " typedef B C; class Z : public C {}; };",
219 ZIsDerivedFromX));
220 EXPECT_TRUE(
221 matches("class X {}; typedef X A; typedef A B; "
222 "class Z : public B {};", ZIsDerivedFromX));
223 EXPECT_TRUE(
224 matches("class X {}; typedef X A; typedef A B; typedef B C; "
225 "class Z : public C {};", ZIsDerivedFromX));
226 EXPECT_TRUE(
227 matches("class U {}; typedef U X; typedef X V; "
228 "class Z : public V {};", ZIsDerivedFromX));
229 EXPECT_TRUE(
230 matches("class Base {}; typedef Base X; "
231 "class Z : public Base {};", ZIsDerivedFromX));
232 EXPECT_TRUE(
233 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
234 "class Z : public Base {};", ZIsDerivedFromX));
235 EXPECT_TRUE(
236 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
237 "class Z : public Base {};", ZIsDerivedFromX));
238 EXPECT_TRUE(
239 matches("class A {}; typedef A X; typedef A Y; "
240 "class Z : public Y {};", ZIsDerivedFromX));
241 EXPECT_TRUE(
242 notMatches("template <typename T> class Z;"
243 "template <> class Z<void> {};"
244 "template <typename T> class Z : public Z<void> {};",
245 IsDerivedFromX));
246 EXPECT_TRUE(
247 matches("template <typename T> class X;"
248 "template <> class X<void> {};"
249 "template <typename T> class X : public X<void> {};",
250 IsDerivedFromX));
251 EXPECT_TRUE(matches(
252 "class X {};"
253 "template <typename T> class Z;"
254 "template <> class Z<void> {};"
255 "template <typename T> class Z : public Z<void>, public X {};",
256 ZIsDerivedFromX));
Manuel Klimek987c2f52012-12-04 13:40:29 +0000257 EXPECT_TRUE(
258 notMatches("template<int> struct X;"
259 "template<int i> struct X : public X<i-1> {};",
260 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
261 EXPECT_TRUE(matches(
262 "struct A {};"
263 "template<int> struct X;"
264 "template<int i> struct X : public X<i-1> {};"
265 "template<> struct X<0> : public A {};"
266 "struct B : public X<42> {};",
267 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000268
269 // FIXME: Once we have better matchers for template type matching,
270 // get rid of the Variable(...) matching and match the right template
271 // declarations directly.
272 const char *RecursiveTemplateOneParameter =
273 "class Base1 {}; class Base2 {};"
274 "template <typename T> class Z;"
275 "template <> class Z<void> : public Base1 {};"
276 "template <> class Z<int> : public Base2 {};"
277 "template <> class Z<float> : public Z<void> {};"
278 "template <> class Z<double> : public Z<int> {};"
279 "template <typename T> class Z : public Z<float>, public Z<double> {};"
280 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
281 EXPECT_TRUE(matches(
282 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000283 varDecl(hasName("z_float"),
284 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000285 EXPECT_TRUE(notMatches(
286 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000287 varDecl(hasName("z_float"),
288 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000289 EXPECT_TRUE(matches(
290 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000291 varDecl(hasName("z_char"),
292 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
293 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000294
295 const char *RecursiveTemplateTwoParameters =
296 "class Base1 {}; class Base2 {};"
297 "template <typename T1, typename T2> class Z;"
298 "template <typename T> class Z<void, T> : public Base1 {};"
299 "template <typename T> class Z<int, T> : public Base2 {};"
300 "template <typename T> class Z<float, T> : public Z<void, T> {};"
301 "template <typename T> class Z<double, T> : public Z<int, T> {};"
302 "template <typename T1, typename T2> class Z : "
303 " public Z<float, T2>, public Z<double, T2> {};"
304 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
305 " Z<char, void> z_char; }";
306 EXPECT_TRUE(matches(
307 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000308 varDecl(hasName("z_float"),
309 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000310 EXPECT_TRUE(notMatches(
311 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000312 varDecl(hasName("z_float"),
313 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000314 EXPECT_TRUE(matches(
315 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000316 varDecl(hasName("z_char"),
317 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
318 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000319 EXPECT_TRUE(matches(
320 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000321 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000322 EXPECT_TRUE(notMatches(
323 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000324 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000325
326 EXPECT_TRUE(matches(
327 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000328 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimekb56da8c2013-08-02 21:24:09 +0000329
330 EXPECT_TRUE(matches(
331 "template<typename T> class X {};"
332 "template<typename T> using Z = X<T>;"
333 "template <typename T> class Y : Z<T> {};",
334 recordDecl(isDerivedFrom(namedDecl(hasName("X"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000335}
336
Edwin Vane6a19a972013-03-06 17:02:57 +0000337TEST(DeclarationMatcher, hasMethod) {
338 EXPECT_TRUE(matches("class A { void func(); };",
339 recordDecl(hasMethod(hasName("func")))));
340 EXPECT_TRUE(notMatches("class A { void func(); };",
341 recordDecl(hasMethod(isPublic()))));
342}
343
Daniel Jasper08f0c532012-09-18 14:17:42 +0000344TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
345 EXPECT_TRUE(matches(
346 "template <typename T> struct A {"
347 " template <typename T2> struct F {};"
348 "};"
349 "template <typename T> struct B : A<T>::template F<T> {};"
350 "B<int> b;",
351 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
352}
353
Edwin Vane742d9e72013-02-25 20:43:32 +0000354TEST(DeclarationMatcher, hasDeclContext) {
355 EXPECT_TRUE(matches(
356 "namespace N {"
357 " namespace M {"
358 " class D {};"
359 " }"
360 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000361 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000362 EXPECT_TRUE(notMatches(
363 "namespace N {"
364 " namespace M {"
365 " class D {};"
366 " }"
367 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000368 recordDecl(hasDeclContext(namespaceDecl(hasName("N"))))));
369
370 EXPECT_TRUE(matches("namespace {"
371 " namespace M {"
372 " class D {};"
373 " }"
374 "}",
375 recordDecl(hasDeclContext(namespaceDecl(
376 hasName("M"), hasDeclContext(namespaceDecl()))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000377}
378
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000379TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000380 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000381 EXPECT_TRUE(notMatches("class X;", ClassX));
382 EXPECT_TRUE(notMatches("class X {};", ClassX));
383}
384
385TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000386 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000387 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
388 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
389}
390
391TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
392 EXPECT_TRUE(notMatches("template<typename T> class X { };"
393 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000394 classTemplateDecl(hasName("X"),
395 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000396}
397
398TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
399 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
400 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000401 classTemplateDecl(hasName("X"),
402 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000403}
404
Daniel Jasper6a124492012-07-12 08:50:38 +0000405TEST(AllOf, AllOverloadsWork) {
406 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000407 "struct T { };"
408 "int f(int, T*, int, int);"
409 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000410 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000411 callExpr(allOf(callee(functionDecl(hasName("f"))),
412 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000413 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000414 callExpr(allOf(callee(functionDecl(hasName("f"))),
415 hasArgument(0, declRefExpr(to(varDecl()))),
416 hasArgument(1, hasType(pointsTo(
417 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000418 EXPECT_TRUE(matches(Program,
419 callExpr(allOf(callee(functionDecl(hasName("f"))),
420 hasArgument(0, declRefExpr(to(varDecl()))),
421 hasArgument(1, hasType(pointsTo(
422 recordDecl(hasName("T"))))),
423 hasArgument(2, integerLiteral(equals(3)))))));
424 EXPECT_TRUE(matches(Program,
425 callExpr(allOf(callee(functionDecl(hasName("f"))),
426 hasArgument(0, declRefExpr(to(varDecl()))),
427 hasArgument(1, hasType(pointsTo(
428 recordDecl(hasName("T"))))),
429 hasArgument(2, integerLiteral(equals(3))),
430 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000431}
432
Manuel Klimek4da21662012-07-06 05:48:52 +0000433TEST(DeclarationMatcher, MatchAnyOf) {
434 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000435 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000436 EXPECT_TRUE(
437 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
438 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
439 EXPECT_TRUE(
440 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
441 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
442
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000443 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000444 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000445 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
446 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
447
Manuel Klimek4da21662012-07-06 05:48:52 +0000448 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000449 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
450 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000451 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
452 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
453 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
454 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
455 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
456 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
457}
458
459TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000460 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000461 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
462 EXPECT_TRUE(matches("class X {};", HasClassX));
463
464 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000465 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000466 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
467 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
468 EXPECT_TRUE(
469 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
470}
471
472TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
473 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000474 recordDecl(
475 has(recordDecl(
476 has(recordDecl(hasName("X"))),
477 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000478 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000479 has(recordDecl(
480 has(recordDecl(hasName("A"))),
481 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000482 hasName("C"))),
483 hasName("F"));
484
485 EXPECT_TRUE(matches(
486 "class F {"
487 " class Z {"
488 " class X {};"
489 " class Y {};"
490 " };"
491 " class C {"
492 " class A {};"
493 " class B {};"
494 " };"
495 "};", Recursive));
496
497 EXPECT_TRUE(matches(
498 "class F {"
499 " class Z {"
500 " class A {};"
501 " class X {};"
502 " class Y {};"
503 " };"
504 " class C {"
505 " class X {};"
506 " class A {};"
507 " class B {};"
508 " };"
509 "};", Recursive));
510
511 EXPECT_TRUE(matches(
512 "class O1 {"
513 " class O2 {"
514 " class F {"
515 " class Z {"
516 " class A {};"
517 " class X {};"
518 " class Y {};"
519 " };"
520 " class C {"
521 " class X {};"
522 " class A {};"
523 " class B {};"
524 " };"
525 " };"
526 " };"
527 "};", Recursive));
528}
529
530TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
531 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000532 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000533 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000534 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000535 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000536 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000537 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000538 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000539 hasName("Y"))),
540 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000541 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000542 anyOf(
543 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000544 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000545 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000546 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000547 hasName("B")))))),
548 hasName("F")));
549
550 EXPECT_TRUE(matches("class F {};", Recursive));
551 EXPECT_TRUE(matches("class Z {};", Recursive));
552 EXPECT_TRUE(matches("class C {};", Recursive));
553 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
554 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
555 EXPECT_TRUE(
556 matches("class O1 { class O2 {"
557 " class M { class N { class B {}; }; }; "
558 "}; };", Recursive));
559}
560
561TEST(DeclarationMatcher, MatchNot) {
562 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000563 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000564 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000565 unless(hasName("X")));
566 EXPECT_TRUE(notMatches("", NotClassX));
567 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
568 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
569 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
570 EXPECT_TRUE(
571 notMatches("class Y {}; class Z {}; class X : public Y {};",
572 NotClassX));
573
574 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000575 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000576 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000577 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000578 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000579 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000580 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
581 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
582 ClassXHasNotClassY));
583}
584
585TEST(DeclarationMatcher, HasDescendant) {
586 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000587 recordDecl(
588 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000589 hasName("Z"));
590 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
591 EXPECT_TRUE(
592 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
593 EXPECT_TRUE(
594 matches("class Z { class A { class Y { class X {}; }; }; };",
595 ZDescendantClassX));
596 EXPECT_TRUE(
597 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
598 ZDescendantClassX));
599 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
600
601 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000602 recordDecl(
603 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000604 hasName("X"))),
605 hasName("Z"));
606 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
607 ZDescendantClassXHasClassY));
608 EXPECT_TRUE(
609 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
610 ZDescendantClassXHasClassY));
611 EXPECT_TRUE(notMatches(
612 "class Z {"
613 " class A {"
614 " class B {"
615 " class X {"
616 " class C {"
617 " class Y {};"
618 " };"
619 " };"
620 " }; "
621 " };"
622 "};", ZDescendantClassXHasClassY));
623
624 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000625 recordDecl(
626 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
627 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000628 hasName("Z"));
629 EXPECT_TRUE(
630 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
631 ZDescendantClassXDescendantClassY));
632 EXPECT_TRUE(matches(
633 "class Z {"
634 " class A {"
635 " class X {"
636 " class B {"
637 " class Y {};"
638 " };"
639 " class Y {};"
640 " };"
641 " };"
642 "};", ZDescendantClassXDescendantClassY));
643}
644
Daniel Jaspera267cf62012-10-29 10:14:44 +0000645// Implements a run method that returns whether BoundNodes contains a
646// Decl bound to Id that can be dynamically cast to T.
647// Optionally checks that the check succeeded a specific number of times.
648template <typename T>
649class VerifyIdIsBoundTo : public BoundNodesCallback {
650public:
651 // Create an object that checks that a node of type \c T was bound to \c Id.
652 // Does not check for a certain number of matches.
653 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
654 : Id(Id), ExpectedCount(-1), Count(0) {}
655
656 // Create an object that checks that a node of type \c T was bound to \c Id.
657 // Checks that there were exactly \c ExpectedCount matches.
658 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
659 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
660
661 // Create an object that checks that a node of type \c T was bound to \c Id.
662 // Checks that there was exactly one match with the name \c ExpectedName.
663 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimek374516c2013-03-14 16:33:21 +0000664 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
665 int ExpectedCount = 1)
666 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
667 ExpectedName(ExpectedName) {}
Daniel Jaspera267cf62012-10-29 10:14:44 +0000668
Stephen Hines651f13c2014-04-23 16:59:28 -0700669 void onEndOfTranslationUnit() override {
Daniel Jaspera267cf62012-10-29 10:14:44 +0000670 if (ExpectedCount != -1)
671 EXPECT_EQ(ExpectedCount, Count);
672 if (!ExpectedName.empty())
673 EXPECT_EQ(ExpectedName, Name);
Peter Collingbourned2bd5892013-11-07 22:30:32 +0000674 Count = 0;
675 Name.clear();
676 }
677
678 ~VerifyIdIsBoundTo() {
679 EXPECT_EQ(0, Count);
680 EXPECT_EQ("", Name);
Daniel Jaspera267cf62012-10-29 10:14:44 +0000681 }
682
683 virtual bool run(const BoundNodes *Nodes) {
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000684 const BoundNodes::IDToNodeMap &M = Nodes->getMap();
Daniel Jaspera267cf62012-10-29 10:14:44 +0000685 if (Nodes->getNodeAs<T>(Id)) {
686 ++Count;
687 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
688 Name = Named->getNameAsString();
689 } else if (const NestedNameSpecifier *NNS =
690 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
691 llvm::raw_string_ostream OS(Name);
692 NNS->print(OS, PrintingPolicy(LangOptions()));
693 }
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000694 BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
695 EXPECT_NE(M.end(), I);
696 if (I != M.end())
697 EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
Daniel Jaspera267cf62012-10-29 10:14:44 +0000698 return true;
699 }
Peter Collingbourne3f0e0402013-11-06 00:27:07 +0000700 EXPECT_TRUE(M.count(Id) == 0 || M.find(Id)->second.template get<T>() == 0);
Daniel Jaspera267cf62012-10-29 10:14:44 +0000701 return false;
702 }
703
Daniel Jasper452abbc2012-10-29 10:48:25 +0000704 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
705 return run(Nodes);
706 }
707
Daniel Jaspera267cf62012-10-29 10:14:44 +0000708private:
709 const std::string Id;
710 const int ExpectedCount;
711 int Count;
712 const std::string ExpectedName;
713 std::string Name;
714};
715
716TEST(HasDescendant, MatchesDescendantTypes) {
717 EXPECT_TRUE(matches("void f() { int i = 3; }",
718 decl(hasDescendant(loc(builtinType())))));
719 EXPECT_TRUE(matches("void f() { int i = 3; }",
720 stmt(hasDescendant(builtinType()))));
721
722 EXPECT_TRUE(matches("void f() { int i = 3; }",
723 stmt(hasDescendant(loc(builtinType())))));
724 EXPECT_TRUE(matches("void f() { int i = 3; }",
725 stmt(hasDescendant(qualType(builtinType())))));
726
727 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
728 stmt(hasDescendant(isInteger()))));
729
730 EXPECT_TRUE(matchAndVerifyResultTrue(
731 "void f() { int a; float c; int d; int e; }",
732 functionDecl(forEachDescendant(
733 varDecl(hasDescendant(isInteger())).bind("x"))),
734 new VerifyIdIsBoundTo<Decl>("x", 3)));
735}
736
737TEST(HasDescendant, MatchesDescendantsOfTypes) {
738 EXPECT_TRUE(matches("void f() { int*** i; }",
739 qualType(hasDescendant(builtinType()))));
740 EXPECT_TRUE(matches("void f() { int*** i; }",
741 qualType(hasDescendant(
742 pointerType(pointee(builtinType()))))));
743 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000744 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000745
746 EXPECT_TRUE(matchAndVerifyResultTrue(
747 "void f() { int*** i; }",
748 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
749 new VerifyIdIsBoundTo<Type>("x", 2)));
750}
751
752TEST(Has, MatchesChildrenOfTypes) {
753 EXPECT_TRUE(matches("int i;",
754 varDecl(hasName("i"), has(isInteger()))));
755 EXPECT_TRUE(notMatches("int** i;",
756 varDecl(hasName("i"), has(isInteger()))));
757 EXPECT_TRUE(matchAndVerifyResultTrue(
758 "int (*f)(float, int);",
759 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
760 new VerifyIdIsBoundTo<QualType>("x", 2)));
761}
762
763TEST(Has, MatchesChildTypes) {
764 EXPECT_TRUE(matches(
765 "int* i;",
766 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
767 EXPECT_TRUE(notMatches(
768 "int* i;",
769 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
770}
771
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000772TEST(Enum, DoesNotMatchClasses) {
773 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
774}
775
776TEST(Enum, MatchesEnums) {
777 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
778}
779
780TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000781 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000782 EXPECT_TRUE(matches("enum X{ A };", Matcher));
783 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
784 EXPECT_TRUE(notMatches("enum X {};", Matcher));
785}
786
Manuel Klimek4da21662012-07-06 05:48:52 +0000787TEST(StatementMatcher, Has) {
788 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000789 expr(hasType(pointsTo(recordDecl(hasName("X")))),
790 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000791
792 EXPECT_TRUE(matches(
793 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
794 EXPECT_TRUE(notMatches(
795 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
796}
797
798TEST(StatementMatcher, HasDescendant) {
799 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000800 expr(hasType(pointsTo(recordDecl(hasName("X")))),
801 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000802
803 EXPECT_TRUE(matches(
804 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
805 HasDescendantVariableI));
806 EXPECT_TRUE(notMatches(
807 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
808 HasDescendantVariableI));
809}
810
811TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000813
814 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
815 EXPECT_TRUE(notMatches("class A {};", TypeA));
816
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000817 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000818
819 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
820 TypeDerivedFromA));
821 EXPECT_TRUE(notMatches("class A {};", TypeA));
822
823 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000824 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000825
826 EXPECT_TRUE(
827 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
828}
829
Manuel Klimek4da21662012-07-06 05:48:52 +0000830TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000831 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000832
833 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000834 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000835
836 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000837 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000838
839 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000840 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000841
842 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
843 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000844 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000845
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000846 StatementMatcher MethodX =
847 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000848
849 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
850 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000851 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000852}
853
854TEST(Matcher, BindTheSameNameInAlternatives) {
855 StatementMatcher matcher = anyOf(
856 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000857 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000858 hasRHS(integerLiteral(equals(0)))),
859 binaryOperator(hasOperatorName("+"),
860 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000861 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000862
863 EXPECT_TRUE(matchAndVerifyResultTrue(
864 // The first branch of the matcher binds x to 0 but then fails.
865 // The second branch binds x to f() and succeeds.
866 "int f() { return 0 + f(); }",
867 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000868 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000869}
870
Manuel Klimek66341c52012-08-30 19:41:06 +0000871TEST(Matcher, BindsIDForMemoizedResults) {
872 // Using the same matcher in two match expressions will make memoization
873 // kick in.
874 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
875 EXPECT_TRUE(matchAndVerifyResultTrue(
876 "class A { class B { class X {}; }; };",
877 DeclarationMatcher(anyOf(
878 recordDecl(hasName("A"), hasDescendant(ClassX)),
879 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000880 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000881}
882
Daniel Jasper189f2e42012-12-03 15:43:25 +0000883TEST(HasDeclaration, HasDeclarationOfEnumType) {
884 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
885 expr(hasType(pointsTo(
886 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
887}
888
Edwin Vaneb45083d2013-02-25 14:32:42 +0000889TEST(HasDeclaration, HasGetDeclTraitTest) {
890 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
891 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
892 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
893}
894
Edwin Vane52380602013-02-19 17:14:34 +0000895TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
896 EXPECT_TRUE(matches("typedef int X; X a;",
897 varDecl(hasName("a"),
898 hasType(typedefType(hasDeclaration(decl()))))));
899
900 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
901}
902
Edwin Vane3abf7782013-02-25 14:49:29 +0000903TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
904 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
905 varDecl(hasType(templateSpecializationType(
906 hasDeclaration(namedDecl(hasName("A"))))))));
907}
908
Manuel Klimek4da21662012-07-06 05:48:52 +0000909TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000910 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000911 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000912 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000913 EXPECT_TRUE(
914 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000915 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000916 EXPECT_TRUE(
917 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000918 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000919}
920
921TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000922 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000923 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000924 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000925 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000926 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000927 EXPECT_TRUE(
928 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000929 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000930}
931
932TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000933 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000934 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000935 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000936 EXPECT_TRUE(
937 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000938 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000939}
940
941TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000942 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000943 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000944 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000945 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000946 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000947}
948
Manuel Klimek1a68afd2013-06-20 13:08:29 +0000949TEST(HasTypeLoc, MatchesDeclaratorDecls) {
950 EXPECT_TRUE(matches("int x;",
951 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
952
953 // Make sure we don't crash on implicit constructors.
954 EXPECT_TRUE(notMatches("class X {}; X x;",
955 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
956}
957
Manuel Klimek4da21662012-07-06 05:48:52 +0000958TEST(Matcher, Call) {
959 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000960 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000961 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000962
963 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
964 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
965
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000966 StatementMatcher MethodOnY =
967 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000968
969 EXPECT_TRUE(
970 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
971 MethodOnY));
972 EXPECT_TRUE(
973 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
974 MethodOnY));
975 EXPECT_TRUE(
976 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
977 MethodOnY));
978 EXPECT_TRUE(
979 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
980 MethodOnY));
981 EXPECT_TRUE(
982 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
983 MethodOnY));
984
985 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000986 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000987
988 EXPECT_TRUE(
989 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
990 MethodOnYPointer));
991 EXPECT_TRUE(
992 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
993 MethodOnYPointer));
994 EXPECT_TRUE(
995 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
996 MethodOnYPointer));
997 EXPECT_TRUE(
998 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
999 MethodOnYPointer));
1000 EXPECT_TRUE(
1001 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1002 MethodOnYPointer));
1003}
1004
Daniel Jasper31f7c082012-10-01 13:40:41 +00001005TEST(Matcher, Lambda) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001006 EXPECT_TRUE(matches("auto f = [] (int i) { return i; };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00001007 lambdaExpr()));
1008}
1009
1010TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +00001011 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
1012 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00001013 forRangeStmt()));
1014 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
1015 forRangeStmt()));
1016}
1017
1018TEST(Matcher, UserDefinedLiteral) {
1019 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
1020 " return i + 1;"
1021 "}"
1022 "char c = 'a'_inc;",
1023 userDefinedLiteral()));
1024}
1025
Daniel Jasperb54b7642012-09-20 14:12:57 +00001026TEST(Matcher, FlowControl) {
1027 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
1028 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
1029 continueStmt()));
1030 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
1031 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
1032 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
1033}
1034
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001035TEST(HasType, MatchesAsString) {
1036 EXPECT_TRUE(
1037 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001038 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001039 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001040 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001041 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001042 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001043 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Stephen Hines651f13c2014-04-23 16:59:28 -07001044 fieldDecl(hasType(asString("struct (anonymous namespace)::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001045}
1046
Manuel Klimek4da21662012-07-06 05:48:52 +00001047TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001048 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001049 // Unary operator
1050 EXPECT_TRUE(matches("class Y { }; "
1051 "bool operator!(Y x) { return false; }; "
1052 "Y y; bool c = !y;", OpCall));
1053 // No match -- special operators like "new", "delete"
1054 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1055 // we need to figure out include paths in the test.
1056 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1057 // "class Y { }; "
1058 // "void *operator new(size_t size) { return 0; } "
1059 // "Y *y = new Y;", OpCall));
1060 EXPECT_TRUE(notMatches("class Y { }; "
1061 "void operator delete(void *p) { } "
1062 "void a() {Y *y = new Y; delete y;}", OpCall));
1063 // Binary operator
1064 EXPECT_TRUE(matches("class Y { }; "
1065 "bool operator&&(Y x, Y y) { return true; }; "
1066 "Y a; Y b; bool c = a && b;",
1067 OpCall));
1068 // No match -- normal operator, not an overloaded one.
1069 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1070 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1071}
1072
1073TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1074 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001075 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001076 EXPECT_TRUE(matches("class Y { }; "
1077 "bool operator&&(Y x, Y y) { return true; }; "
1078 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1079 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001080 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001081 EXPECT_TRUE(notMatches("class Y { }; "
1082 "bool operator&&(Y x, Y y) { return true; }; "
1083 "Y a; Y b; bool c = a && b;",
1084 OpCallLessLess));
Edwin Vane6a19a972013-03-06 17:02:57 +00001085 DeclarationMatcher ClassWithOpStar =
1086 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1087 EXPECT_TRUE(matches("class Y { int operator*(); };",
1088 ClassWithOpStar));
1089 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1090 ClassWithOpStar)) ;
Manuel Klimek4da21662012-07-06 05:48:52 +00001091}
1092
Daniel Jasper278057f2012-11-15 03:29:05 +00001093TEST(Matcher, NestedOverloadedOperatorCalls) {
1094 EXPECT_TRUE(matchAndVerifyResultTrue(
1095 "class Y { }; "
1096 "Y& operator&&(Y& x, Y& y) { return x; }; "
1097 "Y a; Y b; Y c; Y d = a && b && c;",
1098 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1099 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1100 EXPECT_TRUE(matches(
1101 "class Y { }; "
1102 "Y& operator&&(Y& x, Y& y) { return x; }; "
1103 "Y a; Y b; Y c; Y d = a && b && c;",
1104 operatorCallExpr(hasParent(operatorCallExpr()))));
1105 EXPECT_TRUE(matches(
1106 "class Y { }; "
1107 "Y& operator&&(Y& x, Y& y) { return x; }; "
1108 "Y a; Y b; Y c; Y d = a && b && c;",
1109 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1110}
1111
Manuel Klimek4da21662012-07-06 05:48:52 +00001112TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001113 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001114 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001115
1116 EXPECT_TRUE(
1117 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1118 MethodOnY));
1119 EXPECT_TRUE(
1120 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1121 MethodOnY));
1122 EXPECT_TRUE(
1123 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1124 MethodOnY));
1125 EXPECT_TRUE(
1126 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1127 MethodOnY));
1128 EXPECT_TRUE(
1129 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1130 MethodOnY));
1131
1132 EXPECT_TRUE(matches(
1133 "class Y {"
1134 " public: virtual void x();"
1135 "};"
1136 "class X : public Y {"
1137 " public: virtual void x();"
1138 "};"
1139 "void z() { X *x; x->Y::x(); }", MethodOnY));
1140}
1141
1142TEST(Matcher, VariableUsage) {
1143 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001144 declRefExpr(to(
1145 varDecl(hasInitializer(
1146 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001147
1148 EXPECT_TRUE(matches(
1149 "class Y {"
1150 " public:"
1151 " bool x() const;"
1152 "};"
1153 "void z(const Y &y) {"
1154 " bool b = y.x();"
1155 " if (b) {}"
1156 "}", Reference));
1157
1158 EXPECT_TRUE(notMatches(
1159 "class Y {"
1160 " public:"
1161 " bool x() const;"
1162 "};"
1163 "void z(const Y &y) {"
1164 " bool b = y.x();"
1165 "}", Reference));
1166}
1167
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001168TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001169 EXPECT_TRUE(matches(
1170 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001171 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001172}
1173
Manuel Klimek4da21662012-07-06 05:48:52 +00001174TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001175 StatementMatcher CallOnVariableY =
1176 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001177
1178 EXPECT_TRUE(matches(
1179 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1180 EXPECT_TRUE(matches(
1181 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1182 EXPECT_TRUE(matches(
1183 "class Y { public: void x(); };"
1184 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1185 EXPECT_TRUE(matches(
1186 "class Y { public: void x(); };"
1187 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1188 EXPECT_TRUE(notMatches(
1189 "class Y { public: void x(); };"
1190 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1191 CallOnVariableY));
1192}
1193
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001194TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1195 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1196 unaryExprOrTypeTraitExpr()));
1197 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1198 alignOfExpr(anything())));
1199 // FIXME: Uncomment once alignof is enabled.
1200 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1201 // unaryExprOrTypeTraitExpr()));
1202 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1203 // sizeOfExpr()));
1204}
1205
1206TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1207 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1208 hasArgumentOfType(asString("int")))));
1209 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1210 hasArgumentOfType(asString("float")))));
1211 EXPECT_TRUE(matches(
1212 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001213 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001214 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001215 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001216}
1217
Manuel Klimek4da21662012-07-06 05:48:52 +00001218TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001219 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001220}
1221
1222TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001223 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001224}
1225
1226TEST(MemberExpression, MatchesVariable) {
1227 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001228 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001229 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001230 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001231 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001232 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001233}
1234
1235TEST(MemberExpression, MatchesStaticVariable) {
1236 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001237 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001238 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001239 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001240 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001241 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001242}
1243
Daniel Jasper6a124492012-07-12 08:50:38 +00001244TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001245 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1246 EXPECT_TRUE(matches(
1247 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1248 callExpr(hasArgument(0, declRefExpr(
1249 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001250}
1251
1252TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001253 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001254 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001255 callExpr(hasArgument(0, declRefExpr(
1256 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001257}
1258
Manuel Klimek4da21662012-07-06 05:48:52 +00001259TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1260 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001261 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001262 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001263 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001264 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001265 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001266}
1267
1268TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1269 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001270 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001271 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001272 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001273 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001274 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001275}
1276
1277TEST(IsArrow, MatchesMemberCallsViaArrow) {
1278 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001279 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001280 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001281 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001282 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001283 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001284}
1285
1286TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001287 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001288
1289 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1290 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1291}
1292
1293TEST(Callee, MatchesMemberExpressions) {
1294 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001295 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001296 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001297 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001298}
1299
1300TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001301 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001302
1303 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1304 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1305
Stephen Hines651f13c2014-04-23 16:59:28 -07001306 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
1307 llvm::Triple::Win32) {
1308 // FIXME: Make this work for MSVC.
1309 // Dependent contexts, but a non-dependent call.
1310 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1311 CallFunctionF));
1312 EXPECT_TRUE(
1313 matches("void f(); template <int N> struct S { void g() { f(); } };",
1314 CallFunctionF));
1315 }
Manuel Klimek4da21662012-07-06 05:48:52 +00001316
1317 // Depedent calls don't match.
1318 EXPECT_TRUE(
1319 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1320 CallFunctionF));
1321 EXPECT_TRUE(
1322 notMatches("void f(int);"
1323 "template <typename T> struct S { void g(T t) { f(t); } };",
1324 CallFunctionF));
1325}
1326
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001327TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1328 EXPECT_TRUE(
1329 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001330 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001331}
1332
1333TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1334 EXPECT_TRUE(
1335 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001336 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001337}
1338
1339TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1340 EXPECT_TRUE(
1341 notMatches("void g(); template <typename T> void f(T t) {}"
1342 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001343 functionTemplateDecl(hasName("f"),
1344 hasDescendant(declRefExpr(to(
1345 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001346}
1347
Manuel Klimek4da21662012-07-06 05:48:52 +00001348TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001349 StatementMatcher CallArgumentY = callExpr(
1350 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001351
1352 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1353 EXPECT_TRUE(
1354 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1355 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1356
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001357 StatementMatcher WrongIndex = callExpr(
1358 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001359 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1360}
1361
1362TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001363 StatementMatcher CallArgumentY = callExpr(
1364 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001365 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1366 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1367 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1368}
1369
1370TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001371 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001372
1373 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1374 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1375 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1376}
1377
Daniel Jasper36e29d62012-12-04 11:54:27 +00001378TEST(Matcher, ParameterCount) {
1379 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1380 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1381 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1382 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1383 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1384}
1385
Manuel Klimek4da21662012-07-06 05:48:52 +00001386TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001387 DeclarationMatcher ReferenceClassX = varDecl(
1388 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001389 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1390 ReferenceClassX));
1391 EXPECT_TRUE(
1392 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
Michael Han4b6730d2013-09-11 15:53:29 +00001393 // The match here is on the implicit copy constructor code for
1394 // class X, not on code 'X x = y'.
Manuel Klimek4da21662012-07-06 05:48:52 +00001395 EXPECT_TRUE(
Michael Han4b6730d2013-09-11 15:53:29 +00001396 matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1397 EXPECT_TRUE(
1398 notMatches("class X {}; extern X x;", ReferenceClassX));
Manuel Klimek4da21662012-07-06 05:48:52 +00001399 EXPECT_TRUE(
1400 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1401}
1402
Edwin Vane6a19a972013-03-06 17:02:57 +00001403TEST(QualType, hasCanonicalType) {
1404 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1405 "int a;"
1406 "int_ref b = a;",
1407 varDecl(hasType(qualType(referenceType())))));
1408 EXPECT_TRUE(
1409 matches("typedef int &int_ref;"
1410 "int a;"
1411 "int_ref b = a;",
1412 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1413}
1414
Edwin Vane7b69cd02013-04-02 18:15:55 +00001415TEST(QualType, hasLocalQualifiers) {
1416 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1417 varDecl(hasType(hasLocalQualifiers()))));
1418 EXPECT_TRUE(matches("int *const j = nullptr;",
1419 varDecl(hasType(hasLocalQualifiers()))));
1420 EXPECT_TRUE(matches("int *volatile k;",
1421 varDecl(hasType(hasLocalQualifiers()))));
1422 EXPECT_TRUE(notMatches("int m;",
1423 varDecl(hasType(hasLocalQualifiers()))));
1424}
1425
Manuel Klimek4da21662012-07-06 05:48:52 +00001426TEST(HasParameter, CallsInnerMatcher) {
1427 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001428 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001429 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001430 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001431}
1432
1433TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1434 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001435 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001436}
1437
1438TEST(HasType, MatchesParameterVariableTypesStrictly) {
1439 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001440 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001441 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001442 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001443 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001444 methodDecl(hasParameter(0,
1445 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001446 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001447 methodDecl(hasParameter(0,
1448 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001449}
1450
1451TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1452 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001453 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001454 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001455 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001456}
1457
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001458TEST(Returns, MatchesReturnTypes) {
1459 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001460 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001461 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001462 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001463 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001464 functionDecl(returns(hasDeclaration(
1465 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001466}
1467
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001468TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001469 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1470 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1471 functionDecl(isExternC())));
1472 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001473}
1474
Manuel Klimek4da21662012-07-06 05:48:52 +00001475TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1476 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001477 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001478}
1479
1480TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1481 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001482 methodDecl(hasAnyParameter(hasType(pointsTo(
1483 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001484}
1485
Stephen Hines651f13c2014-04-23 16:59:28 -07001486TEST(HasName, MatchesParameterVariableDeclarations) {
Manuel Klimek4da21662012-07-06 05:48:52 +00001487 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001488 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001489 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001490 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001491}
1492
Daniel Jasper371f9392012-08-01 08:40:24 +00001493TEST(Matcher, MatchesClassTemplateSpecialization) {
1494 EXPECT_TRUE(matches("template<typename T> struct A {};"
1495 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001496 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001497 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001498 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001499 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001500 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001501}
1502
Manuel Klimek1a68afd2013-06-20 13:08:29 +00001503TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1504 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1505 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1506}
1507
1508TEST(ParmVarDecl, MatchesParmVars) {
1509 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1510 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1511}
1512
Daniel Jasper371f9392012-08-01 08:40:24 +00001513TEST(Matcher, MatchesTypeTemplateArgument) {
1514 EXPECT_TRUE(matches(
1515 "template<typename T> struct B {};"
1516 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001517 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001518 asString("int"))))));
1519}
1520
1521TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1522 EXPECT_TRUE(matches(
1523 "struct B { int next; };"
1524 "template<int(B::*next_ptr)> struct A {};"
1525 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001526 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1527 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001528
1529 EXPECT_TRUE(notMatches(
1530 "template <typename T> struct A {};"
1531 "A<int> a;",
1532 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1533 refersToDeclaration(decl())))));
Stephen Hines651f13c2014-04-23 16:59:28 -07001534
1535 EXPECT_TRUE(matches(
1536 "struct B { int next; };"
1537 "template<int(B::*next_ptr)> struct A {};"
1538 "A<&B::next> a;",
1539 templateSpecializationType(hasAnyTemplateArgument(isExpr(
1540 hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))));
1541
1542 EXPECT_TRUE(notMatches(
1543 "template <typename T> struct A {};"
1544 "A<int> a;",
1545 templateSpecializationType(hasAnyTemplateArgument(
1546 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001547}
1548
1549TEST(Matcher, MatchesSpecificArgument) {
1550 EXPECT_TRUE(matches(
1551 "template<typename T, typename U> class A {};"
1552 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001553 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001554 1, refersToType(asString("int"))))));
1555 EXPECT_TRUE(notMatches(
1556 "template<typename T, typename U> class A {};"
1557 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001558 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001559 1, refersToType(asString("int"))))));
Stephen Hines651f13c2014-04-23 16:59:28 -07001560
1561 EXPECT_TRUE(matches(
1562 "template<typename T, typename U> class A {};"
1563 "A<bool, int> a;",
1564 templateSpecializationType(hasTemplateArgument(
1565 1, refersToType(asString("int"))))));
1566 EXPECT_TRUE(notMatches(
1567 "template<typename T, typename U> class A {};"
1568 "A<int, bool> a;",
1569 templateSpecializationType(hasTemplateArgument(
1570 1, refersToType(asString("int"))))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001571}
1572
Daniel Jasperf3197e92013-02-25 12:02:08 +00001573TEST(Matcher, MatchesAccessSpecDecls) {
1574 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1575 EXPECT_TRUE(
1576 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1577 EXPECT_TRUE(
1578 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1579 EXPECT_TRUE(
1580 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1581
1582 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1583}
1584
Edwin Vane5771a2f2013-04-09 20:46:36 +00001585TEST(Matcher, MatchesVirtualMethod) {
1586 EXPECT_TRUE(matches("class X { virtual int f(); };",
1587 methodDecl(isVirtual(), hasName("::X::f"))));
1588 EXPECT_TRUE(notMatches("class X { int f(); };",
1589 methodDecl(isVirtual())));
1590}
1591
Stephen Hines651f13c2014-04-23 16:59:28 -07001592TEST(Matcher, MatchesPureMethod) {
1593 EXPECT_TRUE(matches("class X { virtual int f() = 0; };",
1594 methodDecl(isPure(), hasName("::X::f"))));
1595 EXPECT_TRUE(notMatches("class X { int f(); };",
1596 methodDecl(isPure())));
1597}
1598
Edwin Vane32a6ebc2013-05-09 17:00:17 +00001599TEST(Matcher, MatchesConstMethod) {
1600 EXPECT_TRUE(matches("struct A { void foo() const; };",
1601 methodDecl(isConst())));
1602 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1603 methodDecl(isConst())));
1604}
1605
Edwin Vane5771a2f2013-04-09 20:46:36 +00001606TEST(Matcher, MatchesOverridingMethod) {
1607 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1608 "class Y : public X { int f(); };",
1609 methodDecl(isOverride(), hasName("::Y::f"))));
1610 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1611 "class Y : public X { int f(); };",
1612 methodDecl(isOverride(), hasName("::X::f"))));
1613 EXPECT_TRUE(notMatches("class X { int f(); }; "
1614 "class Y : public X { int f(); };",
1615 methodDecl(isOverride())));
1616 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1617 methodDecl(isOverride())));
1618}
1619
Manuel Klimek4da21662012-07-06 05:48:52 +00001620TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001621 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001622
1623 EXPECT_TRUE(
1624 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1625 EXPECT_TRUE(
1626 matches("class X { public: X(); }; void x() { X x = X(); }",
1627 Constructor));
1628 EXPECT_TRUE(
1629 matches("class X { public: X(int); }; void x() { X x = 0; }",
1630 Constructor));
1631 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1632}
1633
1634TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001635 StatementMatcher Constructor = constructExpr(
1636 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001637
1638 EXPECT_TRUE(
1639 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1640 Constructor));
1641 EXPECT_TRUE(
1642 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1643 Constructor));
1644 EXPECT_TRUE(
1645 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1646 Constructor));
1647 EXPECT_TRUE(
1648 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1649 Constructor));
1650
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001651 StatementMatcher WrongIndex = constructExpr(
1652 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001653 EXPECT_TRUE(
1654 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1655 WrongIndex));
1656}
1657
1658TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001659 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001660
1661 EXPECT_TRUE(
1662 matches("class X { public: X(int); }; void x() { X x(0); }",
1663 Constructor1Arg));
1664 EXPECT_TRUE(
1665 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1666 Constructor1Arg));
1667 EXPECT_TRUE(
1668 matches("class X { public: X(int); }; void x() { X x = 0; }",
1669 Constructor1Arg));
1670 EXPECT_TRUE(
1671 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1672 Constructor1Arg));
1673}
1674
Stephen Hines651f13c2014-04-23 16:59:28 -07001675TEST(Matcher, ConstructorListInitialization) {
1676 StatementMatcher ConstructorListInit = constructExpr(isListInitialization());
1677
1678 EXPECT_TRUE(
1679 matches("class X { public: X(int); }; void x() { X x{0}; }",
1680 ConstructorListInit));
1681 EXPECT_FALSE(
1682 matches("class X { public: X(int); }; void x() { X x(0); }",
1683 ConstructorListInit));
1684}
1685
Manuel Klimek70b9db92012-10-23 10:40:50 +00001686TEST(Matcher,ThisExpr) {
1687 EXPECT_TRUE(
1688 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1689 EXPECT_TRUE(
1690 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1691}
1692
Manuel Klimek4da21662012-07-06 05:48:52 +00001693TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001694 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001695
1696 std::string ClassString = "class string { public: string(); ~string(); }; ";
1697
1698 EXPECT_TRUE(
1699 matches(ClassString +
1700 "string GetStringByValue();"
1701 "void FunctionTakesString(string s);"
1702 "void run() { FunctionTakesString(GetStringByValue()); }",
1703 TempExpression));
1704
1705 EXPECT_TRUE(
1706 notMatches(ClassString +
1707 "string* GetStringPointer(); "
1708 "void FunctionTakesStringPtr(string* s);"
1709 "void run() {"
1710 " string* s = GetStringPointer();"
1711 " FunctionTakesStringPtr(GetStringPointer());"
1712 " FunctionTakesStringPtr(s);"
1713 "}",
1714 TempExpression));
1715
1716 EXPECT_TRUE(
1717 notMatches("class no_dtor {};"
1718 "no_dtor GetObjByValue();"
1719 "void ConsumeObj(no_dtor param);"
1720 "void run() { ConsumeObj(GetObjByValue()); }",
1721 TempExpression));
1722}
1723
Sam Panzere16acd32012-08-24 22:04:44 +00001724TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1725 std::string ClassString =
1726 "class string { public: string(); int length(); }; ";
1727
1728 EXPECT_TRUE(
1729 matches(ClassString +
1730 "string GetStringByValue();"
1731 "void FunctionTakesString(string s);"
1732 "void run() { FunctionTakesString(GetStringByValue()); }",
1733 materializeTemporaryExpr()));
1734
1735 EXPECT_TRUE(
1736 notMatches(ClassString +
1737 "string* GetStringPointer(); "
1738 "void FunctionTakesStringPtr(string* s);"
1739 "void run() {"
1740 " string* s = GetStringPointer();"
1741 " FunctionTakesStringPtr(GetStringPointer());"
1742 " FunctionTakesStringPtr(s);"
1743 "}",
1744 materializeTemporaryExpr()));
1745
1746 EXPECT_TRUE(
1747 notMatches(ClassString +
1748 "string GetStringByValue();"
1749 "void run() { int k = GetStringByValue().length(); }",
1750 materializeTemporaryExpr()));
1751
1752 EXPECT_TRUE(
1753 notMatches(ClassString +
1754 "string GetStringByValue();"
1755 "void run() { GetStringByValue(); }",
1756 materializeTemporaryExpr()));
1757}
1758
Manuel Klimek4da21662012-07-06 05:48:52 +00001759TEST(ConstructorDeclaration, SimpleCase) {
1760 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001761 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001762 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001763 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001764}
1765
1766TEST(ConstructorDeclaration, IsImplicit) {
1767 // This one doesn't match because the constructor is not added by the
1768 // compiler (it is not needed).
1769 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001770 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001771 // The compiler added the implicit default constructor.
1772 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001773 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001774 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001775 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001776}
1777
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001778TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1779 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001780 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001781}
1782
1783TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001784 EXPECT_TRUE(notMatches("class Foo {};",
1785 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001786}
1787
Manuel Klimek4da21662012-07-06 05:48:52 +00001788TEST(HasAnyConstructorInitializer, SimpleCase) {
1789 EXPECT_TRUE(notMatches(
1790 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001791 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001792 EXPECT_TRUE(matches(
1793 "class Foo {"
1794 " Foo() : foo_() { }"
1795 " int foo_;"
1796 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001797 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001798}
1799
1800TEST(HasAnyConstructorInitializer, ForField) {
1801 static const char Code[] =
1802 "class Baz { };"
1803 "class Foo {"
1804 " Foo() : foo_() { }"
1805 " Baz foo_;"
1806 " Baz bar_;"
1807 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001808 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1809 forField(hasType(recordDecl(hasName("Baz"))))))));
1810 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001811 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001812 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1813 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001814}
1815
1816TEST(HasAnyConstructorInitializer, WithInitializer) {
1817 static const char Code[] =
1818 "class Foo {"
1819 " Foo() : foo_(0) { }"
1820 " int foo_;"
1821 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001822 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001823 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001824 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001825 withInitializer(integerLiteral(equals(1)))))));
1826}
1827
1828TEST(HasAnyConstructorInitializer, IsWritten) {
1829 static const char Code[] =
1830 "struct Bar { Bar(){} };"
1831 "class Foo {"
1832 " Foo() : foo_() { }"
1833 " Bar foo_;"
1834 " Bar bar_;"
1835 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001836 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001837 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001838 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001839 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001840 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001841 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1842}
1843
1844TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001845 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001846
1847 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1848 EXPECT_TRUE(
1849 matches("class X { public: X(); }; void x() { new X(); }", New));
1850 EXPECT_TRUE(
1851 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1852 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1853}
1854
1855TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001856 StatementMatcher New = constructExpr(
1857 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001858
1859 EXPECT_TRUE(
1860 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1861 New));
1862 EXPECT_TRUE(
1863 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1864 New));
1865 EXPECT_TRUE(
1866 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1867 New));
1868
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001869 StatementMatcher WrongIndex = constructExpr(
1870 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001871 EXPECT_TRUE(
1872 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1873 WrongIndex));
1874}
1875
1876TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001877 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001878
1879 EXPECT_TRUE(
1880 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1881 EXPECT_TRUE(
1882 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1883 New));
1884}
1885
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001886TEST(Matcher, DeleteExpression) {
1887 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001888 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001889}
1890
Manuel Klimek4da21662012-07-06 05:48:52 +00001891TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001892 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001893
1894 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1895 EXPECT_TRUE(
1896 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1897 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1898}
1899
1900TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001901 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001902 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1903 // wide string
1904 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1905 // with escaped characters
1906 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1907 // no matching -- though the data type is the same, there is no string literal
1908 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1909}
1910
1911TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001912 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001913 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1914 // wide character
1915 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1916 // wide character, Hex encoded, NOT MATCHED!
1917 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1918 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1919}
1920
1921TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001922 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001923 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1924 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1925 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1926 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1927
1928 // Non-matching cases (character literals, float and double)
1929 EXPECT_TRUE(notMatches("int i = L'a';",
1930 HasIntLiteral)); // this is actually a character
1931 // literal cast to int
1932 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1933 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1934 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1935}
1936
Daniel Jasperc5ae7172013-07-26 18:52:58 +00001937TEST(Matcher, FloatLiterals) {
1938 StatementMatcher HasFloatLiteral = floatLiteral();
1939 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1940 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1941 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1942 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1943 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1944
1945 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1946}
1947
Daniel Jasper31f7c082012-10-01 13:40:41 +00001948TEST(Matcher, NullPtrLiteral) {
1949 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1950}
1951
Daniel Jasperb54b7642012-09-20 14:12:57 +00001952TEST(Matcher, AsmStatement) {
1953 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1954}
1955
Manuel Klimek4da21662012-07-06 05:48:52 +00001956TEST(Matcher, Conditions) {
1957 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1958
1959 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1960 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1961 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1962 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1963 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1964}
1965
1966TEST(MatchBinaryOperator, HasOperatorName) {
1967 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1968
1969 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1970 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1971}
1972
1973TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1974 StatementMatcher OperatorTrueFalse =
1975 binaryOperator(hasLHS(boolLiteral(equals(true))),
1976 hasRHS(boolLiteral(equals(false))));
1977
1978 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1979 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1980 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1981}
1982
1983TEST(MatchBinaryOperator, HasEitherOperand) {
1984 StatementMatcher HasOperand =
1985 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1986
1987 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1988 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1989 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1990}
1991
1992TEST(Matcher, BinaryOperatorTypes) {
1993 // Integration test that verifies the AST provides all binary operators in
1994 // a way we expect.
1995 // FIXME: Operator ','
1996 EXPECT_TRUE(
1997 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1998 EXPECT_TRUE(
1999 matches("bool b; bool c = (b = true);",
2000 binaryOperator(hasOperatorName("="))));
2001 EXPECT_TRUE(
2002 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
2003 EXPECT_TRUE(
2004 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
2005 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
2006 EXPECT_TRUE(
2007 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
2008 EXPECT_TRUE(
2009 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
2010 EXPECT_TRUE(
2011 matches("int i = 1; int j = (i <<= 2);",
2012 binaryOperator(hasOperatorName("<<="))));
2013 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
2014 EXPECT_TRUE(
2015 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
2016 EXPECT_TRUE(
2017 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
2018 EXPECT_TRUE(
2019 matches("int i = 1; int j = (i >>= 2);",
2020 binaryOperator(hasOperatorName(">>="))));
2021 EXPECT_TRUE(
2022 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
2023 EXPECT_TRUE(
2024 matches("int i = 42; int j = (i ^= 42);",
2025 binaryOperator(hasOperatorName("^="))));
2026 EXPECT_TRUE(
2027 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
2028 EXPECT_TRUE(
2029 matches("int i = 42; int j = (i %= 42);",
2030 binaryOperator(hasOperatorName("%="))));
2031 EXPECT_TRUE(
2032 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
2033 EXPECT_TRUE(
2034 matches("bool b = true && false;",
2035 binaryOperator(hasOperatorName("&&"))));
2036 EXPECT_TRUE(
2037 matches("bool b = true; bool c = (b &= false);",
2038 binaryOperator(hasOperatorName("&="))));
2039 EXPECT_TRUE(
2040 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
2041 EXPECT_TRUE(
2042 matches("bool b = true || false;",
2043 binaryOperator(hasOperatorName("||"))));
2044 EXPECT_TRUE(
2045 matches("bool b = true; bool c = (b |= false);",
2046 binaryOperator(hasOperatorName("|="))));
2047 EXPECT_TRUE(
2048 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
2049 EXPECT_TRUE(
2050 matches("int i = 42; int j = (i *= 23);",
2051 binaryOperator(hasOperatorName("*="))));
2052 EXPECT_TRUE(
2053 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
2054 EXPECT_TRUE(
2055 matches("int i = 42; int j = (i /= 23);",
2056 binaryOperator(hasOperatorName("/="))));
2057 EXPECT_TRUE(
2058 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
2059 EXPECT_TRUE(
2060 matches("int i = 42; int j = (i += 23);",
2061 binaryOperator(hasOperatorName("+="))));
2062 EXPECT_TRUE(
2063 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
2064 EXPECT_TRUE(
2065 matches("int i = 42; int j = (i -= 23);",
2066 binaryOperator(hasOperatorName("-="))));
2067 EXPECT_TRUE(
2068 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
2069 binaryOperator(hasOperatorName("->*"))));
2070 EXPECT_TRUE(
2071 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
2072 binaryOperator(hasOperatorName(".*"))));
2073
2074 // Member expressions as operators are not supported in matches.
2075 EXPECT_TRUE(
2076 notMatches("struct A { void x(A *a) { a->x(this); } };",
2077 binaryOperator(hasOperatorName("->"))));
2078
2079 // Initializer assignments are not represented as operator equals.
2080 EXPECT_TRUE(
2081 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2082
2083 // Array indexing is not represented as operator.
2084 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2085
2086 // Overloaded operators do not match at all.
2087 EXPECT_TRUE(notMatches(
2088 "struct A { bool operator&&(const A &a) const { return false; } };"
2089 "void x() { A a, b; a && b; }",
2090 binaryOperator()));
2091}
2092
2093TEST(MatchUnaryOperator, HasOperatorName) {
2094 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2095
2096 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2097 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2098}
2099
2100TEST(MatchUnaryOperator, HasUnaryOperand) {
2101 StatementMatcher OperatorOnFalse =
2102 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2103
2104 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2105 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2106}
2107
2108TEST(Matcher, UnaryOperatorTypes) {
2109 // Integration test that verifies the AST provides all unary operators in
2110 // a way we expect.
2111 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2112 EXPECT_TRUE(
2113 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2114 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2115 EXPECT_TRUE(
2116 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2117 EXPECT_TRUE(
2118 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2119 EXPECT_TRUE(
2120 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2121 EXPECT_TRUE(
2122 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2123 EXPECT_TRUE(
2124 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2125 EXPECT_TRUE(
2126 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2127 EXPECT_TRUE(
2128 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2129
2130 // We don't match conversion operators.
2131 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2132
2133 // Function calls are not represented as operator.
2134 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2135
2136 // Overloaded operators do not match at all.
2137 // FIXME: We probably want to add that.
2138 EXPECT_TRUE(notMatches(
2139 "struct A { bool operator!() const { return false; } };"
2140 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2141}
2142
2143TEST(Matcher, ConditionalOperator) {
2144 StatementMatcher Conditional = conditionalOperator(
2145 hasCondition(boolLiteral(equals(true))),
2146 hasTrueExpression(boolLiteral(equals(false))));
2147
2148 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2149 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2150 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2151
2152 StatementMatcher ConditionalFalse = conditionalOperator(
2153 hasFalseExpression(boolLiteral(equals(false))));
2154
2155 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2156 EXPECT_TRUE(
2157 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2158}
2159
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002160TEST(ArraySubscriptMatchers, ArraySubscripts) {
2161 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2162 arraySubscriptExpr()));
2163 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2164 arraySubscriptExpr()));
2165}
2166
2167TEST(ArraySubscriptMatchers, ArrayIndex) {
2168 EXPECT_TRUE(matches(
2169 "int i[2]; void f() { i[1] = 1; }",
2170 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2171 EXPECT_TRUE(matches(
2172 "int i[2]; void f() { 1[i] = 1; }",
2173 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2174 EXPECT_TRUE(notMatches(
2175 "int i[2]; void f() { i[1] = 1; }",
2176 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2177}
2178
2179TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2180 EXPECT_TRUE(matches(
2181 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002182 arraySubscriptExpr(hasBase(implicitCastExpr(
2183 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002184}
2185
Manuel Klimek4da21662012-07-06 05:48:52 +00002186TEST(Matcher, HasNameSupportsNamespaces) {
2187 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002188 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002189 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002190 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002191 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002192 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002193 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002194 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002195 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002196 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002197 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002198 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002199 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002200 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002201 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002202 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002203 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002204 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002205 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002206 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002207 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002208 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002209 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002210 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002211}
2212
2213TEST(Matcher, HasNameSupportsOuterClasses) {
2214 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002215 matches("class A { class B { class C; }; };",
2216 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002217 EXPECT_TRUE(
2218 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002219 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002220 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002221 matches("class A { class B { class C; }; };",
2222 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002223 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002224 matches("class A { class B { class C; }; };",
2225 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002226 EXPECT_TRUE(
2227 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002228 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002229 EXPECT_TRUE(
2230 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002231 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002232 EXPECT_TRUE(
2233 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002234 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002235 EXPECT_TRUE(
2236 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002237 recordDecl(hasName("::C"))));
2238 EXPECT_TRUE(
2239 notMatches("class A { class B { class C; }; };",
2240 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002241 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002242 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002243 EXPECT_TRUE(
2244 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002245 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002246}
2247
2248TEST(Matcher, IsDefinition) {
2249 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002250 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002251 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2252 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2253
2254 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002255 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002256 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2257 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2258
2259 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002260 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002261 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2262 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2263}
2264
2265TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002266 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002267 ofClass(hasName("X")))));
2268
2269 EXPECT_TRUE(
2270 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2271 EXPECT_TRUE(
2272 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2273 Constructor));
2274 EXPECT_TRUE(
2275 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2276 Constructor));
2277}
2278
2279TEST(Matcher, VisitsTemplateInstantiations) {
2280 EXPECT_TRUE(matches(
2281 "class A { public: void x(); };"
2282 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002283 "void f() { B<A> b; b.y(); }",
2284 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002285
2286 EXPECT_TRUE(matches(
2287 "class A { public: void x(); };"
2288 "class C {"
2289 " public:"
2290 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2291 "};"
2292 "void f() {"
2293 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002294 "}",
2295 recordDecl(hasName("C"),
2296 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002297}
2298
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002299TEST(Matcher, HandlesNullQualTypes) {
2300 // FIXME: Add a Type matcher so we can replace uses of this
2301 // variable with Type(True())
2302 const TypeMatcher AnyType = anything();
2303
2304 // We don't really care whether this matcher succeeds; we're testing that
2305 // it completes without crashing.
2306 EXPECT_TRUE(matches(
2307 "struct A { };"
2308 "template <typename T>"
2309 "void f(T t) {"
2310 " T local_t(t /* this becomes a null QualType in the AST */);"
2311 "}"
2312 "void g() {"
2313 " f(0);"
2314 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002315 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002316 anyOf(
2317 TypeMatcher(hasDeclaration(anything())),
2318 pointsTo(AnyType),
2319 references(AnyType)
2320 // Other QualType matchers should go here.
2321 ))))));
2322}
2323
Manuel Klimek4da21662012-07-06 05:48:52 +00002324// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002325AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002326 // Make sure all special variables are used: node, match_finder,
2327 // bound_nodes_builder, and the parameter named 'AMatcher'.
2328 return AMatcher.matches(Node, Finder, Builder);
2329}
2330
2331TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002332 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002333
2334 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002335 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002336
2337 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002338 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002339
2340 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002341 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002342}
2343
2344AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +00002345 polymorphicHas,
2346 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2347 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002348 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002349 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002350 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2351 ASTMatchFinder::BK_First);
2352}
2353
2354TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002355 DeclarationMatcher HasClassB =
2356 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002357
2358 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002359 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002360
2361 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002362 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002363
2364 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002365 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002366
2367 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002368 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002369
2370 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2371}
2372
2373TEST(For, FindsForLoops) {
2374 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2375 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002376 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2377 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002378 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002379}
2380
Daniel Jasper6a124492012-07-12 08:50:38 +00002381TEST(For, ForLoopInternals) {
2382 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2383 forStmt(hasCondition(anything()))));
2384 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2385 forStmt(hasLoopInit(anything()))));
2386}
2387
Stephen Hines651f13c2014-04-23 16:59:28 -07002388TEST(For, ForRangeLoopInternals) {
2389 EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2390 forRangeStmt(hasLoopVariable(anything()))));
2391}
2392
Daniel Jasper6a124492012-07-12 08:50:38 +00002393TEST(For, NegativeForLoopInternals) {
2394 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002395 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002396 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2397 forStmt(hasLoopInit(anything()))));
2398}
2399
Manuel Klimek4da21662012-07-06 05:48:52 +00002400TEST(For, ReportsNoFalsePositives) {
2401 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2402 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2403}
2404
2405TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002406 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2407 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2408 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002409}
2410
2411TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2412 // It's not a compound statement just because there's "{}" in the source
2413 // text. This is an AST search, not grep.
2414 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002415 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002416 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002417 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002418}
2419
Daniel Jasper6a124492012-07-12 08:50:38 +00002420TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002421 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002422 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002423 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002424 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002425 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002426 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002427 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002428 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002429}
2430
2431TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2432 // The simplest case: every compound statement is in a function
2433 // definition, and the function body itself must be a compound
2434 // statement.
2435 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002436 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002437}
2438
2439TEST(HasAnySubstatement, IsNotRecursive) {
2440 // It's really "has any immediate substatement".
2441 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002442 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002443}
2444
2445TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2446 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002447 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002448}
2449
2450TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2451 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002452 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002453}
2454
2455TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2456 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002457 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002458 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002459 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002460}
2461
2462TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2463 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002464 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002465 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002466 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002467 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002468 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002469}
2470
2471TEST(StatementCountIs, WorksWithMultipleStatements) {
2472 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002473 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002474}
2475
2476TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2477 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002478 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002479 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002480 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002481 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002482 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002483 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002484 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002485}
2486
2487TEST(Member, WorksInSimplestCase) {
2488 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002489 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002490}
2491
2492TEST(Member, DoesNotMatchTheBaseExpression) {
2493 // Don't pick out the wrong part of the member expression, this should
2494 // be checking the member (name) only.
2495 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002496 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002497}
2498
2499TEST(Member, MatchesInMemberFunctionCall) {
2500 EXPECT_TRUE(matches("void f() {"
2501 " struct { void first() {}; } s;"
2502 " s.first();"
2503 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002504 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002505}
2506
Daniel Jasperc711af22012-10-23 15:46:39 +00002507TEST(Member, MatchesMember) {
2508 EXPECT_TRUE(matches(
2509 "struct A { int i; }; void f() { A a; a.i = 2; }",
2510 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2511 EXPECT_TRUE(notMatches(
2512 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2513 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2514}
2515
Daniel Jasperf3197e92013-02-25 12:02:08 +00002516TEST(Member, UnderstandsAccess) {
2517 EXPECT_TRUE(matches(
2518 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2519 EXPECT_TRUE(notMatches(
2520 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2521 EXPECT_TRUE(notMatches(
2522 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2523
2524 EXPECT_TRUE(notMatches(
2525 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2526 EXPECT_TRUE(notMatches(
2527 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2528 EXPECT_TRUE(matches(
2529 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2530
2531 EXPECT_TRUE(notMatches(
2532 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2533 EXPECT_TRUE(matches("class A { protected: int i; };",
2534 fieldDecl(isProtected(), hasName("i"))));
2535 EXPECT_TRUE(notMatches(
2536 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2537
2538 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2539 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2540 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2541 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2542}
2543
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002544TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002545 // Fails in C++11 mode
2546 EXPECT_TRUE(matchesConditionally(
2547 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2548 "class X { void *operator new(std::size_t); };",
2549 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002550
2551 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002552 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002553
Daniel Jasper31f7c082012-10-01 13:40:41 +00002554 // Fails in C++11 mode
2555 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002556 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2557 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002558 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002559}
2560
Manuel Klimek4da21662012-07-06 05:48:52 +00002561TEST(HasObjectExpression, DoesNotMatchMember) {
2562 EXPECT_TRUE(notMatches(
2563 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002564 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002565}
2566
2567TEST(HasObjectExpression, MatchesBaseOfVariable) {
2568 EXPECT_TRUE(matches(
2569 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002570 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002571 EXPECT_TRUE(matches(
2572 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002573 memberExpr(hasObjectExpression(
2574 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002575}
2576
2577TEST(HasObjectExpression,
2578 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2579 EXPECT_TRUE(matches(
2580 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002581 memberExpr(hasObjectExpression(
2582 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002583 EXPECT_TRUE(matches(
2584 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002585 memberExpr(hasObjectExpression(
2586 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002587}
2588
2589TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002590 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2591 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2592 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2593 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002594}
2595
2596TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002597 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002598}
2599
2600TEST(IsConstQualified, MatchesConstInt) {
2601 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002602 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002603}
2604
2605TEST(IsConstQualified, MatchesConstPointer) {
2606 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002607 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002608}
2609
2610TEST(IsConstQualified, MatchesThroughTypedef) {
2611 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002612 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002613 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002614 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002615}
2616
2617TEST(IsConstQualified, DoesNotMatchInappropriately) {
2618 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002619 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002620 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002621 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002622}
2623
Sam Panzer089e5b32012-08-16 16:58:10 +00002624TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002625 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2626 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2627 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2628 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002629}
2630TEST(CastExpression, MatchesImplicitCasts) {
2631 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002632 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002633 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002634 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002635}
2636
2637TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002638 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2639 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2640 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2641 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002642}
2643
Manuel Klimek4da21662012-07-06 05:48:52 +00002644TEST(ReinterpretCast, MatchesSimpleCase) {
2645 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002646 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002647}
2648
2649TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002650 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002651 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002652 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002653 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002654 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002655 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2656 "B b;"
2657 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002658 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002659}
2660
2661TEST(FunctionalCast, MatchesSimpleCase) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002662 std::string foo_class = "class Foo { public: Foo(const char*); };";
Manuel Klimek4da21662012-07-06 05:48:52 +00002663 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002664 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002665}
2666
2667TEST(FunctionalCast, DoesNotMatchOtherCasts) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002668 std::string FooClass = "class Foo { public: Foo(const char*); };";
Manuel Klimek4da21662012-07-06 05:48:52 +00002669 EXPECT_TRUE(
2670 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002671 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002672 EXPECT_TRUE(
2673 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002674 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002675}
2676
2677TEST(DynamicCast, MatchesSimpleCase) {
2678 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2679 "B b;"
2680 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002681 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002682}
2683
2684TEST(StaticCast, MatchesSimpleCase) {
2685 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002686 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002687}
2688
2689TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002690 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002691 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002692 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002693 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002694 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002695 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2696 "B b;"
2697 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002698 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002699}
2700
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002701TEST(CStyleCast, MatchesSimpleCase) {
2702 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2703}
2704
2705TEST(CStyleCast, DoesNotMatchOtherCasts) {
2706 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2707 "char q, *r = const_cast<char*>(&q);"
2708 "void* s = reinterpret_cast<char*>(&s);"
2709 "struct B { virtual ~B() {} }; struct D : B {};"
2710 "B b;"
2711 "D* t = dynamic_cast<D*>(&b);",
2712 cStyleCastExpr()));
2713}
2714
Manuel Klimek4da21662012-07-06 05:48:52 +00002715TEST(HasDestinationType, MatchesSimpleCase) {
2716 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002717 staticCastExpr(hasDestinationType(
2718 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002719}
2720
Sam Panzer089e5b32012-08-16 16:58:10 +00002721TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2722 // This test creates an implicit const cast.
2723 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002724 implicitCastExpr(
2725 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002726 // This test creates an implicit array-to-pointer cast.
2727 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002728 implicitCastExpr(hasImplicitDestinationType(
2729 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002730}
2731
2732TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2733 // This test creates an implicit cast from int to char.
2734 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002735 implicitCastExpr(hasImplicitDestinationType(
2736 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002737 // This test creates an implicit array-to-pointer cast.
2738 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002739 implicitCastExpr(hasImplicitDestinationType(
2740 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002741}
2742
2743TEST(ImplicitCast, MatchesSimpleCase) {
2744 // This test creates an implicit const cast.
2745 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002746 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002747 // This test creates an implicit cast from int to char.
2748 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002749 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002750 // This test creates an implicit array-to-pointer cast.
2751 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002752 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002753}
2754
2755TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002756 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002757 // are present, and that it ignores explicit and paren casts.
2758
2759 // These two test cases have no casts.
2760 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002761 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002762 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002763 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002764
2765 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002766 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002767 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002768 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002769
2770 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002771 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002772}
2773
2774TEST(IgnoringImpCasts, MatchesImpCasts) {
2775 // This test checks that ignoringImpCasts matches when implicit casts are
2776 // present and its inner matcher alone does not match.
2777 // Note that this test creates an implicit const cast.
2778 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002779 varDecl(hasInitializer(ignoringImpCasts(
2780 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002781 // This test creates an implict cast from int to char.
2782 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002783 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002784 integerLiteral(equals(0)))))));
2785}
2786
2787TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2788 // These tests verify that ignoringImpCasts does not match if the inner
2789 // matcher does not match.
2790 // Note that the first test creates an implicit const cast.
2791 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002792 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002793 unless(anything()))))));
2794 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002795 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002796 unless(anything()))))));
2797
2798 // These tests verify that ignoringImplictCasts does not look through explicit
2799 // casts or parentheses.
2800 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002801 varDecl(hasInitializer(ignoringImpCasts(
2802 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002803 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002804 varDecl(hasInitializer(ignoringImpCasts(
2805 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002806 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002807 varDecl(hasInitializer(ignoringImpCasts(
2808 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002809 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002810 varDecl(hasInitializer(ignoringImpCasts(
2811 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002812}
2813
2814TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2815 // This test verifies that expressions that do not have implicit casts
2816 // still match the inner matcher.
2817 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002818 varDecl(hasInitializer(ignoringImpCasts(
2819 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002820}
2821
2822TEST(IgnoringParenCasts, MatchesParenCasts) {
2823 // This test checks that ignoringParenCasts matches when parentheses and/or
2824 // casts are present and its inner matcher alone does not match.
2825 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002826 varDecl(hasInitializer(ignoringParenCasts(
2827 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002828 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002829 varDecl(hasInitializer(ignoringParenCasts(
2830 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002831
2832 // This test creates an implict cast from int to char in addition to the
2833 // parentheses.
2834 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002835 varDecl(hasInitializer(ignoringParenCasts(
2836 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002837
2838 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002839 varDecl(hasInitializer(ignoringParenCasts(
2840 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002841 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002842 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002843 integerLiteral(equals(0)))))));
2844}
2845
2846TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2847 // This test verifies that expressions that do not have any casts still match.
2848 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002849 varDecl(hasInitializer(ignoringParenCasts(
2850 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002851}
2852
2853TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2854 // These tests verify that ignoringImpCasts does not match if the inner
2855 // matcher does not match.
2856 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002857 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002858 unless(anything()))))));
2859
2860 // This test creates an implicit cast from int to char in addition to the
2861 // parentheses.
2862 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002863 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002864 unless(anything()))))));
2865
2866 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002867 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002868 unless(anything()))))));
2869}
2870
2871TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2872 // This test checks that ignoringParenAndImpCasts matches when
2873 // parentheses and/or implicit casts are present and its inner matcher alone
2874 // does not match.
2875 // Note that this test creates an implicit const cast.
2876 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002877 varDecl(hasInitializer(ignoringParenImpCasts(
2878 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002879 // This test creates an implicit cast from int to char.
2880 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002881 varDecl(hasInitializer(ignoringParenImpCasts(
2882 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002883}
2884
2885TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2886 // This test verifies that expressions that do not have parentheses or
2887 // implicit casts still match.
2888 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002889 varDecl(hasInitializer(ignoringParenImpCasts(
2890 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002891 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002892 varDecl(hasInitializer(ignoringParenImpCasts(
2893 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002894}
2895
2896TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2897 // These tests verify that ignoringParenImpCasts does not match if
2898 // the inner matcher does not match.
2899 // This test creates an implicit cast.
2900 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002901 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002902 unless(anything()))))));
2903 // These tests verify that ignoringParenAndImplictCasts does not look
2904 // through explicit casts.
2905 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002906 varDecl(hasInitializer(ignoringParenImpCasts(
2907 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002908 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002909 varDecl(hasInitializer(ignoringParenImpCasts(
2910 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002911 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002912 varDecl(hasInitializer(ignoringParenImpCasts(
2913 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002914}
2915
Manuel Klimek715c9562012-07-25 10:02:02 +00002916TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002917 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2918 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002919 implicitCastExpr(
2920 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002921}
2922
Manuel Klimek715c9562012-07-25 10:02:02 +00002923TEST(HasSourceExpression, MatchesExplicitCasts) {
2924 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002925 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002926 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002927 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002928}
2929
Manuel Klimek4da21662012-07-06 05:48:52 +00002930TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002931 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002932}
2933
2934TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002935 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002936}
2937
2938TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002939 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002940}
2941
2942TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002943 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002944}
2945
Stephen Hines651f13c2014-04-23 16:59:28 -07002946TEST(ExprWithCleanups, MatchesExprWithCleanups) {
2947 EXPECT_TRUE(matches("struct Foo { ~Foo(); };"
2948 "const Foo f = Foo();",
2949 varDecl(hasInitializer(exprWithCleanups()))));
2950 EXPECT_FALSE(matches("struct Foo { };"
2951 "const Foo f = Foo();",
2952 varDecl(hasInitializer(exprWithCleanups()))));
2953}
2954
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002955TEST(InitListExpression, MatchesInitListExpression) {
2956 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2957 initListExpr(hasType(asString("int [2]")))));
2958 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002959 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002960}
2961
2962TEST(UsingDeclaration, MatchesUsingDeclarations) {
2963 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2964 usingDecl()));
2965}
2966
2967TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2968 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2969 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2970}
2971
2972TEST(UsingDeclaration, MatchesSpecificTarget) {
2973 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2974 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002975 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002976 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2977 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002978 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002979}
2980
2981TEST(UsingDeclaration, ThroughUsingDeclaration) {
2982 EXPECT_TRUE(matches(
2983 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002984 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002985 EXPECT_TRUE(notMatches(
2986 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002987 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002988}
2989
Sam Panzer425f41b2012-08-16 17:20:59 +00002990TEST(SingleDecl, IsSingleDecl) {
2991 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002992 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002993 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2994 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2995 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2996 SingleDeclStmt));
2997}
2998
2999TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003000 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00003001
3002 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003003 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00003004 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003005 declStmt(containsDeclaration(0, MatchesInit),
3006 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00003007 unsigned WrongIndex = 42;
3008 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003009 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00003010 MatchesInit))));
3011}
3012
3013TEST(DeclCount, DeclCountIsCorrect) {
3014 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003015 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00003016 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003017 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00003018 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003019 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00003020}
3021
Manuel Klimek4da21662012-07-06 05:48:52 +00003022TEST(While, MatchesWhileLoops) {
3023 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
3024 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
3025 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
3026}
3027
3028TEST(Do, MatchesDoLoops) {
3029 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
3030 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
3031}
3032
3033TEST(Do, DoesNotMatchWhileLoops) {
3034 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
3035}
3036
3037TEST(SwitchCase, MatchesCase) {
3038 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
3039 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
3040 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
3041 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
3042}
3043
Daniel Jasperb54b7642012-09-20 14:12:57 +00003044TEST(SwitchCase, MatchesSwitch) {
3045 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
3046 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
3047 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
3048 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
3049}
3050
Peter Collingbourneacf02712013-05-10 11:52:02 +00003051TEST(SwitchCase, MatchesEachCase) {
3052 EXPECT_TRUE(notMatches("void x() { switch(42); }",
3053 switchStmt(forEachSwitchCase(caseStmt()))));
3054 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
3055 switchStmt(forEachSwitchCase(caseStmt()))));
3056 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
3057 switchStmt(forEachSwitchCase(caseStmt()))));
3058 EXPECT_TRUE(notMatches(
3059 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
3060 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
3061 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
3062 switchStmt(forEachSwitchCase(
3063 caseStmt(hasCaseConstant(integerLiteral()))))));
3064 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
3065 switchStmt(forEachSwitchCase(
3066 caseStmt(hasCaseConstant(integerLiteral()))))));
3067 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
3068 switchStmt(forEachSwitchCase(
3069 caseStmt(hasCaseConstant(integerLiteral()))))));
3070 EXPECT_TRUE(matchAndVerifyResultTrue(
3071 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
3072 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
3073 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
3074}
3075
Manuel Klimek06963012013-07-19 11:50:54 +00003076TEST(ForEachConstructorInitializer, MatchesInitializers) {
3077 EXPECT_TRUE(matches(
3078 "struct X { X() : i(42), j(42) {} int i, j; };",
3079 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
3080}
3081
Daniel Jasperb54b7642012-09-20 14:12:57 +00003082TEST(ExceptionHandling, SimpleCases) {
3083 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
3084 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
3085 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
3086 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
3087 throwExpr()));
3088 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
3089 throwExpr()));
3090}
3091
Manuel Klimek4da21662012-07-06 05:48:52 +00003092TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
3093 EXPECT_TRUE(notMatches(
3094 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003095 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003096 EXPECT_TRUE(notMatches(
3097 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003098 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003099}
3100
3101TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3102 EXPECT_TRUE(matches(
3103 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003104 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003105}
3106
3107TEST(ForEach, BindsOneNode) {
3108 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003109 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003110 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003111}
3112
3113TEST(ForEach, BindsMultipleNodes) {
3114 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003115 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003116 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003117}
3118
3119TEST(ForEach, BindsRecursiveCombinations) {
3120 EXPECT_TRUE(matchAndVerifyResultTrue(
3121 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003122 recordDecl(hasName("C"),
3123 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003124 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003125}
3126
3127TEST(ForEachDescendant, BindsOneNode) {
3128 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003129 recordDecl(hasName("C"),
3130 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003131 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003132}
3133
Daniel Jasper5f684e92012-11-16 18:39:22 +00003134TEST(ForEachDescendant, NestedForEachDescendant) {
3135 DeclarationMatcher m = recordDecl(
3136 isDefinition(), decl().bind("x"), hasName("C"));
3137 EXPECT_TRUE(matchAndVerifyResultTrue(
3138 "class A { class B { class C {}; }; };",
3139 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3140 new VerifyIdIsBoundTo<Decl>("x", "C")));
3141
Manuel Klimek054d0492013-06-19 15:42:45 +00003142 // Check that a partial match of 'm' that binds 'x' in the
3143 // first part of anyOf(m, anything()) will not overwrite the
3144 // binding created by the earlier binding in the hasDescendant.
3145 EXPECT_TRUE(matchAndVerifyResultTrue(
3146 "class A { class B { class C {}; }; };",
3147 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3148 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper5f684e92012-11-16 18:39:22 +00003149}
3150
Manuel Klimek4da21662012-07-06 05:48:52 +00003151TEST(ForEachDescendant, BindsMultipleNodes) {
3152 EXPECT_TRUE(matchAndVerifyResultTrue(
3153 "class C { class D { int x; int y; }; "
3154 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003155 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003156 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003157}
3158
3159TEST(ForEachDescendant, BindsRecursiveCombinations) {
3160 EXPECT_TRUE(matchAndVerifyResultTrue(
3161 "class C { class D { "
3162 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003163 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3164 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003165 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003166}
3167
Manuel Klimek054d0492013-06-19 15:42:45 +00003168TEST(ForEachDescendant, BindsCombinations) {
3169 EXPECT_TRUE(matchAndVerifyResultTrue(
3170 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3171 "(true) {} }",
3172 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3173 forEachDescendant(whileStmt().bind("while"))),
3174 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3175}
3176
3177TEST(Has, DoesNotDeleteBindings) {
3178 EXPECT_TRUE(matchAndVerifyResultTrue(
3179 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3180 new VerifyIdIsBoundTo<Decl>("x", 1)));
3181}
3182
3183TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3184 // Those matchers cover all the cases where an inner matcher is called
3185 // and there is not a 1:1 relationship between the match of the outer
3186 // matcher and the match of the inner matcher.
3187 // The pattern to look for is:
3188 // ... return InnerMatcher.matches(...); ...
3189 // In which case no special handling is needed.
3190 //
3191 // On the other hand, if there are multiple alternative matches
3192 // (for example forEach*) or matches might be discarded (for example has*)
3193 // the implementation must make sure that the discarded matches do not
3194 // affect the bindings.
3195 // When new such matchers are added, add a test here that:
3196 // - matches a simple node, and binds it as the first thing in the matcher:
3197 // recordDecl(decl().bind("x"), hasName("X")))
3198 // - uses the matcher under test afterwards in a way that not the first
3199 // alternative is matched; for anyOf, that means the first branch
3200 // would need to return false; for hasAncestor, it means that not
3201 // the direct parent matches the inner matcher.
3202
3203 EXPECT_TRUE(matchAndVerifyResultTrue(
3204 "class X { int y; };",
3205 recordDecl(
3206 recordDecl().bind("x"), hasName("::X"),
3207 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3208 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3209 EXPECT_TRUE(matchAndVerifyResultTrue(
3210 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3211 anyOf(unless(anything()), anything())),
3212 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3213 EXPECT_TRUE(matchAndVerifyResultTrue(
3214 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3215 classTemplateSpecializationDecl(
3216 decl().bind("x"),
3217 hasAnyTemplateArgument(refersToType(asString("int")))),
3218 new VerifyIdIsBoundTo<Decl>("x", 1)));
3219 EXPECT_TRUE(matchAndVerifyResultTrue(
3220 "class X { void f(); void g(); };",
3221 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3222 new VerifyIdIsBoundTo<Decl>("x", 1)));
3223 EXPECT_TRUE(matchAndVerifyResultTrue(
3224 "class X { X() : a(1), b(2) {} double a; int b; };",
3225 recordDecl(decl().bind("x"),
3226 has(constructorDecl(
3227 hasAnyConstructorInitializer(forField(hasName("b")))))),
3228 new VerifyIdIsBoundTo<Decl>("x", 1)));
3229 EXPECT_TRUE(matchAndVerifyResultTrue(
3230 "void x(int, int) { x(0, 42); }",
3231 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3232 new VerifyIdIsBoundTo<Expr>("x", 1)));
3233 EXPECT_TRUE(matchAndVerifyResultTrue(
3234 "void x(int, int y) {}",
3235 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3236 new VerifyIdIsBoundTo<Decl>("x", 1)));
3237 EXPECT_TRUE(matchAndVerifyResultTrue(
3238 "void x() { return; if (true) {} }",
3239 functionDecl(decl().bind("x"),
3240 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3241 new VerifyIdIsBoundTo<Decl>("x", 1)));
3242 EXPECT_TRUE(matchAndVerifyResultTrue(
3243 "namespace X { void b(int); void b(); }"
3244 "using X::b;",
3245 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3246 functionDecl(parameterCountIs(1))))),
3247 new VerifyIdIsBoundTo<Decl>("x", 1)));
3248 EXPECT_TRUE(matchAndVerifyResultTrue(
3249 "class A{}; class B{}; class C : B, A {};",
3250 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3251 new VerifyIdIsBoundTo<Decl>("x", 1)));
3252 EXPECT_TRUE(matchAndVerifyResultTrue(
3253 "class A{}; typedef A B; typedef A C; typedef A D;"
3254 "class E : A {};",
3255 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3256 new VerifyIdIsBoundTo<Decl>("x", 1)));
3257 EXPECT_TRUE(matchAndVerifyResultTrue(
3258 "class A { class B { void f() {} }; };",
3259 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3260 new VerifyIdIsBoundTo<Decl>("x", 1)));
3261 EXPECT_TRUE(matchAndVerifyResultTrue(
3262 "template <typename T> struct A { struct B {"
3263 " void f() { if(true) {} }"
3264 "}; };"
3265 "void t() { A<int>::B b; b.f(); }",
3266 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3267 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3268 EXPECT_TRUE(matchAndVerifyResultTrue(
3269 "class A {};",
3270 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3271 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek06963012013-07-19 11:50:54 +00003272 EXPECT_TRUE(matchAndVerifyResultTrue(
3273 "class A { A() : s(), i(42) {} const char *s; int i; };",
3274 constructorDecl(hasName("::A::A"), decl().bind("x"),
3275 forEachConstructorInitializer(forField(hasName("i")))),
3276 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek054d0492013-06-19 15:42:45 +00003277}
3278
Daniel Jasper11c98772012-11-11 22:14:55 +00003279TEST(ForEachDescendant, BindsCorrectNodes) {
3280 EXPECT_TRUE(matchAndVerifyResultTrue(
3281 "class C { void f(); int i; };",
3282 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3283 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3284 EXPECT_TRUE(matchAndVerifyResultTrue(
3285 "class C { void f() {} int i; };",
3286 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3287 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3288}
3289
Manuel Klimek152ea0e2013-02-04 10:59:20 +00003290TEST(FindAll, BindsNodeOnMatch) {
3291 EXPECT_TRUE(matchAndVerifyResultTrue(
3292 "class A {};",
3293 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3294 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3295}
3296
3297TEST(FindAll, BindsDescendantNodeOnMatch) {
3298 EXPECT_TRUE(matchAndVerifyResultTrue(
3299 "class A { int a; int b; };",
3300 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3301 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3302}
3303
3304TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3305 EXPECT_TRUE(matchAndVerifyResultTrue(
3306 "class A { int a; int b; };",
3307 recordDecl(hasName("::A"),
3308 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3309 fieldDecl().bind("v"))))),
3310 new VerifyIdIsBoundTo<Decl>("v", 3)));
3311
3312 EXPECT_TRUE(matchAndVerifyResultTrue(
3313 "class A { class B {}; class C {}; };",
3314 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3315 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3316}
3317
Manuel Klimek73876732013-02-04 09:42:38 +00003318TEST(EachOf, TriggersForEachMatch) {
3319 EXPECT_TRUE(matchAndVerifyResultTrue(
3320 "class A { int a; int b; };",
3321 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3322 has(fieldDecl(hasName("b")).bind("v")))),
3323 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3324}
3325
3326TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3327 EXPECT_TRUE(matchAndVerifyResultTrue(
3328 "class A { int a; int c; };",
3329 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3330 has(fieldDecl(hasName("b")).bind("v")))),
3331 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3332 EXPECT_TRUE(matchAndVerifyResultTrue(
3333 "class A { int c; int b; };",
3334 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3335 has(fieldDecl(hasName("b")).bind("v")))),
3336 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3337 EXPECT_TRUE(notMatches(
3338 "class A { int c; int d; };",
3339 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3340 has(fieldDecl(hasName("b")).bind("v"))))));
3341}
Manuel Klimek4da21662012-07-06 05:48:52 +00003342
3343TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3344 // Make sure that we can both match the class by name (::X) and by the type
3345 // the template was instantiated with (via a field).
3346
3347 EXPECT_TRUE(matches(
3348 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003349 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003350
3351 EXPECT_TRUE(matches(
3352 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003353 recordDecl(isTemplateInstantiation(), hasDescendant(
3354 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003355}
3356
3357TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3358 EXPECT_TRUE(matches(
3359 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003360 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003361 isTemplateInstantiation())));
3362}
3363
3364TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3365 EXPECT_TRUE(matches(
3366 "template <typename T> class X { T t; }; class A {};"
3367 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003368 recordDecl(isTemplateInstantiation(), hasDescendant(
3369 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003370}
3371
3372TEST(IsTemplateInstantiation,
3373 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3374 EXPECT_TRUE(matches(
3375 "template <typename T> class X {};"
3376 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003377 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003378}
3379
3380TEST(IsTemplateInstantiation,
3381 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3382 EXPECT_TRUE(matches(
3383 "class A {};"
3384 "class X {"
3385 " template <typename U> class Y { U u; };"
3386 " Y<A> y;"
3387 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003388 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003389}
3390
3391TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3392 // FIXME: Figure out whether this makes sense. It doesn't affect the
3393 // normal use case as long as the uppermost instantiation always is marked
3394 // as template instantiation, but it might be confusing as a predicate.
3395 EXPECT_TRUE(matches(
3396 "class A {};"
3397 "template <typename T> class X {"
3398 " template <typename U> class Y { U u; };"
3399 " Y<T> y;"
3400 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003401 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003402}
3403
3404TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3405 EXPECT_TRUE(notMatches(
3406 "template <typename T> class X {}; class A {};"
3407 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003408 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003409}
3410
3411TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3412 EXPECT_TRUE(notMatches(
3413 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003414 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003415}
3416
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003417TEST(IsExplicitTemplateSpecialization,
3418 DoesNotMatchPrimaryTemplate) {
3419 EXPECT_TRUE(notMatches(
3420 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003421 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003422 EXPECT_TRUE(notMatches(
3423 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003424 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003425}
3426
3427TEST(IsExplicitTemplateSpecialization,
3428 DoesNotMatchExplicitTemplateInstantiations) {
3429 EXPECT_TRUE(notMatches(
3430 "template <typename T> class X {};"
3431 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003432 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003433 EXPECT_TRUE(notMatches(
3434 "template <typename T> void f(T t) {}"
3435 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003436 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003437}
3438
3439TEST(IsExplicitTemplateSpecialization,
3440 DoesNotMatchImplicitTemplateInstantiations) {
3441 EXPECT_TRUE(notMatches(
3442 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003443 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003444 EXPECT_TRUE(notMatches(
3445 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003446 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003447}
3448
3449TEST(IsExplicitTemplateSpecialization,
3450 MatchesExplicitTemplateSpecializations) {
3451 EXPECT_TRUE(matches(
3452 "template <typename T> class X {};"
3453 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003454 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003455 EXPECT_TRUE(matches(
3456 "template <typename T> void f(T t) {}"
3457 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003458 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003459}
3460
Manuel Klimek579b1202012-09-07 09:26:10 +00003461TEST(HasAncenstor, MatchesDeclarationAncestors) {
3462 EXPECT_TRUE(matches(
3463 "class A { class B { class C {}; }; };",
3464 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3465}
3466
3467TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3468 EXPECT_TRUE(notMatches(
3469 "class A { class B { class C {}; }; };",
3470 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3471}
3472
3473TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3474 EXPECT_TRUE(matches(
3475 "class A { class B { void f() { C c; } class C {}; }; };",
3476 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3477 hasAncestor(recordDecl(hasName("A"))))))));
3478}
3479
3480TEST(HasAncenstor, MatchesStatementAncestors) {
3481 EXPECT_TRUE(matches(
3482 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003483 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003484}
3485
3486TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3487 EXPECT_TRUE(matches(
3488 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003489 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003490}
3491
3492TEST(HasAncestor, BindsRecursiveCombinations) {
3493 EXPECT_TRUE(matchAndVerifyResultTrue(
3494 "class C { class D { class E { class F { int y; }; }; }; };",
3495 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003496 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003497}
3498
3499TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3500 EXPECT_TRUE(matchAndVerifyResultTrue(
3501 "class C { class D { class E { class F { int y; }; }; }; };",
3502 fieldDecl(hasAncestor(
3503 decl(
3504 hasDescendant(recordDecl(isDefinition(),
3505 hasAncestor(recordDecl())))
3506 ).bind("d")
3507 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003508 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003509}
3510
Manuel Klimek374516c2013-03-14 16:33:21 +00003511TEST(HasAncestor, MatchesClosestAncestor) {
3512 EXPECT_TRUE(matchAndVerifyResultTrue(
3513 "template <typename T> struct C {"
3514 " void f(int) {"
3515 " struct I { void g(T) { int x; } } i; i.g(42);"
3516 " }"
3517 "};"
3518 "template struct C<int>;",
3519 varDecl(hasName("x"),
3520 hasAncestor(functionDecl(hasParameter(
3521 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3522 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3523}
3524
Manuel Klimek579b1202012-09-07 09:26:10 +00003525TEST(HasAncestor, MatchesInTemplateInstantiations) {
3526 EXPECT_TRUE(matches(
3527 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3528 "A<int>::B::C a;",
3529 fieldDecl(hasType(asString("int")),
3530 hasAncestor(recordDecl(hasName("A"))))));
3531}
3532
3533TEST(HasAncestor, MatchesInImplicitCode) {
3534 EXPECT_TRUE(matches(
3535 "struct X {}; struct A { A() {} X x; };",
3536 constructorDecl(
3537 hasAnyConstructorInitializer(withInitializer(expr(
3538 hasAncestor(recordDecl(hasName("A")))))))));
3539}
3540
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003541TEST(HasParent, MatchesOnlyParent) {
3542 EXPECT_TRUE(matches(
3543 "void f() { if (true) { int x = 42; } }",
3544 compoundStmt(hasParent(ifStmt()))));
3545 EXPECT_TRUE(notMatches(
3546 "void f() { for (;;) { int x = 42; } }",
3547 compoundStmt(hasParent(ifStmt()))));
3548 EXPECT_TRUE(notMatches(
3549 "void f() { if (true) for (;;) { int x = 42; } }",
3550 compoundStmt(hasParent(ifStmt()))));
3551}
3552
Manuel Klimek30ace372012-12-06 14:42:48 +00003553TEST(HasAncestor, MatchesAllAncestors) {
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 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3560 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3561}
3562
3563TEST(HasParent, MatchesAllParents) {
3564 EXPECT_TRUE(matches(
3565 "template <typename T> struct C { static void f() { 42; } };"
3566 "void t() { C<int>::f(); }",
3567 integerLiteral(
3568 equals(42),
3569 hasParent(compoundStmt(hasParent(functionDecl(
3570 hasParent(recordDecl(isTemplateInstantiation())))))))));
3571 EXPECT_TRUE(matches(
3572 "template <typename T> struct C { static void f() { 42; } };"
3573 "void t() { C<int>::f(); }",
3574 integerLiteral(
3575 equals(42),
3576 hasParent(compoundStmt(hasParent(functionDecl(
3577 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3578 EXPECT_TRUE(matches(
3579 "template <typename T> struct C { static void f() { 42; } };"
3580 "void t() { C<int>::f(); }",
3581 integerLiteral(equals(42),
3582 hasParent(compoundStmt(allOf(
3583 hasParent(functionDecl(
3584 hasParent(recordDecl(isTemplateInstantiation())))),
3585 hasParent(functionDecl(hasParent(recordDecl(
3586 unless(isTemplateInstantiation())))))))))));
Manuel Klimek374516c2013-03-14 16:33:21 +00003587 EXPECT_TRUE(
3588 notMatches("template <typename T> struct C { static void f() {} };"
3589 "void t() { C<int>::f(); }",
3590 compoundStmt(hasParent(recordDecl()))));
Manuel Klimek30ace372012-12-06 14:42:48 +00003591}
3592
Daniel Jasperce620072012-10-17 08:52:59 +00003593TEST(TypeMatching, MatchesTypes) {
3594 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3595}
3596
3597TEST(TypeMatching, MatchesArrayTypes) {
3598 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3599 EXPECT_TRUE(matches("int a[42];", arrayType()));
3600 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3601
3602 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3603 arrayType(hasElementType(builtinType()))));
3604
3605 EXPECT_TRUE(matches(
3606 "int const a[] = { 2, 3 };",
3607 qualType(arrayType(hasElementType(builtinType())))));
3608 EXPECT_TRUE(matches(
3609 "int const a[] = { 2, 3 };",
3610 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3611 EXPECT_TRUE(matches(
3612 "typedef const int T; T x[] = { 1, 2 };",
3613 qualType(isConstQualified(), arrayType())));
3614
3615 EXPECT_TRUE(notMatches(
3616 "int a[] = { 2, 3 };",
3617 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3618 EXPECT_TRUE(notMatches(
3619 "int a[] = { 2, 3 };",
3620 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3621 EXPECT_TRUE(notMatches(
3622 "int const a[] = { 2, 3 };",
3623 qualType(arrayType(hasElementType(builtinType())),
3624 unless(isConstQualified()))));
3625
3626 EXPECT_TRUE(matches("int a[2];",
3627 constantArrayType(hasElementType(builtinType()))));
3628 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3629}
3630
3631TEST(TypeMatching, MatchesComplexTypes) {
3632 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3633 EXPECT_TRUE(matches(
3634 "_Complex float f;",
3635 complexType(hasElementType(builtinType()))));
3636 EXPECT_TRUE(notMatches(
3637 "_Complex float f;",
3638 complexType(hasElementType(isInteger()))));
3639}
3640
3641TEST(TypeMatching, MatchesConstantArrayTypes) {
3642 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3643 EXPECT_TRUE(notMatches(
3644 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3645 constantArrayType(hasElementType(builtinType()))));
3646
3647 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3648 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3649 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3650}
3651
3652TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3653 EXPECT_TRUE(matches(
3654 "template <typename T, int Size> class array { T data[Size]; };",
3655 dependentSizedArrayType()));
3656 EXPECT_TRUE(notMatches(
3657 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3658 dependentSizedArrayType()));
3659}
3660
3661TEST(TypeMatching, MatchesIncompleteArrayType) {
3662 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3663 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3664
3665 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3666 incompleteArrayType()));
3667}
3668
3669TEST(TypeMatching, MatchesVariableArrayType) {
3670 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3671 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3672
3673 EXPECT_TRUE(matches(
3674 "void f(int b) { int a[b]; }",
3675 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3676 varDecl(hasName("b")))))))));
3677}
3678
3679TEST(TypeMatching, MatchesAtomicTypes) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003680 if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
3681 llvm::Triple::Win32) {
3682 // FIXME: Make this work for MSVC.
3683 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
Daniel Jasperce620072012-10-17 08:52:59 +00003684
Stephen Hines651f13c2014-04-23 16:59:28 -07003685 EXPECT_TRUE(matches("_Atomic(int) i;",
3686 atomicType(hasValueType(isInteger()))));
3687 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3688 atomicType(hasValueType(isInteger()))));
3689 }
Daniel Jasperce620072012-10-17 08:52:59 +00003690}
3691
3692TEST(TypeMatching, MatchesAutoTypes) {
3693 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3694 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3695 autoType()));
3696
Richard Smith9b131752013-04-30 21:23:01 +00003697 // FIXME: Matching against the type-as-written can't work here, because the
3698 // type as written was not deduced.
3699 //EXPECT_TRUE(matches("auto a = 1;",
3700 // autoType(hasDeducedType(isInteger()))));
3701 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3702 // autoType(hasDeducedType(isInteger()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003703}
3704
Daniel Jaspera267cf62012-10-29 10:14:44 +00003705TEST(TypeMatching, MatchesFunctionTypes) {
3706 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3707 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3708}
3709
Edwin Vane88be2fd2013-04-01 18:33:34 +00003710TEST(TypeMatching, MatchesParenType) {
3711 EXPECT_TRUE(
3712 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3713 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3714
3715 EXPECT_TRUE(matches(
3716 "int (*ptr_to_func)(int);",
3717 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3718 EXPECT_TRUE(notMatches(
3719 "int (*ptr_to_array)[4];",
3720 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3721}
3722
Daniel Jasperce620072012-10-17 08:52:59 +00003723TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003724 // FIXME: Reactive when these tests can be more specific (not matching
3725 // implicit code on certain platforms), likely when we have hasDescendant for
3726 // Types/TypeLocs.
3727 //EXPECT_TRUE(matchAndVerifyResultTrue(
3728 // "int* a;",
3729 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3730 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3731 //EXPECT_TRUE(matchAndVerifyResultTrue(
3732 // "int* a;",
3733 // pointerTypeLoc().bind("loc"),
3734 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003735 EXPECT_TRUE(matches(
3736 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003737 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003738 EXPECT_TRUE(matches(
3739 "int** a;",
3740 loc(pointerType(pointee(pointerType())))));
3741 EXPECT_TRUE(matches(
3742 "int* b; int* * const a = &b;",
3743 loc(qualType(isConstQualified(), pointerType()))));
3744
3745 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003746 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3747 hasType(blockPointerType()))));
3748 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3749 hasType(memberPointerType()))));
3750 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3751 hasType(pointerType()))));
3752 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3753 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003754 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3755 hasType(lValueReferenceType()))));
3756 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3757 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003758
Daniel Jasper1802daf2012-10-17 13:35:36 +00003759 Fragment = "int *ptr;";
3760 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3761 hasType(blockPointerType()))));
3762 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3763 hasType(memberPointerType()))));
3764 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3765 hasType(pointerType()))));
3766 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3767 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003768
Daniel Jasper1802daf2012-10-17 13:35:36 +00003769 Fragment = "int a; int &ref = a;";
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()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003778 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3779 hasType(lValueReferenceType()))));
3780 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3781 hasType(rValueReferenceType()))));
3782
3783 Fragment = "int &&ref = 2;";
3784 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3785 hasType(blockPointerType()))));
3786 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3787 hasType(memberPointerType()))));
3788 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3789 hasType(pointerType()))));
3790 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3791 hasType(referenceType()))));
3792 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3793 hasType(lValueReferenceType()))));
3794 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3795 hasType(rValueReferenceType()))));
3796}
3797
3798TEST(TypeMatching, AutoRefTypes) {
3799 std::string Fragment = "auto a = 1;"
3800 "auto b = a;"
3801 "auto &c = a;"
3802 "auto &&d = c;"
3803 "auto &&e = 2;";
3804 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3805 hasType(referenceType()))));
3806 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3807 hasType(referenceType()))));
3808 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3809 hasType(referenceType()))));
3810 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3811 hasType(lValueReferenceType()))));
3812 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3813 hasType(rValueReferenceType()))));
3814 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3815 hasType(referenceType()))));
3816 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3817 hasType(lValueReferenceType()))));
3818 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3819 hasType(rValueReferenceType()))));
3820 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3821 hasType(referenceType()))));
3822 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3823 hasType(lValueReferenceType()))));
3824 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3825 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003826}
3827
3828TEST(TypeMatching, PointeeTypes) {
3829 EXPECT_TRUE(matches("int b; int &a = b;",
3830 referenceType(pointee(builtinType()))));
3831 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3832
3833 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003834 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003835
3836 EXPECT_TRUE(matches(
3837 "int const *A;",
3838 pointerType(pointee(isConstQualified(), builtinType()))));
3839 EXPECT_TRUE(notMatches(
3840 "int *A;",
3841 pointerType(pointee(isConstQualified(), builtinType()))));
3842}
3843
3844TEST(TypeMatching, MatchesPointersToConstTypes) {
3845 EXPECT_TRUE(matches("int b; int * const a = &b;",
3846 loc(pointerType())));
3847 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003848 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003849 EXPECT_TRUE(matches(
3850 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003851 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003852 EXPECT_TRUE(matches(
3853 "int b; const int * a = &b;",
3854 pointerType(pointee(builtinType()))));
3855}
3856
3857TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003858 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3859 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003860}
3861
Edwin Vane3abf7782013-02-25 14:49:29 +00003862TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003863 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003864 templateSpecializationType()));
3865}
3866
Edwin Vane742d9e72013-02-25 20:43:32 +00003867TEST(TypeMatching, MatchesRecordType) {
3868 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003869 EXPECT_TRUE(matches("struct S{}; S s;",
3870 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3871 EXPECT_TRUE(notMatches("int i;",
3872 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003873}
3874
3875TEST(TypeMatching, MatchesElaboratedType) {
3876 EXPECT_TRUE(matches(
3877 "namespace N {"
3878 " namespace M {"
3879 " class D {};"
3880 " }"
3881 "}"
3882 "N::M::D d;", elaboratedType()));
3883 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3884 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3885}
3886
3887TEST(ElaboratedTypeNarrowing, hasQualifier) {
3888 EXPECT_TRUE(matches(
3889 "namespace N {"
3890 " namespace M {"
3891 " class D {};"
3892 " }"
3893 "}"
3894 "N::M::D d;",
3895 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3896 EXPECT_TRUE(notMatches(
3897 "namespace M {"
3898 " class D {};"
3899 "}"
3900 "M::D d;",
3901 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003902 EXPECT_TRUE(notMatches(
3903 "struct D {"
3904 "} d;",
3905 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003906}
3907
3908TEST(ElaboratedTypeNarrowing, namesType) {
3909 EXPECT_TRUE(matches(
3910 "namespace N {"
3911 " namespace M {"
3912 " class D {};"
3913 " }"
3914 "}"
3915 "N::M::D d;",
3916 elaboratedType(elaboratedType(namesType(recordType(
3917 hasDeclaration(namedDecl(hasName("D")))))))));
3918 EXPECT_TRUE(notMatches(
3919 "namespace M {"
3920 " class D {};"
3921 "}"
3922 "M::D d;",
3923 elaboratedType(elaboratedType(namesType(typedefType())))));
3924}
3925
Daniel Jaspera7564432012-09-13 13:11:25 +00003926TEST(NNS, MatchesNestedNameSpecifiers) {
3927 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3928 nestedNameSpecifier()));
3929 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3930 nestedNameSpecifier()));
3931 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3932 nestedNameSpecifier()));
3933
3934 EXPECT_TRUE(matches(
3935 "struct A { static void f() {} }; void g() { A::f(); }",
3936 nestedNameSpecifier()));
3937 EXPECT_TRUE(notMatches(
3938 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3939 nestedNameSpecifier()));
3940}
3941
Daniel Jasperb54b7642012-09-20 14:12:57 +00003942TEST(NullStatement, SimpleCases) {
3943 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3944 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3945}
3946
Daniel Jaspera7564432012-09-13 13:11:25 +00003947TEST(NNS, MatchesTypes) {
3948 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3949 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3950 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3951 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3952 Matcher));
3953 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3954}
3955
3956TEST(NNS, MatchesNamespaceDecls) {
3957 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3958 specifiesNamespace(hasName("ns")));
3959 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3960 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3961 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3962}
3963
3964TEST(NNS, BindsNestedNameSpecifiers) {
3965 EXPECT_TRUE(matchAndVerifyResultTrue(
3966 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3967 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3968 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3969}
3970
3971TEST(NNS, BindsNestedNameSpecifierLocs) {
3972 EXPECT_TRUE(matchAndVerifyResultTrue(
3973 "namespace ns { struct B {}; } ns::B b;",
3974 loc(nestedNameSpecifier()).bind("loc"),
3975 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3976}
3977
3978TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3979 EXPECT_TRUE(matches(
3980 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3981 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3982 EXPECT_TRUE(matches(
3983 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003984 nestedNameSpecifierLoc(hasPrefix(
3985 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003986}
3987
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003988TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3989 std::string Fragment =
3990 "namespace a { struct A { struct B { struct C {}; }; }; };"
3991 "void f() { a::A::B::C c; }";
3992 EXPECT_TRUE(matches(
3993 Fragment,
3994 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3995 hasDescendant(nestedNameSpecifier(
3996 specifiesNamespace(hasName("a")))))));
3997 EXPECT_TRUE(notMatches(
3998 Fragment,
3999 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
4000 has(nestedNameSpecifier(
4001 specifiesNamespace(hasName("a")))))));
4002 EXPECT_TRUE(matches(
4003 Fragment,
4004 nestedNameSpecifier(specifiesType(asString("struct a::A")),
4005 has(nestedNameSpecifier(
4006 specifiesNamespace(hasName("a")))))));
4007
4008 // Not really useful because a NestedNameSpecifier can af at most one child,
4009 // but to complete the interface.
4010 EXPECT_TRUE(matchAndVerifyResultTrue(
4011 Fragment,
4012 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
4013 forEach(nestedNameSpecifier().bind("x"))),
4014 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
4015}
4016
4017TEST(NNS, NestedNameSpecifiersAsDescendants) {
4018 std::string Fragment =
4019 "namespace a { struct A { struct B { struct C {}; }; }; };"
4020 "void f() { a::A::B::C c; }";
4021 EXPECT_TRUE(matches(
4022 Fragment,
4023 decl(hasDescendant(nestedNameSpecifier(specifiesType(
4024 asString("struct a::A")))))));
4025 EXPECT_TRUE(matchAndVerifyResultTrue(
4026 Fragment,
4027 functionDecl(hasName("f"),
4028 forEachDescendant(nestedNameSpecifier().bind("x"))),
4029 // Nested names: a, a::A and a::A::B.
4030 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
4031}
4032
4033TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
4034 std::string Fragment =
4035 "namespace a { struct A { struct B { struct C {}; }; }; };"
4036 "void f() { a::A::B::C c; }";
4037 EXPECT_TRUE(matches(
4038 Fragment,
4039 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4040 hasDescendant(loc(nestedNameSpecifier(
4041 specifiesNamespace(hasName("a"))))))));
4042 EXPECT_TRUE(notMatches(
4043 Fragment,
4044 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4045 has(loc(nestedNameSpecifier(
4046 specifiesNamespace(hasName("a"))))))));
4047 EXPECT_TRUE(matches(
4048 Fragment,
4049 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
4050 has(loc(nestedNameSpecifier(
4051 specifiesNamespace(hasName("a"))))))));
4052
4053 EXPECT_TRUE(matchAndVerifyResultTrue(
4054 Fragment,
4055 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
4056 forEach(nestedNameSpecifierLoc().bind("x"))),
4057 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
4058}
4059
4060TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
4061 std::string Fragment =
4062 "namespace a { struct A { struct B { struct C {}; }; }; };"
4063 "void f() { a::A::B::C c; }";
4064 EXPECT_TRUE(matches(
4065 Fragment,
4066 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
4067 asString("struct a::A"))))))));
4068 EXPECT_TRUE(matchAndVerifyResultTrue(
4069 Fragment,
4070 functionDecl(hasName("f"),
4071 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
4072 // Nested names: a, a::A and a::A::B.
4073 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
4074}
4075
Manuel Klimek60969f52013-02-01 13:41:35 +00004076template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004077public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004078 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
4079 StringRef InnerId)
4080 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00004081 }
4082
Manuel Klimek60969f52013-02-01 13:41:35 +00004083 virtual bool run(const BoundNodes *Nodes) { return false; }
4084
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004085 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4086 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004087 return selectFirst<const T>(InnerId,
4088 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004089 }
4090private:
4091 std::string Id;
4092 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004093 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004094};
4095
4096TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004097 EXPECT_TRUE(matchAndVerifyResultTrue(
4098 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4099 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004100 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4101 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004102 EXPECT_TRUE(matchAndVerifyResultFalse(
4103 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4104 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004105 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4106 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004107}
4108
4109TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004110 EXPECT_TRUE(matchAndVerifyResultTrue(
4111 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004112 new VerifyMatchOnNode<clang::Stmt>(
4113 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004114 EXPECT_TRUE(matchAndVerifyResultFalse(
4115 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004116 new VerifyMatchOnNode<clang::Stmt>(
4117 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004118}
4119
4120TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4121 EXPECT_TRUE(matchAndVerifyResultTrue(
4122 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4123 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004124 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004125 EXPECT_TRUE(matchAndVerifyResultFalse(
4126 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4127 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004128 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004129}
4130
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00004131template <typename T>
4132class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4133public:
4134 virtual bool run(const BoundNodes *Nodes) { return false; }
4135
4136 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4137 const T *Node = Nodes->getNodeAs<T>("");
4138 return verify(*Nodes, *Context, Node);
4139 }
4140
4141 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4142 return selectFirst<const T>(
4143 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4144 *Node, Context)) != NULL;
4145 }
4146 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4147 return selectFirst<const T>(
4148 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4149 *Node, Context)) != NULL;
4150 }
4151};
4152
4153TEST(IsEqualTo, MatchesNodesByIdentity) {
4154 EXPECT_TRUE(matchAndVerifyResultTrue(
4155 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4156 new VerifyAncestorHasChildIsEqual<Decl>()));
4157 EXPECT_TRUE(
4158 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4159 new VerifyAncestorHasChildIsEqual<Stmt>()));
4160}
4161
Manuel Klimeke5793282012-11-02 01:31:03 +00004162class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4163public:
4164 VerifyStartOfTranslationUnit() : Called(false) {}
4165 virtual void run(const MatchFinder::MatchResult &Result) {
4166 EXPECT_TRUE(Called);
4167 }
4168 virtual void onStartOfTranslationUnit() {
4169 Called = true;
4170 }
4171 bool Called;
4172};
4173
4174TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4175 MatchFinder Finder;
4176 VerifyStartOfTranslationUnit VerifyCallback;
4177 Finder.addMatcher(decl(), &VerifyCallback);
Stephen Hines651f13c2014-04-23 16:59:28 -07004178 std::unique_ptr<FrontendActionFactory> Factory(
4179 newFrontendActionFactory(&Finder));
Manuel Klimeke5793282012-11-02 01:31:03 +00004180 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4181 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne51fcdf82013-11-07 22:30:36 +00004182
4183 VerifyCallback.Called = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07004184 std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
Peter Collingbourne51fcdf82013-11-07 22:30:36 +00004185 ASSERT_TRUE(AST.get());
4186 Finder.matchAST(AST->getASTContext());
4187 EXPECT_TRUE(VerifyCallback.Called);
Manuel Klimeke5793282012-11-02 01:31:03 +00004188}
4189
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004190class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4191public:
4192 VerifyEndOfTranslationUnit() : Called(false) {}
4193 virtual void run(const MatchFinder::MatchResult &Result) {
4194 EXPECT_FALSE(Called);
4195 }
4196 virtual void onEndOfTranslationUnit() {
4197 Called = true;
4198 }
4199 bool Called;
4200};
4201
4202TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4203 MatchFinder Finder;
4204 VerifyEndOfTranslationUnit VerifyCallback;
4205 Finder.addMatcher(decl(), &VerifyCallback);
Stephen Hines651f13c2014-04-23 16:59:28 -07004206 std::unique_ptr<FrontendActionFactory> Factory(
4207 newFrontendActionFactory(&Finder));
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004208 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4209 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne51fcdf82013-11-07 22:30:36 +00004210
4211 VerifyCallback.Called = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07004212 std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
Peter Collingbourne51fcdf82013-11-07 22:30:36 +00004213 ASSERT_TRUE(AST.get());
4214 Finder.matchAST(AST->getASTContext());
4215 EXPECT_TRUE(VerifyCallback.Called);
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004216}
4217
Manuel Klimekcf52ca62013-06-20 14:06:32 +00004218TEST(EqualsBoundNodeMatcher, QualType) {
4219 EXPECT_TRUE(matches(
4220 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4221 hasInitializer(ignoringParenImpCasts(
4222 hasType(qualType(equalsBoundNode("type"))))))));
4223 EXPECT_TRUE(notMatches("int i = 1.f;",
4224 varDecl(hasType(qualType().bind("type")),
4225 hasInitializer(ignoringParenImpCasts(hasType(
4226 qualType(equalsBoundNode("type"))))))));
4227}
4228
4229TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4230 EXPECT_TRUE(notMatches(
4231 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4232 hasInitializer(ignoringParenImpCasts(
4233 hasType(qualType(equalsBoundNode("type"))))))));
4234}
4235
4236TEST(EqualsBoundNodeMatcher, Stmt) {
4237 EXPECT_TRUE(
4238 matches("void f() { if(true) {} }",
4239 stmt(allOf(ifStmt().bind("if"),
4240 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4241
4242 EXPECT_TRUE(notMatches(
4243 "void f() { if(true) { if (true) {} } }",
4244 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4245}
4246
4247TEST(EqualsBoundNodeMatcher, Decl) {
4248 EXPECT_TRUE(matches(
4249 "class X { class Y {}; };",
4250 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4251 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4252
4253 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4254 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4255 has(decl(equalsBoundNode("record")))))));
4256}
4257
4258TEST(EqualsBoundNodeMatcher, Type) {
4259 EXPECT_TRUE(matches(
4260 "class X { int a; int b; };",
4261 recordDecl(
4262 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4263 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4264
4265 EXPECT_TRUE(notMatches(
4266 "class X { int a; double b; };",
4267 recordDecl(
4268 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4269 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4270}
4271
4272TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
Manuel Klimekcf52ca62013-06-20 14:06:32 +00004273 EXPECT_TRUE(matchAndVerifyResultTrue(
4274 "int f() {"
4275 " if (1) {"
4276 " int i = 9;"
4277 " }"
4278 " int j = 10;"
4279 " {"
4280 " float k = 9.0;"
4281 " }"
4282 " return 0;"
4283 "}",
4284 // Look for variable declarations within functions whose type is the same
4285 // as the function return type.
4286 functionDecl(returns(qualType().bind("type")),
4287 forEachDescendant(varDecl(hasType(
4288 qualType(equalsBoundNode("type")))).bind("decl"))),
4289 // Only i and j should match, not k.
4290 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4291}
4292
4293TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4294 EXPECT_TRUE(matchAndVerifyResultTrue(
4295 "void f() {"
4296 " int x;"
4297 " double d;"
4298 " x = d + x - d + x;"
4299 "}",
4300 functionDecl(
4301 hasName("f"), forEachDescendant(varDecl().bind("d")),
4302 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4303 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4304}
4305
Stephen Hines651f13c2014-04-23 16:59:28 -07004306TEST(EqualsBoundNodeMatcher, UnlessDescendantsOfAncestorsMatch) {
4307 EXPECT_TRUE(matchAndVerifyResultTrue(
4308 "struct StringRef { int size() const; const char* data() const; };"
4309 "void f(StringRef v) {"
4310 " v.data();"
4311 "}",
4312 memberCallExpr(
4313 callee(methodDecl(hasName("data"))),
4314 on(declRefExpr(to(varDecl(hasType(recordDecl(hasName("StringRef"))))
4315 .bind("var")))),
4316 unless(hasAncestor(stmt(hasDescendant(memberCallExpr(
4317 callee(methodDecl(anyOf(hasName("size"), hasName("length")))),
4318 on(declRefExpr(to(varDecl(equalsBoundNode("var")))))))))))
4319 .bind("data"),
4320 new VerifyIdIsBoundTo<Expr>("data", 1)));
4321
4322 EXPECT_FALSE(matches(
4323 "struct StringRef { int size() const; const char* data() const; };"
4324 "void f(StringRef v) {"
4325 " v.data();"
4326 " v.size();"
4327 "}",
4328 memberCallExpr(
4329 callee(methodDecl(hasName("data"))),
4330 on(declRefExpr(to(varDecl(hasType(recordDecl(hasName("StringRef"))))
4331 .bind("var")))),
4332 unless(hasAncestor(stmt(hasDescendant(memberCallExpr(
4333 callee(methodDecl(anyOf(hasName("size"), hasName("length")))),
4334 on(declRefExpr(to(varDecl(equalsBoundNode("var")))))))))))
4335 .bind("data")));
4336}
4337
Manuel Klimek4da21662012-07-06 05:48:52 +00004338} // end namespace ast_matchers
4339} // end namespace clang