forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_generator.cpp
More file actions
255 lines (186 loc) · 4.59 KB
/
Copy pathcode_generator.cpp
File metadata and controls
255 lines (186 loc) · 4.59 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
// 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.
#include <cppast/code_generator.hpp>
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("code_generator")
{
// no need to check much here, as each entity check separately
auto code = R"(using type=int;
type* var;
template<template<typename>class... T>
struct templated{
};
using struct_=int;
struct_ var2;
struct foo{
using my_int=int;
my_int a;
auto func(int)->int(*(*)(int))[42];
private:
int const b=42;
};
int(*(*(foo::* mptr)(int))(int))[42];
enum class bar
:int{
a,
b=42
};
void func(int(*)(int));
extern void(* ptr)(int(*)(int))=&func;)";
auto file = parse({}, "code_generator.cpp", code);
SECTION("basic")
{
REQUIRE(get_code(*file) == code);
}
SECTION("formatting")
{
auto synopsis = R"(using type = int;
type* var;
template <template <typename> class ... T>
struct templated
{
};
using struct_ = int;
struct_ var2;
struct foo
{
using my_int = int;
my_int a;
auto func(int) -> int(*(*)(int))[42];
private:
int const b = 42;
};
int(*(*(foo::* mptr)(int))(int))[42];
enum class bar
: int
{
a,
b = 42
};
void func(int(*)(int));
extern void(* ptr)(int(*)(int)) = &func;
)";
class formatted_generator : public test_generator
{
public:
using test_generator::test_generator;
private:
formatting do_get_formatting() const override
{
return formatting_flags::brace_nl | formatting_flags::operator_ws
| formatting_flags::comma_ws;
}
} generator(code_generator::generation_options{});
generate_code(generator, *file);
REQUIRE(generator.str() == synopsis);
}
SECTION("exclude target")
{
auto code = R"(
namespace a {}
namespace b = a;
using c = int*;
typedef int d;)";
auto synopsis = R"(namespace a{
}
namespace b=excluded;
using c=excluded;
using d=excluded;)";
auto file = parse({}, "code_generator_exclude_target.cpp", code);
REQUIRE(get_code(*file, code_generator::exclude_target) == synopsis);
}
SECTION("exclude return")
{
auto code = R"(
void a();
template <typename T>
auto b() -> int*;
struct foo
{
int c() const&;
operator const int ();
};
)";
auto synopsis = R"(excluded a();
template<typename T>
excluded b();
struct foo{
excluded c()const&;
operator excluded();
};)";
auto file = parse({}, "code_generator_exclude_return.cpp", code);
REQUIRE(get_code(*file, code_generator::exclude_return) == synopsis);
}
SECTION("exclude")
{
class exclude_generator : public test_generator
{
public:
using test_generator::test_generator;
private:
generation_options do_get_options(const cpp_entity& e,
cpp_access_specifier_kind cur_access) override
{
if (cur_access == cpp_protected)
// exclude all protected
return code_generator::exclude;
else if (e.name().front() == 'e')
// exclude all entities starting with `e`
// add declaration flag to detect check for equality
return code_generator::exclude | code_generator::declaration;
else if (e.name() == "FOO")
// don't show macro replacement
return code_generator::declaration;
return code_generator::exclude_noexcept_condition;
}
};
auto code = R"(
void e();
void func(int a, int e, int c);
#define FOO hidden
template <typename e1, typename e2>
void tfunc(int a) noexcept(false);
struct base {};
struct e_t {};
struct bar : e_t, base {};
class foo : e_t, protected base
{
int a;
public:
int e1;
private:
int b;
protected:
int p1;
public:
int c;
int e2;
private:
int e3;
};
void func2() noexcept(0 == 1 && 42);
)";
auto synopsis = R"(void func(int a,int c);
#define FOO
void tfunc(int a)noexcept(false);
struct base{
};
struct bar
:base{
};
class foo{
int a;
int b;
public:
int c;
};
void func2()noexcept(excluded);
)";
auto file = parse({}, "code_generator_exclude.cpp", code);
exclude_generator generator(code_generator::generation_options{});
generate_code(generator, *file);
REQUIRE(generator.str() == synopsis);
}
}