blob: 9ccd3dc04133e620ffa236a50493b83e8cd44ba0 [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 Benzaquenef7eb022013-06-21 15:51:31 +000079 MatcherList 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));
88 return *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
140TEST(ParserTest, ParseMatcher) {
141 MockSema Sema;
142 const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
143 const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
144 const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000145 Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000146 for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
147 EXPECT_EQ("", Sema.Errors[i]);
148 }
149
150 EXPECT_EQ(1ULL, Sema.Values.size());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000151 EXPECT_EQ(ExpectedFoo, Sema.Values[0].getMatchers().matchers()[0]->getID());
152 EXPECT_EQ("Yo!", static_cast<const DummyDynTypedMatcher *>(
153 Sema.Values[0].getMatchers().matchers()[0])->boundID());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000154
155 EXPECT_EQ(3ULL, Sema.Matchers.size());
156 const MockSema::MatcherInfo Bar = Sema.Matchers[0];
157 EXPECT_EQ("Bar", Bar.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000158 EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
159 EXPECT_EQ(1ULL, Bar.Args.size());
160 EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000161
162 const MockSema::MatcherInfo Baz = Sema.Matchers[1];
163 EXPECT_EQ("Baz", Baz.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000164 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000165 EXPECT_EQ(1ULL, Baz.Args.size());
166 EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
167
168 const MockSema::MatcherInfo Foo = Sema.Matchers[2];
169 EXPECT_EQ("Foo", Foo.MatcherName);
170 EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
171 EXPECT_EQ(2ULL, Foo.Args.size());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000172 EXPECT_EQ(ExpectedBar,
173 Foo.Args[0].Value.getMatchers().matchers()[0]->getID());
174 EXPECT_EQ(ExpectedBaz,
175 Foo.Args[1].Value.getMatchers().matchers()[0]->getID());
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000176 EXPECT_EQ("Yo!", Foo.BoundID);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000177}
178
179using ast_matchers::internal::Matcher;
180
181TEST(ParserTest, FullParserTest) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000182 Diagnostics Error;
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000183 OwningPtr<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000184 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
185 " hasOperatorName(\"+\"))))",
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000186 &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000187 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000188 Matcher<Decl> M = Matcher<Decl>::constructFrom(*VarDecl);
189 EXPECT_TRUE(matches("int x = 1 + false;", M));
190 EXPECT_FALSE(matches("int x = true + 1;", M));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000191 EXPECT_FALSE(matches("int x = 1 - false;", M));
192 EXPECT_FALSE(matches("int x = true - 1;", M));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000193
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000194 OwningPtr<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000195 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000196 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000197 M = Matcher<Decl>::constructFrom(*HasParameter);
198
199 EXPECT_TRUE(matches("void f(int a, int x);", M));
200 EXPECT_FALSE(matches("void f(int x, int a);", M));
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000201
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000202 EXPECT_TRUE(Parser::parseMatcherExpression(
203 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))", &Error) == NULL);
204 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
205 "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
206 "2:20: Error building matcher hasLHS.\n"
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000207 "2:27: Incorrect type for arg 1. "
208 "(Expected = Matcher<Expr>) != (Actual = String)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000209 Error.toStringFull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000210}
211
212std::string ParseWithError(StringRef Code) {
213 Diagnostics Error;
214 VariantValue Value;
215 Parser::parseExpression(Code, &Value, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000216 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000217}
218
219std::string ParseMatcherWithError(StringRef Code) {
220 Diagnostics Error;
221 Parser::parseMatcherExpression(Code, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000222 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000223}
224
225TEST(ParserTest, Errors) {
226 EXPECT_EQ(
227 "1:5: Error parsing matcher. Found token <123> while looking for '('.",
228 ParseWithError("Foo 123"));
229 EXPECT_EQ(
230 "1:9: Error parsing matcher. Found token <123> while looking for ','.",
231 ParseWithError("Foo(\"A\" 123)"));
232 EXPECT_EQ(
233 "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
234 ParseWithError("Foo("));
235 EXPECT_EQ("1:1: End of code found while looking for token.",
236 ParseWithError(""));
237 EXPECT_EQ("Input value is not a matcher expression.",
238 ParseMatcherWithError("\"A\""));
239 EXPECT_EQ("1:1: Error parsing argument 1 for matcher Foo.\n"
240 "1:5: Invalid token <(> found when looking for a value.",
241 ParseWithError("Foo(("));
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000242 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
243 EXPECT_EQ("1:11: Malformed bind() expression.",
244 ParseWithError("isArrow().biind"));
245 EXPECT_EQ("1:15: Malformed bind() expression.",
246 ParseWithError("isArrow().bind"));
247 EXPECT_EQ("1:16: Malformed bind() expression.",
248 ParseWithError("isArrow().bind(foo"));
249 EXPECT_EQ("1:21: Malformed bind() expression.",
250 ParseWithError("isArrow().bind(\"foo\""));
251 EXPECT_EQ("1:1: Error building matcher isArrow.\n"
252 "1:1: Matcher does not support binding.",
253 ParseWithError("isArrow().bind(\"foo\")"));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000254 EXPECT_EQ("Input value has unresolved overloaded type: "
255 "Matcher<DoStmt|ForStmt|WhileStmt>",
256 ParseMatcherWithError("hasBody(stmt())"));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000257}
258
259} // end anonymous namespace
260} // end namespace dynamic
261} // end namespace ast_matchers
262} // end namespace clang