1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
//===- unittest/ASTMatchers/Dynamic/ParserTest.cpp - Parser unit tests -===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===-------------------------------------------------------------------===//
#include <string>
#include <vector>
#include "../ASTMatchersTest.h"
#include "clang/ASTMatchers/Dynamic/Parser.h"
#include "clang/ASTMatchers/Dynamic/Registry.h"
#include "gtest/gtest.h"
#include "llvm/ADT/StringMap.h"
namespace clang {
namespace ast_matchers {
namespace dynamic {
namespace {
class DummyDynTypedMatcher : public DynTypedMatcher {
public:
DummyDynTypedMatcher(uint64_t ID) : ID(ID) {}
typedef ast_matchers::internal::ASTMatchFinder ASTMatchFinder;
typedef ast_matchers::internal::BoundNodesTreeBuilder BoundNodesTreeBuilder;
virtual bool matches(const ast_type_traits::DynTypedNode DynNode,
ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const {
return false;
}
/// \brief Makes a copy of this matcher object.
virtual DynTypedMatcher *clone() const {
return new DummyDynTypedMatcher(ID);
}
/// \brief Returns a unique ID for the matcher.
virtual uint64_t getID() const { return ID; }
private:
uint64_t ID;
};
class MockSema : public Parser::Sema {
public:
virtual ~MockSema() {}
uint64_t expectMatcher(StringRef MatcherName) {
uint64_t ID = ExpectedMatchers.size() + 1;
ExpectedMatchers[MatcherName] = ID;
return ID;
}
void parse(StringRef Code) {
Diagnostics Error;
VariantValue Value;
Parser::parseExpression(Code, this, &Value, &Error);
Values.push_back(Value);
Errors.push_back(Error.ToStringFull());
}
DynTypedMatcher *actOnMatcherExpression(StringRef MatcherName,
const SourceRange &NameRange,
ArrayRef<ParserValue> Args,
Diagnostics *Error) {
MatcherInfo ToStore = { MatcherName, NameRange, Args };
Matchers.push_back(ToStore);
return new DummyDynTypedMatcher(ExpectedMatchers[MatcherName]);
}
struct MatcherInfo {
StringRef MatcherName;
SourceRange NameRange;
std::vector<ParserValue> Args;
};
std::vector<std::string> Errors;
std::vector<VariantValue> Values;
std::vector<MatcherInfo> Matchers;
llvm::StringMap<uint64_t> ExpectedMatchers;
};
TEST(ParserTest, ParseString) {
MockSema Sema;
Sema.parse("\"Foo\"");
Sema.parse("\"\"");
Sema.parse("\"Baz");
EXPECT_EQ(3ULL, Sema.Values.size());
EXPECT_EQ("Foo", Sema.Values[0].getString());
EXPECT_EQ("", Sema.Values[1].getString());
EXPECT_EQ("1:1: Error parsing string token: <\"Baz>", Sema.Errors[2]);
}
bool matchesRange(const SourceRange &Range, unsigned StartLine,
unsigned EndLine, unsigned StartColumn, unsigned EndColumn) {
EXPECT_EQ(StartLine, Range.Start.Line);
EXPECT_EQ(EndLine, Range.End.Line);
EXPECT_EQ(StartColumn, Range.Start.Column);
EXPECT_EQ(EndColumn, Range.End.Column);
return Range.Start.Line == StartLine && Range.End.Line == EndLine &&
Range.Start.Column == StartColumn && Range.End.Column == EndColumn;
}
TEST(ParserTest, ParseMatcher) {
MockSema Sema;
const uint64_t ExpectedFoo = Sema.expectMatcher("Foo");
const uint64_t ExpectedBar = Sema.expectMatcher("Bar");
const uint64_t ExpectedBaz = Sema.expectMatcher("Baz");
Sema.parse(" Foo ( Bar (), Baz( \n \"B A,Z\") ) ");
for (size_t i = 0, e = Sema.Errors.size(); i != e; ++i) {
EXPECT_EQ("", Sema.Errors[i]);
}
EXPECT_EQ(1ULL, Sema.Values.size());
EXPECT_EQ(ExpectedFoo, Sema.Values[0].getMatcher().getID());
EXPECT_EQ(3ULL, Sema.Matchers.size());
const MockSema::MatcherInfo Bar = Sema.Matchers[0];
EXPECT_EQ("Bar", Bar.MatcherName);
EXPECT_TRUE(matchesRange(Bar.NameRange, 1, 1, 8, 14));
EXPECT_EQ(0ULL, Bar.Args.size());
const MockSema::MatcherInfo Baz = Sema.Matchers[1];
EXPECT_EQ("Baz", Baz.MatcherName);
EXPECT_TRUE(matchesRange(Baz.NameRange, 1, 2, 16, 10));
EXPECT_EQ(1ULL, Baz.Args.size());
EXPECT_EQ("B A,Z", Baz.Args[0].Value.getString());
const MockSema::MatcherInfo Foo = Sema.Matchers[2];
EXPECT_EQ("Foo", Foo.MatcherName);
EXPECT_TRUE(matchesRange(Foo.NameRange, 1, 2, 2, 12));
EXPECT_EQ(2ULL, Foo.Args.size());
EXPECT_EQ(ExpectedBar, Foo.Args[0].Value.getMatcher().getID());
EXPECT_EQ(ExpectedBaz, Foo.Args[1].Value.getMatcher().getID());
}
using ast_matchers::internal::Matcher;
TEST(ParserTest, FullParserTest) {
OwningPtr<DynTypedMatcher> Matcher(Parser::parseMatcherExpression(
"hasInitializer(binaryOperator(hasLHS(integerLiteral())))", NULL));
EXPECT_TRUE(matchesDynamic("int x = 1 + false;", *Matcher));
EXPECT_FALSE(matchesDynamic("int x = true + 1;", *Matcher));
Diagnostics Error;
EXPECT_TRUE(Parser::parseMatcherExpression(
"hasInitializer(\n binaryOperator(hasLHS(\"A\")))", &Error) == NULL);
EXPECT_EQ("1:1: Error parsing argument 1 for matcher hasInitializer.\n"
"2:5: Error parsing argument 1 for matcher binaryOperator.\n"
"2:20: Error building matcher hasLHS.\n"
"2:27: Incorrect type on function hasLHS for arg 1.",
Error.ToStringFull());
}
std::string ParseWithError(StringRef Code) {
Diagnostics Error;
VariantValue Value;
Parser::parseExpression(Code, &Value, &Error);
return Error.ToStringFull();
}
std::string ParseMatcherWithError(StringRef Code) {
Diagnostics Error;
Parser::parseMatcherExpression(Code, &Error);
return Error.ToStringFull();
}
TEST(ParserTest, Errors) {
EXPECT_EQ(
"1:5: Error parsing matcher. Found token <123> while looking for '('.",
ParseWithError("Foo 123"));
EXPECT_EQ(
"1:9: Error parsing matcher. Found token <123> while looking for ','.",
ParseWithError("Foo(\"A\" 123)"));
EXPECT_EQ(
"1:4: Error parsing matcher. Found end-of-code while looking for ')'.",
ParseWithError("Foo("));
EXPECT_EQ("1:1: End of code found while looking for token.",
ParseWithError(""));
EXPECT_EQ("Input value is not a matcher expression.",
ParseMatcherWithError("\"A\""));
EXPECT_EQ("1:1: Error parsing argument 1 for matcher Foo.\n"
"1:5: Invalid token <(> found when looking for a value.",
ParseWithError("Foo(("));
}
} // end anonymous namespace
} // end namespace dynamic
} // end namespace ast_matchers
} // end namespace clang
|