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