forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor.cpp
More file actions
313 lines (250 loc) · 6.26 KB
/
Copy pathpreprocessor.cpp
File metadata and controls
313 lines (250 loc) · 6.26 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <catch.hpp>
#include <fstream>
#include "libclang/preprocessor.hpp"
#include "test_parser.hpp"
#include <cppast/cpp_variable.hpp>
using namespace cppast;
TEST_CASE("preprocessor escaped character", "[!hide][clang4]")
{
write_file("ppec.hpp", R"(
)");
// This is an actual macro from the rapidjson source (reader.h)
write_file("ppec.cpp", R"(
#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
static const char escape[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/',
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0,
0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0,
0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
#undef Z16
#include "ppec.hpp"
)");
libclang_compile_config config;
config.set_flags(cpp_standard::cpp_latest);
SECTION("fast")
{
config.fast_preprocessing(true);
}
SECTION("normal")
{
config.fast_preprocessing(false);
}
auto preprocessed = detail::preprocess(config, "ppec.cpp", default_logger().get());
REQUIRE(preprocessed.includes.size() == 1);
REQUIRE(preprocessed.includes[0].file_name == "ppec.hpp");
}
TEST_CASE("preprocessing use external macro")
{
bool fast_preprocessing = false;
SECTION("fast_preprocessing")
{
fast_preprocessing = true;
}
SECTION("normal")
{
fast_preprocessing = false;
}
auto file = parse({}, "preprocessing_external_macro.cpp", R"(
#include <cmath>
#ifdef _GLIBCXX_RELEASE
// this requires libstdc++
/// auto result=(__builtin_nanf(""));
auto result = NAN;
#endif
)",
fast_preprocessing);
test_visit<cpp_variable>(*file, [&](const cpp_variable&) {});
}
TEST_CASE("fast_preprocessing include guard")
{
auto file_name = "fast_preprocessing_include_guard.hpp";
write_file(file_name, R"(
// This is a C++ comment that should get skipped.
/// This as well.
/* C style comments
#ifndef FALSE_POSITIVE
#define FALSE_POSITIVE
*/ #ifndef INCLUDE_GUARD // the include guard macro
#define INCLUDE_GUARD
struct foo {};
#endif
)");
libclang_compile_config config;
config.set_flags(cpp_standard::cpp_latest);
config.fast_preprocessing(true);
try
{
auto result = detail::preprocess(config, file_name, default_logger().get());
REQUIRE(result.macros.size() == 1u);
REQUIRE(result.macros[0].macro->name() == "INCLUDE_GUARD");
}
catch (libclang_error& ex)
{
FAIL(ex.what());
}
}
TEST_CASE("preprocessor line numbers")
{
bool fast_preprocessing = false;
SECTION("fast_preprocessing")
{
fast_preprocessing = true;
}
SECTION("normal")
{
fast_preprocessing = false;
}
auto code = R"(/// 1
#include <iostream>
/// 5
#include <string>
int var;
/// 11
#define foo \
\
\
int \
main()
/// 19
foo {}
/// 23
/* C comment
spanning
multiple
lines
*/
/**
*/
#include <vector>
/// 37
)";
auto file = parse({}, "preprocessor_line_numbers.cpp", code, fast_preprocessing);
for (auto& comment : file->unmatched_comments())
{
if (comment.content[0] != '\n')
REQUIRE(comment.line == std::stoi(comment.content));
}
REQUIRE((file->unmatched_comments().size() == 6u + 1u));
}
TEST_CASE("comment content")
{
auto code = R"(
/// simple comment
///no space
/// multi
/// line
/// comment
/** C comment */
/**C comment no space*/
/** Multiline
C
comment
with indent */
/** Multiline
C
comment
with
indent */
/** Multiline
* C
* comment
* with
* indent
* star */
)";
auto file = parse({}, "comment-content.cpp", code);
auto comments = file->unmatched_comments();
REQUIRE((comments.size() == 8u));
REQUIRE(comments[0u].content == "simple comment");
REQUIRE(comments[1u].content == "no space");
REQUIRE(comments[2u].content == "multi\nline\ncomment");
REQUIRE(comments[3u].content == "C comment");
REQUIRE(comments[4u].content == "C comment no space");
REQUIRE(comments[5u].content == "Multiline\nC\n comment\nwith indent");
REQUIRE(comments[6u].content == "Multiline\nC\n comment\n with\n indent");
REQUIRE(comments[7u].content == "Multiline\nC\ncomment\nwith\nindent\nstar");
}
TEST_CASE("comment matching")
{
bool fast_preprocessing = false;
SECTION("fast_preprocessing")
{
fast_preprocessing = true;
}
SECTION("normal")
{
fast_preprocessing = false;
}
auto code = R"(
/// u
/// a
/// a
struct a {};
/// u
/** b
* b */
void b(int, float)
{
auto c = '#';
}
/** u */
//! c
/// c
enum class c
{
d, //< d
/// d
e, //< e
/// e
/** f
f **/
f,
};
/// g
/// g
#define g(name) \
class name \
{ \
};
/// h
/// h
g(h)
/*! i
i */
using i = int;
/// cstddef
/// cstddef
#include <cstddef>
/// j
/// j
template <typename T/**/>
void j();
)";
auto file = parse({}, "comment-matching.cpp", code, fast_preprocessing);
visit(*file, [&](const cpp_entity& e, visitor_info) {
if (e.kind() == cpp_entity_kind::file_t)
return true;
else if (e.name().empty())
return true;
else if (is_templated(e))
return true;
INFO(e.name());
REQUIRE(e.comment());
REQUIRE(e.comment().value() == e.name() + "\n" + e.name());
return true;
});
auto add = 0u;
for (auto& comment : file->unmatched_comments())
{
if (comment.content == "cstddef\ncstddef")
// happens if include parsing is not supported
// error is still going to be detected because if it is supported, the entity will be
// matched above
add = 1u;
else
REQUIRE(comment.content == "u");
}
REQUIRE((file->unmatched_comments().size() == 3u + add));
}