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