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     ast_matchers::internal::Matcher<Stmt> M = stmt();
30     ExpectedMatchers.insert(std::make_pair(MatcherName, M));
31     return M.getID();
32   }
33 
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 
42   llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName,
43                                                 const SourceRange &NameRange,
44                                                 Diagnostics *Error) {
45     const ExpectedMatchersTy::value_type *Matcher =
46         &*ExpectedMatchers.find(MatcherName);
47     return reinterpret_cast<MatcherCtor>(Matcher);
48   }
49 
50   VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
51                                         const SourceRange &NameRange,
52                                         StringRef BindID,
53                                         ArrayRef<ParserValue> Args,
54                                         Diagnostics *Error) {
55     const ExpectedMatchersTy::value_type *Matcher =
56         reinterpret_cast<const ExpectedMatchersTy::value_type *>(Ctor);
57     MatcherInfo ToStore = { Matcher->first, NameRange, Args, BindID };
58     Matchers.push_back(ToStore);
59     return VariantMatcher::SingleMatcher(Matcher->second);
60   }
61 
62   struct MatcherInfo {
63     StringRef MatcherName;
64     SourceRange NameRange;
65     std::vector<ParserValue> Args;
66     std::string BoundID;
67   };
68 
69   std::vector<std::string> Errors;
70   std::vector<VariantValue> Values;
71   std::vector<MatcherInfo> Matchers;
72   typedef std::map<std::string, ast_matchers::internal::Matcher<Stmt> >
73   ExpectedMatchersTy;
74   ExpectedMatchersTy ExpectedMatchers;
75 };
76 
77 TEST(ParserTest, ParseUnsigned) {
78   MockSema Sema;
79   Sema.parse("0");
80   Sema.parse("123");
81   Sema.parse("0x1f");
82   Sema.parse("12345678901");
83   Sema.parse("1a1");
84   EXPECT_EQ(5U, Sema.Values.size());
85   EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
86   EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
87   EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
88   EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
89   EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
90 }
91 
92 TEST(ParserTest, ParseString) {
93   MockSema Sema;
94   Sema.parse("\"Foo\"");
95   Sema.parse("\"\"");
96   Sema.parse("\"Baz");
97   EXPECT_EQ(3ULL, Sema.Values.size());
98   EXPECT_EQ("Foo", Sema.Values[0].getString());
99   EXPECT_EQ("", Sema.Values[1].getString());
100   EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
101 }
102 
103 bool matchesRange(const SourceRange &Range, unsigned StartLine,
104                   unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
105   EXPECT_EQ(StartLine, Range.Start.Line);
106   EXPECT_EQ(EndLine, Range.End.Line);
107   EXPECT_EQ(StartColumn, Range.Start.Column);
108   EXPECT_EQ(EndColumn, Range.End.Column);
109   return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
110          Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
111 }
112 
113 llvm::Optional<DynTypedMatcher> getSingleMatcher(const VariantValue &Value) {
114   llvm::Optional<DynTypedMatcher> Result =
115       Value.getMatcher().getSingleMatcher();
116   EXPECT_TRUE(Result.hasValue());
117   return Result;
118 }
119 
120 TEST(ParserTest, ParseMatcher) {
121   MockSema Sema;
122   const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
123   const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
124   const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
125   Sema.parse(" Foo ( Bar ( 17), Baz( \n \"B A,Z\") ) .bind( \"Yo!\") ");
126   for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
127     EXPECT_EQ("", Sema.Errors[i]);
128   }
129 
130   EXPECT_EQ(1ULL, Sema.Values.size());
131   EXPECT_EQ(ExpectedFoo, getSingleMatcher(Sema.Values[0])->getID());
132 
133   EXPECT_EQ(3ULL, Sema.Matchers.size());
134   const MockSema::MatcherInfo Bar = Sema.Matchers[0];
135   EXPECT_EQ("Bar", Bar.MatcherName);
136   EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 17));
137   EXPECT_EQ(1ULL, Bar.Args.size());
138   EXPECT_EQ(17U, Bar.Args[0].Value.getUnsigned());
139 
140   const MockSema::MatcherInfo Baz = Sema.Matchers[1];
141   EXPECT_EQ("Baz", Baz.MatcherName);
142   EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 19, 10));
143   EXPECT_EQ(1ULL, Baz.Args.size());
144   EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
145 
146   const MockSema::MatcherInfo Foo = Sema.Matchers[2];
147   EXPECT_EQ("Foo", Foo.MatcherName);
148   EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
149   EXPECT_EQ(2ULL, Foo.Args.size());
150   EXPECT_EQ(ExpectedBar, getSingleMatcher(Foo.Args[0].Value)->getID());
151   EXPECT_EQ(ExpectedBaz, getSingleMatcher(Foo.Args[1].Value)->getID());
152   EXPECT_EQ("Yo!", Foo.BoundID);
153 }
154 
155 using ast_matchers::internal::Matcher;
156 
157 TEST(ParserTest, FullParserTest) {
158   Diagnostics Error;
159   llvm::Optional<DynTypedMatcher> VarDecl(Parser::parseMatcherExpression(
160       "varDecl(hasInitializer(binaryOperator(hasLHS(integerLiteral()),"
161       "                                      hasOperatorName(\"+\"))))",
162       &Error));
163   EXPECT_EQ("", Error.toStringFull());
164   Matcher<Decl> M = VarDecl->unconditionalConvertTo<Decl>();
165   EXPECT_TRUE(matches("int x = 1 + false;", M));
166   EXPECT_FALSE(matches("int x = true + 1;", M));
167   EXPECT_FALSE(matches("int x = 1 - false;", M));
168   EXPECT_FALSE(matches("int x = true - 1;", M));
169 
170   llvm::Optional<DynTypedMatcher> HasParameter(Parser::parseMatcherExpression(
171       "functionDecl(hasParameter(1, hasName(\"x\")))", &Error));
172   EXPECT_EQ("", Error.toStringFull());
173   M = HasParameter->unconditionalConvertTo<Decl>();
174 
175   EXPECT_TRUE(matches("void f(int a, int x);", M));
176   EXPECT_FALSE(matches("void f(int x, int a);", M));
177 
178   EXPECT_TRUE(!Parser::parseMatcherExpression(
179                    "hasInitializer(\n    binaryOperator(hasLHS(\"A\")))",
180                    &Error).hasValue());
181   EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
182             "2:5: Error parsing argument 1 for matcher binaryOperator.\n"
183             "2:20: Error building matcher hasLHS.\n"
184             "2:27: Incorrect type for arg 1. "
185             "(Expected = Matcher<Expr>) != (Actual = String)",
186             Error.toStringFull());
187 }
188 
189 std::string ParseWithError(StringRef Code) {
190   Diagnostics Error;
191   VariantValue Value;
192   Parser::parseExpression(Code, &Value, &Error);
193   return Error.toStringFull();
194 }
195 
196 std::string ParseMatcherWithError(StringRef Code) {
197   Diagnostics Error;
198   Parser::parseMatcherExpression(Code, &Error);
199   return Error.toStringFull();
200 }
201 
202 TEST(ParserTest, Errors) {
203   EXPECT_EQ(
204       "1:5: Error parsing matcher. Found token <123> while looking for '('.",
205       ParseWithError("Foo 123"));
206   EXPECT_EQ(
207       "1:1: Matcher not found: Foo\n"
208       "1:9: Error parsing matcher. Found token <123> while looking for ','.",
209       ParseWithError("Foo(\"A\" 123)"));
210   EXPECT_EQ(
211       "1:1: Matcher not found: Foo\n"
212       "1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
213       ParseWithError("Foo("));
214   EXPECT_EQ("1:1: End of code found while looking for token.",
215             ParseWithError(""));
216   EXPECT_EQ("Input value is not a matcher expression.",
217             ParseMatcherWithError("\"A\""));
218   EXPECT_EQ("1:1: Matcher not found: Foo\n"
219             "1:1: Error parsing argument 1 for matcher Foo.\n"
220             "1:5: Invalid token <(> found when looking for a value.",
221             ParseWithError("Foo(("));
222   EXPECT_EQ("1:7: Expected end of code.", ParseWithError("expr()a"));
223   EXPECT_EQ("1:11: Malformed bind() expression.",
224             ParseWithError("isArrow().biind"));
225   EXPECT_EQ("1:15: Malformed bind() expression.",
226             ParseWithError("isArrow().bind"));
227   EXPECT_EQ("1:16: Malformed bind() expression.",
228             ParseWithError("isArrow().bind(foo"));
229   EXPECT_EQ("1:21: Malformed bind() expression.",
230             ParseWithError("isArrow().bind(\"foo\""));
231   EXPECT_EQ("1:1: Error building matcher isArrow.\n"
232             "1:1: Matcher does not support binding.",
233             ParseWithError("isArrow().bind(\"foo\")"));
234   EXPECT_EQ("Input value has unresolved overloaded type: "
235             "Matcher<DoStmt|ForStmt|WhileStmt>",
236             ParseMatcherWithError("hasBody(stmt())"));
237 }
238 
239 TEST(ParserTest, OverloadErrors) {
240   EXPECT_EQ("1:1: Error building matcher callee.\n"
241             "1:8: Candidate 1: Incorrect type for arg 1. "
242             "(Expected = Matcher<Stmt>) != (Actual = String)\n"
243             "1:8: Candidate 2: Incorrect type for arg 1. "
244             "(Expected = Matcher<Decl>) != (Actual = String)",
245             ParseWithError("callee(\"A\")"));
246 }
247 
248 TEST(ParserTest, Completion) {
249   std::vector<MatcherCompletion> Comps =
250       Parser::completeExpression("while", 5);
251   ASSERT_EQ(1u, Comps.size());
252   EXPECT_EQ("Stmt(", Comps[0].TypedText);
253   EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
254             Comps[0].MatcherDecl);
255 
256   Comps = Parser::completeExpression("whileStmt().", 12);
257   ASSERT_EQ(1u, Comps.size());
258   EXPECT_EQ("bind(\"", Comps[0].TypedText);
259   EXPECT_EQ("bind", Comps[0].MatcherDecl);
260 }
261 
262 }  // end anonymous namespace
263 }  // end namespace dynamic
264 }  // end namespace ast_matchers
265 }  // end namespace clang
266