blob: 4e3239ff5a5054ea6a313d2c5160863dd02565da [file] [log] [blame]
Manuel Klimekf7f295f2013-05-14 09:13:00 +00001//===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser 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
Manuel Klimekf7f295f2013-05-14 09:13:00 +000010#include "../ASTMatchersTest.h"
11#include "clang/ASTMatchers/Dynamic/Parser.h"
12#include "clang/ASTMatchers/Dynamic/Registry.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070013#include "llvm/ADT/Optional.h"
Manuel Klimekf7f295f2013-05-14 09:13:00 +000014#include "llvm/ADT/StringMap.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070015#include "gtest/gtest.h"
16#include <string>
17#include <vector>
Manuel Klimekf7f295f2013-05-14 09:13:00 +000018
19namespace clang {
20namespace ast_matchers {
21namespace dynamic {
22namespace {
23
Manuel Klimekf7f295f2013-05-14 09:13:00 +000024class MockSema : public Parser::Sema {
25public:
26 virtual ~MockSema() {}
27
28 uint64_t expectMatcher(StringRef MatcherName) {
Samuel Benzaquenb7488d72013-10-29 14:37:15 +000029 ast_matchers::internal::Matcher<Stmt> M = stmt();
30 ExpectedMatchers.insert(std::make_pair(MatcherName, M));
31 return M.getID();
Manuel Klimekf7f295f2013-05-14 09:13:00 +000032 }
33
34 void parse(StringRef Code) {
35 Diagnostics Error;
36 VariantValue Value;
37 Parser::parseExpression(Code, this, &Value, &Error);
38 Values.push_back(Value);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +000039 Errors.push_back(Error.toStringFull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000040 }
41
Stephen Hines6bcf27b2014-05-29 04:14:42 -070042 llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName) {
Stephen Hines651f13c2014-04-23 16:59:28 -070043 const ExpectedMatchersTy::value_type *Matcher =
44 &*ExpectedMatchers.find(MatcherName);
45 return reinterpret_cast<MatcherCtor>(Matcher);
46 }
47
48 VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
Samuel Benzaquen9d028072013-08-13 14:54:51 +000049 const SourceRange &NameRange,
50 StringRef BindID,
51 ArrayRef<ParserValue> Args,
52 Diagnostics *Error) {
Stephen Hines651f13c2014-04-23 16:59:28 -070053 const ExpectedMatchersTy::value_type *Matcher =
54 reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
55 MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
Manuel Klimekf7f295f2013-05-14 09:13:00 +000056 Matchers.push_back(ToStore);
Stephen Hines651f13c2014-04-23 16:59:28 -070057 return VariantMatcher::SingleMatcher(Matcher->second);
Manuel Klimekf7f295f2013-05-14 09:13:00 +000058 }
59
60 struct MatcherInfo {
61 StringRef MatcherName;
62 SourceRange NameRange;
63 std::vector<ParserValue> Args;
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000064 std::string BoundID;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000065 };
66
67 std::vector<std::string> Errors;
68 std::vector<VariantValue> Values;
69 std::vector<MatcherInfo> Matchers;
Stephen Hines651f13c2014-04-23 16:59:28 -070070 typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
71 ExpectedMatchersTy;
72 ExpectedMatchersTy ExpectedMatchers;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000073};
74
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000075TEST(ParserTest, ParseUnsigned) {
76 MockSema Sema;
77 Sema.parse("0");
78 Sema.parse("123");
79 Sema.parse("0x1f");
80 Sema.parse("12345678901");
81 Sema.parse("1a1");
82 EXPECT_EQ(5U, Sema.Values.size());
83 EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
84 EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
85 EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
86 EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
87 EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
88}
89
Manuel Klimekf7f295f2013-05-14 09:13:00 +000090TEST(ParserTest, ParseString) {
91 MockSema Sema;
92 Sema.parse("\"Foo\"");
93 Sema.parse("\"\"");
94 Sema.parse("\"Baz");
95 EXPECT_EQ(3ULL, Sema.Values.size());
96 EXPECT_EQ("Foo", Sema.Values[0].getString());
97 EXPECT_EQ("", Sema.Values[1].getString());
98 EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
99}
100
101bool matchesRange(const SourceRange &Range, unsigned StartLine,
102 unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
103 EXPECT_EQ(StartLine, Range.Start.Line);
104 EXPECT_EQ(EndLine, Range.End.Line);
105 EXPECT_EQ(StartColumn, Range.Start.Column);
106 EXPECT_EQ(EndColumn, Range.End.Column);
107 return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
108 Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
109}
110
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000111llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
112 llvm::Optional<DynTypedMatcher> Result =
113 Value.getMatcher().getSingleMatcher();
114 EXPECT_TRUE(Result.hasValue());
115 return Result;
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000116}
117
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000118TEST(ParserTest, ParseMatcher) {
119 MockSema Sema;
120 const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
121 const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
122 const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000123 Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000124 for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
125 EXPECT_EQ("", Sema.Errors[i]);
126 }
127
128 EXPECT_EQ(1ULL, Sema.Values.size());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000129 EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000130
131 EXPECT_EQ(3ULL, Sema.Matchers.size());
132 const MockSema::MatcherInfo Bar = Sema.Matchers[0];
133 EXPECT_EQ("Bar", Bar.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000134 EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
135 EXPECT_EQ(1ULL, Bar.Args.size());
136 EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000137
138 const MockSema::MatcherInfo Baz = Sema.Matchers[1];
139 EXPECT_EQ("Baz", Baz.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000140 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000141 EXPECT_EQ(1ULL, Baz.Args.size());
142 EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
143
144 const MockSema::MatcherInfo Foo = Sema.Matchers[2];
145 EXPECT_EQ("Foo", Foo.MatcherName);
146 EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
147 EXPECT_EQ(2ULL, Foo.Args.size());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000148 EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
149 EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000150 EXPECT_EQ("Yo!", Foo.BoundID);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000151}
152
153using ast_matchers::internal::Matcher;
154
155TEST(ParserTest, FullParserTest) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000156 Diagnostics Error;
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000157 llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000158 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
159 " hasOperatorName(\"+\"))))",
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000160 &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000161 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000162 Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000163 EXPECT_TRUE(matches("int x = 1 + false;", M));
164 EXPECT_FALSE(matches("int x = true + 1;", M));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000165 EXPECT_FALSE(matches("int x = 1 - false;", M));
166 EXPECT_FALSE(matches("int x = true - 1;", M));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000167
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000168 llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000169 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000170 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000171 M = HasParameter->unconditionalConvertTo<Decl>();
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000172
173 EXPECT_TRUE(matches("void f(int a, int x);", M));
174 EXPECT_FALSE(matches("void f(int x, int a);", M));
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000175
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700176 // Test named values.
177 struct NamedSema : public Parser::RegistrySema {
178 public:
179 virtual VariantValue getNamedValue(StringRef Name) {
180 if (Name == "nameX")
181 return std::string("x");
182 if (Name == "param0")
183 return VariantMatcher::SingleMatcher(hasParameter(0, hasName("a")));
184 return VariantValue();
185 }
186 };
187 NamedSema Sema;
188 llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues(
189 Parser::parseMatcherExpression(
190 "functionDecl(param0, hasParameter(1, hasName(nameX)))", &Sema,
191 &Error));
192 EXPECT_EQ("", Error.toStringFull());
193 M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>();
194
195 EXPECT_TRUE(matches("void f(int a, int x);", M));
196 EXPECT_FALSE(matches("void f(int x, int a);", M));
197
198
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000199 EXPECT_TRUE(!Parser::parseMatcherExpression(
200 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))",
201 &Error).hasValue());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000202 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
203 "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
204 "2:20: Error building matcher hasLHS.\n"
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000205 "2:27: Incorrect type for arg 1. "
206 "(Expected = Matcher<Expr>) != (Actual = String)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000207 Error.toStringFull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000208}
209
210std::string ParseWithError(StringRef Code) {
211 Diagnostics Error;
212 VariantValue Value;
213 Parser::parseExpression(Code, &Value, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000214 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000215}
216
217std::string ParseMatcherWithError(StringRef Code) {
218 Diagnostics Error;
219 Parser::parseMatcherExpression(Code, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000220 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000221}
222
223TEST(ParserTest, Errors) {
224 EXPECT_EQ(
225 "1:5: Error parsing matcher. Found token <123> while looking for '('.",
226 ParseWithError("Foo 123"));
227 EXPECT_EQ(
Stephen Hines651f13c2014-04-23 16:59:28 -0700228 "1:1: Matcher not found: Foo\n"
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000229 "1:9: Error parsing matcher. Found token <123> while looking for ','.",
230 ParseWithError("Foo(\"A\" 123)"));
231 EXPECT_EQ(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700232 "1:1: Error parsing argument 1 for matcher stmt.\n"
233 "1:6: Value not found: someValue",
234 ParseWithError("stmt(someValue)"));
235 EXPECT_EQ(
Stephen Hines651f13c2014-04-23 16:59:28 -0700236 "1:1: Matcher not found: Foo\n"
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000237 "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
238 ParseWithError("Foo("));
239 EXPECT_EQ("1:1: End of code found while looking for token.",
240 ParseWithError(""));
241 EXPECT_EQ("Input value is not a matcher expression.",
242 ParseMatcherWithError("\"A\""));
Stephen Hines651f13c2014-04-23 16:59:28 -0700243 EXPECT_EQ("1:1: Matcher not found: Foo\n"
244 "1:1: Error parsing argument 1 for matcher Foo.\n"
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000245 "1:5: Invalid token <(> found when looking for a value.",
246 ParseWithError("Foo(("));
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000247 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
248 EXPECT_EQ("1:11: Malformed bind() expression.",
249 ParseWithError("isArrow().biind"));
250 EXPECT_EQ("1:15: Malformed bind() expression.",
251 ParseWithError("isArrow().bind"));
252 EXPECT_EQ("1:16: Malformed bind() expression.",
253 ParseWithError("isArrow().bind(foo"));
254 EXPECT_EQ("1:21: Malformed bind() expression.",
255 ParseWithError("isArrow().bind(\"foo\""));
256 EXPECT_EQ("1:1: Error building matcher isArrow.\n"
257 "1:1: Matcher does not support binding.",
258 ParseWithError("isArrow().bind(\"foo\")"));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000259 EXPECT_EQ("Input value has unresolved overloaded type: "
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700260 "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt>",
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000261 ParseMatcherWithError("hasBody(stmt())"));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000262}
263
Samuel Benzaquen0e1896a2013-07-22 16:13:57 +0000264TEST(ParserTest, OverloadErrors) {
265 EXPECT_EQ("1:1: Error building matcher callee.\n"
266 "1:8: Candidate 1: Incorrect type for arg 1. "
267 "(Expected = Matcher<Stmt>) != (Actual = String)\n"
268 "1:8: Candidate 2: Incorrect type for arg 1. "
269 "(Expected = Matcher<Decl>) != (Actual = String)",
270 ParseWithError("callee(\"A\")"));
271}
272
Stephen Hines651f13c2014-04-23 16:59:28 -0700273TEST(ParserTest, Completion) {
274 std::vector<MatcherCompletion> Comps =
275 Parser::completeExpression("while", 5);
276 ASSERT_EQ(1u, Comps.size());
277 EXPECT_EQ("Stmt(", Comps[0].TypedText);
278 EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
279 Comps[0].MatcherDecl);
280
281 Comps = Parser::completeExpression("whileStmt().", 12);
282 ASSERT_EQ(1u, Comps.size());
283 EXPECT_EQ("bind(\"", Comps[0].TypedText);
284 EXPECT_EQ("bind", Comps[0].MatcherDecl);
285}
286
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000287} // end anonymous namespace
288} // end namespace dynamic
289} // end namespace ast_matchers
290} // end namespace clang