blob: cdf4f92db656f592578da326a640519153593325 [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 Hines651f13c2014-04-23 16:59:28 -070042 llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName,
43 const SourceRange &NameRange,
44 Diagnostics *Error) {
45 const ExpectedMatchersTy::value_type *Matcher =
46 &*ExpectedMatchers.find(MatcherName);
47 return reinterpret_cast<MatcherCtor>(Matcher);
48 }
49
50 VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
Samuel Benzaquen9d028072013-08-13 14:54:51 +000051 const SourceRange &NameRange,
52 StringRef BindID,
53 ArrayRef<ParserValue> Args,
54 Diagnostics *Error) {
Stephen Hines651f13c2014-04-23 16:59:28 -070055 const ExpectedMatchersTy::value_type *Matcher =
56 reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
57 MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
Manuel Klimekf7f295f2013-05-14 09:13:00 +000058 Matchers.push_back(ToStore);
Stephen Hines651f13c2014-04-23 16:59:28 -070059 return VariantMatcher::SingleMatcher(Matcher->second);
Manuel Klimekf7f295f2013-05-14 09:13:00 +000060 }
61
62 struct MatcherInfo {
63 StringRef MatcherName;
64 SourceRange NameRange;
65 std::vector<ParserValue> Args;
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000066 std::string BoundID;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000067 };
68
69 std::vector<std::string> Errors;
70 std::vector<VariantValue> Values;
71 std::vector<MatcherInfo> Matchers;
Stephen Hines651f13c2014-04-23 16:59:28 -070072 typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
73 ExpectedMatchersTy;
74 ExpectedMatchersTy ExpectedMatchers;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000075};
76
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000077TEST(ParserTest, ParseUnsigned) {
78 MockSema Sema;
79 Sema.parse("0");
80 Sema.parse("123");
81 Sema.parse("0x1f");
82 Sema.parse("12345678901");
83 Sema.parse("1a1");
84 EXPECT_EQ(5U, Sema.Values.size());
85 EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
86 EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
87 EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
88 EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
89 EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
90}
91
Manuel Klimekf7f295f2013-05-14 09:13:00 +000092TEST(ParserTest, ParseString) {
93 MockSema Sema;
94 Sema.parse("\"Foo\"");
95 Sema.parse("\"\"");
96 Sema.parse("\"Baz");
97 EXPECT_EQ(3ULL, Sema.Values.size());
98 EXPECT_EQ("Foo", Sema.Values[0].getString());
99 EXPECT_EQ("", Sema.Values[1].getString());
100 EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
101}
102
103bool matchesRange(const SourceRange &Range, unsigned StartLine,
104 unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
105 EXPECT_EQ(StartLine, Range.Start.Line);
106 EXPECT_EQ(EndLine, Range.End.Line);
107 EXPECT_EQ(StartColumn, Range.Start.Column);
108 EXPECT_EQ(EndColumn, Range.End.Column);
109 return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
110 Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
111}
112
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000113llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
114 llvm::Optional<DynTypedMatcher> Result =
115 Value.getMatcher().getSingleMatcher();
116 EXPECT_TRUE(Result.hasValue());
117 return Result;
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000118}
119
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000120TEST(ParserTest, ParseMatcher) {
121 MockSema Sema;
122 const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
123 const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
124 const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000125 Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000126 for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
127 EXPECT_EQ("", Sema.Errors[i]);
128 }
129
130 EXPECT_EQ(1ULL, Sema.Values.size());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000131 EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000132
133 EXPECT_EQ(3ULL, Sema.Matchers.size());
134 const MockSema::MatcherInfo Bar = Sema.Matchers[0];
135 EXPECT_EQ("Bar", Bar.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000136 EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
137 EXPECT_EQ(1ULL, Bar.Args.size());
138 EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000139
140 const MockSema::MatcherInfo Baz = Sema.Matchers[1];
141 EXPECT_EQ("Baz", Baz.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000142 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000143 EXPECT_EQ(1ULL, Baz.Args.size());
144 EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
145
146 const MockSema::MatcherInfo Foo = Sema.Matchers[2];
147 EXPECT_EQ("Foo", Foo.MatcherName);
148 EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
149 EXPECT_EQ(2ULL, Foo.Args.size());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000150 EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
151 EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000152 EXPECT_EQ("Yo!", Foo.BoundID);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000153}
154
155using ast_matchers::internal::Matcher;
156
157TEST(ParserTest, FullParserTest) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000158 Diagnostics Error;
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000159 llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000160 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
161 " hasOperatorName(\"+\"))))",
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000162 &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000163 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000164 Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000165 EXPECT_TRUE(matches("int x = 1 + false;", M));
166 EXPECT_FALSE(matches("int x = true + 1;", M));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000167 EXPECT_FALSE(matches("int x = 1 - false;", M));
168 EXPECT_FALSE(matches("int x = true - 1;", M));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000169
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000170 llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000171 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000172 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000173 M = HasParameter->unconditionalConvertTo<Decl>();
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000174
175 EXPECT_TRUE(matches("void f(int a, int x);", M));
176 EXPECT_FALSE(matches("void f(int x, int a);", M));
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000177
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000178 EXPECT_TRUE(!Parser::parseMatcherExpression(
179 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))",
180 &Error).hasValue());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000181 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
182 "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
183 "2:20: Error building matcher hasLHS.\n"
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000184 "2:27: Incorrect type for arg 1. "
185 "(Expected = Matcher<Expr>) != (Actual = String)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000186 Error.toStringFull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000187}
188
189std::string ParseWithError(StringRef Code) {
190 Diagnostics Error;
191 VariantValue Value;
192 Parser::parseExpression(Code, &Value, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000193 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000194}
195
196std::string ParseMatcherWithError(StringRef Code) {
197 Diagnostics Error;
198 Parser::parseMatcherExpression(Code, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000199 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000200}
201
202TEST(ParserTest, Errors) {
203 EXPECT_EQ(
204 "1:5: Error parsing matcher. Found token <123> while looking for '('.",
205 ParseWithError("Foo 123"));
206 EXPECT_EQ(
Stephen Hines651f13c2014-04-23 16:59:28 -0700207 "1:1: Matcher not found: Foo\n"
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000208 "1:9: Error parsing matcher. Found token <123> while looking for ','.",
209 ParseWithError("Foo(\"A\" 123)"));
210 EXPECT_EQ(
Stephen Hines651f13c2014-04-23 16:59:28 -0700211 "1:1: Matcher not found: Foo\n"
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000212 "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
213 ParseWithError("Foo("));
214 EXPECT_EQ("1:1: End of code found while looking for token.",
215 ParseWithError(""));
216 EXPECT_EQ("Input value is not a matcher expression.",
217 ParseMatcherWithError("\"A\""));
Stephen Hines651f13c2014-04-23 16:59:28 -0700218 EXPECT_EQ("1:1: Matcher not found: Foo\n"
219 "1:1: Error parsing argument 1 for matcher Foo.\n"
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000220 "1:5: Invalid token <(> found when looking for a value.",
221 ParseWithError("Foo(("));
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000222 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
223 EXPECT_EQ("1:11: Malformed bind() expression.",
224 ParseWithError("isArrow().biind"));
225 EXPECT_EQ("1:15: Malformed bind() expression.",
226 ParseWithError("isArrow().bind"));
227 EXPECT_EQ("1:16: Malformed bind() expression.",
228 ParseWithError("isArrow().bind(foo"));
229 EXPECT_EQ("1:21: Malformed bind() expression.",
230 ParseWithError("isArrow().bind(\"foo\""));
231 EXPECT_EQ("1:1: Error building matcher isArrow.\n"
232 "1:1: Matcher does not support binding.",
233 ParseWithError("isArrow().bind(\"foo\")"));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000234 EXPECT_EQ("Input value has unresolved overloaded type: "
235 "Matcher<DoStmt|ForStmt|WhileStmt>",
236 ParseMatcherWithError("hasBody(stmt())"));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000237}
238
Samuel Benzaquen0e1896a2013-07-22 16:13:57 +0000239TEST(ParserTest, OverloadErrors) {
240 EXPECT_EQ("1:1: Error building matcher callee.\n"
241 "1:8: Candidate 1: Incorrect type for arg 1. "
242 "(Expected = Matcher<Stmt>) != (Actual = String)\n"
243 "1:8: Candidate 2: Incorrect type for arg 1. "
244 "(Expected = Matcher<Decl>) != (Actual = String)",
245 ParseWithError("callee(\"A\")"));
246}
247
Stephen Hines651f13c2014-04-23 16:59:28 -0700248TEST(ParserTest, Completion) {
249 std::vector<MatcherCompletion> Comps =
250 Parser::completeExpression("while", 5);
251 ASSERT_EQ(1u, Comps.size());
252 EXPECT_EQ("Stmt(", Comps[0].TypedText);
253 EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
254 Comps[0].MatcherDecl);
255
256 Comps = Parser::completeExpression("whileStmt().", 12);
257 ASSERT_EQ(1u, Comps.size());
258 EXPECT_EQ("bind(\"", Comps[0].TypedText);
259 EXPECT_EQ("bind", Comps[0].MatcherDecl);
260}
261
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000262} // end anonymous namespace
263} // end namespace dynamic
264} // end namespace ast_matchers
265} // end namespace clang