1 //===- 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 "../ASTMatchersTest.h"
11 #include "clang/ASTMatchers/Dynamic/Parser.h"
12 #include "clang/ASTMatchers/Dynamic/Registry.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "gtest/gtest.h"
16 #include <string>
17 #include <vector>
18
19 namespace clang {
20 namespace ast_matchers {
21 namespace dynamic {
22 namespace {
23
24 class MockSema : public Parser::Sema {
25 public:
~MockSema()26 virtual ~MockSema() {}
27
expectMatcher(StringRef MatcherName)28 uint64_t expectMatcher(StringRef MatcherName) {
29 ast_matchers::internal::Matcher<Stmt> M = stmt();
30 ExpectedMatchers.insert(std::make_pair(MatcherName, M));
31 return M.getID();
32 }
33
parse(StringRef Code)34 void parse(StringRef Code) {
35 Diagnostics Error;
36 VariantValue Value;
37 Parser::parseExpression(Code, this, &Value, &Error);
38 Values.push_back(Value);
39 Errors.push_back(Error.toStringFull());
40 }
41
lookupMatcherCtor(StringRef MatcherName)42 llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName) {
43 const ExpectedMatchersTy::value_type *Matcher =
44 &*ExpectedMatchers.find(MatcherName);
45 return reinterpret_cast<MatcherCtor>(Matcher);
46 }
47
actOnMatcherExpression(MatcherCtor Ctor,const SourceRange & NameRange,StringRef BindID,ArrayRef<ParserValue> Args,Diagnostics * Error)48 VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
49 const SourceRange &NameRange,
50 StringRef BindID,
51 ArrayRef<ParserValue> Args,
52 Diagnostics *Error) {
53 const ExpectedMatchersTy::value_type *Matcher =
54 reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
55 MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
56 Matchers.push_back(ToStore);
57 return VariantMatcher::SingleMatcher(Matcher->second);
58 }
59
60 struct MatcherInfo {
61 StringRef MatcherName;
62 SourceRange NameRange;
63 std::vector<ParserValue> Args;
64 std::string BoundID;
65 };
66
67 std::vector<std::string> Errors;
68 std::vector<VariantValue> Values;
69 std::vector<MatcherInfo> Matchers;
70 typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
71 ExpectedMatchersTy;
72 ExpectedMatchersTy ExpectedMatchers;
73 };
74
TEST(ParserTest,ParseUnsigned)75 TEST(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
TEST(ParserTest,ParseString)90 TEST(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
matchesRange(const SourceRange & Range,unsigned StartLine,unsigned EndLine,unsigned StartColumn,unsigned EndColumn)101 bool 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
getSingleMatcher(const VariantValue & Value)111 llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
112 llvm::Optional<DynTypedMatcher> Result =
113 Value.getMatcher().getSingleMatcher();
114 EXPECT_TRUE(Result.hasValue());
115 return Result;
116 }
117
TEST(ParserTest,ParseMatcher)118 TEST(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");
123 Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
124 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());
129 EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());
130
131 EXPECT_EQ(3ULL, Sema.Matchers.size());
132 const MockSema::MatcherInfo Bar = Sema.Matchers[0];
133 EXPECT_EQ("Bar", Bar.MatcherName);
134 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());
137
138 const MockSema::MatcherInfo Baz = Sema.Matchers[1];
139 EXPECT_EQ("Baz", Baz.MatcherName);
140 EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
141 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());
148 EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
149 EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
150 EXPECT_EQ("Yo!", Foo.BoundID);
151 }
152
153 using ast_matchers::internal::Matcher;
154
TEST(ParserTest,FullParserTest)155 TEST(ParserTest, FullParserTest) {
156 Diagnostics Error;
157 llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
158 "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
159 " hasOperatorName(\"+\"))))",
160 &Error));
161 EXPECT_EQ("", Error.toStringFull());
162 Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
163 EXPECT_TRUE(matches("int x = 1 + false;", M));
164 EXPECT_FALSE(matches("int x = true + 1;", M));
165 EXPECT_FALSE(matches("int x = 1 - false;", M));
166 EXPECT_FALSE(matches("int x = true - 1;", M));
167
168 llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
169 "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
170 EXPECT_EQ("", Error.toStringFull());
171 M = HasParameter->unconditionalConvertTo<Decl>();
172
173 EXPECT_TRUE(matches("void f(int a, int x);", M));
174 EXPECT_FALSE(matches("void f(int x, int a);", M));
175
176 // 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
199 EXPECT_TRUE(!Parser::parseMatcherExpression(
200 "hasInitializer(\n binaryOperator(hasLHS(\"A\")))",
201 &Error).hasValue());
202 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"
205 "2:27: Incorrect type for arg 1. "
206 "(Expected = Matcher<Expr>) != (Actual = String)",
207 Error.toStringFull());
208 }
209
ParseWithError(StringRef Code)210 std::string ParseWithError(StringRef Code) {
211 Diagnostics Error;
212 VariantValue Value;
213 Parser::parseExpression(Code, &Value, &Error);
214 return Error.toStringFull();
215 }
216
ParseMatcherWithError(StringRef Code)217 std::string ParseMatcherWithError(StringRef Code) {
218 Diagnostics Error;
219 Parser::parseMatcherExpression(Code, &Error);
220 return Error.toStringFull();
221 }
222
TEST(ParserTest,Errors)223 TEST(ParserTest, Errors) {
224 EXPECT_EQ(
225 "1:5: Error parsing matcher. Found token <123> while looking for '('.",
226 ParseWithError("Foo 123"));
227 EXPECT_EQ(
228 "1:1: Matcher not found: Foo\n"
229 "1:9: Error parsing matcher. Found token <123> while looking for ','.",
230 ParseWithError("Foo(\"A\" 123)"));
231 EXPECT_EQ(
232 "1:1: Error parsing argument 1 for matcher stmt.\n"
233 "1:6: Value not found: someValue",
234 ParseWithError("stmt(someValue)"));
235 EXPECT_EQ(
236 "1:1: Matcher not found: Foo\n"
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: Matcher not found: Foo\n"
244 "1:1: Error parsing argument 1 for matcher Foo.\n"
245 "1:5: Invalid token <(> found when looking for a value.",
246 ParseWithError("Foo(("));
247 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\")"));
259 EXPECT_EQ("Input value has unresolved overloaded type: "
260 "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt>",
261 ParseMatcherWithError("hasBody(stmt())"));
262 }
263
TEST(ParserTest,OverloadErrors)264 TEST(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
TEST(ParserTest,Completion)273 TEST(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
287 } // end anonymous namespace
288 } // end namespace dynamic
289 } // end namespace ast_matchers
290 } // end namespace clang
291