blob: f19ec51ad70ea59c808ead5491413bd3c105e6f6 [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
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
Samuel Benzaquen9d028072013-08-13 14:54:51 +000042 VariantMatcher actOnMatcherExpression(StringRef MatcherName,
43 const SourceRange &NameRange,
44 StringRef BindID,
45 ArrayRef<ParserValue> Args,
46 Diagnostics *Error) {
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000047 MatcherInfo ToStore = { MatcherName, NameRange, Args, BindID };
Manuel Klimekf7f295f2013-05-14 09:13:00 +000048 Matchers.push_back(ToStore);
Samuel Benzaquenb7488d72013-10-29 14:37:15 +000049 return VariantMatcher::SingleMatcher(
50 ExpectedMatchers.find(MatcherName)->second);
Manuel Klimekf7f295f2013-05-14 09:13:00 +000051 }
52
53 struct MatcherInfo {
54 StringRef MatcherName;
55 SourceRange NameRange;
56 std::vector<ParserValue> Args;
Samuel Benzaquen4f37d922013-06-03 19:31:08 +000057 std::string BoundID;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000058 };
59
60 std::vector<std::string> Errors;
61 std::vector<VariantValue> Values;
62 std::vector<MatcherInfo> Matchers;
Samuel Benzaquenb7488d72013-10-29 14:37:15 +000063 std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
64 ExpectedMatchers;
Manuel Klimekf7f295f2013-05-14 09:13:00 +000065};
66
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000067TEST(ParserTest, ParseUnsigned) {
68 MockSema Sema;
69 Sema.parse("0");
70 Sema.parse("123");
71 Sema.parse("0x1f");
72 Sema.parse("12345678901");
73 Sema.parse("1a1");
74 EXPECT_EQ(5U, Sema.Values.size());
75 EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
76 EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
77 EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
78 EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
79 EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
80}
81
Manuel Klimekf7f295f2013-05-14 09:13:00 +000082TEST(ParserTest, ParseString) {
83 MockSema Sema;
84 Sema.parse("\"Foo\"");
85 Sema.parse("\"\"");
86 Sema.parse("\"Baz");
87 EXPECT_EQ(3ULL, Sema.Values.size());
88 EXPECT_EQ("Foo", Sema.Values[0].getString());
89 EXPECT_EQ("", Sema.Values[1].getString());
90 EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
91}
92
93bool matchesRange(const SourceRange &Range, unsigned StartLine,
94 unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
95 EXPECT_EQ(StartLine, Range.Start.Line);
96 EXPECT_EQ(EndLine, Range.End.Line);
97 EXPECT_EQ(StartColumn, Range.Start.Column);
98 EXPECT_EQ(EndColumn, Range.End.Column);
99 return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
100 Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
101}
102
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000103llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
104 llvm::Optional<DynTypedMatcher> Result =
105 Value.getMatcher().getSingleMatcher();
106 EXPECT_TRUE(Result.hasValue());
107 return Result;
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000108}
109
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000110TEST(ParserTest, ParseMatcher) {
111 MockSema Sema;
112 const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
113 const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
114 const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000115 Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000116 for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
117 EXPECT_EQ("", Sema.Errors[i]);
118 }
119
120 EXPECT_EQ(1ULL, Sema.Values.size());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000121 EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000122
123 EXPECT_EQ(3ULL, Sema.Matchers.size());
124 const MockSema::MatcherInfo Bar = Sema.Matchers[0];
125 EXPECT_EQ("Bar", Bar.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000126 EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
127 EXPECT_EQ(1ULL, Bar.Args.size());
128 EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000129
130 const MockSema::MatcherInfo Baz = Sema.Matchers[1];
131 EXPECT_EQ("Baz", Baz.MatcherName);
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000132 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000133 EXPECT_EQ(1ULL, Baz.Args.size());
134 EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
135
136 const MockSema::MatcherInfo Foo = Sema.Matchers[2];
137 EXPECT_EQ("Foo", Foo.MatcherName);
138 EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
139 EXPECT_EQ(2ULL, Foo.Args.size());
Samuel Benzaquen9d028072013-08-13 14:54:51 +0000140 EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
141 EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000142 EXPECT_EQ("Yo!", Foo.BoundID);
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000143}
144
145using ast_matchers::internal::Matcher;
146
147TEST(ParserTest, FullParserTest) {
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000148 Diagnostics Error;
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000149 llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000150 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
151 " hasOperatorName(\"+\"))))",
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000152 &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000153 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000154 Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000155 EXPECT_TRUE(matches("int x = 1 + false;", M));
156 EXPECT_FALSE(matches("int x = true + 1;", M));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000157 EXPECT_FALSE(matches("int x = 1 - false;", M));
158 EXPECT_FALSE(matches("int x = true - 1;", M));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000159
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000160 llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
Samuel Benzaquen3f84bb32013-07-15 19:25:06 +0000161 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000162 EXPECT_EQ("", Error.toStringFull());
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000163 M = HasParameter->unconditionalConvertTo<Decl>();
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000164
165 EXPECT_TRUE(matches("void f(int a, int x);", M));
166 EXPECT_FALSE(matches("void f(int x, int a);", M));
Samuel Benzaquen7a337af2013-06-04 15:46:22 +0000167
Samuel Benzaquenb7488d72013-10-29 14:37:15 +0000168 EXPECT_TRUE(!Parser::parseMatcherExpression(
169 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))",
170 &Error).hasValue());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000171 EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
172 "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
173 "2:20: Error building matcher hasLHS.\n"
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000174 "2:27: Incorrect type for arg 1. "
175 "(Expected = Matcher<Expr>) != (Actual = String)",
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000176 Error.toStringFull());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000177}
178
179std::string ParseWithError(StringRef Code) {
180 Diagnostics Error;
181 VariantValue Value;
182 Parser::parseExpression(Code, &Value, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000183 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000184}
185
186std::string ParseMatcherWithError(StringRef Code) {
187 Diagnostics Error;
188 Parser::parseMatcherExpression(Code, &Error);
Samuel Benzaquen2f5a2312013-07-19 20:02:35 +0000189 return Error.toStringFull();
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000190}
191
192TEST(ParserTest, Errors) {
193 EXPECT_EQ(
194 "1:5: Error parsing matcher. Found token <123> while looking for '('.",
195 ParseWithError("Foo 123"));
196 EXPECT_EQ(
197 "1:9: Error parsing matcher. Found token <123> while looking for ','.",
198 ParseWithError("Foo(\"A\" 123)"));
199 EXPECT_EQ(
200 "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
201 ParseWithError("Foo("));
202 EXPECT_EQ("1:1: End of code found while looking for token.",
203 ParseWithError(""));
204 EXPECT_EQ("Input value is not a matcher expression.",
205 ParseMatcherWithError("\"A\""));
206 EXPECT_EQ("1:1: Error parsing argument 1 for matcher Foo.\n"
207 "1:5: Invalid token <(> found when looking for a value.",
208 ParseWithError("Foo(("));
Samuel Benzaquen4f37d922013-06-03 19:31:08 +0000209 EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
210 EXPECT_EQ("1:11: Malformed bind() expression.",
211 ParseWithError("isArrow().biind"));
212 EXPECT_EQ("1:15: Malformed bind() expression.",
213 ParseWithError("isArrow().bind"));
214 EXPECT_EQ("1:16: Malformed bind() expression.",
215 ParseWithError("isArrow().bind(foo"));
216 EXPECT_EQ("1:21: Malformed bind() expression.",
217 ParseWithError("isArrow().bind(\"foo\""));
218 EXPECT_EQ("1:1: Error building matcher isArrow.\n"
219 "1:1: Matcher does not support binding.",
220 ParseWithError("isArrow().bind(\"foo\")"));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000221 EXPECT_EQ("Input value has unresolved overloaded type: "
222 "Matcher<DoStmt|ForStmt|WhileStmt>",
223 ParseMatcherWithError("hasBody(stmt())"));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000224}
225
Samuel Benzaquen0e1896a2013-07-22 16:13:57 +0000226TEST(ParserTest, OverloadErrors) {
227 EXPECT_EQ("1:1: Error building matcher callee.\n"
228 "1:8: Candidate 1: Incorrect type for arg 1. "
229 "(Expected = Matcher<Stmt>) != (Actual = String)\n"
230 "1:8: Candidate 2: Incorrect type for arg 1. "
231 "(Expected = Matcher<Decl>) != (Actual = String)",
232 ParseWithError("callee(\"A\")"));
233}
234
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000235} // end anonymous namespace
236} // end namespace dynamic
237} // end namespace ast_matchers
238} // end namespace clang