blob: fb17a40d746b2e3865ba06c3ea51a9aee840e172 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
Benjamin Kramer8b9ed712012-12-01 17:22:05 +000011#include "clang/AST/PrettyPrinter.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000014#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace ast_matchers {
19
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000020#if GTEST_HAS_DEATH_TEST
Manuel Klimek4da21662012-07-06 05:48:52 +000021TEST(HasNameDeathTest, DiesOnEmptyName) {
22 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000023 DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000024 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
25 }, "");
26}
27
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000028TEST(HasNameDeathTest, DiesOnEmptyPattern) {
29 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000030 DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000031 EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
32 }, "");
33}
34
Manuel Klimek4da21662012-07-06 05:48:52 +000035TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
36 ASSERT_DEBUG_DEATH({
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000037 DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom(""));
Manuel Klimek4da21662012-07-06 05:48:52 +000038 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
39 }, "");
40}
Benjamin Kramer78a0ce42012-07-10 17:30:44 +000041#endif
Manuel Klimek4da21662012-07-06 05:48:52 +000042
Manuel Klimek715c9562012-07-25 10:02:02 +000043TEST(Decl, MatchesDeclarations) {
44 EXPECT_TRUE(notMatches("", decl(usingDecl())));
45 EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
46 decl(usingDecl())));
47}
48
Manuel Klimek4da21662012-07-06 05:48:52 +000049TEST(NameableDeclaration, MatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000050 DeclarationMatcher NamedX = namedDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +000051 EXPECT_TRUE(matches("typedef int X;", NamedX));
52 EXPECT_TRUE(matches("int X;", NamedX));
53 EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
54 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
55 EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
56 EXPECT_TRUE(matches("namespace X { }", NamedX));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000057 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
Manuel Klimek4da21662012-07-06 05:48:52 +000058
59 EXPECT_TRUE(notMatches("#define X 1", NamedX));
60}
61
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000062TEST(NameableDeclaration, REMatchesVariousDecls) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000063 DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000064 EXPECT_TRUE(matches("typedef int Xa;", NamedX));
65 EXPECT_TRUE(matches("int Xb;", NamedX));
66 EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
67 EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
68 EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
69 EXPECT_TRUE(matches("namespace Xij { }", NamedX));
70 EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
71
72 EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
73
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000074 DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000075 EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
76 EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
77
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000078 DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000079 EXPECT_TRUE(matches("int abc;", Abc));
80 EXPECT_TRUE(matches("int aFOObBARc;", Abc));
81 EXPECT_TRUE(notMatches("int cab;", Abc));
82 EXPECT_TRUE(matches("int cabc;", Abc));
Manuel Klimekec33b6f2012-12-10 07:08:53 +000083
84 DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
85 EXPECT_TRUE(matches("int k;", StartsWithK));
86 EXPECT_TRUE(matches("int kAbc;", StartsWithK));
87 EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
88 EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
89 EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +000090}
91
Manuel Klimek4da21662012-07-06 05:48:52 +000092TEST(DeclarationMatcher, MatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +000093 DeclarationMatcher ClassMatcher(recordDecl());
Manuel Klimeke265c872012-07-10 14:21:30 +000094#if !defined(_MSC_VER)
Manuel Klimek4da21662012-07-06 05:48:52 +000095 EXPECT_FALSE(matches("", ClassMatcher));
Manuel Klimeke265c872012-07-10 14:21:30 +000096#else
97 // Matches class type_info.
98 EXPECT_TRUE(matches("", ClassMatcher));
99#endif
Manuel Klimek4da21662012-07-06 05:48:52 +0000100
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000101 DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000102 EXPECT_TRUE(matches("class X;", ClassX));
103 EXPECT_TRUE(matches("class X {};", ClassX));
104 EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
105 EXPECT_TRUE(notMatches("", ClassX));
106}
107
108TEST(DeclarationMatcher, ClassIsDerived) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000109 DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000110
111 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000112 EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX));
113 EXPECT_TRUE(notMatches("class X;", IsDerivedFromX));
Manuel Klimek4da21662012-07-06 05:48:52 +0000114 EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX));
115 EXPECT_TRUE(notMatches("", IsDerivedFromX));
116
Daniel Jasper63d88722012-09-12 21:14:15 +0000117 DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X"));
Daniel Jasper76dafa72012-09-07 12:48:17 +0000118
119 EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX));
120 EXPECT_TRUE(matches("class X {};", IsAX));
121 EXPECT_TRUE(matches("class X;", IsAX));
122 EXPECT_TRUE(notMatches("class Y;", IsAX));
123 EXPECT_TRUE(notMatches("", IsAX));
124
Manuel Klimek4da21662012-07-06 05:48:52 +0000125 DeclarationMatcher ZIsDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000126 recordDecl(hasName("Z"), isDerivedFrom("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000127 EXPECT_TRUE(
128 matches("class X {}; class Y : public X {}; class Z : public Y {};",
129 ZIsDerivedFromX));
130 EXPECT_TRUE(
131 matches("class X {};"
132 "template<class T> class Y : public X {};"
133 "class Z : public Y<int> {};", ZIsDerivedFromX));
134 EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};",
135 ZIsDerivedFromX));
136 EXPECT_TRUE(
137 matches("template<class T> class X {}; "
138 "template<class T> class Z : public X<T> {};",
139 ZIsDerivedFromX));
140 EXPECT_TRUE(
141 matches("template<class T, class U=T> class X {}; "
142 "template<class T> class Z : public X<T> {};",
143 ZIsDerivedFromX));
144 EXPECT_TRUE(
145 notMatches("template<class X> class A { class Z : public X {}; };",
146 ZIsDerivedFromX));
147 EXPECT_TRUE(
148 matches("template<class X> class A { public: class Z : public X {}; }; "
149 "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX));
150 EXPECT_TRUE(
151 matches("template <class T> class X {}; "
152 "template<class Y> class A { class Z : public X<Y> {}; };",
153 ZIsDerivedFromX));
154 EXPECT_TRUE(
155 notMatches("template<template<class T> class X> class A { "
156 " class Z : public X<int> {}; };", ZIsDerivedFromX));
157 EXPECT_TRUE(
158 matches("template<template<class T> class X> class A { "
159 " public: class Z : public X<int> {}; }; "
160 "template<class T> class X {}; void y() { A<X>::Z z; }",
161 ZIsDerivedFromX));
162 EXPECT_TRUE(
163 notMatches("template<class X> class A { class Z : public X::D {}; };",
164 ZIsDerivedFromX));
165 EXPECT_TRUE(
166 matches("template<class X> class A { public: "
167 " class Z : public X::D {}; }; "
168 "class Y { public: class X {}; typedef X D; }; "
169 "void y() { A<Y>::Z z; }", ZIsDerivedFromX));
170 EXPECT_TRUE(
171 matches("class X {}; typedef X Y; class Z : public Y {};",
172 ZIsDerivedFromX));
173 EXPECT_TRUE(
174 matches("template<class T> class Y { typedef typename T::U X; "
175 " class Z : public X {}; };", ZIsDerivedFromX));
176 EXPECT_TRUE(matches("class X {}; class Z : public ::X {};",
177 ZIsDerivedFromX));
178 EXPECT_TRUE(
179 notMatches("template<class T> class X {}; "
180 "template<class T> class A { class Z : public X<T>::D {}; };",
181 ZIsDerivedFromX));
182 EXPECT_TRUE(
183 matches("template<class T> class X { public: typedef X<T> D; }; "
184 "template<class T> class A { public: "
185 " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }",
186 ZIsDerivedFromX));
187 EXPECT_TRUE(
188 notMatches("template<class X> class A { class Z : public X::D::E {}; };",
189 ZIsDerivedFromX));
190 EXPECT_TRUE(
191 matches("class X {}; typedef X V; typedef V W; class Z : public W {};",
192 ZIsDerivedFromX));
193 EXPECT_TRUE(
194 matches("class X {}; class Y : public X {}; "
195 "typedef Y V; typedef V W; class Z : public W {};",
196 ZIsDerivedFromX));
197 EXPECT_TRUE(
198 matches("template<class T, class U> class X {}; "
199 "template<class T> class A { class Z : public X<T, int> {}; };",
200 ZIsDerivedFromX));
201 EXPECT_TRUE(
202 notMatches("template<class X> class D { typedef X A; typedef A B; "
203 " typedef B C; class Z : public C {}; };",
204 ZIsDerivedFromX));
205 EXPECT_TRUE(
206 matches("class X {}; typedef X A; typedef A B; "
207 "class Z : public B {};", ZIsDerivedFromX));
208 EXPECT_TRUE(
209 matches("class X {}; typedef X A; typedef A B; typedef B C; "
210 "class Z : public C {};", ZIsDerivedFromX));
211 EXPECT_TRUE(
212 matches("class U {}; typedef U X; typedef X V; "
213 "class Z : public V {};", ZIsDerivedFromX));
214 EXPECT_TRUE(
215 matches("class Base {}; typedef Base X; "
216 "class Z : public Base {};", ZIsDerivedFromX));
217 EXPECT_TRUE(
218 matches("class Base {}; typedef Base Base2; typedef Base2 X; "
219 "class Z : public Base {};", ZIsDerivedFromX));
220 EXPECT_TRUE(
221 notMatches("class Base {}; class Base2 {}; typedef Base2 X; "
222 "class Z : public Base {};", ZIsDerivedFromX));
223 EXPECT_TRUE(
224 matches("class A {}; typedef A X; typedef A Y; "
225 "class Z : public Y {};", ZIsDerivedFromX));
226 EXPECT_TRUE(
227 notMatches("template <typename T> class Z;"
228 "template <> class Z<void> {};"
229 "template <typename T> class Z : public Z<void> {};",
230 IsDerivedFromX));
231 EXPECT_TRUE(
232 matches("template <typename T> class X;"
233 "template <> class X<void> {};"
234 "template <typename T> class X : public X<void> {};",
235 IsDerivedFromX));
236 EXPECT_TRUE(matches(
237 "class X {};"
238 "template <typename T> class Z;"
239 "template <> class Z<void> {};"
240 "template <typename T> class Z : public Z<void>, public X {};",
241 ZIsDerivedFromX));
Manuel Klimek987c2f52012-12-04 13:40:29 +0000242 EXPECT_TRUE(
243 notMatches("template<int> struct X;"
244 "template<int i> struct X : public X<i-1> {};",
245 recordDecl(isDerivedFrom(recordDecl(hasName("Some"))))));
246 EXPECT_TRUE(matches(
247 "struct A {};"
248 "template<int> struct X;"
249 "template<int i> struct X : public X<i-1> {};"
250 "template<> struct X<0> : public A {};"
251 "struct B : public X<42> {};",
252 recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000253
254 // FIXME: Once we have better matchers for template type matching,
255 // get rid of the Variable(...) matching and match the right template
256 // declarations directly.
257 const char *RecursiveTemplateOneParameter =
258 "class Base1 {}; class Base2 {};"
259 "template <typename T> class Z;"
260 "template <> class Z<void> : public Base1 {};"
261 "template <> class Z<int> : public Base2 {};"
262 "template <> class Z<float> : public Z<void> {};"
263 "template <> class Z<double> : public Z<int> {};"
264 "template <typename T> class Z : public Z<float>, public Z<double> {};"
265 "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }";
266 EXPECT_TRUE(matches(
267 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000268 varDecl(hasName("z_float"),
269 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000270 EXPECT_TRUE(notMatches(
271 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000272 varDecl(hasName("z_float"),
273 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000274 EXPECT_TRUE(matches(
275 RecursiveTemplateOneParameter,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000276 varDecl(hasName("z_char"),
277 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
278 isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000279
280 const char *RecursiveTemplateTwoParameters =
281 "class Base1 {}; class Base2 {};"
282 "template <typename T1, typename T2> class Z;"
283 "template <typename T> class Z<void, T> : public Base1 {};"
284 "template <typename T> class Z<int, T> : public Base2 {};"
285 "template <typename T> class Z<float, T> : public Z<void, T> {};"
286 "template <typename T> class Z<double, T> : public Z<int, T> {};"
287 "template <typename T1, typename T2> class Z : "
288 " public Z<float, T2>, public Z<double, T2> {};"
289 "void f() { Z<float, void> z_float; Z<double, void> z_double; "
290 " Z<char, void> z_char; }";
291 EXPECT_TRUE(matches(
292 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000293 varDecl(hasName("z_float"),
294 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000295 EXPECT_TRUE(notMatches(
296 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000297 varDecl(hasName("z_float"),
298 hasInitializer(hasType(recordDecl(isDerivedFrom("Base2")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000299 EXPECT_TRUE(matches(
300 RecursiveTemplateTwoParameters,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000301 varDecl(hasName("z_char"),
302 hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"),
303 isDerivedFrom("Base2")))))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000304 EXPECT_TRUE(matches(
305 "namespace ns { class X {}; class Y : public X {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000306 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000307 EXPECT_TRUE(notMatches(
308 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000309 recordDecl(isDerivedFrom("::ns::X"))));
Daniel Jasper20b802d2012-07-17 07:39:27 +0000310
311 EXPECT_TRUE(matches(
312 "class X {}; class Y : public X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000313 recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000314}
315
Edwin Vane6a19a972013-03-06 17:02:57 +0000316TEST(DeclarationMatcher, hasMethod) {
317 EXPECT_TRUE(matches("class A { void func(); };",
318 recordDecl(hasMethod(hasName("func")))));
319 EXPECT_TRUE(notMatches("class A { void func(); };",
320 recordDecl(hasMethod(isPublic()))));
321}
322
Daniel Jasper08f0c532012-09-18 14:17:42 +0000323TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) {
324 EXPECT_TRUE(matches(
325 "template <typename T> struct A {"
326 " template <typename T2> struct F {};"
327 "};"
328 "template <typename T> struct B : A<T>::template F<T> {};"
329 "B<int> b;",
330 recordDecl(hasName("B"), isDerivedFrom(recordDecl()))));
331}
332
Edwin Vane742d9e72013-02-25 20:43:32 +0000333TEST(DeclarationMatcher, hasDeclContext) {
334 EXPECT_TRUE(matches(
335 "namespace N {"
336 " namespace M {"
337 " class D {};"
338 " }"
339 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000340 recordDecl(hasDeclContext(namespaceDecl(hasName("M"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000341 EXPECT_TRUE(notMatches(
342 "namespace N {"
343 " namespace M {"
344 " class D {};"
345 " }"
346 "}",
Daniel Jasperabe92232013-04-08 16:44:05 +0000347 recordDecl(hasDeclContext(namespaceDecl(hasName("N"))))));
348
349 EXPECT_TRUE(matches("namespace {"
350 " namespace M {"
351 " class D {};"
352 " }"
353 "}",
354 recordDecl(hasDeclContext(namespaceDecl(
355 hasName("M"), hasDeclContext(namespaceDecl()))))));
Edwin Vane742d9e72013-02-25 20:43:32 +0000356}
357
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000358TEST(ClassTemplate, DoesNotMatchClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000359 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000360 EXPECT_TRUE(notMatches("class X;", ClassX));
361 EXPECT_TRUE(notMatches("class X {};", ClassX));
362}
363
364TEST(ClassTemplate, MatchesClassTemplate) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000365 DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000366 EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
367 EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
368}
369
370TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
371 EXPECT_TRUE(notMatches("template<typename T> class X { };"
372 "template<> class X<int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000373 classTemplateDecl(hasName("X"),
374 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000375}
376
377TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
378 EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
379 "template<typename T> class X<T, int> { int a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000380 classTemplateDecl(hasName("X"),
381 hasDescendant(fieldDecl(hasName("a"))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +0000382}
383
Daniel Jasper6a124492012-07-12 08:50:38 +0000384TEST(AllOf, AllOverloadsWork) {
385 const char Program[] =
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000386 "struct T { };"
387 "int f(int, T*, int, int);"
388 "void g(int x) { T t; f(x, &t, 3, 4); }";
Daniel Jasper6a124492012-07-12 08:50:38 +0000389 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000390 callExpr(allOf(callee(functionDecl(hasName("f"))),
391 hasArgument(0, declRefExpr(to(varDecl())))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000392 EXPECT_TRUE(matches(Program,
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000393 callExpr(allOf(callee(functionDecl(hasName("f"))),
394 hasArgument(0, declRefExpr(to(varDecl()))),
395 hasArgument(1, hasType(pointsTo(
396 recordDecl(hasName("T")))))))));
Edwin Vane7f43fcf2013-02-12 13:55:40 +0000397 EXPECT_TRUE(matches(Program,
398 callExpr(allOf(callee(functionDecl(hasName("f"))),
399 hasArgument(0, declRefExpr(to(varDecl()))),
400 hasArgument(1, hasType(pointsTo(
401 recordDecl(hasName("T"))))),
402 hasArgument(2, integerLiteral(equals(3)))))));
403 EXPECT_TRUE(matches(Program,
404 callExpr(allOf(callee(functionDecl(hasName("f"))),
405 hasArgument(0, declRefExpr(to(varDecl()))),
406 hasArgument(1, hasType(pointsTo(
407 recordDecl(hasName("T"))))),
408 hasArgument(2, integerLiteral(equals(3))),
409 hasArgument(3, integerLiteral(equals(4)))))));
Daniel Jasper6a124492012-07-12 08:50:38 +0000410}
411
Manuel Klimek4da21662012-07-06 05:48:52 +0000412TEST(DeclarationMatcher, MatchAnyOf) {
413 DeclarationMatcher YOrZDerivedFromX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000414 recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000415 EXPECT_TRUE(
416 matches("class X {}; class Z : public X {};", YOrZDerivedFromX));
417 EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX));
418 EXPECT_TRUE(
419 notMatches("class X {}; class W : public X {};", YOrZDerivedFromX));
420 EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX));
421
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000422 DeclarationMatcher XOrYOrZOrU =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000423 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U")));
Daniel Jasperff2fcb82012-07-15 19:57:12 +0000424 EXPECT_TRUE(matches("class X {};", XOrYOrZOrU));
425 EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU));
426
Manuel Klimek4da21662012-07-06 05:48:52 +0000427 DeclarationMatcher XOrYOrZOrUOrV =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000428 recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"),
429 hasName("V")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000430 EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV));
431 EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV));
432 EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV));
433 EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV));
434 EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV));
435 EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV));
436}
437
438TEST(DeclarationMatcher, MatchHas) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000439 DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000440 EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX));
441 EXPECT_TRUE(matches("class X {};", HasClassX));
442
443 DeclarationMatcher YHasClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000444 recordDecl(hasName("Y"), has(recordDecl(hasName("X"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000445 EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX));
446 EXPECT_TRUE(notMatches("class X {};", YHasClassX));
447 EXPECT_TRUE(
448 notMatches("class Y { class Z { class X {}; }; };", YHasClassX));
449}
450
451TEST(DeclarationMatcher, MatchHasRecursiveAllOf) {
452 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000453 recordDecl(
454 has(recordDecl(
455 has(recordDecl(hasName("X"))),
456 has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000457 hasName("Z"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000458 has(recordDecl(
459 has(recordDecl(hasName("A"))),
460 has(recordDecl(hasName("B"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000461 hasName("C"))),
462 hasName("F"));
463
464 EXPECT_TRUE(matches(
465 "class F {"
466 " class Z {"
467 " class X {};"
468 " class Y {};"
469 " };"
470 " class C {"
471 " class A {};"
472 " class B {};"
473 " };"
474 "};", Recursive));
475
476 EXPECT_TRUE(matches(
477 "class F {"
478 " class Z {"
479 " class A {};"
480 " class X {};"
481 " class Y {};"
482 " };"
483 " class C {"
484 " class X {};"
485 " class A {};"
486 " class B {};"
487 " };"
488 "};", Recursive));
489
490 EXPECT_TRUE(matches(
491 "class O1 {"
492 " class O2 {"
493 " class F {"
494 " class Z {"
495 " class A {};"
496 " class X {};"
497 " class Y {};"
498 " };"
499 " class C {"
500 " class X {};"
501 " class A {};"
502 " class B {};"
503 " };"
504 " };"
505 " };"
506 "};", Recursive));
507}
508
509TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) {
510 DeclarationMatcher Recursive =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000511 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000512 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000513 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000514 anyOf(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000515 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000516 hasName("X"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000517 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000518 hasName("Y"))),
519 hasName("Z")))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000520 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000521 anyOf(
522 hasName("C"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000523 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000524 hasName("A"))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000525 has(recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000526 hasName("B")))))),
527 hasName("F")));
528
529 EXPECT_TRUE(matches("class F {};", Recursive));
530 EXPECT_TRUE(matches("class Z {};", Recursive));
531 EXPECT_TRUE(matches("class C {};", Recursive));
532 EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive));
533 EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive));
534 EXPECT_TRUE(
535 matches("class O1 { class O2 {"
536 " class M { class N { class B {}; }; }; "
537 "}; };", Recursive));
538}
539
540TEST(DeclarationMatcher, MatchNot) {
541 DeclarationMatcher NotClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000542 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000543 isDerivedFrom("Y"),
Manuel Klimek4da21662012-07-06 05:48:52 +0000544 unless(hasName("X")));
545 EXPECT_TRUE(notMatches("", NotClassX));
546 EXPECT_TRUE(notMatches("class Y {};", NotClassX));
547 EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX));
548 EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX));
549 EXPECT_TRUE(
550 notMatches("class Y {}; class Z {}; class X : public Y {};",
551 NotClassX));
552
553 DeclarationMatcher ClassXHasNotClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000554 recordDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +0000555 hasName("X"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000556 has(recordDecl(hasName("Z"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000557 unless(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000558 has(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000559 EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY));
560 EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };",
561 ClassXHasNotClassY));
562}
563
564TEST(DeclarationMatcher, HasDescendant) {
565 DeclarationMatcher ZDescendantClassX =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000566 recordDecl(
567 hasDescendant(recordDecl(hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000568 hasName("Z"));
569 EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX));
570 EXPECT_TRUE(
571 matches("class Z { class Y { class X {}; }; };", ZDescendantClassX));
572 EXPECT_TRUE(
573 matches("class Z { class A { class Y { class X {}; }; }; };",
574 ZDescendantClassX));
575 EXPECT_TRUE(
576 matches("class Z { class A { class B { class Y { class X {}; }; }; }; };",
577 ZDescendantClassX));
578 EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX));
579
580 DeclarationMatcher ZDescendantClassXHasClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000581 recordDecl(
582 hasDescendant(recordDecl(has(recordDecl(hasName("Y"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000583 hasName("X"))),
584 hasName("Z"));
585 EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };",
586 ZDescendantClassXHasClassY));
587 EXPECT_TRUE(
588 matches("class Z { class A { class B { class X { class Y {}; }; }; }; };",
589 ZDescendantClassXHasClassY));
590 EXPECT_TRUE(notMatches(
591 "class Z {"
592 " class A {"
593 " class B {"
594 " class X {"
595 " class C {"
596 " class Y {};"
597 " };"
598 " };"
599 " }; "
600 " };"
601 "};", ZDescendantClassXHasClassY));
602
603 DeclarationMatcher ZDescendantClassXDescendantClassY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000604 recordDecl(
605 hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))),
606 hasName("X"))),
Manuel Klimek4da21662012-07-06 05:48:52 +0000607 hasName("Z"));
608 EXPECT_TRUE(
609 matches("class Z { class A { class X { class B { class Y {}; }; }; }; };",
610 ZDescendantClassXDescendantClassY));
611 EXPECT_TRUE(matches(
612 "class Z {"
613 " class A {"
614 " class X {"
615 " class B {"
616 " class Y {};"
617 " };"
618 " class Y {};"
619 " };"
620 " };"
621 "};", ZDescendantClassXDescendantClassY));
622}
623
Daniel Jaspera267cf62012-10-29 10:14:44 +0000624// Implements a run method that returns whether BoundNodes contains a
625// Decl bound to Id that can be dynamically cast to T.
626// Optionally checks that the check succeeded a specific number of times.
627template <typename T>
628class VerifyIdIsBoundTo : public BoundNodesCallback {
629public:
630 // Create an object that checks that a node of type \c T was bound to \c Id.
631 // Does not check for a certain number of matches.
632 explicit VerifyIdIsBoundTo(llvm::StringRef Id)
633 : Id(Id), ExpectedCount(-1), Count(0) {}
634
635 // Create an object that checks that a node of type \c T was bound to \c Id.
636 // Checks that there were exactly \c ExpectedCount matches.
637 VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
638 : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
639
640 // Create an object that checks that a node of type \c T was bound to \c Id.
641 // Checks that there was exactly one match with the name \c ExpectedName.
642 // Note that \c T must be a NamedDecl for this to work.
Manuel Klimek374516c2013-03-14 16:33:21 +0000643 VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
644 int ExpectedCount = 1)
645 : Id(Id), ExpectedCount(ExpectedCount), Count(0),
646 ExpectedName(ExpectedName) {}
Daniel Jaspera267cf62012-10-29 10:14:44 +0000647
648 ~VerifyIdIsBoundTo() {
649 if (ExpectedCount != -1)
650 EXPECT_EQ(ExpectedCount, Count);
651 if (!ExpectedName.empty())
652 EXPECT_EQ(ExpectedName, Name);
653 }
654
655 virtual bool run(const BoundNodes *Nodes) {
656 if (Nodes->getNodeAs<T>(Id)) {
657 ++Count;
658 if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
659 Name = Named->getNameAsString();
660 } else if (const NestedNameSpecifier *NNS =
661 Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
662 llvm::raw_string_ostream OS(Name);
663 NNS->print(OS, PrintingPolicy(LangOptions()));
664 }
665 return true;
666 }
667 return false;
668 }
669
Daniel Jasper452abbc2012-10-29 10:48:25 +0000670 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
671 return run(Nodes);
672 }
673
Daniel Jaspera267cf62012-10-29 10:14:44 +0000674private:
675 const std::string Id;
676 const int ExpectedCount;
677 int Count;
678 const std::string ExpectedName;
679 std::string Name;
680};
681
682TEST(HasDescendant, MatchesDescendantTypes) {
683 EXPECT_TRUE(matches("void f() { int i = 3; }",
684 decl(hasDescendant(loc(builtinType())))));
685 EXPECT_TRUE(matches("void f() { int i = 3; }",
686 stmt(hasDescendant(builtinType()))));
687
688 EXPECT_TRUE(matches("void f() { int i = 3; }",
689 stmt(hasDescendant(loc(builtinType())))));
690 EXPECT_TRUE(matches("void f() { int i = 3; }",
691 stmt(hasDescendant(qualType(builtinType())))));
692
693 EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }",
694 stmt(hasDescendant(isInteger()))));
695
696 EXPECT_TRUE(matchAndVerifyResultTrue(
697 "void f() { int a; float c; int d; int e; }",
698 functionDecl(forEachDescendant(
699 varDecl(hasDescendant(isInteger())).bind("x"))),
700 new VerifyIdIsBoundTo<Decl>("x", 3)));
701}
702
703TEST(HasDescendant, MatchesDescendantsOfTypes) {
704 EXPECT_TRUE(matches("void f() { int*** i; }",
705 qualType(hasDescendant(builtinType()))));
706 EXPECT_TRUE(matches("void f() { int*** i; }",
707 qualType(hasDescendant(
708 pointerType(pointee(builtinType()))))));
709 EXPECT_TRUE(matches("void f() { int*** i; }",
David Blaikie5be093c2013-02-18 19:04:16 +0000710 typeLoc(hasDescendant(loc(builtinType())))));
Daniel Jaspera267cf62012-10-29 10:14:44 +0000711
712 EXPECT_TRUE(matchAndVerifyResultTrue(
713 "void f() { int*** i; }",
714 qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))),
715 new VerifyIdIsBoundTo<Type>("x", 2)));
716}
717
718TEST(Has, MatchesChildrenOfTypes) {
719 EXPECT_TRUE(matches("int i;",
720 varDecl(hasName("i"), has(isInteger()))));
721 EXPECT_TRUE(notMatches("int** i;",
722 varDecl(hasName("i"), has(isInteger()))));
723 EXPECT_TRUE(matchAndVerifyResultTrue(
724 "int (*f)(float, int);",
725 qualType(functionType(), forEach(qualType(isInteger()).bind("x"))),
726 new VerifyIdIsBoundTo<QualType>("x", 2)));
727}
728
729TEST(Has, MatchesChildTypes) {
730 EXPECT_TRUE(matches(
731 "int* i;",
732 varDecl(hasName("i"), hasType(qualType(has(builtinType()))))));
733 EXPECT_TRUE(notMatches(
734 "int* i;",
735 varDecl(hasName("i"), hasType(qualType(has(pointerType()))))));
736}
737
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000738TEST(Enum, DoesNotMatchClasses) {
739 EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
740}
741
742TEST(Enum, MatchesEnums) {
743 EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
744}
745
746TEST(EnumConstant, Matches) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000747 DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000748 EXPECT_TRUE(matches("enum X{ A };", Matcher));
749 EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
750 EXPECT_TRUE(notMatches("enum X {};", Matcher));
751}
752
Manuel Klimek4da21662012-07-06 05:48:52 +0000753TEST(StatementMatcher, Has) {
754 StatementMatcher HasVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000755 expr(hasType(pointsTo(recordDecl(hasName("X")))),
756 has(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000757
758 EXPECT_TRUE(matches(
759 "class X; X *x(int); void c() { int i; x(i); }", HasVariableI));
760 EXPECT_TRUE(notMatches(
761 "class X; X *x(int); void c() { int i; x(42); }", HasVariableI));
762}
763
764TEST(StatementMatcher, HasDescendant) {
765 StatementMatcher HasDescendantVariableI =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000766 expr(hasType(pointsTo(recordDecl(hasName("X")))),
767 hasDescendant(declRefExpr(to(varDecl(hasName("i"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000768
769 EXPECT_TRUE(matches(
770 "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }",
771 HasDescendantVariableI));
772 EXPECT_TRUE(notMatches(
773 "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }",
774 HasDescendantVariableI));
775}
776
777TEST(TypeMatcher, MatchesClassType) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000778 TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000779
780 EXPECT_TRUE(matches("class A { public: A *a; };", TypeA));
781 EXPECT_TRUE(notMatches("class A {};", TypeA));
782
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000783 TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000784
785 EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };",
786 TypeDerivedFromA));
787 EXPECT_TRUE(notMatches("class A {};", TypeA));
788
789 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000790 recordDecl(hasName("A"), has(recordDecl(hasName("B")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000791
792 EXPECT_TRUE(
793 matches("class A { public: A *a; class B {}; };", TypeAHasClassB));
794}
795
Manuel Klimek4da21662012-07-06 05:48:52 +0000796TEST(Matcher, BindMatchedNodes) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000797 DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000798
799 EXPECT_TRUE(matchAndVerifyResultTrue("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000800 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000801
802 EXPECT_TRUE(matchAndVerifyResultFalse("class X {};",
Daniel Jaspera7564432012-09-13 13:11:25 +0000803 ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000804
805 TypeMatcher TypeAHasClassB = hasDeclaration(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000806 recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000807
808 EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };",
809 TypeAHasClassB,
Daniel Jaspera7564432012-09-13 13:11:25 +0000810 new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000811
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000812 StatementMatcher MethodX =
813 callExpr(callee(methodDecl(hasName("x")))).bind("x");
Manuel Klimek4da21662012-07-06 05:48:52 +0000814
815 EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };",
816 MethodX,
Daniel Jaspera7564432012-09-13 13:11:25 +0000817 new VerifyIdIsBoundTo<CXXMemberCallExpr>("x")));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000818}
819
820TEST(Matcher, BindTheSameNameInAlternatives) {
821 StatementMatcher matcher = anyOf(
822 binaryOperator(hasOperatorName("+"),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000823 hasLHS(expr().bind("x")),
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000824 hasRHS(integerLiteral(equals(0)))),
825 binaryOperator(hasOperatorName("+"),
826 hasLHS(integerLiteral(equals(0))),
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000827 hasRHS(expr().bind("x"))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000828
829 EXPECT_TRUE(matchAndVerifyResultTrue(
830 // The first branch of the matcher binds x to 0 but then fails.
831 // The second branch binds x to f() and succeeds.
832 "int f() { return 0 + f(); }",
833 matcher,
Daniel Jaspera7564432012-09-13 13:11:25 +0000834 new VerifyIdIsBoundTo<CallExpr>("x")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000835}
836
Manuel Klimek66341c52012-08-30 19:41:06 +0000837TEST(Matcher, BindsIDForMemoizedResults) {
838 // Using the same matcher in two match expressions will make memoization
839 // kick in.
840 DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x");
841 EXPECT_TRUE(matchAndVerifyResultTrue(
842 "class A { class B { class X {}; }; };",
843 DeclarationMatcher(anyOf(
844 recordDecl(hasName("A"), hasDescendant(ClassX)),
845 recordDecl(hasName("B"), hasDescendant(ClassX)))),
Daniel Jaspera7564432012-09-13 13:11:25 +0000846 new VerifyIdIsBoundTo<Decl>("x", 2)));
Manuel Klimek66341c52012-08-30 19:41:06 +0000847}
848
Daniel Jasper189f2e42012-12-03 15:43:25 +0000849TEST(HasDeclaration, HasDeclarationOfEnumType) {
850 EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }",
851 expr(hasType(pointsTo(
852 qualType(hasDeclaration(enumDecl(hasName("X")))))))));
853}
854
Edwin Vaneb45083d2013-02-25 14:32:42 +0000855TEST(HasDeclaration, HasGetDeclTraitTest) {
856 EXPECT_TRUE(internal::has_getDecl<TypedefType>::value);
857 EXPECT_TRUE(internal::has_getDecl<RecordType>::value);
858 EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value);
859}
860
Edwin Vane52380602013-02-19 17:14:34 +0000861TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) {
862 EXPECT_TRUE(matches("typedef int X; X a;",
863 varDecl(hasName("a"),
864 hasType(typedefType(hasDeclaration(decl()))))));
865
866 // FIXME: Add tests for other types with getDecl() (e.g. RecordType)
867}
868
Edwin Vane3abf7782013-02-25 14:49:29 +0000869TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) {
870 EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;",
871 varDecl(hasType(templateSpecializationType(
872 hasDeclaration(namedDecl(hasName("A"))))))));
873}
874
Manuel Klimek4da21662012-07-06 05:48:52 +0000875TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000876 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000877 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000878 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000879 EXPECT_TRUE(
880 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000881 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000882 EXPECT_TRUE(
883 matches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000884 expr(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000885}
886
887TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000888 TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X")));
Manuel Klimek4da21662012-07-06 05:48:52 +0000889 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000890 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000891 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000892 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000893 EXPECT_TRUE(
894 matches("class X {}; void y() { X *x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000895 varDecl(hasType(pointsTo(ClassX)))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000896}
897
898TEST(HasType, TakesDeclMatcherAndMatchesExpr) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000899 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000900 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000901 matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000902 EXPECT_TRUE(
903 notMatches("class X {}; void y(X *x) { x; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000904 expr(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000905}
906
907TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000908 DeclarationMatcher ClassX = recordDecl(hasName("X"));
Manuel Klimek4da21662012-07-06 05:48:52 +0000909 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000910 matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000911 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000912 notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000913}
914
Manuel Klimek1a68afd2013-06-20 13:08:29 +0000915TEST(HasTypeLoc, MatchesDeclaratorDecls) {
916 EXPECT_TRUE(matches("int x;",
917 varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
918
919 // Make sure we don't crash on implicit constructors.
920 EXPECT_TRUE(notMatches("class X {}; X x;",
921 declaratorDecl(hasTypeLoc(loc(asString("int"))))));
922}
923
Manuel Klimek4da21662012-07-06 05:48:52 +0000924TEST(Matcher, Call) {
925 // FIXME: Do we want to overload Call() to directly take
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +0000926 // Matcher<Decl>, too?
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000927 StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000928
929 EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
930 EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
931
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000932 StatementMatcher MethodOnY =
933 memberCallExpr(on(hasType(recordDecl(hasName("Y")))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000934
935 EXPECT_TRUE(
936 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
937 MethodOnY));
938 EXPECT_TRUE(
939 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
940 MethodOnY));
941 EXPECT_TRUE(
942 notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
943 MethodOnY));
944 EXPECT_TRUE(
945 notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
946 MethodOnY));
947 EXPECT_TRUE(
948 notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
949 MethodOnY));
950
951 StatementMatcher MethodOnYPointer =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +0000952 memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +0000953
954 EXPECT_TRUE(
955 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
956 MethodOnYPointer));
957 EXPECT_TRUE(
958 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
959 MethodOnYPointer));
960 EXPECT_TRUE(
961 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
962 MethodOnYPointer));
963 EXPECT_TRUE(
964 notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
965 MethodOnYPointer));
966 EXPECT_TRUE(
967 notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
968 MethodOnYPointer));
969}
970
Daniel Jasper31f7c082012-10-01 13:40:41 +0000971TEST(Matcher, Lambda) {
972 EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
973 lambdaExpr()));
974}
975
976TEST(Matcher, ForRange) {
Daniel Jasper1a00fee2012-10-01 15:05:34 +0000977 EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
978 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +0000979 forRangeStmt()));
980 EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
981 forRangeStmt()));
982}
983
984TEST(Matcher, UserDefinedLiteral) {
985 EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
986 " return i + 1;"
987 "}"
988 "char c = 'a'_inc;",
989 userDefinedLiteral()));
990}
991
Daniel Jasperb54b7642012-09-20 14:12:57 +0000992TEST(Matcher, FlowControl) {
993 EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
994 EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
995 continueStmt()));
996 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
997 EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt()));
998 EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
999}
1000
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001001TEST(HasType, MatchesAsString) {
1002 EXPECT_TRUE(
1003 matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001004 memberCallExpr(on(hasType(asString("class Y *"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001005 EXPECT_TRUE(matches("class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001006 methodDecl(hasParameter(0, hasType(asString("int"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001007 EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001008 fieldDecl(hasType(asString("ns::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001009 EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001010 fieldDecl(hasType(asString("struct <anonymous>::A")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001011}
1012
Manuel Klimek4da21662012-07-06 05:48:52 +00001013TEST(Matcher, OverloadedOperatorCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001014 StatementMatcher OpCall = operatorCallExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001015 // Unary operator
1016 EXPECT_TRUE(matches("class Y { }; "
1017 "bool operator!(Y x) { return false; }; "
1018 "Y y; bool c = !y;", OpCall));
1019 // No match -- special operators like "new", "delete"
1020 // FIXME: operator new takes size_t, for which we need stddef.h, for which
1021 // we need to figure out include paths in the test.
1022 // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
1023 // "class Y { }; "
1024 // "void *operator new(size_t size) { return 0; } "
1025 // "Y *y = new Y;", OpCall));
1026 EXPECT_TRUE(notMatches("class Y { }; "
1027 "void operator delete(void *p) { } "
1028 "void a() {Y *y = new Y; delete y;}", OpCall));
1029 // Binary operator
1030 EXPECT_TRUE(matches("class Y { }; "
1031 "bool operator&&(Y x, Y y) { return true; }; "
1032 "Y a; Y b; bool c = a && b;",
1033 OpCall));
1034 // No match -- normal operator, not an overloaded one.
1035 EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
1036 EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
1037}
1038
1039TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
1040 StatementMatcher OpCallAndAnd =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001041 operatorCallExpr(hasOverloadedOperatorName("&&"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001042 EXPECT_TRUE(matches("class Y { }; "
1043 "bool operator&&(Y x, Y y) { return true; }; "
1044 "Y a; Y b; bool c = a && b;", OpCallAndAnd));
1045 StatementMatcher OpCallLessLess =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001046 operatorCallExpr(hasOverloadedOperatorName("<<"));
Manuel Klimek4da21662012-07-06 05:48:52 +00001047 EXPECT_TRUE(notMatches("class Y { }; "
1048 "bool operator&&(Y x, Y y) { return true; }; "
1049 "Y a; Y b; bool c = a && b;",
1050 OpCallLessLess));
Edwin Vane6a19a972013-03-06 17:02:57 +00001051 DeclarationMatcher ClassWithOpStar =
1052 recordDecl(hasMethod(hasOverloadedOperatorName("*")));
1053 EXPECT_TRUE(matches("class Y { int operator*(); };",
1054 ClassWithOpStar));
1055 EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
1056 ClassWithOpStar)) ;
Manuel Klimek4da21662012-07-06 05:48:52 +00001057}
1058
Daniel Jasper278057f2012-11-15 03:29:05 +00001059TEST(Matcher, NestedOverloadedOperatorCalls) {
1060 EXPECT_TRUE(matchAndVerifyResultTrue(
1061 "class Y { }; "
1062 "Y& operator&&(Y& x, Y& y) { return x; }; "
1063 "Y a; Y b; Y c; Y d = a && b && c;",
1064 operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"),
1065 new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2)));
1066 EXPECT_TRUE(matches(
1067 "class Y { }; "
1068 "Y& operator&&(Y& x, Y& y) { return x; }; "
1069 "Y a; Y b; Y c; Y d = a && b && c;",
1070 operatorCallExpr(hasParent(operatorCallExpr()))));
1071 EXPECT_TRUE(matches(
1072 "class Y { }; "
1073 "Y& operator&&(Y& x, Y& y) { return x; }; "
1074 "Y a; Y b; Y c; Y d = a && b && c;",
1075 operatorCallExpr(hasDescendant(operatorCallExpr()))));
1076}
1077
Manuel Klimek4da21662012-07-06 05:48:52 +00001078TEST(Matcher, ThisPointerType) {
Manuel Klimek9f174082012-07-24 13:37:29 +00001079 StatementMatcher MethodOnY =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001080 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001081
1082 EXPECT_TRUE(
1083 matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
1084 MethodOnY));
1085 EXPECT_TRUE(
1086 matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
1087 MethodOnY));
1088 EXPECT_TRUE(
1089 matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
1090 MethodOnY));
1091 EXPECT_TRUE(
1092 matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
1093 MethodOnY));
1094 EXPECT_TRUE(
1095 matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
1096 MethodOnY));
1097
1098 EXPECT_TRUE(matches(
1099 "class Y {"
1100 " public: virtual void x();"
1101 "};"
1102 "class X : public Y {"
1103 " public: virtual void x();"
1104 "};"
1105 "void z() { X *x; x->Y::x(); }", MethodOnY));
1106}
1107
1108TEST(Matcher, VariableUsage) {
1109 StatementMatcher Reference =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001110 declRefExpr(to(
1111 varDecl(hasInitializer(
1112 memberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001113
1114 EXPECT_TRUE(matches(
1115 "class Y {"
1116 " public:"
1117 " bool x() const;"
1118 "};"
1119 "void z(const Y &y) {"
1120 " bool b = y.x();"
1121 " if (b) {}"
1122 "}", Reference));
1123
1124 EXPECT_TRUE(notMatches(
1125 "class Y {"
1126 " public:"
1127 " bool x() const;"
1128 "};"
1129 "void z(const Y &y) {"
1130 " bool b = y.x();"
1131 "}", Reference));
1132}
1133
Manuel Klimek8cb9bf52012-12-04 14:42:08 +00001134TEST(Matcher, FindsVarDeclInFunctionParameter) {
Daniel Jasper9bd28092012-07-30 05:03:25 +00001135 EXPECT_TRUE(matches(
1136 "void f(int i) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001137 varDecl(hasName("i"))));
Daniel Jasper9bd28092012-07-30 05:03:25 +00001138}
1139
Manuel Klimek4da21662012-07-06 05:48:52 +00001140TEST(Matcher, CalledVariable) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001141 StatementMatcher CallOnVariableY =
1142 memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001143
1144 EXPECT_TRUE(matches(
1145 "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
1146 EXPECT_TRUE(matches(
1147 "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
1148 EXPECT_TRUE(matches(
1149 "class Y { public: void x(); };"
1150 "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
1151 EXPECT_TRUE(matches(
1152 "class Y { public: void x(); };"
1153 "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
1154 EXPECT_TRUE(notMatches(
1155 "class Y { public: void x(); };"
1156 "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
1157 CallOnVariableY));
1158}
1159
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001160TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
1161 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
1162 unaryExprOrTypeTraitExpr()));
1163 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
1164 alignOfExpr(anything())));
1165 // FIXME: Uncomment once alignof is enabled.
1166 // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
1167 // unaryExprOrTypeTraitExpr()));
1168 // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
1169 // sizeOfExpr()));
1170}
1171
1172TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) {
1173 EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr(
1174 hasArgumentOfType(asString("int")))));
1175 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
1176 hasArgumentOfType(asString("float")))));
1177 EXPECT_TRUE(matches(
1178 "struct A {}; void x() { A a; int b = sizeof(a); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001179 sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001180 EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001181 hasArgumentOfType(hasDeclaration(recordDecl(hasName("string")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001182}
1183
Manuel Klimek4da21662012-07-06 05:48:52 +00001184TEST(MemberExpression, DoesNotMatchClasses) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001185 EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001186}
1187
1188TEST(MemberExpression, MatchesMemberFunctionCall) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001189 EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001190}
1191
1192TEST(MemberExpression, MatchesVariable) {
1193 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001194 matches("class Y { void x() { this->y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001195 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001196 matches("class Y { void x() { y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001197 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001198 matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001199}
1200
1201TEST(MemberExpression, MatchesStaticVariable) {
1202 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001203 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001204 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001205 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001206 EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001207 memberExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00001208}
1209
Daniel Jasper6a124492012-07-12 08:50:38 +00001210TEST(IsInteger, MatchesIntegers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001211 EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger()))));
1212 EXPECT_TRUE(matches(
1213 "long long i = 0; void f(long long) { }; void g() {f(i);}",
1214 callExpr(hasArgument(0, declRefExpr(
1215 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001216}
1217
1218TEST(IsInteger, ReportsNoFalsePositives) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001219 EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001220 EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001221 callExpr(hasArgument(0, declRefExpr(
1222 to(varDecl(hasType(isInteger()))))))));
Daniel Jasper6a124492012-07-12 08:50:38 +00001223}
1224
Manuel Klimek4da21662012-07-06 05:48:52 +00001225TEST(IsArrow, MatchesMemberVariablesViaArrow) {
1226 EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001227 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001228 EXPECT_TRUE(matches("class Y { void x() { y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001229 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001230 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001231 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001232}
1233
1234TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
1235 EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001236 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001237 EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001238 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001239 EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001240 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001241}
1242
1243TEST(IsArrow, MatchesMemberCallsViaArrow) {
1244 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001245 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001246 EXPECT_TRUE(matches("class Y { void x() { x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001247 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001248 EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001249 memberExpr(isArrow())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001250}
1251
1252TEST(Callee, MatchesDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001253 StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001254
1255 EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX));
1256 EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX));
1257}
1258
1259TEST(Callee, MatchesMemberExpressions) {
1260 EXPECT_TRUE(matches("class Y { void x() { this->x(); } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001261 callExpr(callee(memberExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001262 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001263 notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001264}
1265
1266TEST(Function, MatchesFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001267 StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001268
1269 EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
1270 EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
1271
Manuel Klimeke265c872012-07-10 14:21:30 +00001272#if !defined(_MSC_VER)
1273 // FIXME: Make this work for MSVC.
Manuel Klimek4da21662012-07-06 05:48:52 +00001274 // Dependent contexts, but a non-dependent call.
1275 EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
1276 CallFunctionF));
1277 EXPECT_TRUE(
1278 matches("void f(); template <int N> struct S { void g() { f(); } };",
1279 CallFunctionF));
Manuel Klimeke265c872012-07-10 14:21:30 +00001280#endif
Manuel Klimek4da21662012-07-06 05:48:52 +00001281
1282 // Depedent calls don't match.
1283 EXPECT_TRUE(
1284 notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
1285 CallFunctionF));
1286 EXPECT_TRUE(
1287 notMatches("void f(int);"
1288 "template <typename T> struct S { void g(T t) { f(t); } };",
1289 CallFunctionF));
1290}
1291
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001292TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
1293 EXPECT_TRUE(
1294 matches("template <typename T> void f(T t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001295 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001296}
1297
1298TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
1299 EXPECT_TRUE(
1300 notMatches("void f(double d); void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001301 functionTemplateDecl(hasName("f"))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001302}
1303
1304TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
1305 EXPECT_TRUE(
1306 notMatches("void g(); template <typename T> void f(T t) {}"
1307 "template <> void f(int t) { g(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001308 functionTemplateDecl(hasName("f"),
1309 hasDescendant(declRefExpr(to(
1310 functionDecl(hasName("g"))))))));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00001311}
1312
Manuel Klimek4da21662012-07-06 05:48:52 +00001313TEST(Matcher, Argument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001314 StatementMatcher CallArgumentY = callExpr(
1315 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001316
1317 EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY));
1318 EXPECT_TRUE(
1319 matches("class X { void x(int) { int y; x(y); } };", CallArgumentY));
1320 EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY));
1321
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001322 StatementMatcher WrongIndex = callExpr(
1323 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001324 EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex));
1325}
1326
1327TEST(Matcher, AnyArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001328 StatementMatcher CallArgumentY = callExpr(
1329 hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001330 EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
1331 EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
1332 EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));
1333}
1334
1335TEST(Matcher, ArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001336 StatementMatcher Call1Arg = callExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001337
1338 EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg));
1339 EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg));
1340 EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
1341}
1342
Daniel Jasper36e29d62012-12-04 11:54:27 +00001343TEST(Matcher, ParameterCount) {
1344 DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
1345 EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
1346 EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
1347 EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
1348 EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
1349}
1350
Manuel Klimek4da21662012-07-06 05:48:52 +00001351TEST(Matcher, References) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001352 DeclarationMatcher ReferenceClassX = varDecl(
1353 hasType(references(recordDecl(hasName("X")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001354 EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }",
1355 ReferenceClassX));
1356 EXPECT_TRUE(
1357 matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX));
1358 EXPECT_TRUE(
1359 notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX));
1360 EXPECT_TRUE(
1361 notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX));
1362}
1363
Edwin Vane6a19a972013-03-06 17:02:57 +00001364TEST(QualType, hasCanonicalType) {
1365 EXPECT_TRUE(notMatches("typedef int &int_ref;"
1366 "int a;"
1367 "int_ref b = a;",
1368 varDecl(hasType(qualType(referenceType())))));
1369 EXPECT_TRUE(
1370 matches("typedef int &int_ref;"
1371 "int a;"
1372 "int_ref b = a;",
1373 varDecl(hasType(qualType(hasCanonicalType(referenceType()))))));
1374}
1375
Edwin Vane7b69cd02013-04-02 18:15:55 +00001376TEST(QualType, hasLocalQualifiers) {
1377 EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;",
1378 varDecl(hasType(hasLocalQualifiers()))));
1379 EXPECT_TRUE(matches("int *const j = nullptr;",
1380 varDecl(hasType(hasLocalQualifiers()))));
1381 EXPECT_TRUE(matches("int *volatile k;",
1382 varDecl(hasType(hasLocalQualifiers()))));
1383 EXPECT_TRUE(notMatches("int m;",
1384 varDecl(hasType(hasLocalQualifiers()))));
1385}
1386
Manuel Klimek4da21662012-07-06 05:48:52 +00001387TEST(HasParameter, CallsInnerMatcher) {
1388 EXPECT_TRUE(matches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001389 methodDecl(hasParameter(0, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001390 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001391 methodDecl(hasParameter(0, hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001392}
1393
1394TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) {
1395 EXPECT_TRUE(notMatches("class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001396 methodDecl(hasParameter(42, varDecl()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001397}
1398
1399TEST(HasType, MatchesParameterVariableTypesStrictly) {
1400 EXPECT_TRUE(matches("class X { void x(X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001401 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001402 EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001403 methodDecl(hasParameter(0, hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001404 EXPECT_TRUE(matches("class X { void x(const X *x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001405 methodDecl(hasParameter(0,
1406 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001407 EXPECT_TRUE(matches("class X { void x(const X &x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001408 methodDecl(hasParameter(0,
1409 hasType(references(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001410}
1411
1412TEST(HasAnyParameter, MatchesIndependentlyOfPosition) {
1413 EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001414 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001415 EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001416 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001417}
1418
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001419TEST(Returns, MatchesReturnTypes) {
1420 EXPECT_TRUE(matches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001421 functionDecl(returns(asString("int")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001422 EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001423 functionDecl(returns(asString("float")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001424 EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001425 functionDecl(returns(hasDeclaration(
1426 recordDecl(hasName("Y")))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001427}
1428
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001429TEST(IsExternC, MatchesExternCFunctionDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001430 EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
1431 EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
1432 functionDecl(isExternC())));
1433 EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC())));
Daniel Jasper8cc7efa2012-08-15 18:52:19 +00001434}
1435
Manuel Klimek4da21662012-07-06 05:48:52 +00001436TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) {
1437 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001438 methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001439}
1440
1441TEST(HasAnyParameter, DoesNotMatchThisPointer) {
1442 EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001443 methodDecl(hasAnyParameter(hasType(pointsTo(
1444 recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001445}
1446
1447TEST(HasName, MatchesParameterVariableDeclartions) {
1448 EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001449 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001450 EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001451 methodDecl(hasAnyParameter(hasName("x")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001452}
1453
Daniel Jasper371f9392012-08-01 08:40:24 +00001454TEST(Matcher, MatchesClassTemplateSpecialization) {
1455 EXPECT_TRUE(matches("template<typename T> struct A {};"
1456 "template<> struct A<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001457 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001458 EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001459 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001460 EXPECT_TRUE(notMatches("template<typename T> struct A {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001461 classTemplateSpecializationDecl()));
Daniel Jasper371f9392012-08-01 08:40:24 +00001462}
1463
Manuel Klimek1a68afd2013-06-20 13:08:29 +00001464TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
1465 EXPECT_TRUE(matches("int x;", declaratorDecl()));
1466 EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
1467}
1468
1469TEST(ParmVarDecl, MatchesParmVars) {
1470 EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
1471 EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
1472}
1473
Daniel Jasper371f9392012-08-01 08:40:24 +00001474TEST(Matcher, MatchesTypeTemplateArgument) {
1475 EXPECT_TRUE(matches(
1476 "template<typename T> struct B {};"
1477 "B<int> b;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001478 classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
Daniel Jasper371f9392012-08-01 08:40:24 +00001479 asString("int"))))));
1480}
1481
1482TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) {
1483 EXPECT_TRUE(matches(
1484 "struct B { int next; };"
1485 "template<int(B::*next_ptr)> struct A {};"
1486 "A<&B::next> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001487 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1488 refersToDeclaration(fieldDecl(hasName("next")))))));
Daniel Jasperaaa8e452012-09-29 15:55:18 +00001489
1490 EXPECT_TRUE(notMatches(
1491 "template <typename T> struct A {};"
1492 "A<int> a;",
1493 classTemplateSpecializationDecl(hasAnyTemplateArgument(
1494 refersToDeclaration(decl())))));
Daniel Jasper371f9392012-08-01 08:40:24 +00001495}
1496
1497TEST(Matcher, MatchesSpecificArgument) {
1498 EXPECT_TRUE(matches(
1499 "template<typename T, typename U> class A {};"
1500 "A<bool, int> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001501 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001502 1, refersToType(asString("int"))))));
1503 EXPECT_TRUE(notMatches(
1504 "template<typename T, typename U> class A {};"
1505 "A<int, bool> a;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001506 classTemplateSpecializationDecl(hasTemplateArgument(
Daniel Jasper371f9392012-08-01 08:40:24 +00001507 1, refersToType(asString("int"))))));
1508}
1509
Daniel Jasperf3197e92013-02-25 12:02:08 +00001510TEST(Matcher, MatchesAccessSpecDecls) {
1511 EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl()));
1512 EXPECT_TRUE(
1513 matches("class C { public: int i; };", accessSpecDecl(isPublic())));
1514 EXPECT_TRUE(
1515 notMatches("class C { public: int i; };", accessSpecDecl(isProtected())));
1516 EXPECT_TRUE(
1517 notMatches("class C { public: int i; };", accessSpecDecl(isPrivate())));
1518
1519 EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl()));
1520}
1521
Edwin Vane5771a2f2013-04-09 20:46:36 +00001522TEST(Matcher, MatchesVirtualMethod) {
1523 EXPECT_TRUE(matches("class X { virtual int f(); };",
1524 methodDecl(isVirtual(), hasName("::X::f"))));
1525 EXPECT_TRUE(notMatches("class X { int f(); };",
1526 methodDecl(isVirtual())));
1527}
1528
Edwin Vane32a6ebc2013-05-09 17:00:17 +00001529TEST(Matcher, MatchesConstMethod) {
1530 EXPECT_TRUE(matches("struct A { void foo() const; };",
1531 methodDecl(isConst())));
1532 EXPECT_TRUE(notMatches("struct A { void foo(); };",
1533 methodDecl(isConst())));
1534}
1535
Edwin Vane5771a2f2013-04-09 20:46:36 +00001536TEST(Matcher, MatchesOverridingMethod) {
1537 EXPECT_TRUE(matches("class X { virtual int f(); }; "
1538 "class Y : public X { int f(); };",
1539 methodDecl(isOverride(), hasName("::Y::f"))));
1540 EXPECT_TRUE(notMatches("class X { virtual int f(); }; "
1541 "class Y : public X { int f(); };",
1542 methodDecl(isOverride(), hasName("::X::f"))));
1543 EXPECT_TRUE(notMatches("class X { int f(); }; "
1544 "class Y : public X { int f(); };",
1545 methodDecl(isOverride())));
1546 EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ",
1547 methodDecl(isOverride())));
1548}
1549
Manuel Klimek4da21662012-07-06 05:48:52 +00001550TEST(Matcher, ConstructorCall) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001551 StatementMatcher Constructor = constructExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001552
1553 EXPECT_TRUE(
1554 matches("class X { public: X(); }; void x() { X x; }", Constructor));
1555 EXPECT_TRUE(
1556 matches("class X { public: X(); }; void x() { X x = X(); }",
1557 Constructor));
1558 EXPECT_TRUE(
1559 matches("class X { public: X(int); }; void x() { X x = 0; }",
1560 Constructor));
1561 EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
1562}
1563
1564TEST(Matcher, ConstructorArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001565 StatementMatcher Constructor = constructExpr(
1566 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001567
1568 EXPECT_TRUE(
1569 matches("class X { public: X(int); }; void x() { int y; X x(y); }",
1570 Constructor));
1571 EXPECT_TRUE(
1572 matches("class X { public: X(int); }; void x() { int y; X x = X(y); }",
1573 Constructor));
1574 EXPECT_TRUE(
1575 matches("class X { public: X(int); }; void x() { int y; X x = y; }",
1576 Constructor));
1577 EXPECT_TRUE(
1578 notMatches("class X { public: X(int); }; void x() { int z; X x(z); }",
1579 Constructor));
1580
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001581 StatementMatcher WrongIndex = constructExpr(
1582 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001583 EXPECT_TRUE(
1584 notMatches("class X { public: X(int); }; void x() { int y; X x(y); }",
1585 WrongIndex));
1586}
1587
1588TEST(Matcher, ConstructorArgumentCount) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001589 StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001590
1591 EXPECT_TRUE(
1592 matches("class X { public: X(int); }; void x() { X x(0); }",
1593 Constructor1Arg));
1594 EXPECT_TRUE(
1595 matches("class X { public: X(int); }; void x() { X x = X(0); }",
1596 Constructor1Arg));
1597 EXPECT_TRUE(
1598 matches("class X { public: X(int); }; void x() { X x = 0; }",
1599 Constructor1Arg));
1600 EXPECT_TRUE(
1601 notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }",
1602 Constructor1Arg));
1603}
1604
Manuel Klimek70b9db92012-10-23 10:40:50 +00001605TEST(Matcher,ThisExpr) {
1606 EXPECT_TRUE(
1607 matches("struct X { int a; int f () { return a; } };", thisExpr()));
1608 EXPECT_TRUE(
1609 notMatches("struct X { int f () { int a; return a; } };", thisExpr()));
1610}
1611
Manuel Klimek4da21662012-07-06 05:48:52 +00001612TEST(Matcher, BindTemporaryExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001613 StatementMatcher TempExpression = bindTemporaryExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001614
1615 std::string ClassString = "class string { public: string(); ~string(); }; ";
1616
1617 EXPECT_TRUE(
1618 matches(ClassString +
1619 "string GetStringByValue();"
1620 "void FunctionTakesString(string s);"
1621 "void run() { FunctionTakesString(GetStringByValue()); }",
1622 TempExpression));
1623
1624 EXPECT_TRUE(
1625 notMatches(ClassString +
1626 "string* GetStringPointer(); "
1627 "void FunctionTakesStringPtr(string* s);"
1628 "void run() {"
1629 " string* s = GetStringPointer();"
1630 " FunctionTakesStringPtr(GetStringPointer());"
1631 " FunctionTakesStringPtr(s);"
1632 "}",
1633 TempExpression));
1634
1635 EXPECT_TRUE(
1636 notMatches("class no_dtor {};"
1637 "no_dtor GetObjByValue();"
1638 "void ConsumeObj(no_dtor param);"
1639 "void run() { ConsumeObj(GetObjByValue()); }",
1640 TempExpression));
1641}
1642
Sam Panzere16acd32012-08-24 22:04:44 +00001643TEST(MaterializeTemporaryExpr, MatchesTemporary) {
1644 std::string ClassString =
1645 "class string { public: string(); int length(); }; ";
1646
1647 EXPECT_TRUE(
1648 matches(ClassString +
1649 "string GetStringByValue();"
1650 "void FunctionTakesString(string s);"
1651 "void run() { FunctionTakesString(GetStringByValue()); }",
1652 materializeTemporaryExpr()));
1653
1654 EXPECT_TRUE(
1655 notMatches(ClassString +
1656 "string* GetStringPointer(); "
1657 "void FunctionTakesStringPtr(string* s);"
1658 "void run() {"
1659 " string* s = GetStringPointer();"
1660 " FunctionTakesStringPtr(GetStringPointer());"
1661 " FunctionTakesStringPtr(s);"
1662 "}",
1663 materializeTemporaryExpr()));
1664
1665 EXPECT_TRUE(
1666 notMatches(ClassString +
1667 "string GetStringByValue();"
1668 "void run() { int k = GetStringByValue().length(); }",
1669 materializeTemporaryExpr()));
1670
1671 EXPECT_TRUE(
1672 notMatches(ClassString +
1673 "string GetStringByValue();"
1674 "void run() { GetStringByValue(); }",
1675 materializeTemporaryExpr()));
1676}
1677
Manuel Klimek4da21662012-07-06 05:48:52 +00001678TEST(ConstructorDeclaration, SimpleCase) {
1679 EXPECT_TRUE(matches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001680 constructorDecl(ofClass(hasName("Foo")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001681 EXPECT_TRUE(notMatches("class Foo { Foo(int i); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001682 constructorDecl(ofClass(hasName("Bar")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001683}
1684
1685TEST(ConstructorDeclaration, IsImplicit) {
1686 // This one doesn't match because the constructor is not added by the
1687 // compiler (it is not needed).
1688 EXPECT_TRUE(notMatches("class Foo { };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001689 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001690 // The compiler added the implicit default constructor.
1691 EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001692 constructorDecl(isImplicit())));
Manuel Klimek4da21662012-07-06 05:48:52 +00001693 EXPECT_TRUE(matches("class Foo { Foo(){} };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001694 constructorDecl(unless(isImplicit()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001695}
1696
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001697TEST(DestructorDeclaration, MatchesVirtualDestructor) {
1698 EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001699 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001700}
1701
1702TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001703 EXPECT_TRUE(notMatches("class Foo {};",
1704 destructorDecl(ofClass(hasName("Foo")))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001705}
1706
Manuel Klimek4da21662012-07-06 05:48:52 +00001707TEST(HasAnyConstructorInitializer, SimpleCase) {
1708 EXPECT_TRUE(notMatches(
1709 "class Foo { Foo() { } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001710 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001711 EXPECT_TRUE(matches(
1712 "class Foo {"
1713 " Foo() : foo_() { }"
1714 " int foo_;"
1715 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001716 constructorDecl(hasAnyConstructorInitializer(anything()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001717}
1718
1719TEST(HasAnyConstructorInitializer, ForField) {
1720 static const char Code[] =
1721 "class Baz { };"
1722 "class Foo {"
1723 " Foo() : foo_() { }"
1724 " Baz foo_;"
1725 " Baz bar_;"
1726 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001727 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
1728 forField(hasType(recordDecl(hasName("Baz"))))))));
1729 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001730 forField(hasName("foo_"))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001731 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
1732 forField(hasType(recordDecl(hasName("Bar"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001733}
1734
1735TEST(HasAnyConstructorInitializer, WithInitializer) {
1736 static const char Code[] =
1737 "class Foo {"
1738 " Foo() : foo_(0) { }"
1739 " int foo_;"
1740 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001741 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001742 withInitializer(integerLiteral(equals(0)))))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001743 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001744 withInitializer(integerLiteral(equals(1)))))));
1745}
1746
1747TEST(HasAnyConstructorInitializer, IsWritten) {
1748 static const char Code[] =
1749 "struct Bar { Bar(){} };"
1750 "class Foo {"
1751 " Foo() : foo_() { }"
1752 " Bar foo_;"
1753 " Bar bar_;"
1754 "};";
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001755 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001756 allOf(forField(hasName("foo_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001757 EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001758 allOf(forField(hasName("bar_")), isWritten())))));
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001759 EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer(
Manuel Klimek4da21662012-07-06 05:48:52 +00001760 allOf(forField(hasName("bar_")), unless(isWritten()))))));
1761}
1762
1763TEST(Matcher, NewExpression) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001764 StatementMatcher New = newExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001765
1766 EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
1767 EXPECT_TRUE(
1768 matches("class X { public: X(); }; void x() { new X(); }", New));
1769 EXPECT_TRUE(
1770 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1771 EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
1772}
1773
1774TEST(Matcher, NewExpressionArgument) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001775 StatementMatcher New = constructExpr(
1776 hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001777
1778 EXPECT_TRUE(
1779 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1780 New));
1781 EXPECT_TRUE(
1782 matches("class X { public: X(int); }; void x() { int y; new X(y); }",
1783 New));
1784 EXPECT_TRUE(
1785 notMatches("class X { public: X(int); }; void x() { int z; new X(z); }",
1786 New));
1787
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001788 StatementMatcher WrongIndex = constructExpr(
1789 hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00001790 EXPECT_TRUE(
1791 notMatches("class X { public: X(int); }; void x() { int y; new X(y); }",
1792 WrongIndex));
1793}
1794
1795TEST(Matcher, NewExpressionArgumentCount) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001796 StatementMatcher New = constructExpr(argumentCountIs(1));
Manuel Klimek4da21662012-07-06 05:48:52 +00001797
1798 EXPECT_TRUE(
1799 matches("class X { public: X(int); }; void x() { new X(0); }", New));
1800 EXPECT_TRUE(
1801 notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }",
1802 New));
1803}
1804
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001805TEST(Matcher, DeleteExpression) {
1806 EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001807 deleteExpr()));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00001808}
1809
Manuel Klimek4da21662012-07-06 05:48:52 +00001810TEST(Matcher, DefaultArgument) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00001811 StatementMatcher Arg = defaultArgExpr();
Manuel Klimek4da21662012-07-06 05:48:52 +00001812
1813 EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
1814 EXPECT_TRUE(
1815 matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
1816 EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
1817}
1818
1819TEST(Matcher, StringLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001820 StatementMatcher Literal = stringLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001821 EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
1822 // wide string
1823 EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
1824 // with escaped characters
1825 EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
1826 // no matching -- though the data type is the same, there is no string literal
1827 EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
1828}
1829
1830TEST(Matcher, CharacterLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001831 StatementMatcher CharLiteral = characterLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001832 EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
1833 // wide character
1834 EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
1835 // wide character, Hex encoded, NOT MATCHED!
1836 EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
1837 EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
1838}
1839
1840TEST(Matcher, IntegerLiterals) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00001841 StatementMatcher HasIntLiteral = integerLiteral();
Manuel Klimek4da21662012-07-06 05:48:52 +00001842 EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
1843 EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
1844 EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
1845 EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
1846
1847 // Non-matching cases (character literals, float and double)
1848 EXPECT_TRUE(notMatches("int i = L'a';",
1849 HasIntLiteral)); // this is actually a character
1850 // literal cast to int
1851 EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
1852 EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
1853 EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
1854}
1855
Daniel Jasperc5ae7172013-07-26 18:52:58 +00001856TEST(Matcher, FloatLiterals) {
1857 StatementMatcher HasFloatLiteral = floatLiteral();
1858 EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
1859 EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
1860 EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
1861 EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
1862 EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
1863
1864 EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
1865}
1866
Daniel Jasper31f7c082012-10-01 13:40:41 +00001867TEST(Matcher, NullPtrLiteral) {
1868 EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
1869}
1870
Daniel Jasperb54b7642012-09-20 14:12:57 +00001871TEST(Matcher, AsmStatement) {
1872 EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
1873}
1874
Manuel Klimek4da21662012-07-06 05:48:52 +00001875TEST(Matcher, Conditions) {
1876 StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
1877
1878 EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
1879 EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
1880 EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
1881 EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
1882 EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
1883}
1884
1885TEST(MatchBinaryOperator, HasOperatorName) {
1886 StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||"));
1887
1888 EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr));
1889 EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr));
1890}
1891
1892TEST(MatchBinaryOperator, HasLHSAndHasRHS) {
1893 StatementMatcher OperatorTrueFalse =
1894 binaryOperator(hasLHS(boolLiteral(equals(true))),
1895 hasRHS(boolLiteral(equals(false))));
1896
1897 EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse));
1898 EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse));
1899 EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse));
1900}
1901
1902TEST(MatchBinaryOperator, HasEitherOperand) {
1903 StatementMatcher HasOperand =
1904 binaryOperator(hasEitherOperand(boolLiteral(equals(false))));
1905
1906 EXPECT_TRUE(matches("void x() { true || false; }", HasOperand));
1907 EXPECT_TRUE(matches("void x() { false && true; }", HasOperand));
1908 EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand));
1909}
1910
1911TEST(Matcher, BinaryOperatorTypes) {
1912 // Integration test that verifies the AST provides all binary operators in
1913 // a way we expect.
1914 // FIXME: Operator ','
1915 EXPECT_TRUE(
1916 matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(","))));
1917 EXPECT_TRUE(
1918 matches("bool b; bool c = (b = true);",
1919 binaryOperator(hasOperatorName("="))));
1920 EXPECT_TRUE(
1921 matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!="))));
1922 EXPECT_TRUE(
1923 matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("=="))));
1924 EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<"))));
1925 EXPECT_TRUE(
1926 matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<="))));
1927 EXPECT_TRUE(
1928 matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<"))));
1929 EXPECT_TRUE(
1930 matches("int i = 1; int j = (i <<= 2);",
1931 binaryOperator(hasOperatorName("<<="))));
1932 EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">"))));
1933 EXPECT_TRUE(
1934 matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">="))));
1935 EXPECT_TRUE(
1936 matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>"))));
1937 EXPECT_TRUE(
1938 matches("int i = 1; int j = (i >>= 2);",
1939 binaryOperator(hasOperatorName(">>="))));
1940 EXPECT_TRUE(
1941 matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^"))));
1942 EXPECT_TRUE(
1943 matches("int i = 42; int j = (i ^= 42);",
1944 binaryOperator(hasOperatorName("^="))));
1945 EXPECT_TRUE(
1946 matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%"))));
1947 EXPECT_TRUE(
1948 matches("int i = 42; int j = (i %= 42);",
1949 binaryOperator(hasOperatorName("%="))));
1950 EXPECT_TRUE(
1951 matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&"))));
1952 EXPECT_TRUE(
1953 matches("bool b = true && false;",
1954 binaryOperator(hasOperatorName("&&"))));
1955 EXPECT_TRUE(
1956 matches("bool b = true; bool c = (b &= false);",
1957 binaryOperator(hasOperatorName("&="))));
1958 EXPECT_TRUE(
1959 matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|"))));
1960 EXPECT_TRUE(
1961 matches("bool b = true || false;",
1962 binaryOperator(hasOperatorName("||"))));
1963 EXPECT_TRUE(
1964 matches("bool b = true; bool c = (b |= false);",
1965 binaryOperator(hasOperatorName("|="))));
1966 EXPECT_TRUE(
1967 matches("int i = 42 *23;", binaryOperator(hasOperatorName("*"))));
1968 EXPECT_TRUE(
1969 matches("int i = 42; int j = (i *= 23);",
1970 binaryOperator(hasOperatorName("*="))));
1971 EXPECT_TRUE(
1972 matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/"))));
1973 EXPECT_TRUE(
1974 matches("int i = 42; int j = (i /= 23);",
1975 binaryOperator(hasOperatorName("/="))));
1976 EXPECT_TRUE(
1977 matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+"))));
1978 EXPECT_TRUE(
1979 matches("int i = 42; int j = (i += 23);",
1980 binaryOperator(hasOperatorName("+="))));
1981 EXPECT_TRUE(
1982 matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-"))));
1983 EXPECT_TRUE(
1984 matches("int i = 42; int j = (i -= 23);",
1985 binaryOperator(hasOperatorName("-="))));
1986 EXPECT_TRUE(
1987 matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };",
1988 binaryOperator(hasOperatorName("->*"))));
1989 EXPECT_TRUE(
1990 matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };",
1991 binaryOperator(hasOperatorName(".*"))));
1992
1993 // Member expressions as operators are not supported in matches.
1994 EXPECT_TRUE(
1995 notMatches("struct A { void x(A *a) { a->x(this); } };",
1996 binaryOperator(hasOperatorName("->"))));
1997
1998 // Initializer assignments are not represented as operator equals.
1999 EXPECT_TRUE(
2000 notMatches("bool b = true;", binaryOperator(hasOperatorName("="))));
2001
2002 // Array indexing is not represented as operator.
2003 EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator()));
2004
2005 // Overloaded operators do not match at all.
2006 EXPECT_TRUE(notMatches(
2007 "struct A { bool operator&&(const A &a) const { return false; } };"
2008 "void x() { A a, b; a && b; }",
2009 binaryOperator()));
2010}
2011
2012TEST(MatchUnaryOperator, HasOperatorName) {
2013 StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!"));
2014
2015 EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot));
2016 EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot));
2017}
2018
2019TEST(MatchUnaryOperator, HasUnaryOperand) {
2020 StatementMatcher OperatorOnFalse =
2021 unaryOperator(hasUnaryOperand(boolLiteral(equals(false))));
2022
2023 EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse));
2024 EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse));
2025}
2026
2027TEST(Matcher, UnaryOperatorTypes) {
2028 // Integration test that verifies the AST provides all unary operators in
2029 // a way we expect.
2030 EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!"))));
2031 EXPECT_TRUE(
2032 matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&"))));
2033 EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~"))));
2034 EXPECT_TRUE(
2035 matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*"))));
2036 EXPECT_TRUE(
2037 matches("int i; int j = +i;", unaryOperator(hasOperatorName("+"))));
2038 EXPECT_TRUE(
2039 matches("int i; int j = -i;", unaryOperator(hasOperatorName("-"))));
2040 EXPECT_TRUE(
2041 matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++"))));
2042 EXPECT_TRUE(
2043 matches("int i; int j = i++;", unaryOperator(hasOperatorName("++"))));
2044 EXPECT_TRUE(
2045 matches("int i; int j = --i;", unaryOperator(hasOperatorName("--"))));
2046 EXPECT_TRUE(
2047 matches("int i; int j = i--;", unaryOperator(hasOperatorName("--"))));
2048
2049 // We don't match conversion operators.
2050 EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator()));
2051
2052 // Function calls are not represented as operator.
2053 EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator()));
2054
2055 // Overloaded operators do not match at all.
2056 // FIXME: We probably want to add that.
2057 EXPECT_TRUE(notMatches(
2058 "struct A { bool operator!() const { return false; } };"
2059 "void x() { A a; !a; }", unaryOperator(hasOperatorName("!"))));
2060}
2061
2062TEST(Matcher, ConditionalOperator) {
2063 StatementMatcher Conditional = conditionalOperator(
2064 hasCondition(boolLiteral(equals(true))),
2065 hasTrueExpression(boolLiteral(equals(false))));
2066
2067 EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
2068 EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
2069 EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
2070
2071 StatementMatcher ConditionalFalse = conditionalOperator(
2072 hasFalseExpression(boolLiteral(equals(false))));
2073
2074 EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
2075 EXPECT_TRUE(
2076 notMatches("void x() { true ? false : true; }", ConditionalFalse));
2077}
2078
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002079TEST(ArraySubscriptMatchers, ArraySubscripts) {
2080 EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
2081 arraySubscriptExpr()));
2082 EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
2083 arraySubscriptExpr()));
2084}
2085
2086TEST(ArraySubscriptMatchers, ArrayIndex) {
2087 EXPECT_TRUE(matches(
2088 "int i[2]; void f() { i[1] = 1; }",
2089 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2090 EXPECT_TRUE(matches(
2091 "int i[2]; void f() { 1[i] = 1; }",
2092 arraySubscriptExpr(hasIndex(integerLiteral(equals(1))))));
2093 EXPECT_TRUE(notMatches(
2094 "int i[2]; void f() { i[1] = 1; }",
2095 arraySubscriptExpr(hasIndex(integerLiteral(equals(0))))));
2096}
2097
2098TEST(ArraySubscriptMatchers, MatchesArrayBase) {
2099 EXPECT_TRUE(matches(
2100 "int i[2]; void f() { i[1] = 2; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002101 arraySubscriptExpr(hasBase(implicitCastExpr(
2102 hasSourceExpression(declRefExpr()))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002103}
2104
Manuel Klimek4da21662012-07-06 05:48:52 +00002105TEST(Matcher, HasNameSupportsNamespaces) {
2106 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002107 recordDecl(hasName("a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002108 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002109 recordDecl(hasName("::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002110 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002111 recordDecl(hasName("b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002112 EXPECT_TRUE(matches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002113 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002114 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002115 recordDecl(hasName("c::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002116 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002117 recordDecl(hasName("a::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002118 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002119 recordDecl(hasName("a::b::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002120 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002121 recordDecl(hasName("::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002122 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002123 recordDecl(hasName("::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002124 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002125 recordDecl(hasName("z::a::b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002126 EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002127 recordDecl(hasName("a+b::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002128 EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002129 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002130}
2131
2132TEST(Matcher, HasNameSupportsOuterClasses) {
2133 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002134 matches("class A { class B { class C; }; };",
2135 recordDecl(hasName("A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002136 EXPECT_TRUE(
2137 matches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002138 recordDecl(hasName("::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002139 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002140 matches("class A { class B { class C; }; };",
2141 recordDecl(hasName("B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002142 EXPECT_TRUE(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002143 matches("class A { class B { class C; }; };",
2144 recordDecl(hasName("C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002145 EXPECT_TRUE(
2146 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002147 recordDecl(hasName("c::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002148 EXPECT_TRUE(
2149 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002150 recordDecl(hasName("A::c::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002151 EXPECT_TRUE(
2152 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002153 recordDecl(hasName("A::B::A"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002154 EXPECT_TRUE(
2155 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002156 recordDecl(hasName("::C"))));
2157 EXPECT_TRUE(
2158 notMatches("class A { class B { class C; }; };",
2159 recordDecl(hasName("::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002160 EXPECT_TRUE(notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002161 recordDecl(hasName("z::A::B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002162 EXPECT_TRUE(
2163 notMatches("class A { class B { class C; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002164 recordDecl(hasName("A+B::C"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002165}
2166
2167TEST(Matcher, IsDefinition) {
2168 DeclarationMatcher DefinitionOfClassA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002169 recordDecl(hasName("A"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002170 EXPECT_TRUE(matches("class A {};", DefinitionOfClassA));
2171 EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA));
2172
2173 DeclarationMatcher DefinitionOfVariableA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002174 varDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002175 EXPECT_TRUE(matches("int a;", DefinitionOfVariableA));
2176 EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA));
2177
2178 DeclarationMatcher DefinitionOfMethodA =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002179 methodDecl(hasName("a"), isDefinition());
Manuel Klimek4da21662012-07-06 05:48:52 +00002180 EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA));
2181 EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA));
2182}
2183
2184TEST(Matcher, OfClass) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002185 StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl(
Manuel Klimek4da21662012-07-06 05:48:52 +00002186 ofClass(hasName("X")))));
2187
2188 EXPECT_TRUE(
2189 matches("class X { public: X(); }; void x(int) { X x; }", Constructor));
2190 EXPECT_TRUE(
2191 matches("class X { public: X(); }; void x(int) { X x = X(); }",
2192 Constructor));
2193 EXPECT_TRUE(
2194 notMatches("class Y { public: Y(); }; void x(int) { Y y; }",
2195 Constructor));
2196}
2197
2198TEST(Matcher, VisitsTemplateInstantiations) {
2199 EXPECT_TRUE(matches(
2200 "class A { public: void x(); };"
2201 "template <typename T> class B { public: void y() { T t; t.x(); } };"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002202 "void f() { B<A> b; b.y(); }",
2203 callExpr(callee(methodDecl(hasName("x"))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002204
2205 EXPECT_TRUE(matches(
2206 "class A { public: void x(); };"
2207 "class C {"
2208 " public:"
2209 " template <typename T> class B { public: void y() { T t; t.x(); } };"
2210 "};"
2211 "void f() {"
2212 " C::B<A> b; b.y();"
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002213 "}",
2214 recordDecl(hasName("C"),
2215 hasDescendant(callExpr(callee(methodDecl(hasName("x"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002216}
2217
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002218TEST(Matcher, HandlesNullQualTypes) {
2219 // FIXME: Add a Type matcher so we can replace uses of this
2220 // variable with Type(True())
2221 const TypeMatcher AnyType = anything();
2222
2223 // We don't really care whether this matcher succeeds; we're testing that
2224 // it completes without crashing.
2225 EXPECT_TRUE(matches(
2226 "struct A { };"
2227 "template <typename T>"
2228 "void f(T t) {"
2229 " T local_t(t /* this becomes a null QualType in the AST */);"
2230 "}"
2231 "void g() {"
2232 " f(0);"
2233 "}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002234 expr(hasType(TypeMatcher(
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002235 anyOf(
2236 TypeMatcher(hasDeclaration(anything())),
2237 pointsTo(AnyType),
2238 references(AnyType)
2239 // Other QualType matchers should go here.
2240 ))))));
2241}
2242
Manuel Klimek4da21662012-07-06 05:48:52 +00002243// For testing AST_MATCHER_P().
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002244AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002245 // Make sure all special variables are used: node, match_finder,
2246 // bound_nodes_builder, and the parameter named 'AMatcher'.
2247 return AMatcher.matches(Node, Finder, Builder);
2248}
2249
2250TEST(AstMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002251 DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002252
2253 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002254 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002255
2256 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002257 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002258
2259 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002260 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002261}
2262
2263AST_POLYMORPHIC_MATCHER_P(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +00002264 polymorphicHas,
2265 AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt),
2266 internal::Matcher<Decl>, AMatcher) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002267 return Finder->matchesChildOf(
Manuel Klimeka78d0d62012-09-05 12:12:07 +00002268 Node, AMatcher, Builder,
Manuel Klimek4da21662012-07-06 05:48:52 +00002269 ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
2270 ASTMatchFinder::BK_First);
2271}
2272
2273TEST(AstPolymorphicMatcherPMacro, Works) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002274 DeclarationMatcher HasClassB =
2275 polymorphicHas(recordDecl(hasName("B")).bind("b"));
Manuel Klimek4da21662012-07-06 05:48:52 +00002276
2277 EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002278 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002279
2280 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002281 HasClassB, new VerifyIdIsBoundTo<Decl>("a")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002282
2283 EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
Daniel Jaspera7564432012-09-13 13:11:25 +00002284 HasClassB, new VerifyIdIsBoundTo<Decl>("b")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002285
2286 StatementMatcher StatementHasClassB =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002287 polymorphicHas(recordDecl(hasName("B")));
Manuel Klimek4da21662012-07-06 05:48:52 +00002288
2289 EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
2290}
2291
2292TEST(For, FindsForLoops) {
2293 EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
2294 EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
Daniel Jasper1a00fee2012-10-01 15:05:34 +00002295 EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
2296 "void f() { for (auto &a : as); }",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002297 forStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002298}
2299
Daniel Jasper6a124492012-07-12 08:50:38 +00002300TEST(For, ForLoopInternals) {
2301 EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }",
2302 forStmt(hasCondition(anything()))));
2303 EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }",
2304 forStmt(hasLoopInit(anything()))));
2305}
2306
2307TEST(For, NegativeForLoopInternals) {
2308 EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002309 forStmt(hasCondition(expr()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002310 EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }",
2311 forStmt(hasLoopInit(anything()))));
2312}
2313
Manuel Klimek4da21662012-07-06 05:48:52 +00002314TEST(For, ReportsNoFalsePositives) {
2315 EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
2316 EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
2317}
2318
2319TEST(CompoundStatement, HandlesSimpleCases) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002320 EXPECT_TRUE(notMatches("void f();", compoundStmt()));
2321 EXPECT_TRUE(matches("void f() {}", compoundStmt()));
2322 EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002323}
2324
2325TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
2326 // It's not a compound statement just because there's "{}" in the source
2327 // text. This is an AST search, not grep.
2328 EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002329 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002330 EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002331 compoundStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002332}
2333
Daniel Jasper6a124492012-07-12 08:50:38 +00002334TEST(HasBody, FindsBodyOfForWhileDoLoops) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002335 EXPECT_TRUE(matches("void f() { for(;;) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002336 forStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002337 EXPECT_TRUE(notMatches("void f() { for(;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002338 forStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002339 EXPECT_TRUE(matches("void f() { while(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002340 whileStmt(hasBody(compoundStmt()))));
Daniel Jasper6a124492012-07-12 08:50:38 +00002341 EXPECT_TRUE(matches("void f() { do {} while(true); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002342 doStmt(hasBody(compoundStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002343}
2344
2345TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) {
2346 // The simplest case: every compound statement is in a function
2347 // definition, and the function body itself must be a compound
2348 // statement.
2349 EXPECT_TRUE(matches("void f() { for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002350 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002351}
2352
2353TEST(HasAnySubstatement, IsNotRecursive) {
2354 // It's really "has any immediate substatement".
2355 EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002356 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002357}
2358
2359TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) {
2360 EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002361 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002362}
2363
2364TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) {
2365 EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002366 compoundStmt(hasAnySubstatement(forStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002367}
2368
2369TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) {
2370 EXPECT_TRUE(matches("void f() { }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002371 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002372 EXPECT_TRUE(notMatches("void f() {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002373 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002374}
2375
2376TEST(StatementCountIs, AppearsToMatchOnlyOneCount) {
2377 EXPECT_TRUE(matches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002378 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002379 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002380 compoundStmt(statementCountIs(0))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002381 EXPECT_TRUE(notMatches("void f() { 1; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002382 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002383}
2384
2385TEST(StatementCountIs, WorksWithMultipleStatements) {
2386 EXPECT_TRUE(matches("void f() { 1; 2; 3; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002387 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002388}
2389
2390TEST(StatementCountIs, WorksWithNestedCompoundStatements) {
2391 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002392 compoundStmt(statementCountIs(1))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002393 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002394 compoundStmt(statementCountIs(2))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002395 EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002396 compoundStmt(statementCountIs(3))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002397 EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002398 compoundStmt(statementCountIs(4))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002399}
2400
2401TEST(Member, WorksInSimplestCase) {
2402 EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002403 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002404}
2405
2406TEST(Member, DoesNotMatchTheBaseExpression) {
2407 // Don't pick out the wrong part of the member expression, this should
2408 // be checking the member (name) only.
2409 EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002410 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002411}
2412
2413TEST(Member, MatchesInMemberFunctionCall) {
2414 EXPECT_TRUE(matches("void f() {"
2415 " struct { void first() {}; } s;"
2416 " s.first();"
2417 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002418 memberExpr(member(hasName("first")))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002419}
2420
Daniel Jasperc711af22012-10-23 15:46:39 +00002421TEST(Member, MatchesMember) {
2422 EXPECT_TRUE(matches(
2423 "struct A { int i; }; void f() { A a; a.i = 2; }",
2424 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2425 EXPECT_TRUE(notMatches(
2426 "struct A { float f; }; void f() { A a; a.f = 2.0f; }",
2427 memberExpr(hasDeclaration(fieldDecl(hasType(isInteger()))))));
2428}
2429
Daniel Jasperf3197e92013-02-25 12:02:08 +00002430TEST(Member, UnderstandsAccess) {
2431 EXPECT_TRUE(matches(
2432 "struct A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2433 EXPECT_TRUE(notMatches(
2434 "struct A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2435 EXPECT_TRUE(notMatches(
2436 "struct A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2437
2438 EXPECT_TRUE(notMatches(
2439 "class A { int i; };", fieldDecl(isPublic(), hasName("i"))));
2440 EXPECT_TRUE(notMatches(
2441 "class A { int i; };", fieldDecl(isProtected(), hasName("i"))));
2442 EXPECT_TRUE(matches(
2443 "class A { int i; };", fieldDecl(isPrivate(), hasName("i"))));
2444
2445 EXPECT_TRUE(notMatches(
2446 "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i"))));
2447 EXPECT_TRUE(matches("class A { protected: int i; };",
2448 fieldDecl(isProtected(), hasName("i"))));
2449 EXPECT_TRUE(notMatches(
2450 "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i"))));
2451
2452 // Non-member decls have the AccessSpecifier AS_none and thus aren't matched.
2453 EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i"))));
2454 EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i"))));
2455 EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i"))));
2456}
2457
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002458TEST(Member, MatchesMemberAllocationFunction) {
Daniel Jasper31f7c082012-10-01 13:40:41 +00002459 // Fails in C++11 mode
2460 EXPECT_TRUE(matchesConditionally(
2461 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2462 "class X { void *operator new(std::size_t); };",
2463 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002464
2465 EXPECT_TRUE(matches("class X { void operator delete(void*); };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002466 methodDecl(ofClass(hasName("X")))));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002467
Daniel Jasper31f7c082012-10-01 13:40:41 +00002468 // Fails in C++11 mode
2469 EXPECT_TRUE(matchesConditionally(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002470 "namespace std { typedef typeof(sizeof(int)) size_t; }"
2471 "class X { void operator delete[](void*, std::size_t); };",
Daniel Jasper31f7c082012-10-01 13:40:41 +00002472 methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98"));
Dmitri Gribenko671a0452012-08-18 00:29:27 +00002473}
2474
Manuel Klimek4da21662012-07-06 05:48:52 +00002475TEST(HasObjectExpression, DoesNotMatchMember) {
2476 EXPECT_TRUE(notMatches(
2477 "class X {}; struct Z { X m; }; void f(Z z) { z.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002478 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002479}
2480
2481TEST(HasObjectExpression, MatchesBaseOfVariable) {
2482 EXPECT_TRUE(matches(
2483 "struct X { int m; }; void f(X x) { x.m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002484 memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X")))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002485 EXPECT_TRUE(matches(
2486 "struct X { int m; }; void f(X* x) { x->m; }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002487 memberExpr(hasObjectExpression(
2488 hasType(pointsTo(recordDecl(hasName("X"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002489}
2490
2491TEST(HasObjectExpression,
2492 MatchesObjectExpressionOfImplicitlyFormedMemberExpression) {
2493 EXPECT_TRUE(matches(
2494 "class X {}; struct S { X m; void f() { this->m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002495 memberExpr(hasObjectExpression(
2496 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002497 EXPECT_TRUE(matches(
2498 "class X {}; struct S { X m; void f() { m; } };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002499 memberExpr(hasObjectExpression(
2500 hasType(pointsTo(recordDecl(hasName("S"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002501}
2502
2503TEST(Field, DoesNotMatchNonFieldMembers) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002504 EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m"))));
2505 EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m"))));
2506 EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m"))));
2507 EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002508}
2509
2510TEST(Field, MatchesField) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002511 EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m"))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002512}
2513
2514TEST(IsConstQualified, MatchesConstInt) {
2515 EXPECT_TRUE(matches("const int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002516 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002517}
2518
2519TEST(IsConstQualified, MatchesConstPointer) {
2520 EXPECT_TRUE(matches("int i = 42; int* const p(&i);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002521 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002522}
2523
2524TEST(IsConstQualified, MatchesThroughTypedef) {
2525 EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002526 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002527 EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002528 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002529}
2530
2531TEST(IsConstQualified, DoesNotMatchInappropriately) {
2532 EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002533 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002534 EXPECT_TRUE(notMatches("int const* p;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002535 varDecl(hasType(isConstQualified()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002536}
2537
Sam Panzer089e5b32012-08-16 16:58:10 +00002538TEST(CastExpression, MatchesExplicitCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002539 EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
2540 EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
2541 EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
2542 EXPECT_TRUE(matches("char c = char(0);", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002543}
2544TEST(CastExpression, MatchesImplicitCasts) {
2545 // This test creates an implicit cast from int to char.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002546 EXPECT_TRUE(matches("char c = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002547 // This test creates an implicit cast from lvalue to rvalue.
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002548 EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002549}
2550
2551TEST(CastExpression, DoesNotMatchNonCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002552 EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
2553 EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
2554 EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
2555 EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
Sam Panzer089e5b32012-08-16 16:58:10 +00002556}
2557
Manuel Klimek4da21662012-07-06 05:48:52 +00002558TEST(ReinterpretCast, MatchesSimpleCase) {
2559 EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002560 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002561}
2562
2563TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002564 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002565 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002566 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002567 EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002568 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002569 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2570 "B b;"
2571 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002572 reinterpretCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002573}
2574
2575TEST(FunctionalCast, MatchesSimpleCase) {
2576 std::string foo_class = "class Foo { public: Foo(char*); };";
2577 EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002578 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002579}
2580
2581TEST(FunctionalCast, DoesNotMatchOtherCasts) {
2582 std::string FooClass = "class Foo { public: Foo(char*); };";
2583 EXPECT_TRUE(
2584 notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002585 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002586 EXPECT_TRUE(
2587 notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002588 functionalCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002589}
2590
2591TEST(DynamicCast, MatchesSimpleCase) {
2592 EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
2593 "B b;"
2594 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002595 dynamicCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002596}
2597
2598TEST(StaticCast, MatchesSimpleCase) {
2599 EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002600 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002601}
2602
2603TEST(StaticCast, DoesNotMatchOtherCasts) {
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002604 EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002605 EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002606 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002607 EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002608 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002609 EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
2610 "B b;"
2611 "D* p = dynamic_cast<D*>(&b);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002612 staticCastExpr()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002613}
2614
Daniel Jaspere6d2a962012-09-18 13:36:17 +00002615TEST(CStyleCast, MatchesSimpleCase) {
2616 EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
2617}
2618
2619TEST(CStyleCast, DoesNotMatchOtherCasts) {
2620 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
2621 "char q, *r = const_cast<char*>(&q);"
2622 "void* s = reinterpret_cast<char*>(&s);"
2623 "struct B { virtual ~B() {} }; struct D : B {};"
2624 "B b;"
2625 "D* t = dynamic_cast<D*>(&b);",
2626 cStyleCastExpr()));
2627}
2628
Manuel Klimek4da21662012-07-06 05:48:52 +00002629TEST(HasDestinationType, MatchesSimpleCase) {
2630 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002631 staticCastExpr(hasDestinationType(
2632 pointsTo(TypeMatcher(anything()))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002633}
2634
Sam Panzer089e5b32012-08-16 16:58:10 +00002635TEST(HasImplicitDestinationType, MatchesSimpleCase) {
2636 // This test creates an implicit const cast.
2637 EXPECT_TRUE(matches("int x; const int i = x;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002638 implicitCastExpr(
2639 hasImplicitDestinationType(isInteger()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002640 // This test creates an implicit array-to-pointer cast.
2641 EXPECT_TRUE(matches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002642 implicitCastExpr(hasImplicitDestinationType(
2643 pointsTo(TypeMatcher(anything()))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002644}
2645
2646TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
2647 // This test creates an implicit cast from int to char.
2648 EXPECT_TRUE(notMatches("char c = 0;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002649 implicitCastExpr(hasImplicitDestinationType(
2650 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002651 // This test creates an implicit array-to-pointer cast.
2652 EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002653 implicitCastExpr(hasImplicitDestinationType(
2654 unless(anything())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002655}
2656
2657TEST(ImplicitCast, MatchesSimpleCase) {
2658 // This test creates an implicit const cast.
2659 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002660 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002661 // This test creates an implicit cast from int to char.
2662 EXPECT_TRUE(matches("char c = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002663 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002664 // This test creates an implicit array-to-pointer cast.
2665 EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002666 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002667}
2668
2669TEST(ImplicitCast, DoesNotMatchIncorrectly) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002670 // This test verifies that implicitCastExpr() matches exactly when implicit casts
Sam Panzer089e5b32012-08-16 16:58:10 +00002671 // are present, and that it ignores explicit and paren casts.
2672
2673 // These two test cases have no casts.
2674 EXPECT_TRUE(notMatches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002675 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002676 EXPECT_TRUE(notMatches("int x = 0, &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002677 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002678
2679 EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002680 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002681 EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002682 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002683
2684 EXPECT_TRUE(notMatches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002685 varDecl(hasInitializer(implicitCastExpr()))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002686}
2687
2688TEST(IgnoringImpCasts, MatchesImpCasts) {
2689 // This test checks that ignoringImpCasts matches when implicit casts are
2690 // present and its inner matcher alone does not match.
2691 // Note that this test creates an implicit const cast.
2692 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002693 varDecl(hasInitializer(ignoringImpCasts(
2694 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002695 // This test creates an implict cast from int to char.
2696 EXPECT_TRUE(matches("char x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002697 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002698 integerLiteral(equals(0)))))));
2699}
2700
2701TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) {
2702 // These tests verify that ignoringImpCasts does not match if the inner
2703 // matcher does not match.
2704 // Note that the first test creates an implicit const cast.
2705 EXPECT_TRUE(notMatches("int x; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002706 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002707 unless(anything()))))));
2708 EXPECT_TRUE(notMatches("int x; int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002709 varDecl(hasInitializer(ignoringImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002710 unless(anything()))))));
2711
2712 // These tests verify that ignoringImplictCasts does not look through explicit
2713 // casts or parentheses.
2714 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002715 varDecl(hasInitializer(ignoringImpCasts(
2716 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002717 EXPECT_TRUE(notMatches("int i = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002718 varDecl(hasInitializer(ignoringImpCasts(
2719 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002720 EXPECT_TRUE(notMatches("float i = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002721 varDecl(hasInitializer(ignoringImpCasts(
2722 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002723 EXPECT_TRUE(notMatches("float i = float(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002724 varDecl(hasInitializer(ignoringImpCasts(
2725 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002726}
2727
2728TEST(IgnoringImpCasts, MatchesWithoutImpCasts) {
2729 // This test verifies that expressions that do not have implicit casts
2730 // still match the inner matcher.
2731 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002732 varDecl(hasInitializer(ignoringImpCasts(
2733 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002734}
2735
2736TEST(IgnoringParenCasts, MatchesParenCasts) {
2737 // This test checks that ignoringParenCasts matches when parentheses and/or
2738 // casts are present and its inner matcher alone does not match.
2739 EXPECT_TRUE(matches("int x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002740 varDecl(hasInitializer(ignoringParenCasts(
2741 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002742 EXPECT_TRUE(matches("int x = (((((0)))));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002743 varDecl(hasInitializer(ignoringParenCasts(
2744 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002745
2746 // This test creates an implict cast from int to char in addition to the
2747 // parentheses.
2748 EXPECT_TRUE(matches("char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002749 varDecl(hasInitializer(ignoringParenCasts(
2750 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002751
2752 EXPECT_TRUE(matches("char x = (char)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002753 varDecl(hasInitializer(ignoringParenCasts(
2754 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002755 EXPECT_TRUE(matches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002756 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002757 integerLiteral(equals(0)))))));
2758}
2759
2760TEST(IgnoringParenCasts, MatchesWithoutParenCasts) {
2761 // This test verifies that expressions that do not have any casts still match.
2762 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002763 varDecl(hasInitializer(ignoringParenCasts(
2764 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002765}
2766
2767TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) {
2768 // These tests verify that ignoringImpCasts does not match if the inner
2769 // matcher does not match.
2770 EXPECT_TRUE(notMatches("int x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002771 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002772 unless(anything()))))));
2773
2774 // This test creates an implicit cast from int to char in addition to the
2775 // parentheses.
2776 EXPECT_TRUE(notMatches("char x = ((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002777 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002778 unless(anything()))))));
2779
2780 EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002781 varDecl(hasInitializer(ignoringParenCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002782 unless(anything()))))));
2783}
2784
2785TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) {
2786 // This test checks that ignoringParenAndImpCasts matches when
2787 // parentheses and/or implicit casts are present and its inner matcher alone
2788 // does not match.
2789 // Note that this test creates an implicit const cast.
2790 EXPECT_TRUE(matches("int x = 0; const int y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002791 varDecl(hasInitializer(ignoringParenImpCasts(
2792 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002793 // This test creates an implicit cast from int to char.
2794 EXPECT_TRUE(matches("const char x = (0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002795 varDecl(hasInitializer(ignoringParenImpCasts(
2796 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002797}
2798
2799TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) {
2800 // This test verifies that expressions that do not have parentheses or
2801 // implicit casts still match.
2802 EXPECT_TRUE(matches("int x = 0; int &y = x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002803 varDecl(hasInitializer(ignoringParenImpCasts(
2804 declRefExpr(to(varDecl(hasName("x")))))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002805 EXPECT_TRUE(matches("int x = 0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002806 varDecl(hasInitializer(ignoringParenImpCasts(
2807 integerLiteral(equals(0)))))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002808}
2809
2810TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) {
2811 // These tests verify that ignoringParenImpCasts does not match if
2812 // the inner matcher does not match.
2813 // This test creates an implicit cast.
2814 EXPECT_TRUE(notMatches("char c = ((3));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002815 varDecl(hasInitializer(ignoringParenImpCasts(
Sam Panzer089e5b32012-08-16 16:58:10 +00002816 unless(anything()))))));
2817 // These tests verify that ignoringParenAndImplictCasts does not look
2818 // through explicit casts.
2819 EXPECT_TRUE(notMatches("float y = (float(0));",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002820 varDecl(hasInitializer(ignoringParenImpCasts(
2821 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002822 EXPECT_TRUE(notMatches("float y = (float)0;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002823 varDecl(hasInitializer(ignoringParenImpCasts(
2824 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002825 EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002826 varDecl(hasInitializer(ignoringParenImpCasts(
2827 integerLiteral())))));
Sam Panzer089e5b32012-08-16 16:58:10 +00002828}
2829
Manuel Klimek715c9562012-07-25 10:02:02 +00002830TEST(HasSourceExpression, MatchesImplicitCasts) {
Manuel Klimek4da21662012-07-06 05:48:52 +00002831 EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
2832 "void r() {string a_string; URL url = a_string; }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002833 implicitCastExpr(
2834 hasSourceExpression(constructExpr()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00002835}
2836
Manuel Klimek715c9562012-07-25 10:02:02 +00002837TEST(HasSourceExpression, MatchesExplicitCasts) {
2838 EXPECT_TRUE(matches("float x = static_cast<float>(42);",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002839 explicitCastExpr(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002840 hasSourceExpression(hasDescendant(
Daniel Jasper3680b4f2012-09-18 13:09:13 +00002841 expr(integerLiteral()))))));
Manuel Klimek715c9562012-07-25 10:02:02 +00002842}
2843
Manuel Klimek4da21662012-07-06 05:48:52 +00002844TEST(Statement, DoesNotMatchDeclarations) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002845 EXPECT_TRUE(notMatches("class X {};", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002846}
2847
2848TEST(Statement, MatchesCompoundStatments) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002849 EXPECT_TRUE(matches("void x() {}", stmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002850}
2851
2852TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002853 EXPECT_TRUE(notMatches("void x() {}", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002854}
2855
2856TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002857 EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
Manuel Klimek4da21662012-07-06 05:48:52 +00002858}
2859
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002860TEST(InitListExpression, MatchesInitListExpression) {
2861 EXPECT_TRUE(matches("int a[] = { 1, 2 };",
2862 initListExpr(hasType(asString("int [2]")))));
2863 EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002864 initListExpr(hasType(recordDecl(hasName("B"))))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002865}
2866
2867TEST(UsingDeclaration, MatchesUsingDeclarations) {
2868 EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
2869 usingDecl()));
2870}
2871
2872TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
2873 EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
2874 usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
2875}
2876
2877TEST(UsingDeclaration, MatchesSpecificTarget) {
2878 EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;",
2879 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002880 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002881 EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;",
2882 usingDecl(hasAnyUsingShadowDecl(
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002883 hasTargetDecl(functionDecl())))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002884}
2885
2886TEST(UsingDeclaration, ThroughUsingDeclaration) {
2887 EXPECT_TRUE(matches(
2888 "namespace a { void f(); } using a::f; void g() { f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002889 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002890 EXPECT_TRUE(notMatches(
2891 "namespace a { void f(); } using a::f; void g() { a::f(); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002892 declRefExpr(throughUsingDecl(anything()))));
Daniel Jaspere0e6b9e2012-07-10 20:20:19 +00002893}
2894
Sam Panzer425f41b2012-08-16 17:20:59 +00002895TEST(SingleDecl, IsSingleDecl) {
2896 StatementMatcher SingleDeclStmt =
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002897 declStmt(hasSingleDecl(varDecl(hasInitializer(anything()))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002898 EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt));
2899 EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt));
2900 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
2901 SingleDeclStmt));
2902}
2903
2904TEST(DeclStmt, ContainsDeclaration) {
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002905 DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything()));
Sam Panzer425f41b2012-08-16 17:20:59 +00002906
2907 EXPECT_TRUE(matches("void f() {int a = 4;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002908 declStmt(containsDeclaration(0, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002909 EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002910 declStmt(containsDeclaration(0, MatchesInit),
2911 containsDeclaration(1, MatchesInit))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002912 unsigned WrongIndex = 42;
2913 EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002914 declStmt(containsDeclaration(WrongIndex,
Sam Panzer425f41b2012-08-16 17:20:59 +00002915 MatchesInit))));
2916}
2917
2918TEST(DeclCount, DeclCountIsCorrect) {
2919 EXPECT_TRUE(matches("void f() {int i,j;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002920 declStmt(declCountIs(2))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002921 EXPECT_TRUE(notMatches("void f() {int i,j; int k;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002922 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002923 EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00002924 declStmt(declCountIs(3))));
Sam Panzer425f41b2012-08-16 17:20:59 +00002925}
2926
Manuel Klimek4da21662012-07-06 05:48:52 +00002927TEST(While, MatchesWhileLoops) {
2928 EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
2929 EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
2930 EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
2931}
2932
2933TEST(Do, MatchesDoLoops) {
2934 EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
2935 EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
2936}
2937
2938TEST(Do, DoesNotMatchWhileLoops) {
2939 EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
2940}
2941
2942TEST(SwitchCase, MatchesCase) {
2943 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
2944 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
2945 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
2946 EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
2947}
2948
Daniel Jasperb54b7642012-09-20 14:12:57 +00002949TEST(SwitchCase, MatchesSwitch) {
2950 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
2951 EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
2952 EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
2953 EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
2954}
2955
Peter Collingbourneacf02712013-05-10 11:52:02 +00002956TEST(SwitchCase, MatchesEachCase) {
2957 EXPECT_TRUE(notMatches("void x() { switch(42); }",
2958 switchStmt(forEachSwitchCase(caseStmt()))));
2959 EXPECT_TRUE(matches("void x() { switch(42) case 42:; }",
2960 switchStmt(forEachSwitchCase(caseStmt()))));
2961 EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }",
2962 switchStmt(forEachSwitchCase(caseStmt()))));
2963 EXPECT_TRUE(notMatches(
2964 "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }",
2965 ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt()))))));
2966 EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }",
2967 switchStmt(forEachSwitchCase(
2968 caseStmt(hasCaseConstant(integerLiteral()))))));
2969 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }",
2970 switchStmt(forEachSwitchCase(
2971 caseStmt(hasCaseConstant(integerLiteral()))))));
2972 EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }",
2973 switchStmt(forEachSwitchCase(
2974 caseStmt(hasCaseConstant(integerLiteral()))))));
2975 EXPECT_TRUE(matchAndVerifyResultTrue(
2976 "void x() { switch (42) { case 1: case 2: case 3: default:; } }",
2977 switchStmt(forEachSwitchCase(caseStmt().bind("x"))),
2978 new VerifyIdIsBoundTo<CaseStmt>("x", 3)));
2979}
2980
Manuel Klimek06963012013-07-19 11:50:54 +00002981TEST(ForEachConstructorInitializer, MatchesInitializers) {
2982 EXPECT_TRUE(matches(
2983 "struct X { X() : i(42), j(42) {} int i, j; };",
2984 constructorDecl(forEachConstructorInitializer(ctorInitializer()))));
2985}
2986
Daniel Jasperb54b7642012-09-20 14:12:57 +00002987TEST(ExceptionHandling, SimpleCases) {
2988 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt()));
2989 EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt()));
2990 EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr()));
2991 EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
2992 throwExpr()));
2993 EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
2994 throwExpr()));
2995}
2996
Manuel Klimek4da21662012-07-06 05:48:52 +00002997TEST(HasConditionVariableStatement, DoesNotMatchCondition) {
2998 EXPECT_TRUE(notMatches(
2999 "void x() { if(true) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003000 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003001 EXPECT_TRUE(notMatches(
3002 "void x() { int x; if((x = 42)) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003003 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003004}
3005
3006TEST(HasConditionVariableStatement, MatchesConditionVariables) {
3007 EXPECT_TRUE(matches(
3008 "void x() { if(int* a = 0) {} }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003009 ifStmt(hasConditionVariableStatement(declStmt()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003010}
3011
3012TEST(ForEach, BindsOneNode) {
3013 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003014 recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003015 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003016}
3017
3018TEST(ForEach, BindsMultipleNodes) {
3019 EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003020 recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003021 new VerifyIdIsBoundTo<FieldDecl>("f", 3)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003022}
3023
3024TEST(ForEach, BindsRecursiveCombinations) {
3025 EXPECT_TRUE(matchAndVerifyResultTrue(
3026 "class C { class D { int x; int y; }; class E { int y; int z; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003027 recordDecl(hasName("C"),
3028 forEach(recordDecl(forEach(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003029 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003030}
3031
3032TEST(ForEachDescendant, BindsOneNode) {
3033 EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003034 recordDecl(hasName("C"),
3035 forEachDescendant(fieldDecl(hasName("x")).bind("x"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003036 new VerifyIdIsBoundTo<FieldDecl>("x", 1)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003037}
3038
Daniel Jasper5f684e92012-11-16 18:39:22 +00003039TEST(ForEachDescendant, NestedForEachDescendant) {
3040 DeclarationMatcher m = recordDecl(
3041 isDefinition(), decl().bind("x"), hasName("C"));
3042 EXPECT_TRUE(matchAndVerifyResultTrue(
3043 "class A { class B { class C {}; }; };",
3044 recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))),
3045 new VerifyIdIsBoundTo<Decl>("x", "C")));
3046
Manuel Klimek054d0492013-06-19 15:42:45 +00003047 // Check that a partial match of 'm' that binds 'x' in the
3048 // first part of anyOf(m, anything()) will not overwrite the
3049 // binding created by the earlier binding in the hasDescendant.
3050 EXPECT_TRUE(matchAndVerifyResultTrue(
3051 "class A { class B { class C {}; }; };",
3052 recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))),
3053 new VerifyIdIsBoundTo<Decl>("x", "C")));
Daniel Jasper5f684e92012-11-16 18:39:22 +00003054}
3055
Manuel Klimek4da21662012-07-06 05:48:52 +00003056TEST(ForEachDescendant, BindsMultipleNodes) {
3057 EXPECT_TRUE(matchAndVerifyResultTrue(
3058 "class C { class D { int x; int y; }; "
3059 " class E { class F { int y; int z; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003060 recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003061 new VerifyIdIsBoundTo<FieldDecl>("f", 4)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003062}
3063
3064TEST(ForEachDescendant, BindsRecursiveCombinations) {
3065 EXPECT_TRUE(matchAndVerifyResultTrue(
3066 "class C { class D { "
3067 " class E { class F { class G { int y; int z; }; }; }; }; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003068 recordDecl(hasName("C"), forEachDescendant(recordDecl(
3069 forEachDescendant(fieldDecl().bind("f"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003070 new VerifyIdIsBoundTo<FieldDecl>("f", 8)));
Manuel Klimek4da21662012-07-06 05:48:52 +00003071}
3072
Manuel Klimek054d0492013-06-19 15:42:45 +00003073TEST(ForEachDescendant, BindsCombinations) {
3074 EXPECT_TRUE(matchAndVerifyResultTrue(
3075 "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while "
3076 "(true) {} }",
3077 compoundStmt(forEachDescendant(ifStmt().bind("if")),
3078 forEachDescendant(whileStmt().bind("while"))),
3079 new VerifyIdIsBoundTo<IfStmt>("if", 6)));
3080}
3081
3082TEST(Has, DoesNotDeleteBindings) {
3083 EXPECT_TRUE(matchAndVerifyResultTrue(
3084 "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())),
3085 new VerifyIdIsBoundTo<Decl>("x", 1)));
3086}
3087
3088TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) {
3089 // Those matchers cover all the cases where an inner matcher is called
3090 // and there is not a 1:1 relationship between the match of the outer
3091 // matcher and the match of the inner matcher.
3092 // The pattern to look for is:
3093 // ... return InnerMatcher.matches(...); ...
3094 // In which case no special handling is needed.
3095 //
3096 // On the other hand, if there are multiple alternative matches
3097 // (for example forEach*) or matches might be discarded (for example has*)
3098 // the implementation must make sure that the discarded matches do not
3099 // affect the bindings.
3100 // When new such matchers are added, add a test here that:
3101 // - matches a simple node, and binds it as the first thing in the matcher:
3102 // recordDecl(decl().bind("x"), hasName("X")))
3103 // - uses the matcher under test afterwards in a way that not the first
3104 // alternative is matched; for anyOf, that means the first branch
3105 // would need to return false; for hasAncestor, it means that not
3106 // the direct parent matches the inner matcher.
3107
3108 EXPECT_TRUE(matchAndVerifyResultTrue(
3109 "class X { int y; };",
3110 recordDecl(
3111 recordDecl().bind("x"), hasName("::X"),
3112 anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())),
3113 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3114 EXPECT_TRUE(matchAndVerifyResultTrue(
3115 "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"),
3116 anyOf(unless(anything()), anything())),
3117 new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1)));
3118 EXPECT_TRUE(matchAndVerifyResultTrue(
3119 "template<typename T1, typename T2> class X {}; X<float, int> x;",
3120 classTemplateSpecializationDecl(
3121 decl().bind("x"),
3122 hasAnyTemplateArgument(refersToType(asString("int")))),
3123 new VerifyIdIsBoundTo<Decl>("x", 1)));
3124 EXPECT_TRUE(matchAndVerifyResultTrue(
3125 "class X { void f(); void g(); };",
3126 recordDecl(decl().bind("x"), hasMethod(hasName("g"))),
3127 new VerifyIdIsBoundTo<Decl>("x", 1)));
3128 EXPECT_TRUE(matchAndVerifyResultTrue(
3129 "class X { X() : a(1), b(2) {} double a; int b; };",
3130 recordDecl(decl().bind("x"),
3131 has(constructorDecl(
3132 hasAnyConstructorInitializer(forField(hasName("b")))))),
3133 new VerifyIdIsBoundTo<Decl>("x", 1)));
3134 EXPECT_TRUE(matchAndVerifyResultTrue(
3135 "void x(int, int) { x(0, 42); }",
3136 callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))),
3137 new VerifyIdIsBoundTo<Expr>("x", 1)));
3138 EXPECT_TRUE(matchAndVerifyResultTrue(
3139 "void x(int, int y) {}",
3140 functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))),
3141 new VerifyIdIsBoundTo<Decl>("x", 1)));
3142 EXPECT_TRUE(matchAndVerifyResultTrue(
3143 "void x() { return; if (true) {} }",
3144 functionDecl(decl().bind("x"),
3145 has(compoundStmt(hasAnySubstatement(ifStmt())))),
3146 new VerifyIdIsBoundTo<Decl>("x", 1)));
3147 EXPECT_TRUE(matchAndVerifyResultTrue(
3148 "namespace X { void b(int); void b(); }"
3149 "using X::b;",
3150 usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl(
3151 functionDecl(parameterCountIs(1))))),
3152 new VerifyIdIsBoundTo<Decl>("x", 1)));
3153 EXPECT_TRUE(matchAndVerifyResultTrue(
3154 "class A{}; class B{}; class C : B, A {};",
3155 recordDecl(decl().bind("x"), isDerivedFrom("::A")),
3156 new VerifyIdIsBoundTo<Decl>("x", 1)));
3157 EXPECT_TRUE(matchAndVerifyResultTrue(
3158 "class A{}; typedef A B; typedef A C; typedef A D;"
3159 "class E : A {};",
3160 recordDecl(decl().bind("x"), isDerivedFrom("C")),
3161 new VerifyIdIsBoundTo<Decl>("x", 1)));
3162 EXPECT_TRUE(matchAndVerifyResultTrue(
3163 "class A { class B { void f() {} }; };",
3164 functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3165 new VerifyIdIsBoundTo<Decl>("x", 1)));
3166 EXPECT_TRUE(matchAndVerifyResultTrue(
3167 "template <typename T> struct A { struct B {"
3168 " void f() { if(true) {} }"
3169 "}; };"
3170 "void t() { A<int>::B b; b.f(); }",
3171 ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))),
3172 new VerifyIdIsBoundTo<Stmt>("x", 2)));
3173 EXPECT_TRUE(matchAndVerifyResultTrue(
3174 "class A {};",
3175 recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))),
3176 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek06963012013-07-19 11:50:54 +00003177 EXPECT_TRUE(matchAndVerifyResultTrue(
3178 "class A { A() : s(), i(42) {} const char *s; int i; };",
3179 constructorDecl(hasName("::A::A"), decl().bind("x"),
3180 forEachConstructorInitializer(forField(hasName("i")))),
3181 new VerifyIdIsBoundTo<Decl>("x", 1)));
Manuel Klimek054d0492013-06-19 15:42:45 +00003182}
3183
Daniel Jasper11c98772012-11-11 22:14:55 +00003184TEST(ForEachDescendant, BindsCorrectNodes) {
3185 EXPECT_TRUE(matchAndVerifyResultTrue(
3186 "class C { void f(); int i; };",
3187 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3188 new VerifyIdIsBoundTo<FieldDecl>("decl", 1)));
3189 EXPECT_TRUE(matchAndVerifyResultTrue(
3190 "class C { void f() {} int i; };",
3191 recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))),
3192 new VerifyIdIsBoundTo<FunctionDecl>("decl", 1)));
3193}
3194
Manuel Klimek152ea0e2013-02-04 10:59:20 +00003195TEST(FindAll, BindsNodeOnMatch) {
3196 EXPECT_TRUE(matchAndVerifyResultTrue(
3197 "class A {};",
3198 recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))),
3199 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1)));
3200}
3201
3202TEST(FindAll, BindsDescendantNodeOnMatch) {
3203 EXPECT_TRUE(matchAndVerifyResultTrue(
3204 "class A { int a; int b; };",
3205 recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))),
3206 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3207}
3208
3209TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) {
3210 EXPECT_TRUE(matchAndVerifyResultTrue(
3211 "class A { int a; int b; };",
3212 recordDecl(hasName("::A"),
3213 findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"),
3214 fieldDecl().bind("v"))))),
3215 new VerifyIdIsBoundTo<Decl>("v", 3)));
3216
3217 EXPECT_TRUE(matchAndVerifyResultTrue(
3218 "class A { class B {}; class C {}; };",
3219 recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))),
3220 new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3)));
3221}
3222
Manuel Klimek73876732013-02-04 09:42:38 +00003223TEST(EachOf, TriggersForEachMatch) {
3224 EXPECT_TRUE(matchAndVerifyResultTrue(
3225 "class A { int a; int b; };",
3226 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3227 has(fieldDecl(hasName("b")).bind("v")))),
3228 new VerifyIdIsBoundTo<FieldDecl>("v", 2)));
3229}
3230
3231TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) {
3232 EXPECT_TRUE(matchAndVerifyResultTrue(
3233 "class A { int a; int c; };",
3234 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3235 has(fieldDecl(hasName("b")).bind("v")))),
3236 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3237 EXPECT_TRUE(matchAndVerifyResultTrue(
3238 "class A { int c; int b; };",
3239 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3240 has(fieldDecl(hasName("b")).bind("v")))),
3241 new VerifyIdIsBoundTo<FieldDecl>("v", 1)));
3242 EXPECT_TRUE(notMatches(
3243 "class A { int c; int d; };",
3244 recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
3245 has(fieldDecl(hasName("b")).bind("v"))))));
3246}
Manuel Klimek4da21662012-07-06 05:48:52 +00003247
3248TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) {
3249 // Make sure that we can both match the class by name (::X) and by the type
3250 // the template was instantiated with (via a field).
3251
3252 EXPECT_TRUE(matches(
3253 "template <typename T> class X {}; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003254 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003255
3256 EXPECT_TRUE(matches(
3257 "template <typename T> class X { T t; }; class A {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003258 recordDecl(isTemplateInstantiation(), hasDescendant(
3259 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003260}
3261
3262TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) {
3263 EXPECT_TRUE(matches(
3264 "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003265 functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))),
Manuel Klimek4da21662012-07-06 05:48:52 +00003266 isTemplateInstantiation())));
3267}
3268
3269TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) {
3270 EXPECT_TRUE(matches(
3271 "template <typename T> class X { T t; }; class A {};"
3272 "template class X<A>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003273 recordDecl(isTemplateInstantiation(), hasDescendant(
3274 fieldDecl(hasType(recordDecl(hasName("A"))))))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003275}
3276
3277TEST(IsTemplateInstantiation,
3278 MatchesInstantiationOfPartiallySpecializedClassTemplate) {
3279 EXPECT_TRUE(matches(
3280 "template <typename T> class X {};"
3281 "template <typename T> class X<T*> {}; class A {}; X<A*> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003282 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003283}
3284
3285TEST(IsTemplateInstantiation,
3286 MatchesInstantiationOfClassTemplateNestedInNonTemplate) {
3287 EXPECT_TRUE(matches(
3288 "class A {};"
3289 "class X {"
3290 " template <typename U> class Y { U u; };"
3291 " Y<A> y;"
3292 "};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003293 recordDecl(hasName("::X::Y"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003294}
3295
3296TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) {
3297 // FIXME: Figure out whether this makes sense. It doesn't affect the
3298 // normal use case as long as the uppermost instantiation always is marked
3299 // as template instantiation, but it might be confusing as a predicate.
3300 EXPECT_TRUE(matches(
3301 "class A {};"
3302 "template <typename T> class X {"
3303 " template <typename U> class Y { U u; };"
3304 " Y<T> y;"
3305 "}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003306 recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation()))));
Manuel Klimek4da21662012-07-06 05:48:52 +00003307}
3308
3309TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) {
3310 EXPECT_TRUE(notMatches(
3311 "template <typename T> class X {}; class A {};"
3312 "template <> class X<A> {}; X<A> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003313 recordDecl(hasName("::X"), isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003314}
3315
3316TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) {
3317 EXPECT_TRUE(notMatches(
3318 "class A {}; class Y { A a; };",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003319 recordDecl(isTemplateInstantiation())));
Manuel Klimek4da21662012-07-06 05:48:52 +00003320}
3321
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003322TEST(IsExplicitTemplateSpecialization,
3323 DoesNotMatchPrimaryTemplate) {
3324 EXPECT_TRUE(notMatches(
3325 "template <typename T> class X {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003326 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003327 EXPECT_TRUE(notMatches(
3328 "template <typename T> void f(T t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003329 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003330}
3331
3332TEST(IsExplicitTemplateSpecialization,
3333 DoesNotMatchExplicitTemplateInstantiations) {
3334 EXPECT_TRUE(notMatches(
3335 "template <typename T> class X {};"
3336 "template class X<int>; extern template class X<long>;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003337 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003338 EXPECT_TRUE(notMatches(
3339 "template <typename T> void f(T t) {}"
3340 "template void f(int t); extern template void f(long t);",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003341 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003342}
3343
3344TEST(IsExplicitTemplateSpecialization,
3345 DoesNotMatchImplicitTemplateInstantiations) {
3346 EXPECT_TRUE(notMatches(
3347 "template <typename T> class X {}; X<int> x;",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003348 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003349 EXPECT_TRUE(notMatches(
3350 "template <typename T> void f(T t); void g() { f(10); }",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003351 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003352}
3353
3354TEST(IsExplicitTemplateSpecialization,
3355 MatchesExplicitTemplateSpecializations) {
3356 EXPECT_TRUE(matches(
3357 "template <typename T> class X {};"
3358 "template<> class X<int> {};",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003359 recordDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003360 EXPECT_TRUE(matches(
3361 "template <typename T> void f(T t) {}"
3362 "template<> void f(int t) {}",
Daniel Jasper2dc75ed2012-08-24 05:12:34 +00003363 functionDecl(isExplicitTemplateSpecialization())));
Dmitri Gribenko8456ae62012-08-17 18:42:47 +00003364}
3365
Manuel Klimek579b1202012-09-07 09:26:10 +00003366TEST(HasAncenstor, MatchesDeclarationAncestors) {
3367 EXPECT_TRUE(matches(
3368 "class A { class B { class C {}; }; };",
3369 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A"))))));
3370}
3371
3372TEST(HasAncenstor, FailsIfNoAncestorMatches) {
3373 EXPECT_TRUE(notMatches(
3374 "class A { class B { class C {}; }; };",
3375 recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X"))))));
3376}
3377
3378TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) {
3379 EXPECT_TRUE(matches(
3380 "class A { class B { void f() { C c; } class C {}; }; };",
3381 varDecl(hasName("c"), hasType(recordDecl(hasName("C"),
3382 hasAncestor(recordDecl(hasName("A"))))))));
3383}
3384
3385TEST(HasAncenstor, MatchesStatementAncestors) {
3386 EXPECT_TRUE(matches(
3387 "void f() { if (true) { while (false) { 42; } } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003388 integerLiteral(equals(42), hasAncestor(ifStmt()))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003389}
3390
3391TEST(HasAncestor, DrillsThroughDifferentHierarchies) {
3392 EXPECT_TRUE(matches(
3393 "void f() { if (true) { int x = 42; } }",
Daniel Jasper3680b4f2012-09-18 13:09:13 +00003394 integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f"))))));
Manuel Klimek579b1202012-09-07 09:26:10 +00003395}
3396
3397TEST(HasAncestor, BindsRecursiveCombinations) {
3398 EXPECT_TRUE(matchAndVerifyResultTrue(
3399 "class C { class D { class E { class F { int y; }; }; }; };",
3400 fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))),
Daniel Jaspera7564432012-09-13 13:11:25 +00003401 new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1)));
Manuel Klimek579b1202012-09-07 09:26:10 +00003402}
3403
3404TEST(HasAncestor, BindsCombinationsWithHasDescendant) {
3405 EXPECT_TRUE(matchAndVerifyResultTrue(
3406 "class C { class D { class E { class F { int y; }; }; }; };",
3407 fieldDecl(hasAncestor(
3408 decl(
3409 hasDescendant(recordDecl(isDefinition(),
3410 hasAncestor(recordDecl())))
3411 ).bind("d")
3412 )),
Daniel Jaspera7564432012-09-13 13:11:25 +00003413 new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E")));
Manuel Klimek579b1202012-09-07 09:26:10 +00003414}
3415
Manuel Klimek374516c2013-03-14 16:33:21 +00003416TEST(HasAncestor, MatchesClosestAncestor) {
3417 EXPECT_TRUE(matchAndVerifyResultTrue(
3418 "template <typename T> struct C {"
3419 " void f(int) {"
3420 " struct I { void g(T) { int x; } } i; i.g(42);"
3421 " }"
3422 "};"
3423 "template struct C<int>;",
3424 varDecl(hasName("x"),
3425 hasAncestor(functionDecl(hasParameter(
3426 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"),
3427 new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2)));
3428}
3429
Manuel Klimek579b1202012-09-07 09:26:10 +00003430TEST(HasAncestor, MatchesInTemplateInstantiations) {
3431 EXPECT_TRUE(matches(
3432 "template <typename T> struct A { struct B { struct C { T t; }; }; }; "
3433 "A<int>::B::C a;",
3434 fieldDecl(hasType(asString("int")),
3435 hasAncestor(recordDecl(hasName("A"))))));
3436}
3437
3438TEST(HasAncestor, MatchesInImplicitCode) {
3439 EXPECT_TRUE(matches(
3440 "struct X {}; struct A { A() {} X x; };",
3441 constructorDecl(
3442 hasAnyConstructorInitializer(withInitializer(expr(
3443 hasAncestor(recordDecl(hasName("A")))))))));
3444}
3445
Daniel Jasperc99a3ad2012-10-22 16:26:51 +00003446TEST(HasParent, MatchesOnlyParent) {
3447 EXPECT_TRUE(matches(
3448 "void f() { if (true) { int x = 42; } }",
3449 compoundStmt(hasParent(ifStmt()))));
3450 EXPECT_TRUE(notMatches(
3451 "void f() { for (;;) { int x = 42; } }",
3452 compoundStmt(hasParent(ifStmt()))));
3453 EXPECT_TRUE(notMatches(
3454 "void f() { if (true) for (;;) { int x = 42; } }",
3455 compoundStmt(hasParent(ifStmt()))));
3456}
3457
Manuel Klimek30ace372012-12-06 14:42:48 +00003458TEST(HasAncestor, MatchesAllAncestors) {
3459 EXPECT_TRUE(matches(
3460 "template <typename T> struct C { static void f() { 42; } };"
3461 "void t() { C<int>::f(); }",
3462 integerLiteral(
3463 equals(42),
3464 allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
3465 hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
3466}
3467
3468TEST(HasParent, MatchesAllParents) {
3469 EXPECT_TRUE(matches(
3470 "template <typename T> struct C { static void f() { 42; } };"
3471 "void t() { C<int>::f(); }",
3472 integerLiteral(
3473 equals(42),
3474 hasParent(compoundStmt(hasParent(functionDecl(
3475 hasParent(recordDecl(isTemplateInstantiation())))))))));
3476 EXPECT_TRUE(matches(
3477 "template <typename T> struct C { static void f() { 42; } };"
3478 "void t() { C<int>::f(); }",
3479 integerLiteral(
3480 equals(42),
3481 hasParent(compoundStmt(hasParent(functionDecl(
3482 hasParent(recordDecl(unless(isTemplateInstantiation()))))))))));
3483 EXPECT_TRUE(matches(
3484 "template <typename T> struct C { static void f() { 42; } };"
3485 "void t() { C<int>::f(); }",
3486 integerLiteral(equals(42),
3487 hasParent(compoundStmt(allOf(
3488 hasParent(functionDecl(
3489 hasParent(recordDecl(isTemplateInstantiation())))),
3490 hasParent(functionDecl(hasParent(recordDecl(
3491 unless(isTemplateInstantiation())))))))))));
Manuel Klimek374516c2013-03-14 16:33:21 +00003492 EXPECT_TRUE(
3493 notMatches("template <typename T> struct C { static void f() {} };"
3494 "void t() { C<int>::f(); }",
3495 compoundStmt(hasParent(recordDecl()))));
Manuel Klimek30ace372012-12-06 14:42:48 +00003496}
3497
Daniel Jasperce620072012-10-17 08:52:59 +00003498TEST(TypeMatching, MatchesTypes) {
3499 EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
3500}
3501
3502TEST(TypeMatching, MatchesArrayTypes) {
3503 EXPECT_TRUE(matches("int a[] = {2,3};", arrayType()));
3504 EXPECT_TRUE(matches("int a[42];", arrayType()));
3505 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType()));
3506
3507 EXPECT_TRUE(notMatches("struct A {}; A a[7];",
3508 arrayType(hasElementType(builtinType()))));
3509
3510 EXPECT_TRUE(matches(
3511 "int const a[] = { 2, 3 };",
3512 qualType(arrayType(hasElementType(builtinType())))));
3513 EXPECT_TRUE(matches(
3514 "int const a[] = { 2, 3 };",
3515 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3516 EXPECT_TRUE(matches(
3517 "typedef const int T; T x[] = { 1, 2 };",
3518 qualType(isConstQualified(), arrayType())));
3519
3520 EXPECT_TRUE(notMatches(
3521 "int a[] = { 2, 3 };",
3522 qualType(isConstQualified(), arrayType(hasElementType(builtinType())))));
3523 EXPECT_TRUE(notMatches(
3524 "int a[] = { 2, 3 };",
3525 qualType(arrayType(hasElementType(isConstQualified(), builtinType())))));
3526 EXPECT_TRUE(notMatches(
3527 "int const a[] = { 2, 3 };",
3528 qualType(arrayType(hasElementType(builtinType())),
3529 unless(isConstQualified()))));
3530
3531 EXPECT_TRUE(matches("int a[2];",
3532 constantArrayType(hasElementType(builtinType()))));
3533 EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger())));
3534}
3535
3536TEST(TypeMatching, MatchesComplexTypes) {
3537 EXPECT_TRUE(matches("_Complex float f;", complexType()));
3538 EXPECT_TRUE(matches(
3539 "_Complex float f;",
3540 complexType(hasElementType(builtinType()))));
3541 EXPECT_TRUE(notMatches(
3542 "_Complex float f;",
3543 complexType(hasElementType(isInteger()))));
3544}
3545
3546TEST(TypeMatching, MatchesConstantArrayTypes) {
3547 EXPECT_TRUE(matches("int a[2];", constantArrayType()));
3548 EXPECT_TRUE(notMatches(
3549 "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
3550 constantArrayType(hasElementType(builtinType()))));
3551
3552 EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
3553 EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
3554 EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
3555}
3556
3557TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
3558 EXPECT_TRUE(matches(
3559 "template <typename T, int Size> class array { T data[Size]; };",
3560 dependentSizedArrayType()));
3561 EXPECT_TRUE(notMatches(
3562 "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
3563 dependentSizedArrayType()));
3564}
3565
3566TEST(TypeMatching, MatchesIncompleteArrayType) {
3567 EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
3568 EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
3569
3570 EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
3571 incompleteArrayType()));
3572}
3573
3574TEST(TypeMatching, MatchesVariableArrayType) {
3575 EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
3576 EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
3577
3578 EXPECT_TRUE(matches(
3579 "void f(int b) { int a[b]; }",
3580 variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
3581 varDecl(hasName("b")))))))));
3582}
3583
3584TEST(TypeMatching, MatchesAtomicTypes) {
3585 EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
3586
3587 EXPECT_TRUE(matches("_Atomic(int) i;",
3588 atomicType(hasValueType(isInteger()))));
3589 EXPECT_TRUE(notMatches("_Atomic(float) f;",
3590 atomicType(hasValueType(isInteger()))));
3591}
3592
3593TEST(TypeMatching, MatchesAutoTypes) {
3594 EXPECT_TRUE(matches("auto i = 2;", autoType()));
3595 EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
3596 autoType()));
3597
Richard Smith9b131752013-04-30 21:23:01 +00003598 // FIXME: Matching against the type-as-written can't work here, because the
3599 // type as written was not deduced.
3600 //EXPECT_TRUE(matches("auto a = 1;",
3601 // autoType(hasDeducedType(isInteger()))));
3602 //EXPECT_TRUE(notMatches("auto b = 2.0;",
3603 // autoType(hasDeducedType(isInteger()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003604}
3605
Daniel Jaspera267cf62012-10-29 10:14:44 +00003606TEST(TypeMatching, MatchesFunctionTypes) {
3607 EXPECT_TRUE(matches("int (*f)(int);", functionType()));
3608 EXPECT_TRUE(matches("void f(int i) {}", functionType()));
3609}
3610
Edwin Vane88be2fd2013-04-01 18:33:34 +00003611TEST(TypeMatching, MatchesParenType) {
3612 EXPECT_TRUE(
3613 matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
3614 EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
3615
3616 EXPECT_TRUE(matches(
3617 "int (*ptr_to_func)(int);",
3618 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3619 EXPECT_TRUE(notMatches(
3620 "int (*ptr_to_array)[4];",
3621 varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
3622}
3623
Daniel Jasperce620072012-10-17 08:52:59 +00003624TEST(TypeMatching, PointerTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003625 // FIXME: Reactive when these tests can be more specific (not matching
3626 // implicit code on certain platforms), likely when we have hasDescendant for
3627 // Types/TypeLocs.
3628 //EXPECT_TRUE(matchAndVerifyResultTrue(
3629 // "int* a;",
3630 // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
3631 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
3632 //EXPECT_TRUE(matchAndVerifyResultTrue(
3633 // "int* a;",
3634 // pointerTypeLoc().bind("loc"),
3635 // new VerifyIdIsBoundTo<TypeLoc>("loc", 1)));
Daniel Jasperce620072012-10-17 08:52:59 +00003636 EXPECT_TRUE(matches(
3637 "int** a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003638 loc(pointerType(pointee(qualType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003639 EXPECT_TRUE(matches(
3640 "int** a;",
3641 loc(pointerType(pointee(pointerType())))));
3642 EXPECT_TRUE(matches(
3643 "int* b; int* * const a = &b;",
3644 loc(qualType(isConstQualified(), pointerType()))));
3645
3646 std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
Daniel Jasper1802daf2012-10-17 13:35:36 +00003647 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3648 hasType(blockPointerType()))));
3649 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3650 hasType(memberPointerType()))));
3651 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3652 hasType(pointerType()))));
3653 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3654 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003655 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3656 hasType(lValueReferenceType()))));
3657 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3658 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003659
Daniel Jasper1802daf2012-10-17 13:35:36 +00003660 Fragment = "int *ptr;";
3661 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3662 hasType(blockPointerType()))));
3663 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3664 hasType(memberPointerType()))));
3665 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
3666 hasType(pointerType()))));
3667 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
3668 hasType(referenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003669
Daniel Jasper1802daf2012-10-17 13:35:36 +00003670 Fragment = "int a; int &ref = a;";
3671 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3672 hasType(blockPointerType()))));
3673 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3674 hasType(memberPointerType()))));
3675 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3676 hasType(pointerType()))));
3677 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3678 hasType(referenceType()))));
Edwin Vanef4b48042013-03-07 15:44:40 +00003679 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3680 hasType(lValueReferenceType()))));
3681 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3682 hasType(rValueReferenceType()))));
3683
3684 Fragment = "int &&ref = 2;";
3685 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3686 hasType(blockPointerType()))));
3687 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3688 hasType(memberPointerType()))));
3689 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3690 hasType(pointerType()))));
3691 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3692 hasType(referenceType()))));
3693 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
3694 hasType(lValueReferenceType()))));
3695 EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
3696 hasType(rValueReferenceType()))));
3697}
3698
3699TEST(TypeMatching, AutoRefTypes) {
3700 std::string Fragment = "auto a = 1;"
3701 "auto b = a;"
3702 "auto &c = a;"
3703 "auto &&d = c;"
3704 "auto &&e = 2;";
3705 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
3706 hasType(referenceType()))));
3707 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
3708 hasType(referenceType()))));
3709 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3710 hasType(referenceType()))));
3711 EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
3712 hasType(lValueReferenceType()))));
3713 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
3714 hasType(rValueReferenceType()))));
3715 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3716 hasType(referenceType()))));
3717 EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
3718 hasType(lValueReferenceType()))));
3719 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
3720 hasType(rValueReferenceType()))));
3721 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3722 hasType(referenceType()))));
3723 EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
3724 hasType(lValueReferenceType()))));
3725 EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
3726 hasType(rValueReferenceType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003727}
3728
3729TEST(TypeMatching, PointeeTypes) {
3730 EXPECT_TRUE(matches("int b; int &a = b;",
3731 referenceType(pointee(builtinType()))));
3732 EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType()))));
3733
3734 EXPECT_TRUE(matches("int *a;",
David Blaikie5be093c2013-02-18 19:04:16 +00003735 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003736
3737 EXPECT_TRUE(matches(
3738 "int const *A;",
3739 pointerType(pointee(isConstQualified(), builtinType()))));
3740 EXPECT_TRUE(notMatches(
3741 "int *A;",
3742 pointerType(pointee(isConstQualified(), builtinType()))));
3743}
3744
3745TEST(TypeMatching, MatchesPointersToConstTypes) {
3746 EXPECT_TRUE(matches("int b; int * const a = &b;",
3747 loc(pointerType())));
3748 EXPECT_TRUE(matches("int b; int * const a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003749 loc(pointerType())));
Daniel Jasperce620072012-10-17 08:52:59 +00003750 EXPECT_TRUE(matches(
3751 "int b; const int * a = &b;",
David Blaikie5be093c2013-02-18 19:04:16 +00003752 loc(pointerType(pointee(builtinType())))));
Daniel Jasperce620072012-10-17 08:52:59 +00003753 EXPECT_TRUE(matches(
3754 "int b; const int * a = &b;",
3755 pointerType(pointee(builtinType()))));
3756}
3757
3758TEST(TypeMatching, MatchesTypedefTypes) {
Daniel Jasper1802daf2012-10-17 13:35:36 +00003759 EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
3760 hasType(typedefType()))));
Daniel Jasperce620072012-10-17 08:52:59 +00003761}
3762
Edwin Vane3abf7782013-02-25 14:49:29 +00003763TEST(TypeMatching, MatchesTemplateSpecializationType) {
Edwin Vane742d9e72013-02-25 20:43:32 +00003764 EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
Edwin Vane3abf7782013-02-25 14:49:29 +00003765 templateSpecializationType()));
3766}
3767
Edwin Vane742d9e72013-02-25 20:43:32 +00003768TEST(TypeMatching, MatchesRecordType) {
3769 EXPECT_TRUE(matches("class C{}; C c;", recordType()));
Manuel Klimek0cc798f2013-02-27 11:56:58 +00003770 EXPECT_TRUE(matches("struct S{}; S s;",
3771 recordType(hasDeclaration(recordDecl(hasName("S"))))));
3772 EXPECT_TRUE(notMatches("int i;",
3773 recordType(hasDeclaration(recordDecl(hasName("S"))))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003774}
3775
3776TEST(TypeMatching, MatchesElaboratedType) {
3777 EXPECT_TRUE(matches(
3778 "namespace N {"
3779 " namespace M {"
3780 " class D {};"
3781 " }"
3782 "}"
3783 "N::M::D d;", elaboratedType()));
3784 EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
3785 EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
3786}
3787
3788TEST(ElaboratedTypeNarrowing, hasQualifier) {
3789 EXPECT_TRUE(matches(
3790 "namespace N {"
3791 " namespace M {"
3792 " class D {};"
3793 " }"
3794 "}"
3795 "N::M::D d;",
3796 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
3797 EXPECT_TRUE(notMatches(
3798 "namespace M {"
3799 " class D {};"
3800 "}"
3801 "M::D d;",
3802 elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N")))))));
Edwin Vaneaec89ac2013-03-04 17:51:00 +00003803 EXPECT_TRUE(notMatches(
3804 "struct D {"
3805 "} d;",
3806 elaboratedType(hasQualifier(nestedNameSpecifier()))));
Edwin Vane742d9e72013-02-25 20:43:32 +00003807}
3808
3809TEST(ElaboratedTypeNarrowing, namesType) {
3810 EXPECT_TRUE(matches(
3811 "namespace N {"
3812 " namespace M {"
3813 " class D {};"
3814 " }"
3815 "}"
3816 "N::M::D d;",
3817 elaboratedType(elaboratedType(namesType(recordType(
3818 hasDeclaration(namedDecl(hasName("D")))))))));
3819 EXPECT_TRUE(notMatches(
3820 "namespace M {"
3821 " class D {};"
3822 "}"
3823 "M::D d;",
3824 elaboratedType(elaboratedType(namesType(typedefType())))));
3825}
3826
Daniel Jaspera7564432012-09-13 13:11:25 +00003827TEST(NNS, MatchesNestedNameSpecifiers) {
3828 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
3829 nestedNameSpecifier()));
3830 EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
3831 nestedNameSpecifier()));
3832 EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
3833 nestedNameSpecifier()));
3834
3835 EXPECT_TRUE(matches(
3836 "struct A { static void f() {} }; void g() { A::f(); }",
3837 nestedNameSpecifier()));
3838 EXPECT_TRUE(notMatches(
3839 "struct A { static void f() {} }; void g(A* a) { a->f(); }",
3840 nestedNameSpecifier()));
3841}
3842
Daniel Jasperb54b7642012-09-20 14:12:57 +00003843TEST(NullStatement, SimpleCases) {
3844 EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
3845 EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
3846}
3847
Daniel Jaspera7564432012-09-13 13:11:25 +00003848TEST(NNS, MatchesTypes) {
3849 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3850 specifiesType(hasDeclaration(recordDecl(hasName("A")))));
3851 EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
3852 EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
3853 Matcher));
3854 EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
3855}
3856
3857TEST(NNS, MatchesNamespaceDecls) {
3858 NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
3859 specifiesNamespace(hasName("ns")));
3860 EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
3861 EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
3862 EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
3863}
3864
3865TEST(NNS, BindsNestedNameSpecifiers) {
3866 EXPECT_TRUE(matchAndVerifyResultTrue(
3867 "namespace ns { struct E { struct B {}; }; } ns::E::B b;",
3868 nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"),
3869 new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::")));
3870}
3871
3872TEST(NNS, BindsNestedNameSpecifierLocs) {
3873 EXPECT_TRUE(matchAndVerifyResultTrue(
3874 "namespace ns { struct B {}; } ns::B b;",
3875 loc(nestedNameSpecifier()).bind("loc"),
3876 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1)));
3877}
3878
3879TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
3880 EXPECT_TRUE(matches(
3881 "struct A { struct B { struct C {}; }; }; A::B::C c;",
3882 nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
3883 EXPECT_TRUE(matches(
3884 "struct A { struct B { struct C {}; }; }; A::B::C c;",
Daniel Jasperce620072012-10-17 08:52:59 +00003885 nestedNameSpecifierLoc(hasPrefix(
3886 specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
Daniel Jaspera7564432012-09-13 13:11:25 +00003887}
3888
Daniel Jasperd1ce3c12012-10-30 15:42:00 +00003889TEST(NNS, DescendantsOfNestedNameSpecifiers) {
3890 std::string Fragment =
3891 "namespace a { struct A { struct B { struct C {}; }; }; };"
3892 "void f() { a::A::B::C c; }";
3893 EXPECT_TRUE(matches(
3894 Fragment,
3895 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3896 hasDescendant(nestedNameSpecifier(
3897 specifiesNamespace(hasName("a")))))));
3898 EXPECT_TRUE(notMatches(
3899 Fragment,
3900 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3901 has(nestedNameSpecifier(
3902 specifiesNamespace(hasName("a")))))));
3903 EXPECT_TRUE(matches(
3904 Fragment,
3905 nestedNameSpecifier(specifiesType(asString("struct a::A")),
3906 has(nestedNameSpecifier(
3907 specifiesNamespace(hasName("a")))))));
3908
3909 // Not really useful because a NestedNameSpecifier can af at most one child,
3910 // but to complete the interface.
3911 EXPECT_TRUE(matchAndVerifyResultTrue(
3912 Fragment,
3913 nestedNameSpecifier(specifiesType(asString("struct a::A::B")),
3914 forEach(nestedNameSpecifier().bind("x"))),
3915 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1)));
3916}
3917
3918TEST(NNS, NestedNameSpecifiersAsDescendants) {
3919 std::string Fragment =
3920 "namespace a { struct A { struct B { struct C {}; }; }; };"
3921 "void f() { a::A::B::C c; }";
3922 EXPECT_TRUE(matches(
3923 Fragment,
3924 decl(hasDescendant(nestedNameSpecifier(specifiesType(
3925 asString("struct a::A")))))));
3926 EXPECT_TRUE(matchAndVerifyResultTrue(
3927 Fragment,
3928 functionDecl(hasName("f"),
3929 forEachDescendant(nestedNameSpecifier().bind("x"))),
3930 // Nested names: a, a::A and a::A::B.
3931 new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3)));
3932}
3933
3934TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) {
3935 std::string Fragment =
3936 "namespace a { struct A { struct B { struct C {}; }; }; };"
3937 "void f() { a::A::B::C c; }";
3938 EXPECT_TRUE(matches(
3939 Fragment,
3940 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3941 hasDescendant(loc(nestedNameSpecifier(
3942 specifiesNamespace(hasName("a"))))))));
3943 EXPECT_TRUE(notMatches(
3944 Fragment,
3945 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3946 has(loc(nestedNameSpecifier(
3947 specifiesNamespace(hasName("a"))))))));
3948 EXPECT_TRUE(matches(
3949 Fragment,
3950 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))),
3951 has(loc(nestedNameSpecifier(
3952 specifiesNamespace(hasName("a"))))))));
3953
3954 EXPECT_TRUE(matchAndVerifyResultTrue(
3955 Fragment,
3956 nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))),
3957 forEach(nestedNameSpecifierLoc().bind("x"))),
3958 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1)));
3959}
3960
3961TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) {
3962 std::string Fragment =
3963 "namespace a { struct A { struct B { struct C {}; }; }; };"
3964 "void f() { a::A::B::C c; }";
3965 EXPECT_TRUE(matches(
3966 Fragment,
3967 decl(hasDescendant(loc(nestedNameSpecifier(specifiesType(
3968 asString("struct a::A"))))))));
3969 EXPECT_TRUE(matchAndVerifyResultTrue(
3970 Fragment,
3971 functionDecl(hasName("f"),
3972 forEachDescendant(nestedNameSpecifierLoc().bind("x"))),
3973 // Nested names: a, a::A and a::A::B.
3974 new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3)));
3975}
3976
Manuel Klimek60969f52013-02-01 13:41:35 +00003977template <typename T> class VerifyMatchOnNode : public BoundNodesCallback {
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003978public:
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003979 VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher,
3980 StringRef InnerId)
3981 : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) {
Daniel Jasper452abbc2012-10-29 10:48:25 +00003982 }
3983
Manuel Klimek60969f52013-02-01 13:41:35 +00003984 virtual bool run(const BoundNodes *Nodes) { return false; }
3985
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003986 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
3987 const T *Node = Nodes->getNodeAs<T>(Id);
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003988 return selectFirst<const T>(InnerId,
3989 match(InnerMatcher, *Node, *Context)) != NULL;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003990 }
3991private:
3992 std::string Id;
3993 internal::Matcher<T> InnerMatcher;
Manuel Klimekcf87bff2013-02-06 10:33:21 +00003994 std::string InnerId;
Manuel Klimek3e2aa992012-10-24 14:47:44 +00003995};
3996
3997TEST(MatchFinder, CanMatchDeclarationsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00003998 EXPECT_TRUE(matchAndVerifyResultTrue(
3999 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4000 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004001 "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))),
4002 "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004003 EXPECT_TRUE(matchAndVerifyResultFalse(
4004 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4005 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004006 "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))),
4007 "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004008}
4009
4010TEST(MatchFinder, CanMatchStatementsRecursively) {
Manuel Klimek60969f52013-02-01 13:41:35 +00004011 EXPECT_TRUE(matchAndVerifyResultTrue(
4012 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004013 new VerifyMatchOnNode<clang::Stmt>(
4014 "if", stmt(hasDescendant(forStmt().bind("for"))), "for")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004015 EXPECT_TRUE(matchAndVerifyResultFalse(
4016 "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"),
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004017 new VerifyMatchOnNode<clang::Stmt>(
4018 "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004019}
4020
4021TEST(MatchFinder, CanMatchSingleNodesRecursively) {
4022 EXPECT_TRUE(matchAndVerifyResultTrue(
4023 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4024 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004025 "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y")));
Manuel Klimek60969f52013-02-01 13:41:35 +00004026 EXPECT_TRUE(matchAndVerifyResultFalse(
4027 "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"),
4028 new VerifyMatchOnNode<clang::Decl>(
Manuel Klimekcf87bff2013-02-06 10:33:21 +00004029 "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z")));
Manuel Klimek3e2aa992012-10-24 14:47:44 +00004030}
4031
Manuel Klimekfa37c5c2013-02-07 12:42:10 +00004032template <typename T>
4033class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
4034public:
4035 virtual bool run(const BoundNodes *Nodes) { return false; }
4036
4037 virtual bool run(const BoundNodes *Nodes, ASTContext *Context) {
4038 const T *Node = Nodes->getNodeAs<T>("");
4039 return verify(*Nodes, *Context, Node);
4040 }
4041
4042 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
4043 return selectFirst<const T>(
4044 "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))),
4045 *Node, Context)) != NULL;
4046 }
4047 bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
4048 return selectFirst<const T>(
4049 "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))),
4050 *Node, Context)) != NULL;
4051 }
4052};
4053
4054TEST(IsEqualTo, MatchesNodesByIdentity) {
4055 EXPECT_TRUE(matchAndVerifyResultTrue(
4056 "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
4057 new VerifyAncestorHasChildIsEqual<Decl>()));
4058 EXPECT_TRUE(
4059 matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""),
4060 new VerifyAncestorHasChildIsEqual<Stmt>()));
4061}
4062
Manuel Klimeke5793282012-11-02 01:31:03 +00004063class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
4064public:
4065 VerifyStartOfTranslationUnit() : Called(false) {}
4066 virtual void run(const MatchFinder::MatchResult &Result) {
4067 EXPECT_TRUE(Called);
4068 }
4069 virtual void onStartOfTranslationUnit() {
4070 Called = true;
4071 }
4072 bool Called;
4073};
4074
4075TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
4076 MatchFinder Finder;
4077 VerifyStartOfTranslationUnit VerifyCallback;
4078 Finder.addMatcher(decl(), &VerifyCallback);
4079 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4080 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4081 EXPECT_TRUE(VerifyCallback.Called);
4082}
4083
Peter Collingbourne8f9e5902013-05-28 19:21:51 +00004084class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
4085public:
4086 VerifyEndOfTranslationUnit() : Called(false) {}
4087 virtual void run(const MatchFinder::MatchResult &Result) {
4088 EXPECT_FALSE(Called);
4089 }
4090 virtual void onEndOfTranslationUnit() {
4091 Called = true;
4092 }
4093 bool Called;
4094};
4095
4096TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
4097 MatchFinder Finder;
4098 VerifyEndOfTranslationUnit VerifyCallback;
4099 Finder.addMatcher(decl(), &VerifyCallback);
4100 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
4101 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
4102 EXPECT_TRUE(VerifyCallback.Called);
4103}
4104
Manuel Klimekcf52ca62013-06-20 14:06:32 +00004105TEST(EqualsBoundNodeMatcher, QualType) {
4106 EXPECT_TRUE(matches(
4107 "int i = 1;", varDecl(hasType(qualType().bind("type")),
4108 hasInitializer(ignoringParenImpCasts(
4109 hasType(qualType(equalsBoundNode("type"))))))));
4110 EXPECT_TRUE(notMatches("int i = 1.f;",
4111 varDecl(hasType(qualType().bind("type")),
4112 hasInitializer(ignoringParenImpCasts(hasType(
4113 qualType(equalsBoundNode("type"))))))));
4114}
4115
4116TEST(EqualsBoundNodeMatcher, NonMatchingTypes) {
4117 EXPECT_TRUE(notMatches(
4118 "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"),
4119 hasInitializer(ignoringParenImpCasts(
4120 hasType(qualType(equalsBoundNode("type"))))))));
4121}
4122
4123TEST(EqualsBoundNodeMatcher, Stmt) {
4124 EXPECT_TRUE(
4125 matches("void f() { if(true) {} }",
4126 stmt(allOf(ifStmt().bind("if"),
4127 hasParent(stmt(has(stmt(equalsBoundNode("if")))))))));
4128
4129 EXPECT_TRUE(notMatches(
4130 "void f() { if(true) { if (true) {} } }",
4131 stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if")))))));
4132}
4133
4134TEST(EqualsBoundNodeMatcher, Decl) {
4135 EXPECT_TRUE(matches(
4136 "class X { class Y {}; };",
4137 decl(allOf(recordDecl(hasName("::X::Y")).bind("record"),
4138 hasParent(decl(has(decl(equalsBoundNode("record")))))))));
4139
4140 EXPECT_TRUE(notMatches("class X { class Y {}; };",
4141 decl(allOf(recordDecl(hasName("::X")).bind("record"),
4142 has(decl(equalsBoundNode("record")))))));
4143}
4144
4145TEST(EqualsBoundNodeMatcher, Type) {
4146 EXPECT_TRUE(matches(
4147 "class X { int a; int b; };",
4148 recordDecl(
4149 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4150 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4151
4152 EXPECT_TRUE(notMatches(
4153 "class X { int a; double b; };",
4154 recordDecl(
4155 has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
4156 has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))));
4157}
4158
4159TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) {
4160
4161 EXPECT_TRUE(matchAndVerifyResultTrue(
4162 "int f() {"
4163 " if (1) {"
4164 " int i = 9;"
4165 " }"
4166 " int j = 10;"
4167 " {"
4168 " float k = 9.0;"
4169 " }"
4170 " return 0;"
4171 "}",
4172 // Look for variable declarations within functions whose type is the same
4173 // as the function return type.
4174 functionDecl(returns(qualType().bind("type")),
4175 forEachDescendant(varDecl(hasType(
4176 qualType(equalsBoundNode("type")))).bind("decl"))),
4177 // Only i and j should match, not k.
4178 new VerifyIdIsBoundTo<VarDecl>("decl", 2)));
4179}
4180
4181TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) {
4182 EXPECT_TRUE(matchAndVerifyResultTrue(
4183 "void f() {"
4184 " int x;"
4185 " double d;"
4186 " x = d + x - d + x;"
4187 "}",
4188 functionDecl(
4189 hasName("f"), forEachDescendant(varDecl().bind("d")),
4190 forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))),
4191 new VerifyIdIsBoundTo<VarDecl>("d", 5)));
4192}
4193
Manuel Klimek4da21662012-07-06 05:48:52 +00004194} // end namespace ast_matchers
4195} // end namespace clang