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