xref: /minix3/external/bsd/llvm/dist/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser unit tests -===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===-------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "../ASTMatchersTest.h"
11f4a2713aSLionel Sambuc #include "clang/ASTMatchers/Dynamic/Parser.h"
12f4a2713aSLionel Sambuc #include "clang/ASTMatchers/Dynamic/Registry.h"
13*0a6a1f1dSLionel Sambuc #include "llvm/ADT/Optional.h"
14f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h"
15*0a6a1f1dSLionel Sambuc #include "gtest/gtest.h"
16*0a6a1f1dSLionel Sambuc #include <string>
17*0a6a1f1dSLionel Sambuc #include <vector>
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc namespace clang {
20f4a2713aSLionel Sambuc namespace ast_matchers {
21f4a2713aSLionel Sambuc namespace dynamic {
22f4a2713aSLionel Sambuc namespace {
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc class MockSema : public Parser::Sema {
25f4a2713aSLionel Sambuc public:
~MockSema()26f4a2713aSLionel Sambuc   virtual ~MockSema() {}
27f4a2713aSLionel Sambuc 
expectMatcher(StringRef MatcherName)28f4a2713aSLionel Sambuc   uint64_t expectMatcher(StringRef MatcherName) {
29*0a6a1f1dSLionel Sambuc     // Optimizations on the matcher framework make simple matchers like
30*0a6a1f1dSLionel Sambuc     // 'stmt()' to be all the same matcher.
31*0a6a1f1dSLionel Sambuc     // Use a more complex expression to prevent that.
32*0a6a1f1dSLionel Sambuc     ast_matchers::internal::Matcher<Stmt> M = stmt(stmt(), stmt());
33f4a2713aSLionel Sambuc     ExpectedMatchers.insert(std::make_pair(MatcherName, M));
34*0a6a1f1dSLionel Sambuc     return M.getID().second;
35f4a2713aSLionel Sambuc   }
36f4a2713aSLionel Sambuc 
parse(StringRef Code)37f4a2713aSLionel Sambuc   void parse(StringRef Code) {
38f4a2713aSLionel Sambuc     Diagnostics Error;
39f4a2713aSLionel Sambuc     VariantValue Value;
40f4a2713aSLionel Sambuc     Parser::parseExpression(Code, this, &Value, &Error);
41f4a2713aSLionel Sambuc     Values.push_back(Value);
42f4a2713aSLionel Sambuc     Errors.push_back(Error.toStringFull());
43f4a2713aSLionel Sambuc   }
44f4a2713aSLionel Sambuc 
lookupMatcherCtor(StringRef MatcherName)45*0a6a1f1dSLionel Sambuc   llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName) {
46*0a6a1f1dSLionel Sambuc     const ExpectedMatchersTy::value_type *Matcher =
47*0a6a1f1dSLionel Sambuc         &*ExpectedMatchers.find(MatcherName);
48*0a6a1f1dSLionel Sambuc     return reinterpret_cast<MatcherCtor>(Matcher);
49*0a6a1f1dSLionel Sambuc   }
50*0a6a1f1dSLionel Sambuc 
actOnMatcherExpression(MatcherCtor Ctor,const SourceRange & NameRange,StringRef BindID,ArrayRef<ParserValue> Args,Diagnostics * Error)51*0a6a1f1dSLionel Sambuc   VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
52f4a2713aSLionel Sambuc                                         const SourceRange &NameRange,
53f4a2713aSLionel Sambuc                                         StringRef BindID,
54f4a2713aSLionel Sambuc                                         ArrayRef<ParserValue> Args,
55f4a2713aSLionel Sambuc                                         Diagnostics *Error) {
56*0a6a1f1dSLionel Sambuc     const ExpectedMatchersTy::value_type *Matcher =
57*0a6a1f1dSLionel Sambuc         reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
58*0a6a1f1dSLionel Sambuc     MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
59f4a2713aSLionel Sambuc     Matchers.push_back(ToStore);
60*0a6a1f1dSLionel Sambuc     return VariantMatcher::SingleMatcher(Matcher->second);
61f4a2713aSLionel Sambuc   }
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc   struct MatcherInfo {
64f4a2713aSLionel Sambuc     StringRef MatcherName;
65f4a2713aSLionel Sambuc     SourceRange NameRange;
66f4a2713aSLionel Sambuc     std::vector<ParserValue> Args;
67f4a2713aSLionel Sambuc     std::string BoundID;
68f4a2713aSLionel Sambuc   };
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc   std::vector<std::string> Errors;
71f4a2713aSLionel Sambuc   std::vector<VariantValue> Values;
72f4a2713aSLionel Sambuc   std::vector<MatcherInfo> Matchers;
73*0a6a1f1dSLionel Sambuc   typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
74*0a6a1f1dSLionel Sambuc   ExpectedMatchersTy;
75*0a6a1f1dSLionel Sambuc   ExpectedMatchersTy ExpectedMatchers;
76f4a2713aSLionel Sambuc };
77f4a2713aSLionel Sambuc 
TEST(ParserTest,ParseUnsigned)78f4a2713aSLionel Sambuc TEST(ParserTest, ParseUnsigned) {
79f4a2713aSLionel Sambuc   MockSema Sema;
80f4a2713aSLionel Sambuc   Sema.parse("0");
81f4a2713aSLionel Sambuc   Sema.parse("123");
82f4a2713aSLionel Sambuc   Sema.parse("0x1f");
83f4a2713aSLionel Sambuc   Sema.parse("12345678901");
84f4a2713aSLionel Sambuc   Sema.parse("1a1");
85f4a2713aSLionel Sambuc   EXPECT_EQ(5U, Sema.Values.size());
86f4a2713aSLionel Sambuc   EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
87f4a2713aSLionel Sambuc   EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
88f4a2713aSLionel Sambuc   EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
89f4a2713aSLionel Sambuc   EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
90f4a2713aSLionel Sambuc   EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc 
TEST(ParserTest,ParseString)93f4a2713aSLionel Sambuc TEST(ParserTest, ParseString) {
94f4a2713aSLionel Sambuc   MockSema Sema;
95f4a2713aSLionel Sambuc   Sema.parse("\"Foo\"");
96f4a2713aSLionel Sambuc   Sema.parse("\"\"");
97f4a2713aSLionel Sambuc   Sema.parse("\"Baz");
98f4a2713aSLionel Sambuc   EXPECT_EQ(3ULL, Sema.Values.size());
99f4a2713aSLionel Sambuc   EXPECT_EQ("Foo", Sema.Values[0].getString());
100f4a2713aSLionel Sambuc   EXPECT_EQ("", Sema.Values[1].getString());
101f4a2713aSLionel Sambuc   EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc 
matchesRange(const SourceRange & Range,unsigned StartLine,unsigned EndLine,unsigned StartColumn,unsigned EndColumn)104f4a2713aSLionel Sambuc bool matchesRange(const SourceRange &Range, unsigned StartLine,
105f4a2713aSLionel Sambuc                   unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
106f4a2713aSLionel Sambuc   EXPECT_EQ(StartLine, Range.Start.Line);
107f4a2713aSLionel Sambuc   EXPECT_EQ(EndLine, Range.End.Line);
108f4a2713aSLionel Sambuc   EXPECT_EQ(StartColumn, Range.Start.Column);
109f4a2713aSLionel Sambuc   EXPECT_EQ(EndColumn, Range.End.Column);
110f4a2713aSLionel Sambuc   return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
111f4a2713aSLionel Sambuc          Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
112f4a2713aSLionel Sambuc }
113f4a2713aSLionel Sambuc 
getSingleMatcher(const VariantValue & Value)114f4a2713aSLionel Sambuc llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
115f4a2713aSLionel Sambuc   llvm::Optional<DynTypedMatcher> Result =
116f4a2713aSLionel Sambuc       Value.getMatcher().getSingleMatcher();
117f4a2713aSLionel Sambuc   EXPECT_TRUE(Result.hasValue());
118f4a2713aSLionel Sambuc   return Result;
119f4a2713aSLionel Sambuc }
120f4a2713aSLionel Sambuc 
TEST(ParserTest,ParseMatcher)121f4a2713aSLionel Sambuc TEST(ParserTest, ParseMatcher) {
122f4a2713aSLionel Sambuc   MockSema Sema;
123f4a2713aSLionel Sambuc   const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
124f4a2713aSLionel Sambuc   const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
125f4a2713aSLionel Sambuc   const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
126f4a2713aSLionel Sambuc   Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
127f4a2713aSLionel Sambuc   for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
128f4a2713aSLionel Sambuc     EXPECT_EQ("", Sema.Errors[i]);
129f4a2713aSLionel Sambuc   }
130f4a2713aSLionel Sambuc 
131*0a6a1f1dSLionel Sambuc   EXPECT_NE(ExpectedFoo, ExpectedBar);
132*0a6a1f1dSLionel Sambuc   EXPECT_NE(ExpectedFoo, ExpectedBaz);
133*0a6a1f1dSLionel Sambuc   EXPECT_NE(ExpectedBar, ExpectedBaz);
134*0a6a1f1dSLionel Sambuc 
135f4a2713aSLionel Sambuc   EXPECT_EQ(1ULL, Sema.Values.size());
136*0a6a1f1dSLionel Sambuc   EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID().second);
137f4a2713aSLionel Sambuc 
138f4a2713aSLionel Sambuc   EXPECT_EQ(3ULL, Sema.Matchers.size());
139f4a2713aSLionel Sambuc   const MockSema::MatcherInfo Bar = Sema.Matchers[0];
140f4a2713aSLionel Sambuc   EXPECT_EQ("Bar", Bar.MatcherName);
141f4a2713aSLionel Sambuc   EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
142f4a2713aSLionel Sambuc   EXPECT_EQ(1ULL, Bar.Args.size());
143f4a2713aSLionel Sambuc   EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc   const MockSema::MatcherInfo Baz = Sema.Matchers[1];
146f4a2713aSLionel Sambuc   EXPECT_EQ("Baz", Baz.MatcherName);
147f4a2713aSLionel Sambuc   EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
148f4a2713aSLionel Sambuc   EXPECT_EQ(1ULL, Baz.Args.size());
149f4a2713aSLionel Sambuc   EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc   const MockSema::MatcherInfo Foo = Sema.Matchers[2];
152f4a2713aSLionel Sambuc   EXPECT_EQ("Foo", Foo.MatcherName);
153f4a2713aSLionel Sambuc   EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
154f4a2713aSLionel Sambuc   EXPECT_EQ(2ULL, Foo.Args.size());
155*0a6a1f1dSLionel Sambuc   EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID().second);
156*0a6a1f1dSLionel Sambuc   EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID().second);
157f4a2713aSLionel Sambuc   EXPECT_EQ("Yo!", Foo.BoundID);
158f4a2713aSLionel Sambuc }
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc using ast_matchers::internal::Matcher;
161f4a2713aSLionel Sambuc 
getTestNamedValues()162*0a6a1f1dSLionel Sambuc Parser::NamedValueMap getTestNamedValues() {
163*0a6a1f1dSLionel Sambuc   Parser::NamedValueMap Values;
164*0a6a1f1dSLionel Sambuc   Values["nameX"] = std::string("x");
165*0a6a1f1dSLionel Sambuc   Values["hasParamA"] =
166*0a6a1f1dSLionel Sambuc       VariantMatcher::SingleMatcher(hasParameter(0, hasName("a")));
167*0a6a1f1dSLionel Sambuc   return Values;
168*0a6a1f1dSLionel Sambuc }
169*0a6a1f1dSLionel Sambuc 
TEST(ParserTest,FullParserTest)170f4a2713aSLionel Sambuc TEST(ParserTest, FullParserTest) {
171f4a2713aSLionel Sambuc   Diagnostics Error;
172f4a2713aSLionel Sambuc   llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
173f4a2713aSLionel Sambuc       "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
174f4a2713aSLionel Sambuc       "                                      hasOperatorName(\"+\"))))",
175f4a2713aSLionel Sambuc       &Error));
176f4a2713aSLionel Sambuc   EXPECT_EQ("", Error.toStringFull());
177f4a2713aSLionel Sambuc   Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
178f4a2713aSLionel Sambuc   EXPECT_TRUE(matches("int x = 1 + false;", M));
179f4a2713aSLionel Sambuc   EXPECT_FALSE(matches("int x = true + 1;", M));
180f4a2713aSLionel Sambuc   EXPECT_FALSE(matches("int x = 1 - false;", M));
181f4a2713aSLionel Sambuc   EXPECT_FALSE(matches("int x = true - 1;", M));
182f4a2713aSLionel Sambuc 
183f4a2713aSLionel Sambuc   llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
184f4a2713aSLionel Sambuc       "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
185f4a2713aSLionel Sambuc   EXPECT_EQ("", Error.toStringFull());
186f4a2713aSLionel Sambuc   M = HasParameter->unconditionalConvertTo<Decl>();
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc   EXPECT_TRUE(matches("void f(int a, int x);", M));
189f4a2713aSLionel Sambuc   EXPECT_FALSE(matches("void f(int x, int a);", M));
190f4a2713aSLionel Sambuc 
191*0a6a1f1dSLionel Sambuc   // Test named values.
192*0a6a1f1dSLionel Sambuc   auto NamedValues = getTestNamedValues();
193*0a6a1f1dSLionel Sambuc   llvm::Optional<DynTypedMatcher> HasParameterWithNamedValues(
194*0a6a1f1dSLionel Sambuc       Parser::parseMatcherExpression(
195*0a6a1f1dSLionel Sambuc           "functionDecl(hasParamA, hasParameter(1, hasName(nameX)))",
196*0a6a1f1dSLionel Sambuc           nullptr, &NamedValues, &Error));
197*0a6a1f1dSLionel Sambuc   EXPECT_EQ("", Error.toStringFull());
198*0a6a1f1dSLionel Sambuc   M = HasParameterWithNamedValues->unconditionalConvertTo<Decl>();
199*0a6a1f1dSLionel Sambuc 
200*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(matches("void f(int a, int x);", M));
201*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(matches("void f(int x, int a);", M));
202*0a6a1f1dSLionel Sambuc 
203*0a6a1f1dSLionel Sambuc 
204f4a2713aSLionel Sambuc   EXPECT_TRUE(!Parser::parseMatcherExpression(
205f4a2713aSLionel Sambuc                    "hasInitializer(\n    binaryOperator(hasLHS(\"A\")))",
206f4a2713aSLionel Sambuc                    &Error).hasValue());
207f4a2713aSLionel Sambuc   EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
208f4a2713aSLionel Sambuc             "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
209f4a2713aSLionel Sambuc             "2:20: Error building matcher hasLHS.\n"
210f4a2713aSLionel Sambuc             "2:27: Incorrect type for arg 1. "
211f4a2713aSLionel Sambuc             "(Expected = Matcher<Expr>) != (Actual = String)",
212f4a2713aSLionel Sambuc             Error.toStringFull());
213f4a2713aSLionel Sambuc }
214f4a2713aSLionel Sambuc 
ParseWithError(StringRef Code)215f4a2713aSLionel Sambuc std::string ParseWithError(StringRef Code) {
216f4a2713aSLionel Sambuc   Diagnostics Error;
217f4a2713aSLionel Sambuc   VariantValue Value;
218f4a2713aSLionel Sambuc   Parser::parseExpression(Code, &Value, &Error);
219f4a2713aSLionel Sambuc   return Error.toStringFull();
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc 
ParseMatcherWithError(StringRef Code)222f4a2713aSLionel Sambuc std::string ParseMatcherWithError(StringRef Code) {
223f4a2713aSLionel Sambuc   Diagnostics Error;
224f4a2713aSLionel Sambuc   Parser::parseMatcherExpression(Code, &Error);
225f4a2713aSLionel Sambuc   return Error.toStringFull();
226f4a2713aSLionel Sambuc }
227f4a2713aSLionel Sambuc 
TEST(ParserTest,Errors)228f4a2713aSLionel Sambuc TEST(ParserTest, Errors) {
229f4a2713aSLionel Sambuc   EXPECT_EQ(
230f4a2713aSLionel Sambuc       "1:5: Error parsing matcher. Found token <123> while looking for '('.",
231f4a2713aSLionel Sambuc       ParseWithError("Foo 123"));
232f4a2713aSLionel Sambuc   EXPECT_EQ(
233*0a6a1f1dSLionel Sambuc       "1:1: Matcher not found: Foo\n"
234f4a2713aSLionel Sambuc       "1:9: Error parsing matcher. Found token <123> while looking for ','.",
235f4a2713aSLionel Sambuc       ParseWithError("Foo(\"A\" 123)"));
236f4a2713aSLionel Sambuc   EXPECT_EQ(
237*0a6a1f1dSLionel Sambuc       "1:1: Error parsing argument 1 for matcher stmt.\n"
238*0a6a1f1dSLionel Sambuc       "1:6: Value not found: someValue",
239*0a6a1f1dSLionel Sambuc       ParseWithError("stmt(someValue)"));
240*0a6a1f1dSLionel Sambuc   EXPECT_EQ(
241*0a6a1f1dSLionel Sambuc       "1:1: Matcher not found: Foo\n"
242f4a2713aSLionel Sambuc       "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
243f4a2713aSLionel Sambuc       ParseWithError("Foo("));
244f4a2713aSLionel Sambuc   EXPECT_EQ("1:1: End of code found while looking for token.",
245f4a2713aSLionel Sambuc             ParseWithError(""));
246f4a2713aSLionel Sambuc   EXPECT_EQ("Input value is not a matcher expression.",
247f4a2713aSLionel Sambuc             ParseMatcherWithError("\"A\""));
248*0a6a1f1dSLionel Sambuc   EXPECT_EQ("1:1: Matcher not found: Foo\n"
249*0a6a1f1dSLionel Sambuc             "1:1: Error parsing argument 1 for matcher Foo.\n"
250f4a2713aSLionel Sambuc             "1:5: Invalid token <(> found when looking for a value.",
251f4a2713aSLionel Sambuc             ParseWithError("Foo(("));
252f4a2713aSLionel Sambuc   EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
253f4a2713aSLionel Sambuc   EXPECT_EQ("1:11: Malformed bind() expression.",
254f4a2713aSLionel Sambuc             ParseWithError("isArrow().biind"));
255f4a2713aSLionel Sambuc   EXPECT_EQ("1:15: Malformed bind() expression.",
256f4a2713aSLionel Sambuc             ParseWithError("isArrow().bind"));
257f4a2713aSLionel Sambuc   EXPECT_EQ("1:16: Malformed bind() expression.",
258f4a2713aSLionel Sambuc             ParseWithError("isArrow().bind(foo"));
259f4a2713aSLionel Sambuc   EXPECT_EQ("1:21: Malformed bind() expression.",
260f4a2713aSLionel Sambuc             ParseWithError("isArrow().bind(\"foo\""));
261f4a2713aSLionel Sambuc   EXPECT_EQ("1:1: Error building matcher isArrow.\n"
262f4a2713aSLionel Sambuc             "1:1: Matcher does not support binding.",
263f4a2713aSLionel Sambuc             ParseWithError("isArrow().bind(\"foo\")"));
264f4a2713aSLionel Sambuc   EXPECT_EQ("Input value has unresolved overloaded type: "
265*0a6a1f1dSLionel Sambuc             "Matcher<DoStmt|ForStmt|WhileStmt|CXXForRangeStmt>",
266f4a2713aSLionel Sambuc             ParseMatcherWithError("hasBody(stmt())"));
267f4a2713aSLionel Sambuc }
268f4a2713aSLionel Sambuc 
TEST(ParserTest,OverloadErrors)269f4a2713aSLionel Sambuc TEST(ParserTest, OverloadErrors) {
270f4a2713aSLionel Sambuc   EXPECT_EQ("1:1: Error building matcher callee.\n"
271f4a2713aSLionel Sambuc             "1:8: Candidate 1: Incorrect type for arg 1. "
272f4a2713aSLionel Sambuc             "(Expected = Matcher<Stmt>) != (Actual = String)\n"
273f4a2713aSLionel Sambuc             "1:8: Candidate 2: Incorrect type for arg 1. "
274f4a2713aSLionel Sambuc             "(Expected = Matcher<Decl>) != (Actual = String)",
275f4a2713aSLionel Sambuc             ParseWithError("callee(\"A\")"));
276f4a2713aSLionel Sambuc }
277f4a2713aSLionel Sambuc 
TEST(ParserTest,CompletionRegistry)278*0a6a1f1dSLionel Sambuc TEST(ParserTest, CompletionRegistry) {
279*0a6a1f1dSLionel Sambuc   std::vector<MatcherCompletion> Comps =
280*0a6a1f1dSLionel Sambuc       Parser::completeExpression("while", 5);
281*0a6a1f1dSLionel Sambuc   ASSERT_EQ(1u, Comps.size());
282*0a6a1f1dSLionel Sambuc   EXPECT_EQ("Stmt(", Comps[0].TypedText);
283*0a6a1f1dSLionel Sambuc   EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
284*0a6a1f1dSLionel Sambuc             Comps[0].MatcherDecl);
285*0a6a1f1dSLionel Sambuc 
286*0a6a1f1dSLionel Sambuc   Comps = Parser::completeExpression("whileStmt().", 12);
287*0a6a1f1dSLionel Sambuc   ASSERT_EQ(1u, Comps.size());
288*0a6a1f1dSLionel Sambuc   EXPECT_EQ("bind(\"", Comps[0].TypedText);
289*0a6a1f1dSLionel Sambuc   EXPECT_EQ("bind", Comps[0].MatcherDecl);
290*0a6a1f1dSLionel Sambuc }
291*0a6a1f1dSLionel Sambuc 
TEST(ParserTest,CompletionNamedValues)292*0a6a1f1dSLionel Sambuc TEST(ParserTest, CompletionNamedValues) {
293*0a6a1f1dSLionel Sambuc   // Can complete non-matcher types.
294*0a6a1f1dSLionel Sambuc   auto NamedValues = getTestNamedValues();
295*0a6a1f1dSLionel Sambuc   StringRef Code = "functionDecl(hasName(";
296*0a6a1f1dSLionel Sambuc   std::vector<MatcherCompletion> Comps =
297*0a6a1f1dSLionel Sambuc       Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
298*0a6a1f1dSLionel Sambuc   ASSERT_EQ(1u, Comps.size());
299*0a6a1f1dSLionel Sambuc   EXPECT_EQ("nameX", Comps[0].TypedText);
300*0a6a1f1dSLionel Sambuc   EXPECT_EQ("String nameX", Comps[0].MatcherDecl);
301*0a6a1f1dSLionel Sambuc 
302*0a6a1f1dSLionel Sambuc   // Can complete if there are names in the expression.
303*0a6a1f1dSLionel Sambuc   Code = "methodDecl(hasName(nameX), ";
304*0a6a1f1dSLionel Sambuc   Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
305*0a6a1f1dSLionel Sambuc   EXPECT_LT(0u, Comps.size());
306*0a6a1f1dSLionel Sambuc 
307*0a6a1f1dSLionel Sambuc   // Can complete names and registry together.
308*0a6a1f1dSLionel Sambuc   Code = "methodDecl(hasP";
309*0a6a1f1dSLionel Sambuc   Comps = Parser::completeExpression(Code, Code.size(), nullptr, &NamedValues);
310*0a6a1f1dSLionel Sambuc   ASSERT_EQ(3u, Comps.size());
311*0a6a1f1dSLionel Sambuc   EXPECT_EQ("aramA", Comps[0].TypedText);
312*0a6a1f1dSLionel Sambuc   EXPECT_EQ("Matcher<FunctionDecl> hasParamA", Comps[0].MatcherDecl);
313*0a6a1f1dSLionel Sambuc 
314*0a6a1f1dSLionel Sambuc   EXPECT_EQ("arameter(", Comps[1].TypedText);
315*0a6a1f1dSLionel Sambuc   EXPECT_EQ(
316*0a6a1f1dSLionel Sambuc       "Matcher<FunctionDecl> hasParameter(unsigned, Matcher<ParmVarDecl>)",
317*0a6a1f1dSLionel Sambuc       Comps[1].MatcherDecl);
318*0a6a1f1dSLionel Sambuc 
319*0a6a1f1dSLionel Sambuc   EXPECT_EQ("arent(", Comps[2].TypedText);
320*0a6a1f1dSLionel Sambuc   EXPECT_EQ("Matcher<Decl> hasParent(Matcher<Decl|Stmt>)",
321*0a6a1f1dSLionel Sambuc             Comps[2].MatcherDecl);
322*0a6a1f1dSLionel Sambuc }
323*0a6a1f1dSLionel Sambuc 
324f4a2713aSLionel Sambuc }  // end anonymous namespace
325f4a2713aSLionel Sambuc }  // end namespace dynamic
326f4a2713aSLionel Sambuc }  // end namespace ast_matchers
327f4a2713aSLionel Sambuc }  // end namespace clang
328