forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.hpp
More file actions
258 lines (217 loc) · 7.37 KB
/
Copy pathtest_parser.hpp
File metadata and controls
258 lines (217 loc) · 7.37 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (C) 2017-2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef CPPAST_TEST_PARSER_HPP_INCLUDED
#define CPPAST_TEST_PARSER_HPP_INCLUDED
#include <fstream>
#include <catch.hpp>
#include <cppast/code_generator.hpp>
#include <cppast/cpp_class.hpp>
#include <cppast/cpp_entity_kind.hpp>
#include <cppast/cpp_expression.hpp>
#include <cppast/cpp_type.hpp>
#include <cppast/libclang_parser.hpp>
#include <cppast/visitor.hpp>
inline void write_file(const char* name, const char* code)
{
std::ofstream file(name);
file << code;
}
inline std::unique_ptr<cppast::cpp_file> parse_file(const cppast::cpp_entity_index& idx,
const char* name,
bool fast_preprocessing = false)
{
using namespace cppast;
libclang_compile_config config;
config.set_flags(cpp_standard::cpp_latest);
config.fast_preprocessing(fast_preprocessing);
libclang_parser p(default_logger());
std::unique_ptr<cppast::cpp_file> result;
REQUIRE_NOTHROW(result = p.parse(idx, name, config));
REQUIRE(!p.error());
return result;
}
inline std::unique_ptr<cppast::cpp_file> parse(const cppast::cpp_entity_index& idx,
const char* name, const char* code,
bool fast_preprocessing = false)
{
write_file(name, code);
return parse_file(idx, name, fast_preprocessing);
}
class test_generator : public cppast::code_generator
{
public:
test_generator(generation_options options) : options_(std::move(options)) {}
const std::string& str() const noexcept
{
return str_;
}
private:
generation_options do_get_options(const cppast::cpp_entity&,
cppast::cpp_access_specifier_kind) override
{
return options_;
}
void do_indent() override
{
++indent_;
}
void do_unindent() override
{
if (indent_)
--indent_;
}
void do_write_token_seq(cppast::string_view tokens) override
{
if (was_newline_)
{
str_ += std::string(indent_ * 2u, ' ');
was_newline_ = false;
}
str_ += tokens.c_str();
}
void do_write_newline() override
{
str_ += "\n";
was_newline_ = true;
}
std::string str_;
generation_options options_;
unsigned indent_ = 0;
bool was_newline_ = false;
};
inline std::string get_code(const cppast::cpp_entity& e,
cppast::code_generator::generation_options options = {})
{
test_generator generator(options);
cppast::generate_code(generator, e);
auto str = generator.str();
if (!str.empty() && str.back() == '\n')
str.pop_back();
return str;
}
template <typename Func, typename T>
auto visit_callback(bool, Func f, const T& t) -> decltype(f(t) == true)
{
return f(t);
}
template <typename Func, typename T>
bool visit_callback(int check, Func f, const T& t)
{
f(t);
return check == 1;
}
template <typename T, typename Func>
unsigned test_visit(const cppast::cpp_file& file, Func f, bool check_code = true)
{
auto count = 0u;
cppast::visit(file, [&](const cppast::cpp_entity& e, cppast::visitor_info info) {
if (info.event == cppast::visitor_info::container_entity_exit)
return true; // already handled
if (e.kind() == T::kind())
{
auto& obj = static_cast<const T&>(e);
auto check_cur = visit_callback(check_code, f, obj);
++count;
if (check_cur)
{
INFO(e.name());
REQUIRE(e.comment());
REQUIRE(e.comment().value() == get_code(e));
}
}
return true;
});
return count;
}
// number of direct children
template <class Entity>
unsigned count_children(const Entity& cont)
{
return unsigned(std::distance(cont.begin(), cont.end()));
}
// ignores templated scopes
inline std::string full_name(const cppast::cpp_entity& e)
{
if (e.name().empty())
return "";
else if (cppast::is_parameter(e.kind()))
// parameters don't have a full name
return e.name();
std::string scopes;
for (auto cur = e.parent(); cur; cur = cur.value().parent())
// prepend each scope, if there is any
type_safe::with(cur.value().scope_name(), [&](const cppast::cpp_scope_name& cur_scope) {
scopes = cur_scope.name() + "::" + scopes;
});
if (e.kind() == cppast::cpp_entity_kind::class_t)
{
auto& c = static_cast<const cppast::cpp_class&>(e);
return scopes + c.semantic_scope() + c.name();
}
else
return scopes + e.name();
}
// checks the full name/parent
inline void check_parent(const cppast::cpp_entity& e, const char* parent_name,
const char* full_name)
{
REQUIRE(e.parent());
REQUIRE(e.parent().value().name() == parent_name);
REQUIRE(::full_name(e) == full_name);
}
bool equal_types(const cppast::cpp_entity_index& idx, const cppast::cpp_type& parsed,
const cppast::cpp_type& synthesized);
inline bool equal_expressions(const cppast::cpp_expression& parsed,
const cppast::cpp_expression& synthesized)
{
using namespace cppast;
if (parsed.kind() != synthesized.kind())
return false;
switch (parsed.kind())
{
case cpp_expression_kind::unexposed_t:
return static_cast<const cpp_unexposed_expression&>(parsed).expression().as_string()
== static_cast<const cpp_unexposed_expression&>(synthesized)
.expression()
.as_string();
case cpp_expression_kind::literal_t:
return static_cast<const cpp_literal_expression&>(parsed).value()
== static_cast<const cpp_literal_expression&>(synthesized).value();
}
return false;
}
template <typename T, class Predicate>
bool equal_ref(const cppast::cpp_entity_index& idx,
const cppast::basic_cpp_entity_ref<T, Predicate>& parsed,
const cppast::basic_cpp_entity_ref<T, Predicate>& synthesized,
const char* full_name_override = nullptr)
{
if (parsed.name() != synthesized.name())
return false;
else if (parsed.is_overloaded() != synthesized.is_overloaded())
return false;
else if (parsed.is_overloaded())
return false;
auto entities = parsed.get(idx);
if (entities.size() != 1u)
return false;
return entities[0u]->name().empty()
|| full_name(*entities[0u]) == (full_name_override ? full_name_override : parsed.name());
}
template <typename T>
void check_template_parameters(
const T& templ, std::initializer_list<std::pair<cppast::cpp_entity_kind, const char*>> params)
{
// no need to check more
auto cur = params.begin();
for (auto& param : templ.parameters())
{
REQUIRE(cur != params.end());
REQUIRE(param.kind() == cur->first);
REQUIRE(param.name() == cur->second);
++cur;
}
REQUIRE(cur == params.end());
}
#endif // CPPAST_TEST_PARSER_HPP_INCLUDED