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