-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclang.c
1474 lines (1255 loc) · 34.1 KB
/
clang.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 "clang.h"
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include "libks/arena-buffer.h"
#include "libks/arena.h"
#include "libks/buffer.h"
#include "libks/compiler.h"
#include "libks/list.h"
#include "libks/map.h"
#include "libks/vector.h"
#include "comment.h"
#include "cpp-align.h"
#include "cpp-include-guard.h"
#include "cpp-include.h"
#include "lexer-callbacks.h"
#include "lexer.h"
#include "token.h"
#include "trace-types.h"
#include "trace.h"
#define clang_trace(cl, fmt, ...) \
trace(TRACE_CLANG, (cl)->op, (fmt), __VA_ARGS__)
struct clang {
const struct style *st;
const struct options *op;
struct cpp_include *ci;
struct token_list prefixes;
VECTOR(struct token *) branches;
VECTOR(struct token *) stamps;
struct {
struct arena_scope *eternal_scope;
struct arena *scratch;
} arena;
};
struct clang_token {
struct {
struct token *parent;
struct token *pv;
struct token *nx;
} branch;
enum clang_token_type type;
};
static void clang_branch_enter(struct clang *, struct lexer *,
struct token *,
struct token *);
static void clang_branch_link(struct clang *, struct lexer *,
struct token *,
struct token *);
static void clang_branch_leave(struct clang *, struct lexer *,
struct token *,
struct token *);
static void clang_branch_purge(struct clang *, struct lexer *);
static void clang_free(void *);
static void clang_after_read(struct lexer *, void *);
static void clang_before_free(struct lexer *, void *);
static struct token *clang_read(struct lexer *, void *);
static struct token *clang_read_prefix(struct clang *,
struct lexer *);
static struct token *clang_read_comment(struct clang *,
struct lexer *,
int);
static struct token *clang_read_cpp(struct clang *, struct lexer *);
static int clang_find_cpp(const struct lexer *,
const struct lexer_state *, int);
static struct token *clang_keyword(struct lexer *);
static const struct token *clang_find_keyword(const struct lexer *,
const struct lexer_state *);
static const struct token *clang_find_keyword1(const char *, size_t);
static const struct token *clang_ellipsis(struct lexer *,
const struct lexer_state *);
static struct token *clang_token_alloc(struct arena_scope *,
const struct token *);
static const char *clang_token_serialize(const struct token *,
struct arena_scope *);
static const char *clang_token_type_serialize(int,
struct arena_scope *);
static const char *clang_token_serialize_prefix(
const struct token *,
struct arena_scope *);
static struct token *clang_end_of_branch(struct lexer *, struct token *,
void *);
static struct token *clang_last_stamped(struct clang *);
static struct token *clang_recover_find_branch(struct token *,
struct token *, int);
static void clang_branch_fold(struct clang *, struct lexer *,
struct token *, struct token **);
static void remove_token(struct lexer *, struct token *);
static void token_move_prefix(struct token *, struct token *,
struct token *);
static void token_branch_exhaust(struct token *);
static struct token *token_branch_find(struct token *);
static void token_branch_link(struct token *, struct token *);
static void token_branch_parent(struct token *, struct token *);
static void token_branch_parent_update_flags(struct token *);
static void token_branch_revert(struct token *);
static void token_prolong(struct token *, struct token *);
static int isnum(unsigned char);
static MAP(const char, *, const struct token *) clang_tokens;
static MAP(const char, *, int) cpp_token_types;
static MAP(const char, *, enum clang_token_type) clang_identifiers;
static const struct token *token_types[TOKEN_NONE + 1];
void
clang_init(void)
{
#define OP(type, keyword, flags) { \
.tk_type = (type), \
.tk_flags = (flags), \
.tk_str = (keyword), \
.tk_len = sizeof((keyword)) - 1, \
},
static struct token keywords[] = { FOR_TOKEN_TYPES(OP) };
static struct token aliases[] = { FOR_TOKEN_ALIASES(OP) };
#undef OP
#define OP(type, normalized_type, keyword) { \
.tk_type = (type), \
.tk_str = (keyword), \
},
const struct token cpp[] = { FOR_TOKEN_CPP(OP) };
#undef OP
#define OP(type, keyword) { \
.key = (keyword), \
.val = (type), \
},
const struct {
const char *key;
enum clang_token_type val;
} identifiers[] = { FOR_CLANG_IDENTIFIERS(OP) };
#undef OP
size_t i;
if (MAP_INIT(clang_tokens))
err(1, NULL);
for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
const struct token *src = &keywords[i];
if (MAP_INSERT_VALUE(clang_tokens, src->tk_str, src) == NULL)
err(1, NULL);
assert(token_types[src->tk_type] == NULL);
token_types[src->tk_type] = src;
}
/* Let aliases inherit token flags. */
for (i = 0; i < sizeof(aliases) / sizeof(aliases[0]); i++) {
struct token *src = &aliases[i];
src->tk_flags = token_types[src->tk_type]->tk_flags;
if (MAP_INSERT_VALUE(clang_tokens, src->tk_str, src) == NULL)
err(1, NULL);
}
if (MAP_INIT(cpp_token_types))
err(1, NULL);
for (i = 0; i < sizeof(cpp) / sizeof(cpp[0]); i++) {
const struct token *src = &cpp[i];
if (MAP_INSERT_VALUE(cpp_token_types, src->tk_str,
src->tk_type) == NULL)
err(1, NULL);
}
if (MAP_INIT(clang_identifiers))
err(1, NULL);
for (i = 0; i < sizeof(identifiers) / sizeof(identifiers[0]); i++) {
if (MAP_INSERT_VALUE(clang_identifiers, identifiers[i].key,
identifiers[i].val) == NULL)
err(1, NULL);
}
}
void
clang_shutdown(void)
{
MAP_FREE(clang_tokens);
MAP_FREE(cpp_token_types);
MAP_FREE(clang_identifiers);
}
struct clang *
clang_alloc(const struct style *st, struct simple *si,
struct arena_scope *eternal_scope, struct arena *scratch,
const struct options *op)
{
struct clang *cl;
cl = arena_calloc(eternal_scope, 1, sizeof(*cl));
arena_cleanup(eternal_scope, clang_free, cl);
LIST_INIT(&cl->prefixes);
cl->st = st;
cl->op = op;
cl->arena.eternal_scope = eternal_scope;
cl->arena.scratch = scratch;
cl->ci = cpp_include_alloc(st, si, &cl->prefixes, eternal_scope,
scratch, op);
if (VECTOR_INIT(cl->branches))
err(1, NULL);
if (VECTOR_INIT(cl->stamps))
err(1, NULL);
return cl;
}
static void
clang_free(void *arg)
{
struct clang *cl = arg;
VECTOR_FREE(cl->branches);
VECTOR_FREE(cl->stamps);
}
struct lexer_callbacks
clang_lexer_callbacks(struct clang *cl)
{
return (struct lexer_callbacks){
.read = clang_read,
.alloc = clang_token_alloc,
.serialize_token = clang_token_serialize,
.serialize_token_type = clang_token_type_serialize,
.serialize_prefix = clang_token_serialize_prefix,
.end_of_branch = clang_end_of_branch,
.move_prefixes = clang_token_move_prefixes,
.after_read = clang_after_read,
.before_free = clang_before_free,
.arg = cl,
};
}
/*
* Take note of the last consumed token, later used while branching and
* recovering.
*/
void
clang_stamp(struct clang *cl, struct lexer *lx)
{
struct token **dst;
struct token *back;
if (!lexer_back(lx, &back))
return;
clang_trace(cl, "stamp %s", lexer_serialize(lx, back));
token_ref(back);
dst = VECTOR_ALLOC(cl->stamps);
if (dst == NULL)
err(1, NULL);
*dst = back;
}
/*
* Returns non-zero if the lexer took the next branch.
*/
int
clang_branch(struct clang *cl, struct lexer *lx, struct token **unmute)
{
struct token *back, *cpp_dst, *cpp_src, *dst, *rm, *seek, *src;
int error = 0;
if (!lexer_back(lx, &back))
return 0;
clang_trace(cl, "back %s", lexer_serialize(lx, back));
cpp_src = token_branch_find(back);
if (cpp_src == NULL)
return 0;
token_ref(cpp_src);
src = token_priv(cpp_src, struct clang_token)->branch.parent;
token_ref(src);
cpp_dst = token_priv(cpp_src, struct clang_token)->branch.nx;
token_ref(cpp_dst);
dst = token_priv(cpp_dst, struct clang_token)->branch.parent;
token_ref(dst);
clang_trace(cl, "branch from %s to %s, covering [%s, %s)",
lexer_serialize(lx, cpp_src), lexer_serialize(lx, cpp_dst),
lexer_serialize(lx, src), lexer_serialize(lx, dst));
clang_token_branch_unlink(cpp_src);
rm = src;
for (;;) {
struct token *nx;
clang_trace(cl, "removing %s", lexer_serialize(lx, rm));
nx = token_next(rm);
remove_token(lx, rm);
if (nx == dst)
break;
rm = nx;
}
/*
* Instruct caller that crossing this token must cause tokens to be
* emitted again.
*/
*unmute = dst;
/* Rewind to last stamped token. */
seek = clang_last_stamped(cl);
if (seek != NULL) {
if (!lexer_seek_after(lx, seek))
error = 1;
} else if (lexer_peek_first(lx, &seek)) {
lexer_seek(lx, seek);
} else {
error = 1;
}
token_rele(dst);
token_rele(cpp_dst);
token_rele(src);
token_rele(cpp_src);
return error ? 0 : 1;
}
/*
* Try to recover after encountering invalid source code. Returns the index of
* the stamped token seeked to, starting from the end. This index should
* correspond to the number of documents that must be removed since we're about
* to parse them again.
*/
int
clang_recover(struct clang *cl, struct lexer *lx, struct token **unmute)
{
struct token *seek = NULL;
struct token *back, *cpp_dst, *cpp_src, *dst, *src, *stamp;
size_t i;
int error = 0;
int ndocs = 1;
if (!lexer_back(lx, &back) && !lexer_peek_first(lx, &back))
return 0;
stamp = clang_last_stamped(cl);
clang_trace(cl, "back %s, stamp %s",
lexer_serialize(lx, back), lexer_serialize(lx, stamp));
cpp_src = clang_recover_find_branch(back, stamp, 0);
if (cpp_src == NULL)
cpp_src = clang_recover_find_branch(back, stamp, 1);
if (cpp_src == NULL)
return 0;
src = token_priv(cpp_src, struct clang_token)->branch.parent;
cpp_dst = token_priv(cpp_src, struct clang_token)->branch.nx;
dst = token_priv(cpp_dst, struct clang_token)->branch.parent;
clang_trace(cl, "branch from %s to %s covering [%s, %s)",
lexer_serialize(lx, cpp_src), lexer_serialize(lx, cpp_dst),
lexer_serialize(lx, src), lexer_serialize(lx, dst));
/*
* Find the offset of the first stamped token before the branch.
* Must be done before getting rid of the branch as stamped tokens might
* be removed.
*/
for (i = VECTOR_LENGTH(cl->stamps); i > 0; i--) {
stamp = cl->stamps[i - 1];
if (!token_is_dangling(stamp) && token_cmp(stamp, cpp_src) < 0)
break;
ndocs++;
}
clang_trace(cl, "removing %d document(s)", ndocs);
/*
* Turn the whole branch into a prefix. As the branch is about to be
* removed, grab a reference since it's needed below.
*/
token_ref(cpp_src);
clang_branch_fold(cl, lx, cpp_src, unmute);
/* Find first stamped token before the branch. */
for (i = VECTOR_LENGTH(cl->stamps); i > 0; i--) {
stamp = cl->stamps[i - 1];
if (!token_is_dangling(stamp) &&
token_cmp(stamp, cpp_src) < 0) {
seek = stamp;
break;
}
}
token_rele(cpp_src);
if (seek != NULL) {
if (!lexer_seek_after(lx, seek))
error = 1;
} else if (lexer_peek_first(lx, &seek)) {
lexer_seek(lx, seek);
} else {
error = 1;
}
return error ? 0 : ndocs;
}
void
clang_token_move_prefixes(struct token *src, struct token *dst)
{
while (!LIST_EMPTY(&src->tk_prefixes)) {
struct token *prefix;
prefix = LIST_LAST(&src->tk_prefixes);
token_move_prefix(prefix, src, dst);
}
}
struct token *
clang_token_branch_next(struct token *tk)
{
return token_priv(tk, struct clang_token)->branch.nx;
}
struct token *
clang_token_branch_parent(struct token *tk)
{
return token_priv(tk, struct clang_token)->branch.parent;
}
void
clang_token_branch_unlink(struct token *tk)
{
struct token *nx, *pv;
int token_type;
pv = token_priv(tk, struct clang_token)->branch.pv;
nx = token_priv(tk, struct clang_token)->branch.nx;
token_type = token_type_normalize(tk);
if (token_type == TOKEN_CPP_IF) {
assert(pv == NULL);
if (token_type_normalize(nx) == TOKEN_CPP_ENDIF) {
token_branch_exhaust(nx);
} else if (token_type_normalize(nx) == TOKEN_CPP_ELSE) {
token_priv(nx, struct clang_token)->branch.pv = NULL;
nx->tk_type = TOKEN_CPP_IF;
token_branch_parent_update_flags(
token_priv(nx, struct clang_token)->branch.parent);
} else {
assert(0 && "Invalid #if previous token");
}
token_branch_exhaust(tk);
} else if (token_type == TOKEN_CPP_ELSE) {
token_priv(pv, struct clang_token)->branch.nx = nx;
token_priv(nx, struct clang_token)->branch.pv = pv;
token_branch_parent_update_flags(
token_priv(pv, struct clang_token)->branch.parent);
token_branch_parent_update_flags(
token_priv(nx, struct clang_token)->branch.parent);
token_branch_exhaust(tk);
} else if (token_type == TOKEN_CPP_ENDIF) {
assert(nx == NULL);
if (token_type_normalize(pv) == TOKEN_CPP_IF) {
token_branch_exhaust(pv);
} else if (token_type_normalize(pv) == TOKEN_CPP_ELSE) {
token_priv(pv, struct clang_token)->branch.nx = NULL;
pv->tk_type = TOKEN_CPP_ENDIF;
token_branch_parent_update_flags(
token_priv(pv, struct clang_token)->branch.parent);
} else {
assert(0 && "Invalid #endif previous token");
}
token_branch_exhaust(tk);
}
}
const struct token *
clang_keyword_token(int token_type)
{
return token_types[token_type];
}
enum clang_token_type
clang_token_type(const struct token *tk)
{
return token_priv(tk, const struct clang_token)->type;
}
static void
clang_after_read(struct lexer *lx, void *arg)
{
struct clang *cl = arg;
clang_branch_purge(cl, lx);
cpp_include_done(cl->ci);
cpp_include_guard(cl->st, lx, cl->arena.eternal_scope,
cl->arena.scratch);
}
static void
clang_before_free(struct lexer *lx, void *arg)
{
struct clang *cl = arg;
struct token *tk;
/* Must unlink all branches to drop references to parent tokens. */
if (lexer_peek_first(lx, &tk)) {
for (; tk != NULL; tk = token_next(tk)) {
struct token *prefix;
LIST_FOREACH(prefix, &tk->tk_prefixes)
clang_token_branch_unlink(prefix);
}
}
while (!VECTOR_EMPTY(cl->stamps)) {
struct token **tail;
tail = VECTOR_POP(cl->stamps);
token_rele(*tail);
}
}
static enum clang_token_type
clang_find_identifier(const char *str, size_t len)
{
enum clang_token_type *type;
type = MAP_FIND_N(clang_identifiers, str, len);
if (type == NULL)
return CLANG_TOKEN_NONE;
return *type;
}
static struct token *
clang_read(struct lexer *lx, void *arg)
{
struct clang *cl = arg;
struct token *prefix, *tk, *tmp;
struct lexer_state st;
int ncomments = 0;
int nlines;
unsigned char ch;
/* Consume all comments and preprocessor directives. */
for (;;) {
prefix = clang_read_prefix(cl, lx);
if (prefix == NULL)
break;
cpp_include_add(cl->ci, lx, prefix);
}
cpp_include_leave(cl->ci, lx);
tk = clang_keyword(lx);
if (tk != NULL)
goto out;
st = lexer_get_state(lx);
if (lexer_getc(lx, &ch))
goto eof;
if (ch == 'L') {
unsigned char peek;
if (lexer_getc(lx, &peek) == 0 && (peek == '"' || peek == '\''))
ch = peek;
else
lexer_ungetc(lx);
}
if (ch == '"' || ch == '\'') {
unsigned char delim = ch;
unsigned char pch = ch;
for (;;) {
if (lexer_getc(lx, &ch))
goto eof;
if (pch == '\\' && ch == '\\')
ch = '\0';
else if (pch != '\\' && ch == delim)
break;
pch = ch;
}
tk = lexer_emit(lx, &st,
delim == '"' ? TOKEN_STRING : TOKEN_LITERAL);
} else if (isdigit(ch) || ch == '.') {
do {
if (lexer_getc(lx, &ch))
goto eof;
} while (isnum(ch));
lexer_ungetc(lx);
tk = lexer_emit(lx, &st, TOKEN_LITERAL);
} else if (isalpha(ch) || ch == '_') {
const struct token *kw;
lexer_match(lx, "AZaz09__");
if ((kw = clang_find_keyword(lx, &st)) != NULL) {
tk = lexer_emit_template(lx, &st, kw);
} else {
/* Fallback, treat everything as an identifier. */
tk = lexer_emit(lx, &st, TOKEN_IDENT);
token_priv(tk, struct clang_token)->type =
clang_find_identifier(tk->tk_str, tk->tk_len);
}
} else if (lexer_eof(lx)) {
eof:
tk = lexer_emit(lx, &st, LEXER_EOF);
} else {
tk = lexer_emit(lx, &st, TOKEN_NONE);
}
out:
LIST_CONCAT(&tk->tk_prefixes, &cl->prefixes);
/*
* Consume trailing/interwined comments. If the token is about to be
* discarded, skip this causing any comment to be treated as a prefix
* instead.
*/
if ((tk->tk_flags & TOKEN_FLAG_DISCARD) == 0) {
for (;;) {
struct token *comment;
comment = clang_read_comment(cl, lx, 0);
if (comment == NULL)
break;
token_list_append(&tk->tk_suffixes, comment);
ncomments++;
}
}
/*
* Trailing whitespace is only honored if it's present immediately after
* the token.
*/
if (ncomments == 0 && lexer_eat_spaces(lx, &tmp)) {
tmp->tk_flags |= TOKEN_FLAG_OPTSPACE | TOKEN_FLAG_DISCARD;
token_list_append(&tk->tk_suffixes, tmp);
}
/* Consume hard line(s). */
nlines = lexer_eat_lines(lx, 0, &tmp);
if (nlines > 0) {
if (nlines == 1)
tmp->tk_flags |= TOKEN_FLAG_OPTLINE;
token_list_append(&tk->tk_suffixes, tmp);
}
/* Establish links between cpp branches. */
LIST_FOREACH(prefix, &tk->tk_prefixes) {
int token_type = token_type_normalize(prefix);
switch (token_type) {
case TOKEN_CPP_IF:
clang_branch_enter(cl, lx, prefix, tk);
break;
case TOKEN_CPP_ELSE:
clang_branch_link(cl, lx, prefix, tk);
break;
case TOKEN_CPP_ENDIF:
clang_branch_leave(cl, lx, prefix, tk);
break;
}
}
return tk;
}
static struct token *
clang_token_alloc(struct arena_scope *s, const struct token *def)
{
return token_alloc(s, sizeof(struct clang_token), def);
}
static const char *
clang_token_type_str(enum clang_token_type type)
{
switch (type) {
#define OP(type, keyword) case type: return &#type[sizeof("CLANG_TOKEN_") - 1];
FOR_CLANG_IDENTIFIERS(OP)
#undef OP
case CLANG_TOKEN_NONE:
break;
}
return NULL;
}
static const char *
clang_token_serialize(const struct token *tk, struct arena_scope *s)
{
return token_serialize_with_extra_flags(tk,
TOKEN_SERIALIZE_VERBATIM | TOKEN_SERIALIZE_QUOTE |
TOKEN_SERIALIZE_POSITION | TOKEN_SERIALIZE_FLAGS,
clang_token_type_str(clang_token_type(tk)), s);
}
static const char *
clang_token_type_serialize(int token_type, struct arena_scope *s)
{
return token_serialize(&(struct token){.tk_type = token_type}, 0, s);
}
static const char *
clang_token_serialize_prefix(const struct token *prefix, struct arena_scope *s)
{
struct buffer *bf;
const struct clang_token *ct;
bf = arena_buffer_alloc(s, 1 << 8);
buffer_printf(bf, "%s", clang_token_serialize(prefix, s));
ct = token_priv(prefix, const struct clang_token);
if (ct->branch.pv != NULL) {
buffer_printf(bf, ", pv %s",
clang_token_serialize(ct->branch.pv, s));
}
if (ct->branch.nx != NULL) {
buffer_printf(bf, ", nx %s",
clang_token_serialize(ct->branch.nx, s));
}
return buffer_str(bf);
}
static struct token *
clang_end_of_branch(struct lexer *UNUSED(lx), struct token *tk,
void *UNUSED(arg))
{
struct token *prefix;
assert(tk->tk_flags & TOKEN_FLAG_BRANCH);
prefix = token_branch_find(tk);
do {
struct clang_token *ct = token_priv(prefix, struct clang_token);
if (ct->branch.nx == NULL)
return ct->branch.parent;
prefix = ct->branch.nx;
} while (prefix != NULL);
return NULL;
}
static struct token *
clang_last_stamped(struct clang *cl)
{
size_t i;
for (i = VECTOR_LENGTH(cl->stamps); i > 0; i--) {
struct token *stamp = cl->stamps[i - 1];
if (!token_is_dangling(stamp))
return stamp;
}
return NULL;
}
/*
* Find the best suited branch to fold relative to the given token while trying
* to recover after encountering invalid source code.
*/
static struct token *
clang_recover_find_branch(struct token *tk, struct token *threshold,
int forward)
{
for (;;) {
struct token *prefix;
LIST_FOREACH_REVERSE(prefix, &tk->tk_prefixes) {
struct token *br, *nx;
int token_type;
token_type = token_type_normalize(prefix);
if (token_type == TOKEN_CPP_IF) {
br = prefix;
} else if (token_type == TOKEN_CPP_ELSE) {
br = prefix;
} else if (token_type == TOKEN_CPP_ENDIF) {
br = token_priv(prefix,
struct clang_token)->branch.pv;
} else {
continue;
}
nx = token_priv(br, struct clang_token)->branch.nx;
if (token_priv(br, struct clang_token)->branch.parent ==
token_priv(nx, struct clang_token)->branch.parent)
continue;
return br;
}
if (forward)
tk = token_next(tk);
else
tk = token_prev(tk);
if (tk == NULL || tk == threshold)
break;
}
return NULL;
}
/*
* Fold tokens covered by the branch into a prefix.
*/
static void
clang_branch_fold(struct clang *cl, struct lexer *lx, struct token *cpp_src,
struct token **unmute)
{
const struct lexer_state st = {
.st_lno = cpp_src->tk_lno,
.st_off = cpp_src->tk_off,
};
struct token *cpp_dst, *dst, *prefix, *pv, *rm, *src;
size_t len;
int dangling = 0;
src = token_priv(cpp_src, struct clang_token)->branch.parent;
token_ref(src);
cpp_dst = token_priv(cpp_src, struct clang_token)->branch.nx;
token_ref(cpp_dst);
dst = token_priv(cpp_dst, struct clang_token)->branch.parent;
token_ref(dst);
len = (cpp_dst->tk_off + cpp_dst->tk_len) - cpp_src->tk_off;
prefix = lexer_emit_template(lx, &st, &(struct token){
.tk_type = TOKEN_CPP,
.tk_flags = TOKEN_FLAG_CPP,
.tk_str = cpp_src->tk_str,
.tk_len = len,
});
/*
* Remove all prefixes hanging of the destination covered by the new
* prefix token.
*/
while (!LIST_EMPTY(&dst->tk_prefixes)) {
struct token *pr;
pr = token_list_first(&dst->tk_prefixes);
clang_trace(cl, "removing prefix %s", lexer_serialize(lx, pr));
clang_token_branch_unlink(pr);
token_list_remove(&dst->tk_prefixes, pr);
if (pr == cpp_dst)
break;
}
clang_trace(cl, "add prefix %s to %s",
lexer_serialize(lx, prefix),
lexer_serialize(lx, dst));
LIST_INSERT_HEAD(&dst->tk_prefixes, prefix);
/*
* Keep any existing prefix not covered by the new prefix token
* by moving them to the destination.
*/
pv = token_prev(cpp_src);
for (;;) {
struct token *tmp;
if (pv == NULL)
break;
clang_trace(cl, "keeping prefix %s", lexer_serialize(lx, pv));
tmp = token_prev(pv);
token_move_prefix(pv, src, dst);
pv = tmp;
}
/*
* Remove all tokens up to the destination covered by the new prefix
* token. Some tokens might already have been removed by an overlapping
* branch, therefore abort while encountering a dangling token.
*/
rm = src;
while (!dangling) {
struct token *nx;
if (rm == dst)
break;
dangling = token_is_dangling(rm);
nx = token_next(rm);
clang_trace(cl, "removing %s", lexer_serialize(lx, rm));
remove_token(lx, rm);
rm = nx;
}
/*
* Instruct caller that crossing this token must cause tokens to be
* emitted again.
*/
*unmute = dst;
token_rele(dst);
token_rele(cpp_dst);
token_rele(src);
}
static void
remove_token(struct lexer *lx, struct token *tk)
{
while (!LIST_EMPTY(&tk->tk_prefixes)) {
struct token *prefix = LIST_FIRST(&tk->tk_prefixes);
clang_token_branch_unlink(prefix);
token_list_remove(&tk->tk_prefixes, prefix);
}
while (!LIST_EMPTY(&tk->tk_suffixes)) {
struct token *suffix = LIST_FIRST(&tk->tk_suffixes);
token_list_remove(&tk->tk_suffixes, suffix);
}
lexer_remove(lx, tk);
}
static void
clang_branch_enter(struct clang *cl, struct lexer *lx, struct token *cpp,
struct token *parent)
{
struct token **br;
clang_trace(cl, "%s", lexer_serialize(lx, cpp));
token_branch_parent(cpp, parent);
br = VECTOR_ALLOC(cl->branches);
if (br == NULL)
err(1, NULL);
*br = cpp;
}
static void
clang_branch_link(struct clang *cl, struct lexer *lx, struct token *cpp,
struct token *parent)
{
struct token **last;
struct token *br;
/* Silently ignore broken branch. */
last = VECTOR_LAST(cl->branches);
if (last == NULL) {
token_branch_revert(cpp);
return;
}
br = *last;
clang_trace(cl, "%s -> %s",
lexer_serialize(lx, br), lexer_serialize(lx, cpp));
token_branch_parent(cpp, parent);
token_branch_link(br, cpp);
*last = cpp;
}
static void
clang_branch_leave(struct clang *cl, struct lexer *lx, struct token *cpp,
struct token *parent)
{
struct token **last;
clang_branch_link(cl, lx, cpp, parent);
last = VECTOR_LAST(cl->branches);
if (last != NULL) {
struct token *br;
for (br = *last; br != NULL;
br = token_priv(br, struct clang_token)->branch.pv) {
token_branch_parent_update_flags(
token_priv(br, struct clang_token)->branch.parent);
}
}
VECTOR_POP(cl->branches);
}
/*
* Purge pending broken branches.
*/
static void
clang_branch_purge(struct clang *cl, struct lexer *lx)
{
while (!VECTOR_EMPTY(cl->branches)) {
struct token **tail;
struct token *pv, *tk;
tail = VECTOR_POP(cl->branches);
tk = *tail;