Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 16 | namespace clang { |
| 17 | namespace ast_matchers { |
| 18 | namespace dynamic { |
| 19 | namespace { |
| 20 | |
| 21 | using ast_matchers::internal::Matcher; |
| 22 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 23 | class RegistryTest : public ::testing::Test { |
| 24 | public: |
| 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 Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 38 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 39 | VariantMatcher constructMatcher(StringRef MatcherName, |
| 40 | Diagnostics *Error = NULL) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 41 | Diagnostics DummyError; |
| 42 | if (!Error) Error = &DummyError; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 43 | const VariantMatcher Out = |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 44 | Registry::constructMatcher(MatcherName, SourceRange(), Args(), Error); |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame] | 45 | EXPECT_EQ("", DummyError.toStringFull()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 46 | return Out; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 47 | } |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 48 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 49 | VariantMatcher constructMatcher(StringRef MatcherName, |
| 50 | const VariantValue &Arg1, |
| 51 | Diagnostics *Error = NULL) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 52 | Diagnostics DummyError; |
| 53 | if (!Error) Error = &DummyError; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 54 | const VariantMatcher Out = Registry::constructMatcher( |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 55 | MatcherName, SourceRange(), Args(Arg1), Error); |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame] | 56 | EXPECT_EQ("", DummyError.toStringFull()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 57 | return Out; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 58 | } |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 59 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 60 | VariantMatcher constructMatcher(StringRef MatcherName, |
| 61 | const VariantValue &Arg1, |
| 62 | const VariantValue &Arg2, |
| 63 | Diagnostics *Error = NULL) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 64 | Diagnostics DummyError; |
| 65 | if (!Error) Error = &DummyError; |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 66 | const VariantMatcher Out = Registry::constructMatcher( |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 67 | MatcherName, SourceRange(), Args(Arg1, Arg2), Error); |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame] | 68 | EXPECT_EQ("", DummyError.toStringFull()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 69 | return Out; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 70 | } |
| 71 | }; |
| 72 | |
| 73 | TEST_F(RegistryTest, CanConstructNoArgs) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 74 | Matcher<Stmt> IsArrowValue = constructMatcher( |
| 75 | "memberExpr", constructMatcher("isArrow")).getTypedMatcher<Stmt>(); |
| 76 | Matcher<Stmt> BoolValue = |
| 77 | constructMatcher("boolLiteral").getTypedMatcher<Stmt>(); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 78 | |
| 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 Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 84 | EXPECT_TRUE(matches(ClassSnippet, IsArrowValue)); |
| 85 | EXPECT_TRUE(matches(BoolSnippet, BoolValue)); |
| 86 | EXPECT_FALSE(matches(ClassSnippet, BoolValue)); |
| 87 | EXPECT_FALSE(matches(BoolSnippet, IsArrowValue)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 90 | TEST_F(RegistryTest, ConstructWithSimpleArgs) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 91 | Matcher<Decl> Value = constructMatcher( |
| 92 | "namedDecl", constructMatcher("hasName", std::string("X"))) |
| 93 | .getTypedMatcher<Decl>(); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 94 | EXPECT_TRUE(matches("class X {};", Value)); |
| 95 | EXPECT_FALSE(matches("int x;", Value)); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 96 | |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 97 | Value = functionDecl(constructMatcher("parameterCountIs", 2) |
| 98 | .getTypedMatcher<FunctionDecl>()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 99 | EXPECT_TRUE(matches("void foo(int,int);", Value)); |
| 100 | EXPECT_FALSE(matches("void foo(int);", Value)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 103 | TEST_F(RegistryTest, ConstructWithMatcherArgs) { |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 104 | Matcher<Decl> HasInitializerSimple = constructMatcher( |
| 105 | "varDecl", constructMatcher("hasInitializer", constructMatcher("stmt"))) |
| 106 | .getTypedMatcher<Decl>(); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 107 | Matcher<Decl> HasInitializerComplex = constructMatcher( |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 108 | "varDecl", |
| 109 | constructMatcher("hasInitializer", constructMatcher("callExpr"))) |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 110 | .getTypedMatcher<Decl>(); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 111 | |
| 112 | std::string code = "int i;"; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 113 | EXPECT_FALSE(matches(code, HasInitializerSimple)); |
| 114 | EXPECT_FALSE(matches(code, HasInitializerComplex)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 115 | |
| 116 | code = "int i = 1;"; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 117 | EXPECT_TRUE(matches(code, HasInitializerSimple)); |
| 118 | EXPECT_FALSE(matches(code, HasInitializerComplex)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 119 | |
| 120 | code = "int y(); int i = y();"; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 121 | EXPECT_TRUE(matches(code, HasInitializerSimple)); |
| 122 | EXPECT_TRUE(matches(code, HasInitializerComplex)); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 123 | |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 124 | Matcher<Decl> HasParameter = |
| 125 | functionDecl(constructMatcher( |
| 126 | "hasParameter", 1, constructMatcher("hasName", std::string("x"))) |
| 127 | .getTypedMatcher<FunctionDecl>()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 128 | EXPECT_TRUE(matches("void f(int a, int x);", HasParameter)); |
| 129 | EXPECT_FALSE(matches("void f(int x, int a);", HasParameter)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Samuel Benzaquen | 0e1896a | 2013-07-22 16:13:57 +0000 | [diff] [blame] | 132 | TEST_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 Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 156 | TEST_F(RegistryTest, PolymorphicMatchers) { |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 157 | const VariantMatcher IsDefinition = constructMatcher("isDefinition"); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 158 | Matcher<Decl> Var = |
| 159 | constructMatcher("varDecl", IsDefinition).getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 160 | Matcher<Decl> Class = |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 161 | constructMatcher("recordDecl", IsDefinition).getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 162 | Matcher<Decl> Func = |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 163 | constructMatcher("functionDecl", IsDefinition).getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 164 | 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 Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 171 | Matcher<Decl> Anything = constructMatcher("anything").getTypedMatcher<Decl>(); |
Samuel Benzaquen | b80aad8 | 2013-08-29 15:39:26 +0000 | [diff] [blame] | 172 | Matcher<Decl> RecordDecl = constructMatcher( |
| 173 | "recordDecl", constructMatcher("hasName", std::string("Foo")), |
| 174 | VariantMatcher::SingleMatcher(Anything)).getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 175 | |
Samuel Benzaquen | b80aad8 | 2013-08-29 15:39:26 +0000 | [diff] [blame] | 176 | EXPECT_TRUE(matches("int Foo;", Anything)); |
| 177 | EXPECT_TRUE(matches("class Foo {};", Anything)); |
| 178 | EXPECT_TRUE(matches("void Foo(){};", Anything)); |
| 179 | EXPECT_FALSE(matches("int Foo;", RecordDecl)); |
| 180 | EXPECT_TRUE(matches("class Foo {};", RecordDecl)); |
| 181 | EXPECT_FALSE(matches("void Foo(){};", RecordDecl)); |
Samuel Benzaquen | 6c1dc78 | 2013-11-18 14:53:42 +0000 | [diff] [blame^] | 182 | |
| 183 | Matcher<Stmt> ConstructExpr = constructMatcher( |
| 184 | "constructExpr", |
| 185 | constructMatcher( |
| 186 | "hasDeclaration", |
| 187 | constructMatcher( |
| 188 | "methodDecl", |
| 189 | constructMatcher( |
| 190 | "ofClass", constructMatcher("hasName", std::string("Foo")))))) |
| 191 | .getTypedMatcher<Stmt>(); |
| 192 | EXPECT_FALSE(matches("class Foo { public: Foo(); };", ConstructExpr)); |
| 193 | EXPECT_TRUE( |
| 194 | matches("class Foo { public: Foo(); }; Foo foo = Foo();", ConstructExpr)); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Samuel Benzaquen | 671840a | 2013-07-17 15:11:30 +0000 | [diff] [blame] | 197 | TEST_F(RegistryTest, TemplateArgument) { |
| 198 | Matcher<Decl> HasTemplateArgument = constructMatcher( |
| 199 | "classTemplateSpecializationDecl", |
| 200 | constructMatcher( |
| 201 | "hasAnyTemplateArgument", |
| 202 | constructMatcher("refersToType", |
| 203 | constructMatcher("asString", std::string("int"))))) |
| 204 | .getTypedMatcher<Decl>(); |
| 205 | EXPECT_TRUE(matches("template<typename T> class A {}; A<int> a;", |
| 206 | HasTemplateArgument)); |
| 207 | EXPECT_FALSE(matches("template<typename T> class A {}; A<char> a;", |
| 208 | HasTemplateArgument)); |
| 209 | } |
| 210 | |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 211 | TEST_F(RegistryTest, TypeTraversal) { |
| 212 | Matcher<Type> M = constructMatcher( |
| 213 | "pointerType", |
| 214 | constructMatcher("pointee", constructMatcher("isConstQualified"), |
| 215 | constructMatcher("isInteger"))).getTypedMatcher<Type>(); |
| 216 | EXPECT_FALSE(matches("int *a;", M)); |
| 217 | EXPECT_TRUE(matches("int const *b;", M)); |
| 218 | |
| 219 | M = constructMatcher( |
| 220 | "arrayType", |
| 221 | constructMatcher("hasElementType", constructMatcher("builtinType"))) |
| 222 | .getTypedMatcher<Type>(); |
| 223 | EXPECT_FALSE(matches("struct A{}; A a[7];;", M)); |
| 224 | EXPECT_TRUE(matches("int b[7];", M)); |
| 225 | } |
| 226 | |
Samuel Benzaquen | 86e4d74 | 2013-07-17 14:28:00 +0000 | [diff] [blame] | 227 | TEST_F(RegistryTest, CXXCtorInitializer) { |
| 228 | Matcher<Decl> CtorDecl = constructMatcher( |
| 229 | "constructorDecl", |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 230 | constructMatcher( |
| 231 | "hasAnyConstructorInitializer", |
| 232 | constructMatcher("forField", |
| 233 | constructMatcher("hasName", std::string("foo"))))) |
Samuel Benzaquen | 86e4d74 | 2013-07-17 14:28:00 +0000 | [diff] [blame] | 234 | .getTypedMatcher<Decl>(); |
| 235 | EXPECT_TRUE(matches("struct Foo { Foo() : foo(1) {} int foo; };", CtorDecl)); |
| 236 | EXPECT_FALSE(matches("struct Foo { Foo() {} int foo; };", CtorDecl)); |
| 237 | EXPECT_FALSE(matches("struct Foo { Foo() : bar(1) {} int bar; };", CtorDecl)); |
| 238 | } |
| 239 | |
Samuel Benzaquen | 6de440e | 2013-07-24 14:48:01 +0000 | [diff] [blame] | 240 | TEST_F(RegistryTest, Adaptative) { |
| 241 | Matcher<Decl> D = constructMatcher( |
| 242 | "recordDecl", |
| 243 | constructMatcher( |
| 244 | "has", |
| 245 | constructMatcher("recordDecl", |
| 246 | constructMatcher("hasName", std::string("X"))))) |
| 247 | .getTypedMatcher<Decl>(); |
| 248 | EXPECT_TRUE(matches("class X {};", D)); |
| 249 | EXPECT_TRUE(matches("class Y { class X {}; };", D)); |
| 250 | EXPECT_FALSE(matches("class Y { class Z {}; };", D)); |
| 251 | |
| 252 | Matcher<Stmt> S = constructMatcher( |
| 253 | "forStmt", |
| 254 | constructMatcher( |
| 255 | "hasDescendant", |
| 256 | constructMatcher("varDecl", |
| 257 | constructMatcher("hasName", std::string("X"))))) |
| 258 | .getTypedMatcher<Stmt>(); |
| 259 | EXPECT_TRUE(matches("void foo() { for(int X;;); }", S)); |
| 260 | EXPECT_TRUE(matches("void foo() { for(;;) { int X; } }", S)); |
| 261 | EXPECT_FALSE(matches("void foo() { for(;;); }", S)); |
| 262 | EXPECT_FALSE(matches("void foo() { if (int X = 0){} }", S)); |
| 263 | |
| 264 | S = constructMatcher( |
| 265 | "compoundStmt", constructMatcher("hasParent", constructMatcher("ifStmt"))) |
| 266 | .getTypedMatcher<Stmt>(); |
| 267 | EXPECT_TRUE(matches("void foo() { if (true) { int x = 42; } }", S)); |
| 268 | EXPECT_FALSE(matches("void foo() { if (true) return; }", S)); |
| 269 | } |
| 270 | |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 271 | TEST_F(RegistryTest, VariadicOp) { |
| 272 | Matcher<Decl> D = constructMatcher( |
Samuel Benzaquen | b80aad8 | 2013-08-29 15:39:26 +0000 | [diff] [blame] | 273 | "anyOf", |
| 274 | constructMatcher("recordDecl", |
| 275 | constructMatcher("hasName", std::string("Foo"))), |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 276 | constructMatcher("namedDecl", |
| 277 | constructMatcher("hasName", std::string("foo")))) |
| 278 | .getTypedMatcher<Decl>(); |
| 279 | |
| 280 | EXPECT_TRUE(matches("void foo(){}", D)); |
| 281 | EXPECT_TRUE(matches("struct Foo{};", D)); |
| 282 | EXPECT_FALSE(matches("int i = 0;", D)); |
| 283 | |
| 284 | D = constructMatcher( |
| 285 | "allOf", constructMatcher("recordDecl"), |
| 286 | constructMatcher( |
| 287 | "namedDecl", |
| 288 | constructMatcher("anyOf", |
| 289 | constructMatcher("hasName", std::string("Foo")), |
| 290 | constructMatcher("hasName", std::string("Bar"))))) |
| 291 | .getTypedMatcher<Decl>(); |
| 292 | |
| 293 | EXPECT_FALSE(matches("void foo(){}", D)); |
| 294 | EXPECT_TRUE(matches("struct Foo{};", D)); |
| 295 | EXPECT_FALSE(matches("int i = 0;", D)); |
| 296 | EXPECT_TRUE(matches("class Bar{};", D)); |
| 297 | EXPECT_FALSE(matches("class OtherBar{};", D)); |
| 298 | } |
| 299 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 300 | TEST_F(RegistryTest, Errors) { |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 301 | // Incorrect argument count. |
| 302 | OwningPtr<Diagnostics> Error(new Diagnostics()); |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 303 | EXPECT_TRUE(constructMatcher("hasInitializer", Error.get()).isNull()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 304 | EXPECT_EQ("Incorrect argument count. (Expected = 1) != (Actual = 0)", |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame] | 305 | Error->toString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 306 | Error.reset(new Diagnostics()); |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 307 | EXPECT_TRUE(constructMatcher("isArrow", std::string(), Error.get()).isNull()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 308 | EXPECT_EQ("Incorrect argument count. (Expected = 0) != (Actual = 1)", |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame] | 309 | Error->toString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 310 | |
| 311 | // Bad argument type |
| 312 | Error.reset(new Diagnostics()); |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 313 | EXPECT_TRUE(constructMatcher("ofClass", std::string(), Error.get()).isNull()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 314 | EXPECT_EQ("Incorrect type for arg 1. (Expected = Matcher<CXXRecordDecl>) != " |
| 315 | "(Actual = String)", |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame] | 316 | Error->toString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 317 | Error.reset(new Diagnostics()); |
Samuel Benzaquen | 9d02807 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 318 | EXPECT_TRUE(constructMatcher("recordDecl", constructMatcher("recordDecl"), |
| 319 | constructMatcher("parameterCountIs", 3), |
| 320 | Error.get()).isNull()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 321 | EXPECT_EQ("Incorrect type for arg 2. (Expected = Matcher<CXXRecordDecl>) != " |
| 322 | "(Actual = Matcher<FunctionDecl>)", |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame] | 323 | Error->toString()); |
Samuel Benzaquen | a735090 | 2013-08-28 18:42:04 +0000 | [diff] [blame] | 324 | |
| 325 | // Bad argument type with variadic. |
| 326 | Error.reset(new Diagnostics()); |
| 327 | EXPECT_TRUE(constructMatcher("anyOf", std::string(), Error.get()).isNull()); |
| 328 | EXPECT_EQ( |
| 329 | "Incorrect type for arg 1. (Expected = Matcher<>) != (Actual = String)", |
| 330 | Error->toString()); |
| 331 | Error.reset(new Diagnostics()); |
| 332 | EXPECT_TRUE(constructMatcher( |
| 333 | "recordDecl", |
| 334 | constructMatcher("allOf", |
| 335 | constructMatcher("isDerivedFrom", std::string("FOO")), |
| 336 | constructMatcher("isArrow")), |
| 337 | Error.get()).isNull()); |
| 338 | EXPECT_EQ("Incorrect type for arg 1. " |
| 339 | "(Expected = Matcher<CXXRecordDecl>) != " |
| 340 | "(Actual = Matcher<CXXRecordDecl>&Matcher<MemberExpr>)", |
| 341 | Error->toString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | } // end anonymous namespace |
| 345 | } // end namespace dynamic |
| 346 | } // end namespace ast_matchers |
| 347 | } // end namespace clang |