blob: 874a4f35a0bc85b70516079e1af9fccdc128417c [file] [log] [blame]
Manuel Klimekf7f295f2013-05-14 09:13:00 +00001//===- unittest/ASTMatchers/Dynamic/RegistryTest.cpp - Registry 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 <vector>
11
12#include "../ASTMatchersTest.h"
13#include "clang/ASTMatchers/Dynamic/Registry.h"
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace ast_matchers {
18namespace dynamic {
19namespace {
20
21using ast_matchers::internal::Matcher;
22
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000023class RegistryTest : public ::testing::Test {
24public:
25 std::vector<ParserValue> Args() { return std::vector<ParserValue>(); }
26 std::vector<ParserValue> Args(const VariantValue &Arg1) {
27 std::vector<ParserValue> Out(1);
28 Out[0].Value = Arg1;
29 return Out;
30 }
31 std::vector<ParserValue> Args(const VariantValue &Arg1,
32 const VariantValue &Arg2) {
33 std::vector<ParserValue> Out(2);
34 Out[0].Value = Arg1;
35 Out[1].Value = Arg2;
36 return Out;
37 }
Manuel Klimekf7f295f2013-05-14 09:13:00 +000038
Samuel Benzaquen9d028072013-08-13 14:54:51 +000039 VariantMatcher constructMatcher(StringRef MatcherName,
40 Diagnostics *Error = NULL) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000041 Diagnostics DummyError;
42 if (!Error) Error = &DummyError;
Samuel Benzaquen9d028072013-08-13 14:54:51 +000043 const VariantMatcher Out =
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000044 Registry::constructMatcher(MatcherName, SourceRange(), Args(), Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +000045 EXPECT_EQ("", DummyError.toStringFull());
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000046 return Out;
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000047 }
Manuel Klimekf7f295f2013-05-14 09:13:00 +000048
Samuel Benzaquen9d028072013-08-13 14:54:51 +000049 VariantMatcher constructMatcher(StringRef MatcherName,
50 const VariantValue &Arg1,
51 Diagnostics *Error = NULL) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000052 Diagnostics DummyError;
53 if (!Error) Error = &DummyError;
Samuel Benzaquen9d028072013-08-13 14:54:51 +000054 const VariantMatcher Out = Registry::constructMatcher(
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000055 MatcherName, SourceRange(), Args(Arg1), Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +000056 EXPECT_EQ("", DummyError.toStringFull());
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000057 return Out;
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000058 }
Manuel Klimekf7f295f2013-05-14 09:13:00 +000059
Samuel Benzaquen9d028072013-08-13 14:54:51 +000060 VariantMatcher constructMatcher(StringRef MatcherName,
61 const VariantValue &Arg1,
62 const VariantValue &Arg2,
63 Diagnostics *Error = NULL) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000064 Diagnostics DummyError;
65 if (!Error) Error = &DummyError;
Samuel Benzaquen9d028072013-08-13 14:54:51 +000066 const VariantMatcher Out = Registry::constructMatcher(
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000067 MatcherName, SourceRange(), Args(Arg1, Arg2), Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +000068 EXPECT_EQ("", DummyError.toStringFull());
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000069 return Out;
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000070 }
71};
72
73TEST_F(RegistryTest, CanConstructNoArgs) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000074 Matcher<Stmt> IsArrowValue = constructMatcher(
75 "memberExpr", constructMatcher("isArrow")).getTypedMatcher<Stmt>();
76 Matcher<Stmt> BoolValue =
77 constructMatcher("boolLiteral").getTypedMatcher<Stmt>();
Manuel Klimekf7f295f2013-05-14 09:13:00 +000078
79 const std::string ClassSnippet = "struct Foo { int x; };\n"
80 "Foo *foo = new Foo;\n"
81 "int i = foo->x;\n";
82 const std::string BoolSnippet = "bool Foo = true;\n";
83
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000084 EXPECT_TRUE(matches(ClassSnippet, IsArrowValue));
85 EXPECT_TRUE(matches(BoolSnippet, BoolValue));
86 EXPECT_FALSE(matches(ClassSnippet, BoolValue));
87 EXPECT_FALSE(matches(BoolSnippet, IsArrowValue));
Manuel Klimekf7f295f2013-05-14 09:13:00 +000088}
89
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000090TEST_F(RegistryTest, ConstructWithSimpleArgs) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000091 Matcher<Decl> Value = constructMatcher(
92 "namedDecl", constructMatcher("hasName", std::string("X")))
93 .getTypedMatcher<Decl>();
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000094 EXPECT_TRUE(matches("class X {};", Value));
95 EXPECT_FALSE(matches("int x;", Value));
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000096
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +000097 Value = functionDecl(constructMatcher("parameterCountIs", 2)
98 .getTypedMatcher<FunctionDecl>());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000099 EXPECT_TRUE(matches("void foo(int,int);", Value));
100 EXPECT_FALSE(matches("void foo(int);", Value));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000101}
102
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000103TEST_F(RegistryTest, ConstructWithMatcherArgs) {
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000104 Matcher<Decl> HasInitializerSimple = constructMatcher(
105 "varDecl", constructMatcher("hasInitializer", constructMatcher("stmt")))
106 .getTypedMatcher<Decl>();
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000107 Matcher<Decl> HasInitializerComplex = constructMatcher(
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000108 "varDecl",
109 constructMatcher("hasInitializer", constructMatcher("callExpr")))
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000110 .getTypedMatcher<Decl>();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000111
112 std::string code = "int i;";
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000113 EXPECT_FALSE(matches(code, HasInitializerSimple));
114 EXPECT_FALSE(matches(code, HasInitializerComplex));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000115
116 code = "int i = 1;";
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000117 EXPECT_TRUE(matches(code, HasInitializerSimple));
118 EXPECT_FALSE(matches(code, HasInitializerComplex));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000119
120 code = "int y(); int i = y();";
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000121 EXPECT_TRUE(matches(code, HasInitializerSimple));
122 EXPECT_TRUE(matches(code, HasInitializerComplex));
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000123
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000124 Matcher<Decl> HasParameter =
125 functionDecl(constructMatcher(
126 "hasParameter", 1, constructMatcher("hasName", std::string("x")))
127 .getTypedMatcher<FunctionDecl>());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000128 EXPECT_TRUE(matches("void f(int a, int x);", HasParameter));
129 EXPECT_FALSE(matches("void f(int x, int a);", HasParameter));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000130}
131
Samuel Benzaquen0e1896a2013-07-22 16:13:57 +0000132TEST_F(RegistryTest, OverloadedMatchers) {
133 Matcher<Stmt> CallExpr0 = constructMatcher(
134 "callExpr",
135 constructMatcher("callee", constructMatcher("memberExpr",
136 constructMatcher("isArrow"))))
137 .getTypedMatcher<Stmt>();
138
139 Matcher<Stmt> CallExpr1 = constructMatcher(
140 "callExpr",
141 constructMatcher(
142 "callee",
143 constructMatcher("methodDecl",
144 constructMatcher("hasName", std::string("x")))))
145 .getTypedMatcher<Stmt>();
146
147 std::string Code = "class Y { public: void x(); }; void z() { Y y; y.x(); }";
148 EXPECT_FALSE(matches(Code, CallExpr0));
149 EXPECT_TRUE(matches(Code, CallExpr1));
150
151 Code = "class Z { public: void z() { this->z(); } };";
152 EXPECT_TRUE(matches(Code, CallExpr0));
153 EXPECT_FALSE(matches(Code, CallExpr1));
154}
155
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000156TEST_F(RegistryTest, PolymorphicMatchers) {
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000157 const VariantMatcher IsDefinition = constructMatcher("isDefinition");
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000158 Matcher<Decl> Var =
159 constructMatcher("varDecl", IsDefinition).getTypedMatcher<Decl>();
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000160 Matcher<Decl> Class =
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000161 constructMatcher("recordDecl", IsDefinition).getTypedMatcher<Decl>();
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000162 Matcher<Decl> Func =
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000163 constructMatcher("functionDecl", IsDefinition).getTypedMatcher<Decl>();
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000164 EXPECT_TRUE(matches("int a;", Var));
165 EXPECT_FALSE(matches("extern int a;", Var));
166 EXPECT_TRUE(matches("class A {};", Class));
167 EXPECT_FALSE(matches("class A;", Class));
168 EXPECT_TRUE(matches("void f(){};", Func));
169 EXPECT_FALSE(matches("void f();", Func));
170
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000171 Matcher<Decl> Anything = constructMatcher("anything").getTypedMatcher<Decl>();
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000172 Matcher<Decl> RecordDecl =
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000173 constructMatcher("recordDecl", VariantMatcher::SingleMatcher(Anything))
174 .getTypedMatcher<Decl>();
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000175
176 EXPECT_TRUE(matches("int a;", Anything));
177 EXPECT_TRUE(matches("class A {};", Anything));
178 EXPECT_TRUE(matches("void f(){};", Anything));
NAKAMURA Takumie0548192013-06-24 13:19:26 +0000179 // FIXME: A couple of tests have been suppressed.
180 // I know it'd be bad with _MSC_VER here, though.
181#if !defined(_MSC_VER)
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000182 EXPECT_FALSE(matches("int a;", RecordDecl));
NAKAMURA Takumie0548192013-06-24 13:19:26 +0000183#endif
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000184 EXPECT_TRUE(matches("class A {};", RecordDecl));
NAKAMURA Takumie0548192013-06-24 13:19:26 +0000185#if !defined(_MSC_VER)
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000186 EXPECT_FALSE(matches("void f(){};", RecordDecl));
NAKAMURA Takumie0548192013-06-24 13:19:26 +0000187#endif
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000188}
189
Samuel Benzaquen671840a2013-07-17 15:11:30 +0000190TEST_F(RegistryTest, TemplateArgument) {
191 Matcher<Decl> HasTemplateArgument = constructMatcher(
192 "classTemplateSpecializationDecl",
193 constructMatcher(
194 "hasAnyTemplateArgument",
195 constructMatcher("refersToType",
196 constructMatcher("asString", std::string("int")))))
197 .getTypedMatcher<Decl>();
198 EXPECT_TRUE(matches("template<typename T> class A {}; A<int> a;",
199 HasTemplateArgument));
200 EXPECT_FALSE(matches("template<typename T> class A {}; A<char> a;",
201 HasTemplateArgument));
202}
203
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000204TEST_F(RegistryTest, TypeTraversal) {
205 Matcher<Type> M = constructMatcher(
206 "pointerType",
207 constructMatcher("pointee", constructMatcher("isConstQualified"),
208 constructMatcher("isInteger"))).getTypedMatcher<Type>();
209 EXPECT_FALSE(matches("int *a;", M));
210 EXPECT_TRUE(matches("int const *b;", M));
211
212 M = constructMatcher(
213 "arrayType",
214 constructMatcher("hasElementType", constructMatcher("builtinType")))
215 .getTypedMatcher<Type>();
216 EXPECT_FALSE(matches("struct A{}; A a[7];;", M));
217 EXPECT_TRUE(matches("int b[7];", M));
218}
219
Samuel Benzaquen86e4d742013-07-17 14:28:00 +0000220TEST_F(RegistryTest, CXXCtorInitializer) {
221 Matcher<Decl> CtorDecl = constructMatcher(
222 "constructorDecl",
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000223 constructMatcher(
224 "hasAnyConstructorInitializer",
225 constructMatcher("forField",
226 constructMatcher("hasName", std::string("foo")))))
Samuel Benzaquen86e4d742013-07-17 14:28:00 +0000227 .getTypedMatcher<Decl>();
228 EXPECT_TRUE(matches("struct Foo { Foo() : foo(1) {} int foo; };", CtorDecl));
229 EXPECT_FALSE(matches("struct Foo { Foo() {} int foo; };", CtorDecl));
230 EXPECT_FALSE(matches("struct Foo { Foo() : bar(1) {} int bar; };", CtorDecl));
231}
232
Samuel Benzaquen6de440e2013-07-24 14:48:01 +0000233TEST_F(RegistryTest, Adaptative) {
234 Matcher<Decl> D = constructMatcher(
235 "recordDecl",
236 constructMatcher(
237 "has",
238 constructMatcher("recordDecl",
239 constructMatcher("hasName", std::string("X")))))
240 .getTypedMatcher<Decl>();
241 EXPECT_TRUE(matches("class X {};", D));
242 EXPECT_TRUE(matches("class Y { class X {}; };", D));
243 EXPECT_FALSE(matches("class Y { class Z {}; };", D));
244
245 Matcher<Stmt> S = constructMatcher(
246 "forStmt",
247 constructMatcher(
248 "hasDescendant",
249 constructMatcher("varDecl",
250 constructMatcher("hasName", std::string("X")))))
251 .getTypedMatcher<Stmt>();
252 EXPECT_TRUE(matches("void foo() { for(int X;;); }", S));
253 EXPECT_TRUE(matches("void foo() { for(;;) { int X; } }", S));
254 EXPECT_FALSE(matches("void foo() { for(;;); }", S));
255 EXPECT_FALSE(matches("void foo() { if (int X = 0){} }", S));
256
257 S = constructMatcher(
258 "compoundStmt", constructMatcher("hasParent", constructMatcher("ifStmt")))
259 .getTypedMatcher<Stmt>();
260 EXPECT_TRUE(matches("void foo() { if (true) { int x = 42; } }", S));
261 EXPECT_FALSE(matches("void foo() { if (true) return; }", S));
262}
263
Samuel Benzaquena7350902013-08-28 18:42:04 +0000264TEST_F(RegistryTest, VariadicOp) {
265 Matcher<Decl> D = constructMatcher(
266 "anyOf", constructMatcher("recordDecl"),
267 constructMatcher("namedDecl",
268 constructMatcher("hasName", std::string("foo"))))
269 .getTypedMatcher<Decl>();
270
271 EXPECT_TRUE(matches("void foo(){}", D));
272 EXPECT_TRUE(matches("struct Foo{};", D));
273 EXPECT_FALSE(matches("int i = 0;", D));
274
275 D = constructMatcher(
276 "allOf", constructMatcher("recordDecl"),
277 constructMatcher(
278 "namedDecl",
279 constructMatcher("anyOf",
280 constructMatcher("hasName", std::string("Foo")),
281 constructMatcher("hasName", std::string("Bar")))))
282 .getTypedMatcher<Decl>();
283
284 EXPECT_FALSE(matches("void foo(){}", D));
285 EXPECT_TRUE(matches("struct Foo{};", D));
286 EXPECT_FALSE(matches("int i = 0;", D));
287 EXPECT_TRUE(matches("class Bar{};", D));
288 EXPECT_FALSE(matches("class OtherBar{};", D));
289}
290
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000291TEST_F(RegistryTest, Errors) {
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000292 // Incorrect argument count.
293 OwningPtr<Diagnostics> Error(new Diagnostics());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000294 EXPECT_TRUE(constructMatcher("hasInitializer", Error.get()).isNull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000295 EXPECT_EQ("Incorrect argument count. (Expected = 1) != (Actual = 0)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000296 Error->toString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000297 Error.reset(new Diagnostics());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000298 EXPECT_TRUE(constructMatcher("isArrow", std::string(), Error.get()).isNull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000299 EXPECT_EQ("Incorrect argument count. (Expected = 0) != (Actual = 1)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000300 Error->toString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000301
302 // Bad argument type
303 Error.reset(new Diagnostics());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000304 EXPECT_TRUE(constructMatcher("ofClass", std::string(), Error.get()).isNull());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000305 EXPECT_EQ("Incorrect type for arg 1. (Expected = Matcher<CXXRecordDecl>) != "
306 "(Actual = String)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000307 Error->toString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000308 Error.reset(new Diagnostics());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000309 EXPECT_TRUE(constructMatcher("recordDecl", constructMatcher("recordDecl"),
310 constructMatcher("parameterCountIs", 3),
311 Error.get()).isNull());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000312 EXPECT_EQ("Incorrect type for arg 2. (Expected = Matcher<CXXRecordDecl>) != "
313 "(Actual = Matcher<FunctionDecl>)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000314 Error->toString());
Samuel Benzaquena7350902013-08-28 18:42:04 +0000315
316 // Bad argument type with variadic.
317 Error.reset(new Diagnostics());
318 EXPECT_TRUE(constructMatcher("anyOf", std::string(), Error.get()).isNull());
319 EXPECT_EQ(
320 "Incorrect type for arg 1. (Expected = Matcher<>) != (Actual = String)",
321 Error->toString());
322 Error.reset(new Diagnostics());
323 EXPECT_TRUE(constructMatcher(
324 "recordDecl",
325 constructMatcher("allOf",
326 constructMatcher("isDerivedFrom", std::string("FOO")),
327 constructMatcher("isArrow")),
328 Error.get()).isNull());
329 EXPECT_EQ("Incorrect type for arg 1. "
330 "(Expected = Matcher<CXXRecordDecl>) != "
331 "(Actual = Matcher<CXXRecordDecl>&Matcher<MemberExpr>)",
332 Error->toString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000333}
334
335} // end anonymous namespace
336} // end namespace dynamic
337} // end namespace ast_matchers
338} // end namespace clang