1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
| //===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser unit tests -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===-------------------------------------------------------------------===//
#include "../ASTMatchersTest.h"
#include "clang/ASTMatchers/Dynamic/Parser.h"
#include "clang/ASTMatchers/Dynamic/Registry.h"
#include "llvm/ADT/Optional.h"
#include "gtest/gtest.h"
#include <string>
#include <vector>
namespace clang {
namespace ast_matchers {
namespace dynamic {
namespace {
class MockSema : public Parser::Sema {
public:
~MockSema() override {}
uint64_t expectMatcher(StringRef MatcherName) {
// Optimizations on the matcher framework make simple matchers like
// 'stmt()' to be all the same matcher.
// Use a more complex expression to prevent that.
ast_matchers::internal::Matcher<Stmt> M = stmt(stmt(), stmt());
ExpectedMatchers.insert(std::make_pair(MatcherName, M));
return M.getID().second;
}
void parse(StringRef Code) {
Diagnostics Error;
VariantValue Value;
Parser::parseExpression(Code, this, &Value, &Error);
Values.push_back(Value);
Errors.push_back(Error.toStringFull());
}
llvm::Optional<MatcherCtor>
lookupMatcherCtor(StringRef MatcherName) override {
const ExpectedMatchersTy::value_type *Matcher =
&*ExpectedMatchers.find(MatcherName);
return reinterpret_cast<MatcherCtor>(Matcher);
}
VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
SourceRange NameRange,
StringRef BindID,
ArrayRef<ParserValue> Args,
Diagnostics *Error) override {
const ExpectedMatchersTy::value_type *Matcher =
reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
Matchers.push_back(ToStore);
return VariantMatcher::SingleMatcher(Matcher->second);
}
struct MatcherInfo {
StringRef MatcherName;
SourceRange NameRange;
std::vector<ParserValue> Args;
std::string BoundID;
};
std::vector<std::string> Errors;
std::vector<VariantValue> Values;
std::vector<MatcherInfo> Matchers;
typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
ExpectedMatchersTy;
ExpectedMatchersTy ExpectedMatchers;
};
TEST(ParserTest, ParseBoolean) {
MockSema Sema;
Sema.parse("true");
Sema.parse("false");
EXPECT_EQ(2U, Sema.Values.size());
EXPECT_TRUE(Sema.Values[0].getBoolean());
EXPECT_FALSE(Sema.Values[1].getBoolean());
}
TEST(ParserTest, ParseDouble) {
MockSema Sema;
Sema.parse("1.0");
Sema.parse("2.0f");
Sema.parse("34.56e-78");
Sema.parse("4.E+6");
Sema.parse("1");
EXPECT_EQ(5U, Sema.Values.size());
EXPECT_EQ(1.0, Sema.Values[0].getDouble());
EXPECT_EQ("1:1: Error parsing numeric literal: <2.0f>", Sema.Errors[1]);
EXPECT_EQ(34.56e-78, Sema.Values[2].getDouble());
EXPECT_EQ(4e+6, Sema.Values[3].getDouble());
EXPECT_FALSE(Sema.Values[4].isDouble());
}
TEST(ParserTest, ParseUnsigned) {
MockSema Sema;
Sema.parse("0");
Sema.parse("123");
Sema.parse("0x1f");
Sema.parse("12345678901");
Sema.parse("1a1");
EXPECT_EQ(5U, Sema.Values.size());
EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
EXPECT_EQ("1:1: Error parsing numeric literal: <12345678901>", Sema.Errors[3]);
EXPECT_EQ("1:1: Error parsing numeric literal: <1a1>", Sema.Errors[4]);
}
TEST(ParserTest, ParseString) {
MockSema Sema;
Sema.parse("\"Foo\"");
Sema.parse("\"\"");
Sema.parse("\"Baz");
EXPECT_EQ(3ULL, Sema.Values.size());
EXPECT_EQ("Foo", Sema.Values[0].getString());
EXPECT_EQ("", Sema.Values[1].getString());
EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
}
bool matchesRange(SourceRange Range, unsigned StartLine,
unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
EXPECT_EQ(StartLine, Range.Start.Line);
EXPECT_EQ(EndLine, Range.End.Line);
EXPECT_EQ(StartColumn, Range.Start.Column);
EXPECT_EQ(EndColumn, Range.End.Column);
return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
}
llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
llvm::Optional<DynTypedMatcher> Result =
Value.getMatcher().getSingleMatcher();
EXPECT_TRUE(Result.hasValue());
return Result;
}
TEST(ParserTest, ParseMatcher) {
MockSema Sema;
const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
for (const auto &E : Sema.Errors) {
EXPECT_EQ("", E);
}
EXPECT_NE(ExpectedFoo, ExpectedBar);
EXPECT_NE(ExpectedFoo, ExpectedBaz);
EXPECT_NE(ExpectedBar, ExpectedBaz);
EXPECT_EQ(1ULL, Sema.Values.size());
EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID().second);
EXPECT_EQ(3ULL, Sema.Matchers.size());
const MockSema::MatcherInfo Bar = Sema.Matchers[0];
EXPECT_EQ("Bar", Bar.MatcherName);
EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
EXPECT_EQ(1ULL, Bar.Args.size());
EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());
const MockSema::MatcherInfo Baz = Sema.Matchers[1];
EXPECT_EQ("Baz", Baz.MatcherName);
EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
EXPECT_EQ(1ULL, Baz.Args.size());
EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
const MockSema::MatcherInfo Foo = Sema.Matchers[2];
EXPECT_EQ("Foo", Foo.MatcherName);
EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
EXPECT_EQ(2ULL, Foo.Args.size());
EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID().second);
EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID().second);
EXPECT_EQ("Yo!", Foo.BoundID);
}
TEST(ParserTest, ParseComment) {
MockSema Sema;
Sema.expectMatcher("Foo");
Sema.parse(" Foo() # Bar() ");
for (const auto &E : Sema.Errors) {
EXPECT_EQ("", E);
}
EXPECT_EQ(1ULL, Sema.Matchers.size());
Sema.parse("Foo(#) ");
EXPECT_EQ("1:4: Error parsing matcher. Found end-of-code while looking for ')'.", Sema.Errors[1]);
}
using ast_matchers::internal::Matcher;
Parser::NamedValueMap getTestNamedValues() {
Parser::NamedValueMap Values;
Values["nameX"] = llvm::StringRef("x");
Values["hasParamA"] = VariantMatcher::SingleMatcher(
functionDecl(hasParameter(0, hasName("a"))));
return Values;
}
TEST(ParserTest, FullParserTest) {
Diagnostics Error;
llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
"varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
" hasOperatorName(\"+\"))))",
&Error));
EXPECT_EQ("", Error.toStringFull());
Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
EXPECT_TRUE(matches("int x = 1 + false;", M));
EXPECT_FALSE(matches("int x = true + 1;", M));
EXPECT_FALSE(matches("int x = 1 - false;", M));
EXPECT_FALSE(matches("int x = true - 1;", M));
llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
"functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
EXPECT_EQ("", Error.toStringFull());
M = HasParameter->unconditionalConvertTo<Decl>();
EXPECT_TRUE(matches("void f(int a, int x);", M));
EXPECT_FALSE(matches("void f(int x, int a);", M));
// Test named values.
auto NamedValues = getTestNamedValues();
llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues(
Parser::parseMatcherExpression(
"functionDecl(hasParamA, hasParameter(1, hasName(nameX)))",
nullptr, &NamedValues, &Error));
EXPECT_EQ("", Error.toStringFull());
M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>();
EXPECT_TRUE(matches("void f(int a, int x);", M));
EXPECT_FALSE(matches("void f(int x, int a);", M));
EXPECT_TRUE(!Parser::parseMatcherExpression(
"hasInitializer(\n binaryOperator(hasLHS(\"A\")))",
&Error).hasValue());
EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
"2:5: Error parsing argument 1 for matcher binaryOperator.\n"
"2:20: Error building matcher hasLHS.\n"
"2:27: Incorrect type for arg 1. "
"(Expected = Matcher<Expr>) != (Actual = String)",
Error.toStringFull());
}
TEST(ParserTest, VariadicMatchTest) {
Diagnostics Error;
llvm::Optional<DynTypedMatcher> OM(Parser::parseMatcherExpression(
"stmt(objcMessageExpr(hasAnySelector(\"methodA\", \"methodB:\")))",
&Error));
EXPECT_EQ("", Error.toStringFull());
auto M = OM->unconditionalConvertTo<Stmt>();
EXPECT_TRUE(matchesObjC("@interface I @end "
"void foo(I* i) { [i methodA]; }", M));
}
std::string ParseWithError(StringRef Code) {
Diagnostics Error;
VariantValue Value;
Parser::parseExpression(Code, &Value, &Error);
return Error.toStringFull();
}
std::string ParseMatcherWithError(StringRef Code) {
Diagnostics Error;
Parser::parseMatcherExpression(Code, &Error);
return Error.toStringFull();
}
TEST(ParserTest, Errors) {
EXPECT_EQ(
"1:5: Error parsing matcher. Found token <123> while looking for '('.",
ParseWithError("Foo 123"));
EXPECT_EQ(
"1:1: Matcher not found: Foo\n"
"1:9: Error parsing matcher. Found token <123> while looking for ','.",
ParseWithError("Foo(\"A\" 123)"));
EXPECT_EQ(
"1:1: Error parsing argument 1 for matcher stmt.\n"
"1:6: Value not found: someValue",
ParseWithError("stmt(someValue)"));
EXPECT_EQ(
"1:1: Matcher not found: Foo\n"
"1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
ParseWithError("Foo("));
EXPECT_EQ("1:1: End of code found while looking for token.",
ParseWithError(""));
EXPECT_EQ("Input value is not a matcher expression.",
ParseMatcherWithError("\"A\""));
EXPECT_EQ("1:1: Matcher not found: Foo\n"
"1:1: Error parsing argument 1 for matcher Foo.\n"
"1:5: Invalid token <(> found when looking for a value.",
ParseWithError("Foo(("));
EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
EXPECT_EQ("1:11: Malformed bind() expression.",
ParseWithError("isArrow().biind"));
EXPECT_EQ("1:15: Malformed bind() expression.",
ParseWithError("isArrow().bind"));
EXPECT_EQ("1:16: Malformed bind() expression.",
ParseWithError("isArrow().bind(foo"));
EXPECT_EQ("1:21: Malformed bind() expression.",
ParseWithError("isArrow().bind(\"foo\""));
EXPECT_EQ("1:1: Error building matcher isArrow.\n"
"1:1: Matcher does not support binding.",
ParseWithError("isArrow().bind(\"foo\")"));
EXPECT_EQ("Input value has unresolved overloaded type: "
"Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt|FunctionDecl>",
ParseMatcherWithError("hasBody(stmt())"));
}
TEST(ParserTest, OverloadErrors) {
EXPECT_EQ("1:1: Error building matcher callee.\n"
"1:8: Candidate 1: Incorrect type for arg 1. "
"(Expected = Matcher<Stmt>) != (Actual = String)\n"
"1:8: Candidate 2: Incorrect type for arg 1. "
"(Expected = Matcher<Decl>) != (Actual = String)",
ParseWithError("callee(\"A\")"));
}
TEST(ParserTest, CompletionRegistry) {
std::vector<MatcherCompletion> Comps =
Parser::completeExpression("while", 5);
ASSERT_EQ(1u, Comps.size());
EXPECT_EQ("Stmt(", Comps[0].TypedText);
EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
Comps[0].MatcherDecl);
Comps = Parser::completeExpression("whileStmt().", 12);
ASSERT_EQ(1u, Comps.size());
EXPECT_EQ("bind(\"", Comps[0].TypedText);
EXPECT_EQ("bind", Comps[0].MatcherDecl);
}
TEST(ParserTest, CompletionNamedValues) {
// Can complete non-matcher types.
auto NamedValues = getTestNamedValues();
StringRef Code = "functionDecl(hasName(";
std::vector<MatcherCompletion> Comps =
Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
ASSERT_EQ(1u, Comps.size());
EXPECT_EQ("nameX", Comps[0].TypedText);
EXPECT_EQ("String nameX", Comps[0].MatcherDecl);
// Can complete if there are names in the expression.
Code = "cxxMethodDecl(hasName(nameX), ";
Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
EXPECT_LT(0u, Comps.size());
// Can complete names and registry together.
Code = "functionDecl(hasP";
Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
ASSERT_EQ(3u, Comps.size());
EXPECT_EQ("arameter(", Comps[0].TypedText);
EXPECT_EQ(
"Matcher<FunctionDecl> hasParameter(unsigned, Matcher<ParmVarDecl>)",
Comps[0].MatcherDecl);
EXPECT_EQ("aramA", Comps[1].TypedText);
EXPECT_EQ("Matcher<Decl> hasParamA", Comps[1].MatcherDecl);
EXPECT_EQ("arent(", Comps[2].TypedText);
EXPECT_EQ(
"Matcher<Decl> "
"hasParent(Matcher<NestedNameSpecifierLoc|TypeLoc|Decl|...>)",
Comps[2].MatcherDecl);
}
TEST(ParserTest, ParseBindOnLet) {
auto NamedValues = getTestNamedValues();
Diagnostics Error;
{
llvm::Optional<DynTypedMatcher> TopLevelLetBinding(
Parser::parseMatcherExpression("hasParamA.bind(\"parmABinding\")",
nullptr, &NamedValues, &Error));
EXPECT_EQ("", Error.toStringFull());
auto M = TopLevelLetBinding->unconditionalConvertTo<Decl>();
EXPECT_TRUE(matchAndVerifyResultTrue(
"void foo(int a);", M,
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding")));
EXPECT_TRUE(matchAndVerifyResultFalse(
"void foo(int b);", M,
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding")));
}
{
llvm::Optional<DynTypedMatcher> NestedLetBinding(
Parser::parseMatcherExpression(
"functionDecl(hasParamA.bind(\"parmABinding\"))", nullptr,
&NamedValues, &Error));
EXPECT_EQ("", Error.toStringFull());
auto M = NestedLetBinding->unconditionalConvertTo<Decl>();
EXPECT_TRUE(matchAndVerifyResultTrue(
"void foo(int a);", M,
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding")));
EXPECT_TRUE(matchAndVerifyResultFalse(
"void foo(int b);", M,
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("parmABinding")));
}
}
} // end anonymous namespace
} // end namespace dynamic
} // end namespace ast_matchers
} // end namespace clang
|