-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreprocessor.cpp
428 lines (404 loc) · 18.5 KB
/
Preprocessor.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Neshny {
/*
supported:
#define A B
#define A(x) (x * x)
#define A (without value)
#undef
#ifdef
#else
#elifdef - else defined
#endif
#include
will support:
#ifndef
#elifndef - else not defined
will not support:
#if
*/
////////////////////////////////////////////////////////////////////////////////
bool IsWhiteSpace(char c) {
return (c == ' ') || (c == '\n') || (c == '\r') || (c == '\t');
}
////////////////////////////////////////////////////////////////////////////////
std::string Preprocess(std::string input, const std::function<std::string(std::string_view, std::string&)>& loader, std::string& err_msg) {
std::string output;
output.reserve(input.size());
struct ReplaceWords {
bool operator<(const ReplaceWords& other) const { return p_Word.size() > other.p_Word.size(); }
std::string p_Word;
std::string p_Replace;
int p_NumArgs;
std::vector<std::variant<std::string, int>> p_FunctionPieces;
};
enum class IfDefMode {
PRE_KEEP,
KEEP,
POST_KEEP
};
std::list<ReplaceWords> replacements = {}; // needs to always be sorted from longest to shortest
std::set<std::string> includes = {}; // can only include a file once
bool multi_line_comment = false;
bool line_comment = false;
bool ignore_until_newline = false;
char last_val = 0;
std::stack<IfDefMode> ifdefs;
for (int c = 0; c < input.size(); c++) {
char val = input[c];
if (ignore_until_newline) {
if (val == '\n') {
ignore_until_newline = false;
}
continue;
}
bool remove_code = (!ifdefs.empty()) && (ifdefs.top() != IfDefMode::KEEP);
if ((!remove_code) && (!multi_line_comment) && (!line_comment) && (val == '*') && (last_val == '/')) {
multi_line_comment = true;
} else if ((!remove_code) && multi_line_comment && (val == '/') && (last_val == '*')) {
multi_line_comment = false;
} else if ((!remove_code) && (!multi_line_comment) && (val == '/') && (last_val == '/')) {
line_comment = true;
} else if ((!remove_code) && (val == '\n')) {
line_comment = false;
} else if ((!multi_line_comment) && (!line_comment) && (val == '#')) {
int remaining = input.size() - c;
// #define
if ((!remove_code) && (remaining >= 7) && (input[c + 1] == 'd') && (input[c + 2] == 'e') && (input[c + 3] == 'f') && (input[c + 4] == 'i') && (input[c + 5] == 'n') && (input[c + 6] == 'e')) {
int name_start = -1;
int name_end = -1;
int def_start = -1;
int def_end = -1;
std::vector<std::string> arg_names;
for (int cc = c + 7; cc < input.size(); cc++) {
char curr = input[cc];
bool newline = curr == '\n';
bool whitespace = IsWhiteSpace(curr);
if (newline) {
if ((name_end < 0) && (name_start >= 0)) {
name_end = cc;
} else if ((def_end < 0) && (def_start >= 0)) {
def_end = cc;
}
break;
} else if ((def_start >= 0) && (!whitespace)) {
def_end = cc + 1;
} else if ((name_end >= 0) && (def_start < 0) && !whitespace) {
def_start = cc;
} else if ((name_start >= 0) && (name_end < 0) && (curr == '(')) {
std::string arg_name = "";
name_end = cc;
for (cc = cc + 1; cc < input.size(); cc++) {
curr = input[cc];
if (curr == ')') {
arg_names.push_back(arg_name);
arg_name = "";
break;
} else if(curr == ',') {
arg_names.push_back(arg_name);
arg_name = "";
} else if(curr != ' ') {
arg_name += curr;
}
}
} else if ((name_start >= 0) && (name_end < 0) && whitespace) {
name_end = cc;
} else if((name_start < 0) && !whitespace) {
name_start = cc;
}
}
if (def_end >= 0) {
auto new_name = input.substr(name_start, name_end - name_start);
for (auto iter = replacements.begin(); iter != replacements.end(); ) {
if (iter->p_Word == new_name) {
iter = replacements.erase(iter);
} else {
iter++;
}
}
replacements.push_back({
new_name,
input.substr(def_start, def_end - def_start),
(int)arg_names.size()
});
if (!arg_names.empty()) {
// this formats the function body into something usable
auto& pieces = replacements.back().p_FunctionPieces;
int first_replace_ind = -1;
for (int cc = def_start; cc < def_end; ) {
bool found_any = false;
for (int a = 0; a < arg_names.size(); a++) {
auto& arg = arg_names[a];
bool matches = true;
for (int ac = 0; ac < arg.size(); ac++) {
int in_ind = cc + ac;
if ((input.size() <= in_ind) || (arg[ac] != input[in_ind])) {
matches = false;
break;
}
}
if (matches) {
found_any = true;
if (first_replace_ind >= 0) {
pieces.push_back(input.substr(first_replace_ind, cc - first_replace_ind));
}
pieces.push_back(a);
cc += arg.size();
first_replace_ind = -1;
break;
}
}
if (!found_any) {
if (first_replace_ind < 0) {
first_replace_ind = cc;
}
cc++;
}
}
if (first_replace_ind >= 0) {
pieces.push_back(input.substr(first_replace_ind, def_end - first_replace_ind));
}
}
ignore_until_newline = true;
c = def_end - 1;
replacements.sort();
continue;
} else if (name_end >= 0) {
replacements.push_back({
input.substr(name_start, name_end - name_start),
std::string(),
0
});
ignore_until_newline = true;
c = name_end - 1;
replacements.sort();
continue;
}
replacements.sort();
// #undef
} else if ((!remove_code) && (remaining >= 6) && (input[c + 1] == 'u') && (input[c + 2] == 'n') && (input[c + 3] == 'd') && (input[c + 4] == 'e') && (input[c + 5] == 'f')) {
int name_start = -1;
int name_end = -1;
for (int cc = c + 7; cc < input.size(); cc++) {
char curr = input[cc];
bool whitespace = IsWhiteSpace(curr);
if ((name_start >= 0) && (name_end < 0) && whitespace) {
name_end = cc;
break;
} else if((name_start < 0) && !whitespace) {
name_start = cc;
}
}
if (name_end >= 0) {
auto name = input.substr(name_start, name_end - name_start);
bool removed = false;
for (auto iter = replacements.begin(); iter != replacements.end(); iter++) {
if (iter->p_Word == name) {
replacements.erase(iter);
c = name_end - 1;
removed = true;
ignore_until_newline = true;
break;
}
}
if (removed) {
continue;
}
}
// #ifdef
} else if ((!remove_code) && (remaining >= 6) && (input[c + 1] == 'i') && (input[c + 2] == 'f') && (input[c + 3] == 'd') && (input[c + 4] == 'e') && (input[c + 5] == 'f')) {
ignore_until_newline = true;
int name_start = -1;
int name_end = -1;
for (int cc = c + 6; cc < input.size(); cc++) {
char curr = input[cc];
bool whitespace = IsWhiteSpace(curr);
if ((name_start >= 0) && (name_end < 0) && whitespace) {
name_end = cc;
break;
}
else if ((name_start < 0) && !whitespace) {
name_start = cc;
}
}
if (name_end >= 0) {
auto name = input.substr(name_start, name_end - name_start);
bool found = false;
for (auto& replace : replacements) {
if (replace.p_Word == name) {
found = true;
break;
}
}
ifdefs.push(found ? IfDefMode::KEEP : IfDefMode::PRE_KEEP);
}
continue;
// #elifdef
} else if ((!ifdefs.empty()) && (remaining >= 8) && (input[c + 1] == 'e') && (input[c + 2] == 'l') && (input[c + 3] == 'i') && (input[c + 4] == 'f') && (input[c + 5] == 'd') && (input[c + 6] == 'e') && (input[c + 7] == 'f')) {
ignore_until_newline = true;
if (ifdefs.top() == IfDefMode::POST_KEEP) {
continue;
}
if (ifdefs.top() == IfDefMode::KEEP) {
ifdefs.pop();
ifdefs.push(IfDefMode::POST_KEEP);
continue;
}
int name_start = -1;
int name_end = -1;
for (int cc = c + 8; cc < input.size(); cc++) {
char curr = input[cc];
bool whitespace = IsWhiteSpace(curr);
if ((name_start >= 0) && (name_end < 0) && whitespace) {
name_end = cc;
break;
}
else if ((name_start < 0) && !whitespace) {
name_start = cc;
}
}
if (name_end >= 0) {
auto name = input.substr(name_start, name_end - name_start);
for (auto& replace : replacements) {
if (replace.p_Word == name) {
ifdefs.pop();
ifdefs.push(IfDefMode::KEEP);
break;
}
}
}
continue;
// #else
} else if ((!ifdefs.empty()) && (remaining >= 5) && (input[c + 1] == 'e') && (input[c + 2] == 'l') && (input[c + 3] == 's') && (input[c + 4] == 'e')) {
ignore_until_newline = true;
if (ifdefs.top() == IfDefMode::POST_KEEP) {
continue;
}
auto new_mode = ifdefs.top() == IfDefMode::KEEP ? IfDefMode::POST_KEEP : IfDefMode::KEEP;
ifdefs.pop();
ifdefs.push(new_mode);
continue;
// #endif
} else if ((!ifdefs.empty()) && (remaining >= 6) && (input[c + 1] == 'e') && (input[c + 2] == 'n') && (input[c + 3] == 'd') && (input[c + 4] == 'i') && (input[c + 5] == 'f')) {
ifdefs.pop();
ignore_until_newline = true;
continue;
// #include
} else if ((!remove_code) && (remaining >= 8) && (input[c + 1] == 'i') && (input[c + 2] == 'n') && (input[c + 3] == 'c') && (input[c + 4] == 'l') && (input[c + 5] == 'u') && (input[c + 6] == 'd') && (input[c + 7] == 'e')) {
int fname_start = -1;
int fname_end = -1;
for (int cc = c + 8; cc < input.size(); cc++) {
char curr = input[cc];
bool is_quote = curr == '"';
bool endline = (curr == '\n') || (curr == '\r');
if (endline) {
break;
} else if ((fname_start >= 0) && (fname_end < 0) && is_quote) {
fname_end = cc;
break;
} else if ((fname_start < 0) && is_quote) {
fname_start = cc + 1;
}
}
ignore_until_newline = true;
if (fname_end >= 0) {
auto fname = input.substr(fname_start, fname_end - fname_start);
if (includes.find(fname) != includes.end()) {
continue;
}
includes.insert(fname);
std::string error;
std::string fname_str(fname.data(), fname.size());
auto included_data = loader(fname_str, error);
if (!included_data.empty()) {
// modifies local copy of input so preprocessor can process args as well
input.replace(c, fname_end - c + 1, included_data);
ignore_until_newline = false;
c--;
continue;
}
output += std::format("#error file \"{}\" not found", fname);
ignore_until_newline = false;
c--;
continue;
}
}
} else if ((!remove_code) && (!multi_line_comment) && (!line_comment)) {
bool found = false;
for (const auto& replace : replacements) {
bool match = true;
for (int i = 0; i < replace.p_Word.size(); i++) {
if (replace.p_Word[i] != input[c + i]) {
match = false;
break;
}
}
if (match) {
if (replace.p_NumArgs > 0) {
int parenthesis = 0;
std::vector<std::string> args;
int arg_start = -1;
int arg_end = -1;
for (int cc = c + replace.p_Word.size(); cc < input.size(); cc++) {
char curr = input[cc];
if (curr == '(') {
parenthesis++;
} else if (curr == ')') {
parenthesis--;
if (parenthesis == 0) {
if ((arg_start >= 0) && (arg_end >= 0)) {
args.push_back(input.substr(arg_start, arg_end - arg_start));
}
arg_end = cc;
break;
}
arg_end = cc + 1;
} else if (curr == ',') {
if ((arg_start >= 0) && (arg_end >= 0)) {
args.push_back(input.substr(arg_start, arg_end - arg_start));
}
arg_start = -1;
arg_end = -1;
} else if (curr != ' ') {
if (arg_start < 0) {
arg_start = cc;
}
arg_end = cc + 1;
}
}
if (replace.p_NumArgs != args.size()) {
err_msg = "Incorrect number of args for macro " + replace.p_Word;
return std::string();
}
std::string replacement;
for (const auto& piece : replace.p_FunctionPieces) {
if (std::holds_alternative<int>(piece)) {
replacement += args[std::get<int>(piece)];
} else {
replacement += std::get<std::string>(piece);
}
}
// modifies local copy of input so preprocessor can process args as well
input.replace(c, arg_end - c + 1, replacement);
c--;
} else if (!replace.p_Replace.empty()) {
output += replace.p_Replace;
c += replace.p_Word.size() - 1;
}
found = true;
break;
}
}
if (found) {
continue;
}
}
if (remove_code) {
continue;
}
output += val;
last_val = val;
}
return output;
}
}