-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocesser.c
1035 lines (911 loc) · 25.6 KB
/
preprocesser.c
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "thrvcc.h"
struct MacroParam {
struct MacroParam *next;
char *name;
};
struct MacroArg {
struct MacroArg *next;
char *name;
struct Token *token;
};
// macro process handler
typedef struct Token *MacroHandlerFn(struct Token *);
// maceo variable
struct Macro {
struct Macro *next;
char *name;
bool is_obj_like; // macro var->true, macro func->false
struct MacroParam *params;
struct Token *body;
bool deleted;
MacroHandlerFn *handler;
};
// #if can be nested, so use the stack to hold nested #if
struct CondIncl {
struct CondIncl *next;
enum { IN_THEN, IN_ELIF, IN_ELSE } ctx; // type
struct Token *token;
bool included;
};
// global Macro stack
static struct Macro *Macros;
// global #if stack
static struct CondIncl *CondIncls;
static struct Macro *find_macro(struct Token *token);
static struct Token *preprocess(struct Token *token);
// if # at the begin of line
static bool is_begin_hash(struct Token *token)
{
return token->at_bol && equal(token, "#");
}
// some preprocesser allow directives like #include to \
// have redundant terminators before line breaks
// skip those terminators
static struct Token *skip_line(struct Token *token)
{
if (token->at_bol)
return token;
warn_token(token, "extra token");
while (!token->at_bol)
token = token->next;
return token;
}
static struct Token *copy_token(struct Token *token)
{
struct Token *t = calloc(1, sizeof(struct Token));
*t = *token;
t->next = NULL;
return t;
}
static struct Token *new_eof(struct Token *token)
{
struct Token *t = copy_token(token);
t->kind = TK_EOF;
t->len = 0;
return t;
}
static struct HideSet *new_hideset(char *name)
{
struct HideSet *hs = calloc(1, sizeof(struct HideSet));
hs->name = name;
return hs;
}
static struct HideSet *hideset_union(struct HideSet *hs1, struct HideSet *hs2)
{
struct HideSet head = {};
struct HideSet *cur = &head;
for (; hs1; hs1 = hs1->next)
cur = cur->next = new_hideset(hs1->name);
cur->next = hs2;
return head.next;
}
// if already in hideset
static bool hideset_contains(struct HideSet *hs, char *s, int len)
{
for (; hs; hs = hs->next)
if (strlen(hs->name) == len && !strncmp(hs->name, s, len))
return true;
return false;
}
static struct HideSet *hideset_intersection(struct HideSet *hs1,
struct HideSet *hs2)
{
struct HideSet head = {};
struct HideSet *cur = &head;
for (; hs1; hs1 = hs1->next)
if (hideset_contains(hs2, hs1->name, strlen(hs1->name)))
cur = cur->next = new_hideset(hs1->name);
return head.next;
}
// iterate all terminators after token, assign hs for every terminator
static struct Token *add_hideset(struct Token *token, struct HideSet *hs)
{
struct Token head = {};
struct Token *cur = &head;
for (; token; token = token->next) {
struct Token *t = copy_token(token);
t->hideset = hideset_union(t->hideset, hs);
cur = cur->next = t;
}
return head.next;
}
static struct Token *append(struct Token *token1, struct Token *token2)
{
if (token1->kind == TK_EOF)
return token2;
struct Token head = {};
struct Token *cur = &head;
for (; token1->kind != TK_EOF; token1 = token1->next)
cur = cur->next = copy_token(token1);
cur->next = token2;
return head.next;
}
// skip #if and #endif
static struct Token *skip_condincl2(struct Token *token)
{
while (token->kind != TK_EOF) {
if (is_begin_hash(token) &&
(equal(token->next, "if") || equal(token->next, "ifdef") ||
equal(token->next, "ifndef"))) {
token = skip_condincl2(token->next->next);
continue;
}
if (is_begin_hash(token) && equal(token->next, "endif"))
return token->next->next;
token = token->next;
}
return token;
}
// if #if is empty, skip to #endif
// skip nest #if too
static struct Token *skip_condincl(struct Token *token)
{
while (token->kind != TK_EOF) {
// skip #if
if (is_begin_hash(token) &&
(equal(token->next, "if") || equal(token->next, "ifdef") ||
equal(token->next, "ifndef"))) {
token = skip_condincl2(token->next->next);
continue;
}
// #else #endif
if (is_begin_hash(token) &&
(equal(token->next, "elif") || equal(token->next, "else") ||
equal(token->next, "endif")))
break;
token = token->next;
}
return token;
}
static char *quote_string(char *str)
{
int buf_size = 3;
// if have \ or ", then need one more space to store escape sign
for (int i = 0; str[i]; i++) {
if (str[i] == '\\' || str[i] == '"')
buf_size++;
buf_size++;
}
char *buf = calloc(1, buf_size);
char *p = buf;
// " at begin
*p++ = '"';
for (int i = 0; str[i]; i++) {
if (str[i] == '\\' || str[i] == '"')
*p++ = '\\';
*p++ = str[i];
}
// "\0 at end
*p++ = '"';
*p++ = '\0';
return buf;
}
// construct a new str terminator
static struct Token *new_str_token(char *str, struct Token *tmpl)
{
char *buf = quote_string(str);
// pass the string and the corresponding macro name into
// the lexical analysis to parse it
return lexer(new_file(tmpl->file->name, tmpl->file->file_no, buf));
}
static struct Token *copy_line(struct Token **rest, struct Token *token)
{
struct Token head = {};
struct Token *cur = &head;
// iterate and copy terminators
for (; !token->at_bol; token = token->next)
cur = cur->next = copy_token(token);
cur->next = new_eof(token);
*rest = token;
return head.next;
}
static struct Token *new_num_token(int val, struct Token *tmpl)
{
char *buf = format("%d\n", val);
return lexer(new_file(tmpl->file->name, tmpl->file->file_no, buf));
}
static struct Token *read_const_expr(struct Token **rest, struct Token *token)
{
token = copy_line(rest, token);
struct Token head = {};
struct Token *cur = &head;
while (token->kind != TK_EOF) {
// "defined(foo)" or "defined foo", if foo exists, 1 ,else 0.
if (equal(token, "defined")) {
struct Token *start = token;
// consume (
bool has_paren = consume(&token, token->next, "(");
if (token->kind != TK_IDENT)
error_token(start,
"macro name must be an identifier");
struct Macro *m = find_macro(token);
token = token->next;
if (has_paren)
token = skip(token, ")");
cur = cur->next = new_num_token(m ? 1 : 0, start);
continue;
}
cur = cur->next = token;
token = token->next;
}
cur->next = token;
return head.next;
}
// read and evaluate constant expressions.
static long eval_constexpr(struct Token **rest, struct Token *token)
{
struct Token *start = token;
struct Token *expr = read_const_expr(rest, token->next);
// parse Macro var
expr = preprocess(expr);
// if empty directive, error out
if (expr->kind == TK_EOF)
error_token(start, "no expression");
// replace legacy identifiers with 0 before evaluating constant expressions
for (struct Token *t = expr; t->kind != TK_EOF; t = t->next) {
if (t->kind == TK_IDENT) {
struct Token *next = t->next;
*t = *new_num_token(0, t);
t->next = next;
}
}
// evaluate value of constexpr
struct Token *rest2;
long val = const_expr(&rest2, expr);
// if have redundant terminators, error out
if (rest2->kind != TK_EOF)
error_token(rest2, "extra token");
return val;
}
static struct CondIncl *push_condincl(struct Token *token, bool included)
{
struct CondIncl *ci = calloc(1, sizeof(struct CondIncl));
ci->next = CondIncls;
ci->ctx = IN_THEN;
ci->token = token;
ci->included = included;
CondIncls = ci;
return ci;
}
static struct Macro *find_macro(struct Token *token)
{
// if not identifier, error
if (token->kind != TK_IDENT)
return NULL;
// iterate Macro stack, if match return matched marco value
for (struct Macro *m = Macros; m; m = m->next)
if (strlen(m->name) == token->len &&
!strncmp(m->name, token->location, token->len))
return m->deleted ? NULL : m;
return NULL;
}
static struct Macro *push_macro(char *name, bool is_obj_like,
struct Token *body)
{
struct Macro *m = calloc(1, sizeof(struct Macro));
m->next = Macros;
m->name = name;
m->is_obj_like = is_obj_like;
m->body = body;
Macros = m;
return m;
}
static struct MacroParam *read_macro_params(struct Token **rest,
struct Token *token)
{
struct MacroParam head = {};
struct MacroParam *cur = &head;
while (!equal(token, ")")) {
if (cur != &head)
token = skip(token, ",");
if (token->kind != TK_IDENT)
error_token(token, "expected an identifier");
struct MacroParam *m = calloc(1, sizeof(struct MacroParam));
m->name = strndup(token->location, token->len);
cur = cur->next = m;
token = token->next;
}
*rest = token->next;
return head.next;
}
static void read_macro_def(struct Token **rest, struct Token *token)
{
if (token->kind != TK_IDENT)
error_token(token, "marco name must be an identifier");
char *name = strndup(token->location, token->len);
token = token->next;
// determine if macro var of macro func.
// macro func if there no space before ()
if (!token->has_space && equal(token, "(")) {
// construct params
struct MacroParam *params =
read_macro_params(&token, token->next);
// push macro func
struct Macro *m =
push_macro(name, false, copy_line(rest, token));
m->params = params;
} else {
push_macro(name, true, copy_line(rest, token));
}
}
static struct MacroArg *read_one_macro_arg(struct Token **rest,
struct Token *token)
{
struct Token head = {};
struct Token *cur = &head;
int level = 0;
while (level > 0 || !equal(token, ",") && !equal(token, ")")) {
if (token->kind == TK_EOF)
error_token(token, "premature end of input");
if (equal(token, "("))
level++;
else if (equal(token, ")"))
level--;
cur = cur->next = copy_token(token);
token = token->next;
}
cur->next = new_eof(token);
struct MacroArg *arg = calloc(1, sizeof(struct MacroArg));
arg->token = head.next;
*rest = token;
return arg;
}
static struct MacroArg *read_macro_args(struct Token **rest,
struct Token *token,
struct MacroParam *params)
{
struct Token *start = token;
token = token->next->next;
struct MacroArg head = {};
struct MacroArg *cur = &head;
struct MacroParam *mp = params;
for (; mp; mp = mp->next) {
if (cur != &head)
token = skip(token, ",");
cur = cur->next = read_one_macro_arg(&token, token);
cur->name = mp->name;
}
// if left, error
if (mp)
error_token(start, "too many arguments");
skip(token, ")");
// return )
*rest = token;
return head.next;
}
static struct MacroArg *find_macro_arg(struct MacroArg *args,
struct Token *token)
{
for (struct MacroArg *ma = args; ma; ma = ma->next)
if (token->len == strlen(ma->name) &&
!strncmp(token->location, ma->name, token->len))
return ma;
return NULL;
}
// concatenates all the terminators in the terminator chain table
// and returns a new string
static char *join_tokens(struct Token *token, struct Token *end)
{
// calculate the length of the final terminator
int len = 1;
for (struct Token *t = token; t != end && t->kind != TK_EOF;
t = t->next) {
// not the first, and preceded by a space, \
// the count is increased by one
if (t != token && t->has_space)
len++;
len += t->len;
}
char *buf = calloc(1, len);
// copy
int pos = 0;
for (struct Token *t = token; t != end && t->kind != TK_EOF;
t = t->next) {
// not the first, and preceded by a space, \
// set space
if (t != token && t->has_space)
buf[pos++] = ' ';
strncpy(buf + pos, t->location, t->len);
pos += t->len;
}
// end with '\0'
buf[pos] = '\0';
return buf;
}
// concatenates the terminators in all args and \
// returns a string terminator
static struct Token *stringize(struct Token *hash, struct Token *arg)
{
char *s = join_tokens(arg, NULL);
return new_str_token(s, hash);
}
// splicing two terminators to build a new one
static struct Token *splice_terminators(struct Token *lhs, struct Token *rhs)
{
// splice
char *buf = format("%.*s%.*s", lhs->len, lhs->location, rhs->len,
rhs->location);
// lex buf, get token stream
struct Token *token =
lexer(new_file(lhs->file->name, lhs->file->file_no, buf));
if (token->next->kind != TK_EOF)
error_token(lhs, "splicing froms '%s', an invalid token", buf);
return token;
}
// replace macro params with args
static struct Token *subst(struct Token *token, struct MacroArg *args)
{
struct Token head = {};
struct Token *cur = &head;
while (token->kind != TK_EOF) {
// #macro arg, replace with corresponding str
if (equal(token, "#")) {
struct MacroArg *arg =
find_macro_arg(args, token->next);
if (!arg)
error_token(
token->next,
"'#' is not followed by a macro parameter");
// stringize
cur = cur->next = stringize(token, arg->token);
token = token->next->next;
continue;
}
// ##__, used for splice terminators
if (equal(token, "##")) {
if (cur == &head)
error_token(
token,
"'##' cannot appear at start of macro expansion");
if (token->next->kind == TK_EOF)
error_token(
token,
"'##' cannot appear at end of macro expansion");
// search next terminator
// if (##__) macro args
struct MacroArg *arg =
find_macro_arg(args, token->next);
if (arg) {
if (arg->token->kind != TK_EOF) {
// splice
*cur = *splice_terminators(cur,
arg->token);
// the left terminators after splice
for (struct Token *t = arg->token->next;
t->kind != TK_EOF; t = t->next)
cur = cur->next = copy_token(t);
}
token = token->next->next;
continue;
}
// if not (##__) macro args, splice direct splicing
*cur = *splice_terminators(cur, token->next);
token = token->next->next;
continue;
}
struct MacroArg *arg = find_macro_arg(args, token);
// __##, used for splice terminator
if (arg && equal(token->next, "##")) {
// read ##'s right terminators
struct Token *rhs = token->next->next;
// case ## left args is empty
if (arg->token->kind == TK_EOF) {
// search ## right args
struct MacroArg *arg2 =
find_macro_arg(args, rhs);
if (arg2) {
// if args
for (struct Token *t = arg2->token;
t->kind != TK_EOF; t = t->next)
cur = cur->next = copy_token(t);
} else {
// if not args
cur = cur->next = copy_token(rhs);
}
token = rhs->next;
continue;
}
// case ## left args not empty
for (struct Token *t = arg->token; t->kind != TK_EOF;
t = t->next)
cur = cur->next = copy_token(t);
token = token->next;
continue;
}
//process macro terminator
if (arg) {
struct Token *t = preprocess(arg->token);
// pass AtBOL and HasSpace
t->at_bol = token->at_bol;
t->has_space = token->has_space;
for (; t->kind != TK_EOF; t = t->next)
cur = cur->next = copy_token(t);
token = token->next;
continue;
}
// process other terminator
cur = cur->next = copy_token(token);
token = token->next;
continue;
}
cur->next = token;
return head.next;
}
// if Macro var and expand success, return true
static bool expand_macro(struct Token **rest, struct Token *token)
{
// determined if in hideset
if (hideset_contains(token->hideset, token->location, token->len))
return false;
// determined if macro var
struct Macro *m = find_macro(token);
if (!m)
return false;
// if macro set its own process function(handler), such as __LINE__
if (m->handler) {
*rest = m->handler(token);
(*rest)->next = token->next;
return true;
}
// if macro var
if (m->is_obj_like) {
// macro expand once, add it into hideset
struct HideSet *hs =
hideset_union(token->hideset, new_hideset(m->name));
// after process this macro var, pass hidset to terminators behind
struct Token *body = add_hideset(m->body, hs);
// reg macros before expanded
for (struct Token *t = body; t->kind != TK_EOF; t = t->next)
t->origin = token;
*rest = append(body, token->next);
// pass at_bol and has_space
(*rest)->at_bol = token->at_bol;
(*rest)->has_space = token->has_space;
return true;
}
// if there no args list behind macro func, process it as normal identifier
if (!equal(token->next, "("))
return false;
// process macro func, and link after token
// read macro func, here macro func's hideset
struct Token *macro_token = token;
struct MacroArg *args = read_macro_args(&token, token, m->params);
// here return ), here macro arg's hideset
struct Token *r_paren = token;
// macro functions may have different hidden sets between them, \
// and the new terminator would not know which hidden set to use.
// we take the intersection of the macro terminator and the right bracket \
// and use it as the new hidden set.
struct HideSet *hs =
hideset_intersection(macro_token->hideset, r_paren->hideset);
// add cur func into hideset
hs = hideset_union(hs, new_hideset(m->name));
// replace macro func params with args
struct Token *body = subst(m->body, args);
// set macro func inner hideset
body = add_hideset(body, hs);
// reg macro func before macro expanded
for (struct Token *t = body; t->kind != TK_EOF; t = t->next)
t->origin = macro_token;
// append
*rest = append(body, token->next);
// pass at_bol and has_space
(*rest)->at_bol = macro_token->at_bol;
(*rest)->has_space = macro_token->has_space;
return true;
}
static char *search_include_paths(char *filename)
{
// absolute path
if (filename[0] == '/')
return filename;
// search
for (int i = 0; i < IncludePaths.len; i++) {
char *path = format("%s/%s", IncludePaths.data[i], filename);
if (file_exists(path))
return path;
}
return NULL;
}
// read #include args
static char *read_include_filename(struct Token **rest, struct Token *token,
bool *is_dquote)
{
// case 1, #include "foo.h"
if (token->kind == TK_STR) {
// could not escape any of the escape characters
*is_dquote = true;
*rest = skip_line(token->next);
return strndup(token->location + 1, token->len - 2);
}
// case 2, #include<foo.h>
if (equal(token, "<")) {
struct Token *start = token;
for (; !equal(token, ">"); token = token->next)
if (token->at_bol || token->kind == TK_EOF)
error_token(token, "expected '>'");
// no ""
*is_dquote = false;
// jmp to begin of line
*rest = skip_line(token->next);
// from '<' to '>', splice into a string
return join_tokens(start->next, token);
}
// case 3, #include FOO
if (token->kind == TK_IDENT) {
// macro expand
struct Token *token2 = preprocess(copy_line(rest, token));
// then read
return read_include_filename(&token2, token2, is_dquote);
}
error_token(token, "expected a filename");
return NULL;
}
static struct Token *include_file(struct Token *token, char *path,
struct Token *filename_token)
{
struct Token *token2 = lexer_file(path);
if (!token2)
error_token(filename_token, "%s: cannot open file: %s", path,
strerror(errno));
return append(token2, token);
}
// iterate terminator, process with macro and directive
static struct Token *preprocess(struct Token *token)
{
struct Token head = {};
struct Token *cur = &head;
while (token->kind != TK_EOF) {
if (expand_macro(&token, token))
continue;
if (!is_begin_hash(token)) {
cur->next = token;
cur = cur->next;
token = token->next;
continue;
}
// start terminator
struct Token *start = token;
// next terminator
token = token->next;
// #include
if (equal(token, "include")) {
// "" exists?
bool is_dquote;
// get filename
char *filename = read_include_filename(
&token, token->next, &is_dquote);
// '/' at begin, absolute path
if (filename[0] != '/' && is_dquote) {
// start with the directory where \
// the current file is located
char *path = format(
"%s/%s",
dirname(strdup(start->file->name)),
filename);
// if path exists, include
if (file_exists(path)) {
token = include_file(token, path,
start->next->next);
continue;
}
}
// include directly
char *path = search_include_paths(filename);
token = include_file(token, path ? path : filename,
start->next->next);
continue;
}
// #define
if (equal(token, "define")) {
read_macro_def(&token, token->next);
continue;
}
// #undef
if (equal(token, "undef")) {
token = token->next;
if (token->kind != TK_IDENT)
error_token(token,
"macro name must be an identifier");
char *name = strndup(token->location, token->len);
token = skip_line(token->next);
struct Macro *m = push_macro(name, true, NULL);
m->deleted = true;
continue;
}
// #if
if (equal(token, "if")) {
long val = eval_constexpr(&token, token);
push_condincl(start, val);
// #if false
if (!val)
token = skip_condincl(token);
continue;
}
// #ifdef
if (equal(token, "ifdef")) {
bool defined = find_macro(token->next);
// push #if stack
push_condincl(token, defined);
token = skip_line(token->next->next);
// if not defined, skip
if (!defined)
token = skip_condincl(token);
continue;
}
// #ifndef
if (equal(token, "ifndef")) {
bool defined = find_macro(token->next);
push_condincl(token, !defined);
token = skip_line(token->next->next);
if (defined)
token = skip_condincl(token);
continue;
}
// #elif
if (equal(token, "elif")) {
if (!CondIncls || CondIncls->ctx == IN_ELSE)
error_token(start, "stray #elif");
CondIncls->ctx = IN_ELIF;
if (!CondIncls->included &&
eval_constexpr(&token, token))
// #if false #elif true
CondIncls->included = true;
else
token = skip_condincl(token);
continue;
}
// #else
if (equal(token, "else")) {
if (!CondIncls || CondIncls->ctx == IN_ELSE)
error_token(start, "stray #else");
CondIncls->ctx = IN_ELSE;
token = skip_line(token->next);
// #if true #else, else skip
if (CondIncls->included)
token = skip_condincl(token);
continue;
}
// #endif
if (equal(token, "endif")) {
if (!CondIncls)
error_token(start, "stray #endif");
CondIncls = CondIncls->next;
token = skip_line(token->next);
continue;
}
// #error
if (equal(token, "error"))
error_token(token, "error");
if (token->at_bol)
continue;
error_token(token, "invalid preprocesser directive");
}
cur->next = token;
return head.next;
}
static void define_macro(char *name, char *buf)
{
struct Token *token = lexer(new_file("<built-in", 1, buf));
push_macro(name, true, token);
}
// add built-in macro and its handler
static struct Macro *add_builtin(char *name, MacroHandlerFn *fn)
{
struct Macro *m = push_macro(name, true, NULL);
m->handler = fn;
return m;
}
static struct Token *file_macro(struct Token *tmpl)
{
// if origin macro exists, use origin after iterate
while (tmpl->origin)
tmpl = tmpl->origin;
return new_str_token(tmpl->file->name, tmpl);
}
static struct Token *line_macro(struct Token *tmpl)
{
// if origin macro exists, use origin after iterate
while (tmpl->origin)
tmpl = tmpl->origin;
return new_num_token(tmpl->line_no, tmpl);
}
static void init_macros(void)
{
define_macro("_LP64", "1");
define_macro("__C99_MACRO_WITH_VA_ARGS", "1");
define_macro("__ELF__", "1");
define_macro("__LP64__", "1");
define_macro("__SIZEOF_DOUBLE__", "8");
define_macro("__SIZEOF_FLOAT__", "4");
define_macro("__SIZEOF_INT__", "4");
define_macro("__SIZEOF_LONG_DOUBLE__", "8");
define_macro("__SIZEOF_LONG_LONG__", "8");
define_macro("__SIZEOF_LONG__", "8");
define_macro("__SIZEOF_POINTER__", "8");
define_macro("__SIZEOF_PTRDIFF_T__", "8");
define_macro("__SIZEOF_SHORT__", "2");
define_macro("__SIZEOF_SIZE_T__", "8");
define_macro("__SIZE_TYPE__", "unsigned long");
define_macro("__STDC_HOSTED__", "1");
define_macro("__STDC_NO_ATOMICS__", "1");
define_macro("__STDC_NO_COMPLEX__", "1");
define_macro("__STDC_NO_THREADS__", "1");
define_macro("__STDC_NO_VLA__", "1");
define_macro("__STDC_VERSION__", "201112L");
define_macro("__STDC__", "1");
define_macro("__USER_LABEL_PREFIX__", "");
define_macro("__alignof__", "_Alignof");
define_macro("__rvcc__", "1");
define_macro("__const__", "const");
define_macro("__gnu_linux__", "1");
define_macro("__inline__", "inline");
define_macro("__linux", "1");
define_macro("__linux__", "1");
define_macro("__signed__", "signed");
define_macro("__typeof__", "typeof");
define_macro("__unix", "1");
define_macro("__unix__", "1");
define_macro("__volatile__", "volatile");
define_macro("linux", "1");
define_macro("unix", "1");
define_macro("__riscv_mul", "1");
define_macro("__riscv_muldiv", "1");
define_macro("__riscv_fdiv", "1");
define_macro("__riscv_xlen", "64");
define_macro("__riscv", "1");
define_macro("__riscv64", "1");
define_macro("__riscv_div", "1");
define_macro("__riscv_float_abi_double", "1");
define_macro("__riscv_flen", "64");
add_builtin("__FILE__", file_macro);
add_builtin("__LINE__", line_macro);
}
// splice adjacent string literals
static void join_adjacent_string_literals(struct Token *token1)
{
// iterate till terminator
while (token1->kind != TK_EOF) {
// judgements
if (token1->kind != TK_STR || token1->next->kind != TK_STR) {
token1 = token1->next;
continue;
}
// point to next adjacent string
struct Token *token2 = token1->next;
while (token2->kind == TK_STR)
token2 = token2->next;
// iterate to reg length of all spliced strings
int len = token1->type->array_len;
for (struct Token *t = token1->next; t != token2; t = t->next)
len = len + t->type->array_len - 1;