forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_friend.cpp
More file actions
292 lines (265 loc) · 9.21 KB
/
Copy pathcpp_friend.cpp
File metadata and controls
292 lines (265 loc) · 9.21 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#include <cppast/cpp_friend.hpp>
#include <cppast/cpp_class.hpp>
#include <cppast/cpp_class_template.hpp>
#include <cppast/cpp_function.hpp>
#include <cppast/cpp_function_template.hpp>
#include <cppast/cpp_template.hpp>
#include <cppast/cpp_template_parameter.hpp>
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("cpp_friend", "[.][clang4]")
{
auto code = R"(
/// b
class b
{
public:
/// b::b
b(const b&) {}
void f() const;
/// operator int
operator int() {}
};
/// g
class g {};
namespace ns
{
namespace other_ns
{
namespace inner
{
/// h2
void h2() {}
}
}
/// h
class h
{
/// friend void other_ns::inner::h2();
friend void other_ns::inner::h2();
};
}
/// b::f
void b::f() const {}
struct foo
{
/// friend struct a;
friend struct a;
/// friend class b;
friend class b;
/// friend void c(int);
friend void c(int);
/// friend int d();
friend int d();
/// friend void e();
friend void e() {}
/// friend b::b(b const&);
friend b::b(const b&);
/// friend void b::f()const;
friend void b::f() const;
/// friend b::operator int();
friend b::operator int();
/// friend class g;
friend g;
/// friend ns::h;
friend ns::h;
};
template <typename T>
struct templ_a {};
namespace ns
{
template <typename T, int I, template <typename> class Templ>
struct templ_b {};
}
template <typename T>
void templ_c();
template <typename T>
struct bar
{
/// friend T;
friend T;
/// friend templ_a<T>;
friend templ_a<T>;
/// friend ns::templ_b<T,0,templ_a>;
friend struct ns::templ_b<T, 0, templ_a>;
/// template<typename U>
/// friend struct i;
template <typename U>
friend struct i;
/// template<typename U>
/// friend void j();
template <typename U>
friend void j();
/// template<typename U>
/// friend void k();
template <typename U>
friend void k() {}
/// friend void templ_c<ns::h>();
friend void templ_c<ns::h>();
};
/// d
int d() {}
)";
cpp_entity_index idx;
auto check_definition = [&](cpp_entity_id id, const char* name) {
auto def = idx.lookup_definition(id);
REQUIRE(def.has_value());
REQUIRE(def.value().comment());
REQUIRE(def.value().comment().value() == name);
};
auto file = parse(idx, "cpp_friend.cpp", code);
auto count = test_visit<cpp_friend>(*file, [&](const cpp_friend& f) {
REQUIRE(f.name().empty());
if (auto entity = f.entity())
{
if (entity.value().kind() == cpp_entity_kind::class_t)
{
auto& c = static_cast<const cpp_class&>(entity.value());
REQUIRE(c.is_declaration());
if (c.name() == "a")
REQUIRE(c.class_kind() == cpp_class_kind::struct_t);
else if (c.name() == "b")
{
REQUIRE(c.class_kind() == cpp_class_kind::class_t);
REQUIRE(c.definition());
check_definition(c.definition().value(), "b");
}
else if (c.name() == "g")
{
REQUIRE(c.class_kind() == cpp_class_kind::class_t);
REQUIRE(c.definition());
check_definition(c.definition().value(), "g");
}
else
REQUIRE(false);
}
else if (entity.value().kind() == cpp_entity_kind::class_template_t)
{
auto& c = static_cast<const cpp_class_template&>(entity.value());
if (c.name() == "i")
REQUIRE(c.class_().class_kind() == cpp_class_kind::struct_t);
else
REQUIRE(false);
}
else if (is_function(entity.value().kind()))
{
auto& func = static_cast<const cpp_function_base&>(entity.value());
if (func.name() == "c")
REQUIRE(func.is_declaration());
else if (func.name() == "d")
{
REQUIRE(func.is_declaration());
REQUIRE(func.definition());
check_definition(func.definition().value(), "d");
}
else if (func.name() == "e")
REQUIRE(func.is_definition());
else if (func.name() == "b")
{
REQUIRE(func.semantic_scope() == "b::");
REQUIRE(func.is_declaration());
REQUIRE(func.definition());
check_definition(func.definition().value(), "b::b");
}
else if (func.name() == "f")
{
REQUIRE(func.semantic_scope() == "b::");
REQUIRE(func.is_declaration());
REQUIRE(func.definition());
check_definition(func.definition().value(), "b::f");
}
else if (func.name() == "h2")
{
REQUIRE(func.semantic_scope() == "other_ns::inner::");
REQUIRE(func.is_declaration());
REQUIRE(func.definition());
check_definition(func.definition().value(), "h2");
}
else if (func.name() == "operator int")
{
REQUIRE(func.semantic_scope() == "b::");
REQUIRE(func.is_declaration());
REQUIRE(func.definition());
check_definition(func.definition().value(), "operator int");
}
else
REQUIRE(false);
}
else if (entity.value().kind() == cpp_entity_kind::function_template_t)
{
auto& func = static_cast<const cpp_function_template&>(entity.value());
if (func.name() == "j")
REQUIRE(func.function().is_declaration());
else if (func.name() == "k")
REQUIRE(func.function().is_definition());
else
REQUIRE(false);
}
else if (entity.value().kind() == cpp_entity_kind::function_template_specialization_t)
{
auto& func
= static_cast<const cpp_function_template_specialization&>(entity.value());
if (func.name() == "templ_c")
{
REQUIRE(func.function().is_declaration());
REQUIRE(!func.arguments_exposed());
REQUIRE(func.unexposed_arguments().as_string() == "ns::h");
}
else
REQUIRE(false);
}
else
REQUIRE(false);
}
else if (auto type = f.type())
{
if (type.value().kind() == cpp_type_kind::user_defined_t)
{
auto& user = static_cast<const cpp_user_defined_type&>(type.value());
REQUIRE(!user.entity().is_overloaded());
if (user.entity().name() == "ns::h")
check_definition(user.entity().id()[0u], "h");
else
REQUIRE(false);
}
else if (type.value().kind() == cpp_type_kind::template_parameter_t)
{
auto& param = static_cast<const cpp_template_parameter_type&>(type.value());
REQUIRE(!param.entity().is_overloaded());
if (param.entity().name() == "T")
REQUIRE(
equal_types(idx, param,
*cpp_template_parameter_type::build(
cpp_template_type_parameter_ref(cpp_entity_id(""), "T"))));
else
REQUIRE(false);
}
else if (type.value().kind() == cpp_type_kind::template_instantiation_t)
{
auto& inst = static_cast<const cpp_template_instantiation_type&>(type.value());
cpp_template_instantiation_type::builder builder(
cpp_template_ref(cpp_entity_id(""), inst.primary_template().name()));
builder.add_argument(cpp_template_parameter_type::build(
cpp_template_type_parameter_ref(cpp_entity_id(""), "T")));
if (inst.primary_template().name() == "templ_a")
REQUIRE(equal_types(idx, inst, *builder.finish()));
else if (inst.primary_template().name() == "ns::templ_b")
{
builder.add_argument(
cpp_literal_expression::build(cpp_builtin_type::build(cpp_int), "0"));
builder.add_argument(cpp_template_ref(cpp_entity_id(""), "templ_a"));
REQUIRE(equal_types(idx, inst, *builder.finish()));
}
else
REQUIRE(false);
}
else
REQUIRE(false);
}
else
REQUIRE(false);
});
REQUIRE(count == 18u);
}