-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpp.c
2535 lines (2230 loc) · 56.6 KB
/
cpp.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
/*
* Routines to implement a C preprocessor
*/
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "c2ada.h"
extern int translate_comments;
/* from scan.c */
extern comment_block_pt fetch_comment_block(void);
#undef NULL
#define NULL 0L
#ifndef MAX_SEARCH
#define MAX_SEARCH 32
#endif
#define SYNC_RST_CSP 0
#define SYNC_INC_CSP 1
struct cpp_file_t
{
char* path;
char* text; /* file buffer */
off_t file_size;
int fd;
int reference_count;
int is_header;
cpp_file_pt next_cpp_file;
};
static cpp_file_t* open_files;
static scan_position_t* curpos;
bool current_unit_is_header;
unsigned char cpp_char_class[128];
static char class_initialized;
#define add(buf, c) buf_add(buf, (int)(c))
#define cond_add(buf, c) \
{ \
if(buf) \
{ \
add((buf), (c)); \
} \
}
#define UNHANDLED() unhandled(__FILE__, __LINE__)
static buffer_t cppbuf;
static bool buffer_initialized;
static char* search_paths[MAX_SEARCH];
static int search_index;
static char* system_search_paths[MAX_SEARCH] = { "/usr/include" };
static int system_search_index = 1;
/* at_file_start is set true when a new file is opened,
* and false once the first token is read from the file.
*/
bool at_file_start;
/* TBD: this is a temporary flag to bypass the handling of
* macros as constants until the whole facility works better.
*/
bool do_const_macros = true;
/*
* The following vars control the state of conditionals
*/
static cpp_control_state_t control_state;
#define scanning() (control_state.cur_scope == control_state.gen_scope)
#define skipping() (control_state.cur_scope != control_state.gen_scope)
#define parsing() (control_state._parsing)
#define want_comments(x) ((translate_comments) ? x : NULL)
static char* last_file;
static int last_line;
#define FN fname()
#define LN fline()
static int grok_actual_param(int);
static bool is_const_macro(macro_t* mac);
static char* fname(void)
/* The name of the current source file. */
{
if(curpos == NULL)
{
assert(last_file != NULL);
return last_file;
}
if(curpos->scan_pos == 0)
return NULL;
return file_name(curpos->scan_pos);
}
static int fline()
{
if(curpos == NULL)
{
assert(last_line != 0);
return last_line;
}
return line_number(curpos->scan_pos);
}
static void range_check(int n, int max, char* msg)
{
if(n >= max)
{
fatal(FN, LN, "%s: %d > %d", msg, n + 1, max);
}
}
static void unhandled(char* file, int line)
{
fatal(FN, LN, "Unhandled case %s:%d", file, line);
}
static void bad_directive()
{
if(scanning())
{
error(FN, LN, "Undefined preprocessor directive");
}
}
static void bad_include()
{
error(FN, LN, "Malformed include directive");
}
static void unexpected(char* msg)
{
error(FN, LN, "Unexpected %s", msg);
}
static void unexpected_eof(char* msg)
{
char buf[128];
sprintf(buf, "end of file %s", msg);
unexpected(buf);
}
static char* charstr(int c)
{
if(is_eof(c))
return "end of file";
if(is_eol(c))
return "end of line";
if(is_alpha(c))
return "identifier";
if(is_digit(c))
return "number";
if(is_white(c))
return "white space";
if(is_punct(c))
return "puctuation";
return "crap";
}
static void expected(char* msg, int c)
{
error(FN, LN, "Expected %s got %s", msg, charstr(c));
}
static int levels_nested()
{
scan_position_t* p;
int level = 0;
for(p = curpos; p; p = p->scan_next)
{
if(p->scan_kind == scan_file)
{
level++;
}
}
return level;
}
static void add_position_directive(buffer_t* buf, int new_line)
{
char b[40];
sprintf(b, "\n# %d \"", LN);
buf_add_str(buf, b);
buf_add_str(buf, FN);
sprintf(b, "\" %d", levels_nested());
buf_add_str(buf, b);
if(new_line)
{
add(buf, '\n');
}
}
static void init_char_class()
{
int i;
if(class_initialized)
return;
cpp_char_class[0] = END_INPUT;
cpp_char_class[PARAM_START] = PARAM_START;
cpp_char_class['#'] = MSTART;
cpp_char_class['_'] = ALPHA;
cpp_char_class['$'] = ALPHA;
for(i = 'g'; i <= 'z'; i++)
{
cpp_char_class[i] = ALPHA;
}
for(i = 'G'; i <= 'Z'; i++)
{
cpp_char_class[i] = ALPHA;
}
for(i = 'A'; i <= 'F'; i++)
{
cpp_char_class[i] = ALPHA | XDIGIT;
}
for(i = 'a'; i <= 'f'; i++)
{
cpp_char_class[i] = ALPHA | XDIGIT;
}
for(i = '0'; i <= '7'; i++)
{
cpp_char_class[i] = DIGIT | XDIGIT;
}
cpp_char_class['8'] = DIGIT | XDIGIT;
cpp_char_class['9'] = DIGIT | XDIGIT;
cpp_char_class[' '] = WHITE;
cpp_char_class['\t'] = WHITE;
cpp_char_class['\f'] = WHITE;
cpp_char_class['\v'] = WHITE;
cpp_char_class['\n'] = END_OF_LINE;
cpp_char_class['\r'] = END_OF_LINE;
class_initialized = 1;
}
static void init_cppbuf()
{
if(buffer_initialized)
{
return;
}
buf_init(&cppbuf);
}
static void rm_file_from_list(cpp_file_t* f)
{
cpp_file_t *p, *last;
for(p = open_files, last = NULL; p; p = p->next_cpp_file)
{
if(p == f)
break;
last = p;
}
assert(p == f);
if(last == NULL)
{
open_files = f->next_cpp_file;
}
else
{
last->next_cpp_file = f->next_cpp_file;
}
}
static cpp_file_t* find_open_file(char* path)
{
cpp_file_t* f;
for(f = open_files; f; f = f->next_cpp_file)
{
assert(f->next_cpp_file != f);
if(!strcmp(f->path, path))
{
return f;
}
}
return NULL;
}
void cpp_search_path(char* path)
{
if(search_index >= MAX_SEARCH)
{
fatal(__FILE__, __LINE__, "Too many search paths added");
}
search_paths[search_index++] = path;
}
void cpp_system_search_path(char* path)
/* Set the path to use for #include <blah.h> directives. */
/* /usr/include is always the last element in the list of paths */
{
if(system_search_index >= MAX_SEARCH)
{
fatal(__FILE__, __LINE__, "Too many system search paths added");
}
system_search_paths[system_search_index - 1] = path;
system_search_paths[system_search_index++] = "/usr/include";
}
int in_system_search_path(char* path)
/*
* Tests whether argument path is in any system_search_paths directory.
* Returns length of directory prefix part of pathname, if so;
* otherwise returns 0.
*/
{
int n;
char* syspath;
int syspathlen;
int result;
for(n = 0; n < system_search_index; n++)
{
syspath = system_search_paths[n];
syspathlen = strlen(syspath);
if(strncmp(path, syspath, syspathlen) == 0)
{
result = syspathlen;
while(path[result] == '/')
result++;
return result;
}
}
return 0;
}
static cpp_file_t* attempt_open(char* path)
{
cpp_file_t* f;
char* mapaddr;
size_t fsize;
int fd;
f = find_open_file(path);
if(f != NULL)
{
f->reference_count++;
return f;
}
if((fd = open(path, O_RDONLY)) == -1)
{
/* Can't access for reading */
return NULL;
}
if((fsize = sizeof_file(fd)) == -1)
{
close(fd);
return NULL;
}
mapaddr = (char*)map_file(fd, fsize);
if(mapaddr == (char*)-1)
{
/* failed mmap */
close(fd);
return NULL;
}
f = NEW(cpp_file_t);
f->path = new_string(path);
f->fd = fd;
f->text = mapaddr;
f->file_size = fsize;
f->reference_count = 1;
assert(open_files != f);
f->next_cpp_file = open_files;
open_files = f;
return f;
}
static char* local_copy(buffer_t* buf, char* str, int size); /*fwd ref */
static cpp_file_t* attempt_open_buf(buffer_t* lbuf)
/* Like attempt_open, but takes a buffer_t* argument. */
{
char* path;
cpp_file_t* result;
char ident[128]; /* local string holder */
path = local_copy(lbuf, ident, sizeof(ident));
result = attempt_open(path);
if(path != ident)
{
free(path);
}
return result;
}
static cpp_file_t*
attempt_open_w_searchpaths(char* name, buffer_t* lbuf, char* dirs[], int ndirs)
/*
* Try to find a filename <name> in any of the <ndirs> directories
* <dirs>. <lbuf> is a scratch buffer.
*/
{
int i;
cpp_file_t* result;
for(i = 0; i < ndirs; i++)
{
buf_init(lbuf);
buf_add_str(lbuf, dirs[i]);
buf_add_str(lbuf, "/");
buf_add_str(lbuf, name);
result = attempt_open_buf(lbuf);
if(result)
return result;
}
return 0;
}
static void push_file(buffer_t* buf, cpp_file_t* f)
{
scan_position_t* pos;
char* dot;
pos = NEW(scan_position_t);
pos->scan_kind = scan_file;
pos->scan.file = f;
pos->scan_index = 0;
pos->scan_pos = add_file(f->path);
pos->scan_next = curpos;
curpos = pos;
dot = strrchr(f->path, '.');
assert((dot != NULL) && ((dot[1] == 'c') || (dot[1] == 'h')));
current_unit_is_header = f->is_header = (dot[1] == 'h');
add_position_directive(buf, 1);
at_file_start = true;
}
int cpp_open(char* path)
{
cpp_file_t* f;
init_char_class();
init_cppbuf();
control_state.skip_else = 0;
control_state.cur_scope = 0;
control_state.gen_scope = 0;
control_state._parsing = 0;
f = attempt_open(path);
if(f == NULL)
{
return 1;
}
push_file(&cppbuf, f);
return 0;
}
void cpp_cleanup()
{
cpp_file_t *f, *next;
curpos = NULL;
for(f = open_files; f; f = next)
{
next = f->next_cpp_file;
unmap_file(f->text, f->file_size);
close(f->fd);
deallocate(f);
}
open_files = NULL;
buf_destroy(&cppbuf);
}
void cpp_set_state(
scan_position_t* newpos,
cpp_control_state_t* new_state,
scan_position_t** savepos,
cpp_control_state_t* save_state
)
{
*savepos = curpos;
*save_state = control_state;
control_state = *new_state;
curpos = newpos;
}
static void kill_file(cpp_file_t* f)
{
unmap_file(f->text, f->file_size);
close(f->fd);
rm_file_from_list(f);
deallocate(f);
}
static void finished_with(scan_position_t* s)
{
cpp_file_t* f;
last_file = file_name(s->scan_pos);
last_line = line_number(s->scan_pos);
switch(s->scan_kind)
{
case scan_file:
f = curpos->scan.file;
deallocate(s);
f->reference_count--;
if(f->reference_count == 0)
{
kill_file(f);
}
break;
case scan_macro_expansion:
case scan_text:
deallocate(s);
break;
default:
assert(0);
break;
}
}
static void unget_char()
{
if(curpos == NULL)
return;
assert(curpos->scan_index > 0);
curpos->scan_index--;
}
static int next_char(void)
/* Return the next character from the current input source */
{
register scan_position_t* cp = curpos; /* Get global curpos into a reg */
cpp_file_t* f;
macro_t* m;
scan_position_t* next;
scan_kind_t kind;
int result;
top:
if(cp == NULL)
return 0;
kind = cp->scan_kind;
switch(kind)
{
case scan_file:
f = cp->scan.file;
if(cp->scan_index >= f->file_size)
{
/* done with file; pop to out file */
next = cp->scan_next;
finished_with(cp);
set_cur_unit_trailer_comment(fetch_comment_block());
cp = curpos = next;
if(next != NULL)
{
current_unit_is_header = next->scan.file->is_header;
}
goto top; /* return next_char(); */
}
result = f->text[cp->scan_index++];
/* Look ahead & detect DOS/WIN CR-LF EOL;
* skip the CR & return the LF
*/
if(('\r' == result) && ('\n' == f->text[cp->scan_index]))
{
result = f->text[cp->scan_index++];
}
break;
case scan_macro_expansion:
m = cp->scan.expansion.expand_macro;
if(cp->scan_index >= m->macro_body_len)
{
/* We're done with macro; pop to next source */
next = cp->scan_next;
finished_with(cp);
cp = curpos = next;
goto top; /* return next_char(); */
}
result = m->macro_body[cp->scan_index++];
break;
default:
assert(kind == scan_text);
result = cp->scan.text[cp->scan_index++];
if(result == 0)
{
/* Done with string; pop to next source */
next = cp->scan_next;
finished_with(cp);
cp = curpos = next;
goto top; /* return next_char */
}
break;
}
return result;
}
static void incline(int sync)
{
if(sync)
{
control_state.position++;
}
else
{
/* force file position directive to be added */
control_state.position = 0;
}
switch(curpos->scan_kind)
{
case scan_file:
curpos->scan_pos++;
break;
default:
break;
}
}
static void check_position(buffer_t* buf)
{
if(parsing())
return;
switch(curpos->scan_kind)
{
case scan_file:
if(control_state.position != curpos->scan_pos)
{
control_state.position = curpos->scan_pos;
add_position_directive(buf, 0);
}
break;
default:
assert(0);
break;
}
}
static void comment_start(buffer_t* buf)
{
if(translate_comments && buf != NULL)
{
add(buf, '/');
add(buf, '*');
}
}
static void cpp_comment_start(buffer_t* buf)
{
if(translate_comments && buf != NULL)
{
add(buf, '/');
add(buf, '/');
}
}
static int scan_white(buffer_t* buf, int c)
{
while(is_white(c))
{
cond_add(buf, c);
c = next_char();
}
return c;
}
static int skip_white(int c)
{
return scan_white(NULL, c);
}
/* When called for a #define, this fails almost all the time
* (see E_CIFAIL in error.h, the only one which worked)
* even though the plain C version below this worked just fine.
*/
static int scan_cpp_comment(buffer_t* buf, bool want_delim)
{
int c = next_char();
for(;;)
{
switch(classof(c))
{
case END_INPUT:
return c;
case DIGIT | XDIGIT:
case ALPHA:
case ALPHA | XDIGIT:
case WHITE:
case MSTART:
case PARAM_START:
case PUNCT:
cond_add(buf, c);
c = next_char();
break;
case END_OF_LINE:
incline(SYNC_RST_CSP);
cond_add(buf, c);
/* There shouldn't be another char to get; don't get it! */
/*c = next_char();*/
/* Added presumptively from scan_c_comment as a bread crumb */
/*if (want_delim) { ... } */
return c;
default:
UNHANDLED();
break;
}
}
}
static int scan_c_comment(buffer_t* buf, bool want_delim)
{
int c = next_char();
for(;;)
{
switch(classof(c))
{
case END_INPUT:
return c;
case DIGIT | XDIGIT:
case ALPHA:
case ALPHA | XDIGIT:
case WHITE:
case MSTART:
cond_add(buf, c);
c = next_char();
break;
case END_OF_LINE:
incline(SYNC_RST_CSP);
cond_add(buf, c);
c = next_char();
break;
case PARAM_START:
c = next_char();
c = next_char();
break;
case PUNCT:
switch(c)
{
case '*':
c = next_char();
if(c == '/')
{
if(want_delim)
{
cond_add(buf, '*');
cond_add(buf, '/');
}
return next_char();
}
else
{
cond_add(buf, '*');
}
break;
case '\\':
cond_add(buf, c);
c = next_char();
cond_add(buf, c);
if(is_eol(c))
{
incline(SYNC_RST_CSP); /* value added */
}
else if(is_eof(c))
{
return c;
}
c = next_char();
break;
default:
cond_add(buf, c);
c = next_char();
break;
}
break;
default:
UNHANDLED();
break;
}
}
}
static int scan_to_end(buffer_t* buf, buffer_t* cbuf, int c)
/* Scan to the end of the current line
* <buf> is the buffer to copy non-comment part of line to;
* <cbuf> the buffer to copy comments to.
* <buf> and <cbuf> may be the same.
* If <cbuf> is null then no comments are copied.
*/
{
while(!(is_eol(c) || is_eof(c)))
{
switch(c)
{
case '/':
c = next_char();
if(c == '*')
{
if(buf == cbuf)
comment_start(cbuf);
c = scan_c_comment(cbuf, buf == cbuf);
}
else if(c == '/')
{
if(buf == cbuf)
cpp_comment_start(cbuf);
c = scan_cpp_comment(cbuf, buf == cbuf);
}
else
{
cond_add(buf, '/');
}
break;
case '\\':
c = next_char();
if(is_eol(c))
{
incline(SYNC_RST_CSP);
c = next_char();
}
else if(is_eof(c))
{
if(buf != NULL)
{
add(buf, '\\');
}
return c;
}
else if(c == '\\')
{
if(buf != NULL)
{
add(buf, '\\');
add(buf, '\\');
}
c = next_char();
}
else if(buf != NULL)
{
add(buf, '\\');
}
break;
default:
if(buf != NULL)
{
add(buf, c);
}
c = next_char();
break;
}
}
return c;
}
static int skip_to_end(int c)
/* skip to the end of the current line */
{
return scan_to_end(NULL, NULL, c);
}
static int finish_num(buffer_t* buf, int c, int mask)
{
while(!is_eof(c))
{
if((cpp_char_class[c] & mask) == 0)
break;
add(buf, c);
c = next_char();
}
return c;
}
static int maybe_magnitude(buffer_t* buf, int c)
{
if(c == 'e' || c == 'E')
{
add(buf, c);
c = next_char();
if(c == '+' || c == '-')
{
add(buf, c);
c = next_char();
}
c = finish_num(buf, c, DIGIT);
}
return c;
}
static int scan_number(buffer_t* buf, int c)
{
add(buf, c);
if(c == '0')
{
c = next_char();
if(c == 'x' || c == 'X')
{
add(buf, c);
c = finish_num(buf, next_char(), XDIGIT);
}
else
{
c = finish_num(buf, c, DIGIT);
}
}
else
{
c = finish_num(buf, next_char(), DIGIT);
}
if(int_modifier(c))
{
do
{
add(buf, c);
c = next_char();
} while(int_modifier(c));
return c;
}
if(c == '.')
{
add(buf, c);
c = finish_num(buf, next_char(), DIGIT);
c = maybe_magnitude(buf, c);
if(float_modifier(c))
{
add(buf, c);
c = next_char();
}
return c;
}
c = maybe_magnitude(buf, c);
if(int_modifier(c))
{
do
{
add(buf, c);
c = next_char();
} while(int_modifier(c));
}
return c;
}
static int scan_ident(buffer_t* buf, int c)
/* Scan an identifier into buffer buf.
* Argument c is first character of identifier.
* Returns first character following identifier.
*/
{
for(; is_alpha_numeric(c); c = next_char())