blob: 71b0f87e027698fce7768e09fe2c99f0a9022abf [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
10#include <string>
11#include <vector>
12
13#include "../ASTMatchersTest.h"
14#include "clang/ASTMatchers/Dynamic/Parser.h"
15#include "clang/ASTMatchers/Dynamic/Registry.h"
16#include "gtest/gtest.h"
17#include "llvm/ADT/StringMap.h"
18
19namespace clang {
20namespace ast_matchers {
21namespace dynamic {
22namespace {
23
24class DummyDynTypedMatcher : public DynTypedMatcher {
25public:
26 DummyDynTypedMatcher(uint64_t ID) : ID(ID) {}
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000027 DummyDynTypedMatcher(uint64_t ID, StringRef BoundID)
28 : ID(ID), BoundID(BoundID) {}
Manuel Klimekf7f295f2013-05-14 09:13:00 +000029
30 typedef ast_matchers::internal::ASTMatchFinder ASTMatchFinder;
31 typedef ast_matchers::internal::BoundNodesTreeBuilder BoundNodesTreeBuilder;
32 virtual bool matches(const ast_type_traits::DynTypedNode DynNode,
33 ASTMatchFinder *Finder,
34 BoundNodesTreeBuilder *Builder) const {
35 return false;
36 }
37
38 /// \brief Makes a copy of this matcher object.
39 virtual DynTypedMatcher *clone() const {
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000040 return new DummyDynTypedMatcher(*this);
Manuel Klimekf7f295f2013-05-14 09:13:00 +000041 }
42
43 /// \brief Returns a unique ID for the matcher.
44 virtual uint64_t getID() const { return ID; }
45
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000046 virtual DynTypedMatcher* tryBind(StringRef BoundID) const {
47 return new DummyDynTypedMatcher(ID, BoundID);
48 }
49
50 StringRef boundID() const { return BoundID; }
51
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000052 virtual ast_type_traits::ASTNodeKind getSupportedKind() const {
53 return ast_type_traits::ASTNodeKind();
54 }
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000055
Manuel Klimekf7f295f2013-05-14 09:13:00 +000056private:
57 uint64_t ID;
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000058 std::string BoundID;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000059};
60
61class MockSema : public Parser::Sema {
62public:
63 virtual ~MockSema() {}
64
65 uint64_t expectMatcher(StringRef MatcherName) {
66 uint64_t ID = ExpectedMatchers.size() + 1;
67 ExpectedMatchers[MatcherName] = ID;
68 return ID;
69 }
70
71 void parse(StringRef Code) {
72 Diagnostics Error;
73 VariantValue Value;
74 Parser::parseExpression(Code, this, &Value, &Error);
75 Values.push_back(Value);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +000076 Errors.push_back(Error.toStringFull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000077 }
78
Samuel Benzaquen9d028072013-08-13 14:54:51 +000079 VariantMatcher actOnMatcherExpression(StringRef MatcherName,
80 const SourceRange &NameRange,
81 StringRef BindID,
82 ArrayRef<ParserValue> Args,
83 Diagnostics *Error) {
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000084 MatcherInfo ToStore = { MatcherName, NameRange, Args, BindID };
Manuel Klimekf7f295f2013-05-14 09:13:00 +000085 Matchers.push_back(ToStore);
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000086 DummyDynTypedMatcher Matcher(ExpectedMatchers[MatcherName]);
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000087 OwningPtr<DynTypedMatcher> Out(Matcher.tryBind(BindID));
Samuel Benzaquen9d028072013-08-13 14:54:51 +000088 return VariantMatcher::SingleMatcher(*Out);
Manuel Klimekf7f295f2013-05-14 09:13:00 +000089 }
90
91 struct MatcherInfo {
92 StringRef MatcherName;
93 SourceRange NameRange;
94 std::vector<ParserValue> Args;
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000095 std::string BoundID;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000096 };
97
98 std::vector<std::string> Errors;
99 std::vector<VariantValue> Values;
100 std::vector<MatcherInfo> Matchers;
101 llvm::StringMap<uint64_t> ExpectedMatchers;
102};
103
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000104TEST(ParserTest, ParseUnsigned) {
105 MockSema Sema;
106 Sema.parse("0");
107 Sema.parse("123");
108 Sema.parse("0x1f");
109 Sema.parse("12345678901");
110 Sema.parse("1a1");
111 EXPECT_EQ(5U, Sema.Values.size());
112 EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
113 EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
114 EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
115 EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
116 EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
117}
118
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000119TEST(ParserTest, ParseString) {
120 MockSema Sema;
121 Sema.parse("\"Foo\"");
122 Sema.parse("\"\"");
123 Sema.parse("\"Baz");
124 EXPECT_EQ(3ULL, Sema.Values.size());
125 EXPECT_EQ("Foo", Sema.Values[0].getString());
126 EXPECT_EQ("", Sema.Values[1].getString());
127 EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
128}
129
130bool matchesRange(const SourceRange &Range, unsigned StartLine,
131 unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
132 EXPECT_EQ(StartLine, Range.Start.Line);
133 EXPECT_EQ(EndLine, Range.End.Line);
134 EXPECT_EQ(StartColumn, Range.Start.Column);
135 EXPECT_EQ(EndColumn, Range.End.Column);
136 return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
137 Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
138}
139
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000140const DynTypedMatcher *getSingleMatcher(const VariantValue &value) {
141 const DynTypedMatcher *Out;
142 EXPECT_TRUE(value.getMatcher().getSingleMatcher(Out));
143 return Out;
144}
145
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000146TEST(ParserTest, ParseMatcher) {
147 MockSema Sema;
148 const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
149 const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
150 const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000151 Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000152 for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
153 EXPECT_EQ("", Sema.Errors[i]);
154 }
155
156 EXPECT_EQ(1ULL, Sema.Values.size());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000157 EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000158 EXPECT_EQ("Yo!", static_cast<const DummyDynTypedMatcher *>(
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000159 getSingleMatcher(Sema.Values[0]))->boundID());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000160
161 EXPECT_EQ(3ULL, Sema.Matchers.size());
162 const MockSema::MatcherInfo Bar = Sema.Matchers[0];
163 EXPECT_EQ("Bar", Bar.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000164 EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
165 EXPECT_EQ(1ULL, Bar.Args.size());
166 EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000167
168 const MockSema::MatcherInfo Baz = Sema.Matchers[1];
169 EXPECT_EQ("Baz", Baz.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000170 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000171 EXPECT_EQ(1ULL, Baz.Args.size());
172 EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
173
174 const MockSema::MatcherInfo Foo = Sema.Matchers[2];
175 EXPECT_EQ("Foo", Foo.MatcherName);
176 EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
177 EXPECT_EQ(2ULL, Foo.Args.size());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000178 EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
179 EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000180 EXPECT_EQ("Yo!", Foo.BoundID);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000181}
182
183using ast_matchers::internal::Matcher;
184
185TEST(ParserTest, FullParserTest) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000186 Diagnostics Error;
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000187 OwningPtr<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000188 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
189 " hasOperatorName(\"+\"))))",
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000190 &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000191 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000192 Matcher<Decl> M = Matcher<Decl>::constructFrom(*VarDecl);
193 EXPECT_TRUE(matches("int x = 1 + false;", M));
194 EXPECT_FALSE(matches("int x = true + 1;", M));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000195 EXPECT_FALSE(matches("int x = 1 - false;", M));
196 EXPECT_FALSE(matches("int x = true - 1;", M));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000197
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000198 OwningPtr<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000199 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000200 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000201 M = Matcher<Decl>::constructFrom(*HasParameter);
202
203 EXPECT_TRUE(matches("void f(int a, int x);", M));
204 EXPECT_FALSE(matches("void f(int x, int a);", M));
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000205
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000206 EXPECT_TRUE(Parser::parseMatcherExpression(
207 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))", &Error) == NULL);
208 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
209 "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
210 "2:20: Error building matcher hasLHS.\n"
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000211 "2:27: Incorrect type for arg 1. "
212 "(Expected = Matcher<Expr>) != (Actual = String)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000213 Error.toStringFull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000214}
215
216std::string ParseWithError(StringRef Code) {
217 Diagnostics Error;
218 VariantValue Value;
219 Parser::parseExpression(Code, &Value, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000220 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000221}
222
223std::string ParseMatcherWithError(StringRef Code) {
224 Diagnostics Error;
225 Parser::parseMatcherExpression(Code, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000226 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000227}
228
229TEST(ParserTest, Errors) {
230 EXPECT_EQ(
231 "1:5: Error parsing matcher. Found token <123> while looking for '('.",
232 ParseWithError("Foo 123"));
233 EXPECT_EQ(
234 "1:9: Error parsing matcher. Found token <123> while looking for ','.",
235 ParseWithError("Foo(\"A\" 123)"));
236 EXPECT_EQ(
237 "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\""));
243 EXPECT_EQ("1:1: Error parsing argument 1 for matcher Foo.\n"
244 "1:5: Invalid token <(> found when looking for a value.",
245 ParseWithError("Foo(("));
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000246 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
247 EXPECT_EQ("1:11: Malformed bind() expression.",
248 ParseWithError("isArrow().biind"));
249 EXPECT_EQ("1:15: Malformed bind() expression.",
250 ParseWithError("isArrow().bind"));
251 EXPECT_EQ("1:16: Malformed bind() expression.",
252 ParseWithError("isArrow().bind(foo"));
253 EXPECT_EQ("1:21: Malformed bind() expression.",
254 ParseWithError("isArrow().bind(\"foo\""));
255 EXPECT_EQ("1:1: Error building matcher isArrow.\n"
256 "1:1: Matcher does not support binding.",
257 ParseWithError("isArrow().bind(\"foo\")"));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000258 EXPECT_EQ("Input value has unresolved overloaded type: "
259 "Matcher<DoStmt|ForStmt|WhileStmt>",
260 ParseMatcherWithError("hasBody(stmt())"));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000261}
262
Samuel Benzaquen0e1896a2013-07-22 16:13:57 +0000263TEST(ParserTest, OverloadErrors) {
264 EXPECT_EQ("1:1: Error building matcher callee.\n"
265 "1:8: Candidate 1: Incorrect type for arg 1. "
266 "(Expected = Matcher<Stmt>) != (Actual = String)\n"
267 "1:8: Candidate 2: Incorrect type for arg 1. "
268 "(Expected = Matcher<Decl>) != (Actual = String)",
269 ParseWithError("callee(\"A\")"));
270}
271
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000272} // end anonymous namespace
273} // end namespace dynamic
274} // end namespace ast_matchers
275} // end namespace clang