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 | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 39 | MatcherList constructMatcher(StringRef MatcherName, |
| 40 | Diagnostics *Error = NULL) { |
| 41 | Diagnostics DummyError; |
| 42 | if (!Error) Error = &DummyError; |
| 43 | const MatcherList Out = |
| 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 | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 49 | MatcherList constructMatcher(StringRef MatcherName, const VariantValue &Arg1, |
| 50 | Diagnostics *Error = NULL) { |
| 51 | Diagnostics DummyError; |
| 52 | if (!Error) Error = &DummyError; |
| 53 | const MatcherList Out = Registry::constructMatcher( |
| 54 | MatcherName, SourceRange(), Args(Arg1), Error); |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame^] | 55 | EXPECT_EQ("", DummyError.toStringFull()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 56 | return Out; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 57 | } |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 58 | |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 59 | MatcherList constructMatcher(StringRef MatcherName, const VariantValue &Arg1, |
| 60 | const VariantValue &Arg2, |
| 61 | Diagnostics *Error = NULL) { |
| 62 | Diagnostics DummyError; |
| 63 | if (!Error) Error = &DummyError; |
| 64 | const MatcherList Out = Registry::constructMatcher( |
| 65 | MatcherName, SourceRange(), Args(Arg1, Arg2), Error); |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame^] | 66 | EXPECT_EQ("", DummyError.toStringFull()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 67 | return Out; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
| 71 | TEST_F(RegistryTest, CanConstructNoArgs) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 72 | Matcher<Stmt> IsArrowValue = constructMatcher( |
| 73 | "memberExpr", constructMatcher("isArrow")).getTypedMatcher<Stmt>(); |
| 74 | Matcher<Stmt> BoolValue = |
| 75 | constructMatcher("boolLiteral").getTypedMatcher<Stmt>(); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 76 | |
| 77 | const std::string ClassSnippet = "struct Foo { int x; };\n" |
| 78 | "Foo *foo = new Foo;\n" |
| 79 | "int i = foo->x;\n"; |
| 80 | const std::string BoolSnippet = "bool Foo = true;\n"; |
| 81 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 82 | EXPECT_TRUE(matches(ClassSnippet, IsArrowValue)); |
| 83 | EXPECT_TRUE(matches(BoolSnippet, BoolValue)); |
| 84 | EXPECT_FALSE(matches(ClassSnippet, BoolValue)); |
| 85 | EXPECT_FALSE(matches(BoolSnippet, IsArrowValue)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 88 | TEST_F(RegistryTest, ConstructWithSimpleArgs) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 89 | Matcher<Decl> Value = constructMatcher( |
| 90 | "namedDecl", constructMatcher("hasName", std::string("X"))) |
| 91 | .getTypedMatcher<Decl>(); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 92 | EXPECT_TRUE(matches("class X {};", Value)); |
| 93 | EXPECT_FALSE(matches("int x;", Value)); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 94 | |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 95 | Value = functionDecl(constructMatcher("parameterCountIs", 2) |
| 96 | .getTypedMatcher<FunctionDecl>()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 97 | EXPECT_TRUE(matches("void foo(int,int);", Value)); |
| 98 | EXPECT_FALSE(matches("void foo(int);", Value)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 101 | TEST_F(RegistryTest, ConstructWithMatcherArgs) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 102 | Matcher<Decl> HasInitializerSimple = |
| 103 | constructMatcher("varDecl", constructMatcher("hasInitializer", stmt())) |
| 104 | .getTypedMatcher<Decl>(); |
| 105 | Matcher<Decl> HasInitializerComplex = constructMatcher( |
| 106 | "varDecl", constructMatcher("hasInitializer", callExpr())) |
| 107 | .getTypedMatcher<Decl>(); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 108 | |
| 109 | std::string code = "int i;"; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 110 | EXPECT_FALSE(matches(code, HasInitializerSimple)); |
| 111 | EXPECT_FALSE(matches(code, HasInitializerComplex)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 112 | |
| 113 | code = "int i = 1;"; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 114 | EXPECT_TRUE(matches(code, HasInitializerSimple)); |
| 115 | EXPECT_FALSE(matches(code, HasInitializerComplex)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 116 | |
| 117 | code = "int y(); int i = y();"; |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 118 | EXPECT_TRUE(matches(code, HasInitializerSimple)); |
| 119 | EXPECT_TRUE(matches(code, HasInitializerComplex)); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 120 | |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 121 | Matcher<Decl> HasParameter = functionDecl(constructMatcher( |
| 122 | "hasParameter", 1, hasName("x")).getTypedMatcher<FunctionDecl>()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 123 | EXPECT_TRUE(matches("void f(int a, int x);", HasParameter)); |
| 124 | EXPECT_FALSE(matches("void f(int x, int a);", HasParameter)); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 127 | TEST_F(RegistryTest, PolymorphicMatchers) { |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 128 | const MatcherList IsDefinition = constructMatcher("isDefinition"); |
| 129 | Matcher<Decl> Var = |
| 130 | constructMatcher("varDecl", IsDefinition).getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 131 | Matcher<Decl> Class = |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 132 | constructMatcher("recordDecl", IsDefinition).getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 133 | Matcher<Decl> Func = |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 134 | constructMatcher("functionDecl", IsDefinition).getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 135 | EXPECT_TRUE(matches("int a;", Var)); |
| 136 | EXPECT_FALSE(matches("extern int a;", Var)); |
| 137 | EXPECT_TRUE(matches("class A {};", Class)); |
| 138 | EXPECT_FALSE(matches("class A;", Class)); |
| 139 | EXPECT_TRUE(matches("void f(){};", Func)); |
| 140 | EXPECT_FALSE(matches("void f();", Func)); |
| 141 | |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 142 | Matcher<Decl> Anything = constructMatcher("anything").getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 143 | Matcher<Decl> RecordDecl = |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 144 | constructMatcher("recordDecl", Anything).getTypedMatcher<Decl>(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 145 | |
| 146 | EXPECT_TRUE(matches("int a;", Anything)); |
| 147 | EXPECT_TRUE(matches("class A {};", Anything)); |
| 148 | EXPECT_TRUE(matches("void f(){};", Anything)); |
NAKAMURA Takumi | e054819 | 2013-06-24 13:19:26 +0000 | [diff] [blame] | 149 | // FIXME: A couple of tests have been suppressed. |
| 150 | // I know it'd be bad with _MSC_VER here, though. |
| 151 | #if !defined(_MSC_VER) |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 152 | EXPECT_FALSE(matches("int a;", RecordDecl)); |
NAKAMURA Takumi | e054819 | 2013-06-24 13:19:26 +0000 | [diff] [blame] | 153 | #endif |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 154 | EXPECT_TRUE(matches("class A {};", RecordDecl)); |
NAKAMURA Takumi | e054819 | 2013-06-24 13:19:26 +0000 | [diff] [blame] | 155 | #if !defined(_MSC_VER) |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 156 | EXPECT_FALSE(matches("void f(){};", RecordDecl)); |
NAKAMURA Takumi | e054819 | 2013-06-24 13:19:26 +0000 | [diff] [blame] | 157 | #endif |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Samuel Benzaquen | 671840a | 2013-07-17 15:11:30 +0000 | [diff] [blame] | 160 | TEST_F(RegistryTest, TemplateArgument) { |
| 161 | Matcher<Decl> HasTemplateArgument = constructMatcher( |
| 162 | "classTemplateSpecializationDecl", |
| 163 | constructMatcher( |
| 164 | "hasAnyTemplateArgument", |
| 165 | constructMatcher("refersToType", |
| 166 | constructMatcher("asString", std::string("int"))))) |
| 167 | .getTypedMatcher<Decl>(); |
| 168 | EXPECT_TRUE(matches("template<typename T> class A {}; A<int> a;", |
| 169 | HasTemplateArgument)); |
| 170 | EXPECT_FALSE(matches("template<typename T> class A {}; A<char> a;", |
| 171 | HasTemplateArgument)); |
| 172 | } |
| 173 | |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 174 | TEST_F(RegistryTest, TypeTraversal) { |
| 175 | Matcher<Type> M = constructMatcher( |
| 176 | "pointerType", |
| 177 | constructMatcher("pointee", constructMatcher("isConstQualified"), |
| 178 | constructMatcher("isInteger"))).getTypedMatcher<Type>(); |
| 179 | EXPECT_FALSE(matches("int *a;", M)); |
| 180 | EXPECT_TRUE(matches("int const *b;", M)); |
| 181 | |
| 182 | M = constructMatcher( |
| 183 | "arrayType", |
| 184 | constructMatcher("hasElementType", constructMatcher("builtinType"))) |
| 185 | .getTypedMatcher<Type>(); |
| 186 | EXPECT_FALSE(matches("struct A{}; A a[7];;", M)); |
| 187 | EXPECT_TRUE(matches("int b[7];", M)); |
| 188 | } |
| 189 | |
Samuel Benzaquen | 86e4d74 | 2013-07-17 14:28:00 +0000 | [diff] [blame] | 190 | TEST_F(RegistryTest, CXXCtorInitializer) { |
| 191 | Matcher<Decl> CtorDecl = constructMatcher( |
| 192 | "constructorDecl", |
| 193 | constructMatcher("hasAnyConstructorInitializer", |
| 194 | constructMatcher("forField", hasName("foo")))) |
| 195 | .getTypedMatcher<Decl>(); |
| 196 | EXPECT_TRUE(matches("struct Foo { Foo() : foo(1) {} int foo; };", CtorDecl)); |
| 197 | EXPECT_FALSE(matches("struct Foo { Foo() {} int foo; };", CtorDecl)); |
| 198 | EXPECT_FALSE(matches("struct Foo { Foo() : bar(1) {} int bar; };", CtorDecl)); |
| 199 | } |
| 200 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 201 | TEST_F(RegistryTest, Errors) { |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 202 | // Incorrect argument count. |
| 203 | OwningPtr<Diagnostics> Error(new Diagnostics()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 204 | EXPECT_TRUE(constructMatcher("hasInitializer", Error.get()).empty()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 205 | EXPECT_EQ("Incorrect argument count. (Expected = 1) != (Actual = 0)", |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame^] | 206 | Error->toString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 207 | Error.reset(new Diagnostics()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 208 | EXPECT_TRUE(constructMatcher("isArrow", std::string(), Error.get()).empty()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 209 | EXPECT_EQ("Incorrect argument count. (Expected = 0) != (Actual = 1)", |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame^] | 210 | Error->toString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 211 | |
| 212 | // Bad argument type |
| 213 | Error.reset(new Diagnostics()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 214 | EXPECT_TRUE(constructMatcher("ofClass", std::string(), Error.get()).empty()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 215 | EXPECT_EQ("Incorrect type for arg 1. (Expected = Matcher<CXXRecordDecl>) != " |
| 216 | "(Actual = String)", |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame^] | 217 | Error->toString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 218 | Error.reset(new Diagnostics()); |
Samuel Benzaquen | 3f84bb3 | 2013-07-15 19:25:06 +0000 | [diff] [blame] | 219 | EXPECT_TRUE(constructMatcher("recordDecl", recordDecl(), parameterCountIs(3), |
| 220 | Error.get()).empty()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 221 | EXPECT_EQ("Incorrect type for arg 2. (Expected = Matcher<CXXRecordDecl>) != " |
| 222 | "(Actual = Matcher<FunctionDecl>)", |
Samuel Benzaquen | 2f5a231 | 2013-07-19 20:02:35 +0000 | [diff] [blame^] | 223 | Error->toString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | } // end anonymous namespace |
| 227 | } // end namespace dynamic |
| 228 | } // end namespace ast_matchers |
| 229 | } // end namespace clang |