forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_variable.cpp
More file actions
264 lines (244 loc) · 11.1 KB
/
Copy pathcpp_variable.cpp
File metadata and controls
264 lines (244 loc) · 11.1 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
// 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/cpp_variable.hpp>
#include <cppast/cpp_array_type.hpp>
#include <cppast/cpp_decltype_type.hpp>
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("cpp_variable")
{
auto code = R"(
// basic
/// int a;
int a;
/// unsigned long long b=42;
unsigned long long b = 42;
/// float c=3.f+0.14f;
float c = 3.f + 0.14f;
// with storage class specifiers
/// extern int d;
extern int d; // actually declaration
/// static int e;
static int e;
/// thread_local int f;
thread_local int f;
/// static thread_local int g;
thread_local static int g;
// constexpr
/// constexpr int const h=12;
constexpr int h = 12;
// inline definition
struct foo {} i;
const struct bar {} j = bar();
struct baz {} k{};
static struct {} l;
// auto
/// auto m=128;
auto m = 128;
/// auto const& n=m;
const auto& n = m;
// decltype
/// decltype(0) o;
decltype(0) o;
/// decltype(o) const& p=o;
const decltype(o)& p = o;
// array
/// int q[42];
int q[42];
/// int r[1]={0};
int r[] = {0};
)";
cpp_entity_index idx;
auto check_variable = [&](const cpp_variable& var, const cpp_type& type,
type_safe::optional_ref<const cpp_expression> default_value,
int storage_class, bool is_constexpr, bool is_declaration) {
if (is_declaration)
{
REQUIRE(var.is_declaration());
REQUIRE(!var.is_definition());
}
else
{
REQUIRE(var.is_definition());
REQUIRE(!var.is_declaration());
}
REQUIRE(equal_types(idx, var.type(), type));
if (var.default_value())
{
REQUIRE(default_value);
INFO(get_code(var));
REQUIRE(equal_expressions(var.default_value().value(), default_value.value()));
}
else
REQUIRE(!default_value);
REQUIRE(var.storage_class() == storage_class);
REQUIRE(var.is_constexpr() == is_constexpr);
};
auto file = parse(idx, "cpp_variable.cpp", code);
auto int_type = cpp_builtin_type::build(cpp_int);
auto count = test_visit<cpp_variable>(*file, [&](const cpp_variable& var) {
if (var.name() == "a")
check_variable(var, *int_type, nullptr, cpp_storage_class_none, false, false);
else if (var.name() == "b")
check_variable(var, *cpp_builtin_type::build(cpp_ulonglong),
// unexposed due to implicit cast, I think
type_safe::ref(
*cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int),
cpp_token_string::tokenize("42"))),
cpp_storage_class_none, false, false);
else if (var.name() == "c")
check_variable(var, *cpp_builtin_type::build(cpp_float),
type_safe::ref(
*cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_float),
cpp_token_string::tokenize(
"3.f+0.14f"))),
cpp_storage_class_none, false, false);
else if (var.name() == "d")
check_variable(var, *int_type, nullptr, cpp_storage_class_extern, false, true);
else if (var.name() == "e")
check_variable(var, *int_type, nullptr, cpp_storage_class_static, false, false);
else if (var.name() == "f")
check_variable(var, *int_type, nullptr, cpp_storage_class_thread_local, false, false);
else if (var.name() == "g")
check_variable(var, *int_type, nullptr,
cpp_storage_class_static | cpp_storage_class_thread_local, false, false);
else if (var.name() == "h")
check_variable(var,
*cpp_cv_qualified_type::build(cpp_builtin_type::build(cpp_int),
cpp_cv_const),
type_safe::ref(
*cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int),
cpp_token_string::tokenize("12"))),
cpp_storage_class_none, true, false);
else if (var.name() == "i")
{
check_variable(var,
*cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "foo")),
nullptr, cpp_storage_class_none, false, false);
return false; // can't check code here
}
else if (var.name() == "j")
{
check_variable(var,
*cpp_cv_qualified_type::build(cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""),
"bar")),
cpp_cv_const),
type_safe::ref(
*cpp_unexposed_expression::build(cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""),
"bar")),
cpp_token_string::tokenize(
"bar()"))),
cpp_storage_class_none, false, false);
return false;
}
else if (var.name() == "k")
{
check_variable(var,
*cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "baz")),
nullptr, cpp_storage_class_none, false, false);
return false;
}
else if (var.name() == "l")
{
check_variable(var, *cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "")),
nullptr, cpp_storage_class_static, false, false);
return false;
}
else if (var.name() == "m")
check_variable(var, *cpp_auto_type::build(),
type_safe::ref(
*cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int),
cpp_token_string::tokenize("128"))),
cpp_storage_class_none, false, false);
else if (var.name() == "n")
check_variable(var,
*cpp_reference_type::
build(cpp_cv_qualified_type::build(cpp_auto_type::build(),
cpp_cv_const),
cpp_ref_lvalue),
type_safe::ref(
*cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int),
cpp_token_string::tokenize("m"))),
cpp_storage_class_none, false, false);
else if (var.name() == "o")
check_variable(var,
*cpp_decltype_type::build(
cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int),
cpp_token_string::tokenize("0"))),
nullptr, cpp_storage_class_none, false, false);
else if (var.name() == "p")
check_variable(var,
*cpp_reference_type::
build(cpp_cv_qualified_type::
build(cpp_decltype_type::build(
cpp_unexposed_expression::
build(cpp_builtin_type::build(cpp_int),
cpp_token_string::tokenize("o"))),
cpp_cv_const),
cpp_ref_lvalue),
type_safe::ref(
*cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int),
cpp_token_string::tokenize("o"))),
cpp_storage_class_none, false, false);
else if (var.name() == "q")
check_variable(var,
*cpp_array_type::build(cpp_builtin_type::build(cpp_int),
cpp_literal_expression::
build(cpp_builtin_type::build(cpp_ulonglong),
"42")),
nullptr, cpp_storage_class_none, false, false);
else if (var.name() == "r")
check_variable(var,
*cpp_array_type::build(cpp_builtin_type::build(cpp_int),
cpp_literal_expression::
build(cpp_builtin_type::build(cpp_ulonglong),
"1")),
type_safe::ref(
*cpp_unexposed_expression::build(cpp_unexposed_type::build(""),
cpp_token_string::tokenize("{0}"))),
cpp_storage_class_none, false, false);
else
REQUIRE(false);
return true;
});
REQUIRE(count == 18u);
}
TEST_CASE("static cpp_variable")
{
auto code = R"(
struct test
{
/// static int a;
static int a;
/// static int const b;
static const int b;
/// static thread_local int c;
static thread_local int c;
};
)";
auto file = parse({}, "static_cpp_variable.cpp", code);
auto count = test_visit<cpp_variable>(*file, [&](const cpp_variable& var) {
REQUIRE(var.is_declaration());
REQUIRE(!var.is_definition());
REQUIRE(!var.default_value());
REQUIRE(!var.is_constexpr());
REQUIRE(is_static(var.storage_class()));
if (var.name() == "a")
REQUIRE(equal_types({}, var.type(), *cpp_builtin_type::build(cpp_int)));
else if (var.name() == "b")
REQUIRE(equal_types({}, var.type(),
*cpp_cv_qualified_type::build(cpp_builtin_type::build(cpp_int),
cpp_cv_const)));
else if (var.name() == "c")
{
REQUIRE(equal_types({}, var.type(), *cpp_builtin_type::build(cpp_int)));
REQUIRE(is_thread_local(var.storage_class()));
}
else
REQUIRE(false);
});
REQUIRE(count == 3u);
}