-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwt.c
1727 lines (1410 loc) · 39.3 KB
/
wt.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
/* Main program of Cfunctions. */
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "argument.h"
#include "backup.h"
#include "cfunctions.h"
#include "config.h"
#include "error-msg.h"
#include "file.h"
#include "sys-or-exit.h"
#include "wt.h"
#include "flex.h"
static int wt_n_mallocs;
#undef BOOL
#define BOOL int
enum bool { FALSE, TRUE };
/* Maximum number of brackets '(' to expect in function arguments. */
#define MAX_ARG_BR_DEPTH 3
/* Name of source file from '#line' directives. */
static const char * global_macro = "HEADER";
/* If the number of "{" characters, 'cfp->curly_braces_depth', goes
beyond the following arbitrary limit, Cfunctions gives a warning
message in case Cfunctions itself has overcounted. */
#define MAX_CURLY_DEPTH 20
/* From here is the CPP stuff. */
#define MAX_CPP_IFS 200
struct cpp_if
{
Cpp_If_Type type;
char * text; /* This can be NULL if not '#if' or '#elif'. */
unsigned printed : 1; /* TRUE if this has been printed */
/* Boolean 'external' is TRUE if this CPP element is outside an
argument or function definition list. This is set for all
elements on which a 'tidy' is performed but not for elements
inside a function for example. */
unsigned external : 1;
/* For debugging: if printed, when was it printed ? */
unsigned print_line;
}
/* The stack of CPP '#if', '#else', '#elif' and '#endif'
statements. */
cpp_if_stack[MAX_CPP_IFS],
/* An empty element which is copied to places in 'cpp_if_stack'
which Cfunctions wants to clean up. */
empty_cpp_if;
#define cpp_stack_top cpp_if_stack[cfp->cpp_if_now]
/* The maximum number of chars allowed in a struct name. */
#define MAX_STRUCT_CHARS 0x100
/* The parsing state information. 'function_reset' resets this to all
zeros. */
typedef struct cfunctions_parse_state
{
/* The number of function arguments seen so far. */
unsigned function_type_n;
/* set to VOID only if the function does not have a return value */
enum { RETURN, VOID } c_return_value;
/* Have we seen 'extern'? This is needed to extract global variables
correctly ('extern' global variables are ignored by Cfunctions). */
unsigned seen_extern : 1;
/* Have we seen 'static'? */
unsigned seen_static : 1;
/* Have we seen the 'inline' keyword? */
unsigned seen_inline : 1;
/* TRUE if function has no side effects. */
unsigned no_side_effects : 1;
/* TRUE if function arguments have been seen */
unsigned seen_arguments : 1;
/* TRUE if the function never returns (for example 'exit').
This is set by 'NO_RETURN'. */
unsigned c_no_return : 1 ;
/* TRUE if PRINT_FORMAT argument was seen. */
unsigned seen_print_format : 1;
/* TRUE if typedef was seen. */
unsigned seen_typedef : 1;
/* TRUE if struct with no name and not static, not in typedef. */
unsigned unnamed_struct : 1;
/* TRUE if this function is a traditional C function. */
unsigned is_trad : 1;
/* TRUE if Cfunctions saw a word after a 'struct', 'union' or 'enum'
statement. */
unsigned saw_word : 1;
/* TRUE for '(void)' functions. */
unsigned void_arguments : 1;
/* True if this is a function pointer. */
unsigned function_pointer : 1;
}
parse_state_t;
/* The top-level thing which is being parsed. */
struct arg * current_arg;
/* The maximum file name length allowed in #line directives. */
#define MAX_LINE_NAME 0x100
struct cfparse
{
/* This keeps track of how many lines have been printed by the CPP
output part. */
unsigned cpp_prints;
/* The top of the 'cpp_if_stack' stack. */
unsigned cpp_if_now;
/* Is Cfunctions copying everything? This is set by '#ifdef
HEADER' statements. */
BOOL verbatiming;
/* The current file name for #line directives. */
char line_source_name[MAX_LINE_NAME];
/* The depth of braces '{' and '}' seen. */
unsigned curly_braces_depth;
/* Are we in a typedef? */
BOOL in_typedef;
/* Arguments to the most recently seen function. */
struct arg ** fargs;
/* Number of arguments to the most recently seen function. */
unsigned n_fargs;
/* Size of allocated memory for 'fargs'. */
unsigned max_fargs;
/* The number of brackets inside an argument list. Brackets inside an
argument list are most likely to happen for function pointer
arguments.
Deficiency: I am not sure where else it could happen. There is a
MAX variable just in case things go wrong. */
int arg_br_depth;
FILE * outfile;
char * command_line;
/* The current line number. This replaces yylineno. */
unsigned ln;
/* Parse state information. */
parse_state_t s;
/* Name of the source file. */
const char * source_name;
}
cfparser;
cfparse_t * cfp = & cfparser;
static void
set_source_name (cfparse_t * cfp, const char * name)
{
cfp->ln = 1;
cfp->source_name = name;
}
static const char *
get_source_name (cfparse_t * cfp)
{
return cfp->source_name;
}
/*
Given a file name, strip out all the directory information leaving
just the file name.
Return value: a pointer to somewhere in `file_name'. If there are
no directories in `file_name', a pointer to `file_name'.
*/
static const char *
strip_dir (const char * file_name)
{
const char * last_dir;
last_dir = strrchr (file_name, '/');
if (! last_dir) {
last_dir = file_name;
}
else {
last_dir++;
}
return last_dir;
}
/*
Check whether a string (file_name) ends in `.c'.
Return value: position of `.c' if present
NULL if .c not at end.
*/
static const char *
is_c_file (const char * file_name)
{
char * s;
s = strstr (file_name, ".c");
if (! s) {
return 0;
}
if (s[2] == '\0') {
return s;
}
else {
return 0;
}
}
/* Print the line declaration for the current line relative to the
source file. */
static void
print_line_number (cfparse_t * cfp)
{
fprintf (cfp->outfile, "\n#line %d \"%s\"\n", cfp->ln,
get_source_name (cfp));
}
/* Check whether a particular quantity has overflowed a maximum, and if
so issue a warning. */
static void
check_overflow (unsigned n, const unsigned max, const char * name)
{
if (n > max) {
line_warning ("possible overflow of %s: %u > %u",
name, n, max);
}
}
/* count a '{'. */
void
brace_open (cfparse_t * cfp)
{
cfp->curly_braces_depth++;
check_overflow (cfp->curly_braces_depth, MAX_CURLY_DEPTH, "curly braces");
}
/* Count a '}'. */
void
brace_close (cfparse_t * cfp)
{
cfp->curly_braces_depth--;
check_overflow (cfp->curly_braces_depth, MAX_CURLY_DEPTH, "curly braces");
}
/* The name do_PRINT_FORMAT is misleading: this is a temporary fix. */
void
do_PRINT_FORMAT (cfparse_t * cfp)
{
cfp->s.seen_print_format = TRUE;
start_initial ();
}
/* This is triggered by void * in the initial state. */
void
do_void_pointer (cfparse_t * cfp, const char * yytext, int yyleng)
{
char * c;
count_lines (cfp, yytext, yyleng);
/* 'void *' functions have a return value */
function_save (cfp, "void", 4);
c = strchr (yytext, '*');
function_save (cfp, c, strlen (c));
}
void
do_start_arguments (cfparse_t * cfp)
{
argument_next (cfp);
arg_put_name (current_arg);
cfp->s.seen_arguments = TRUE;
cfp->arg_br_depth++;
}
void
count_lines (cfparse_t * cfp, const char * yytext, int yyleng)
{
int i;
for (i = 0; i < yyleng; i++) {
if (yytext[i] == '\n') {
cfp->ln++;
}
}
}
void
do_arguments (cfparse_t * cfp, const char * yytext, int yyleng)
{
count_lines (cfp, yytext, yyleng);
arg_put_name (current_arg);
cfp->s.seen_arguments = TRUE;
}
/* Add function pointers in the argument list. */
void
do_function_pointer (cfparse_t * cfp, const char * yytext, int yyleng)
{
if (! current_arg) {
bug (HERE, "null pointer for current arg");
}
current_arg->is_function_pointer = TRUE;
inline_print (cfp, yytext, yyleng);
current_arg->function_pointer = strdup_or_exit (yytext);
}
/* Add function pointer arguments. */
void
do_function_pointer_argument (cfparse_t * cfp, const char * yytext, int yyleng)
{
inline_print (cfp, yytext, yyleng);
if (current_arg->function_pointer_arguments) {
/* Append "yytext" to the end of
current_arg->function_pointer_arguments. */
int new_length;
char * new;
new_length = strlen (current_arg->function_pointer_arguments) +
strlen (yytext) + 1;
new = malloc_or_exit (new_length);
wt_n_mallocs++;
sprintf (new, "%s%s", current_arg->function_pointer_arguments, yytext);
CALLX (free_or_exit (current_arg->function_pointer_arguments));
wt_n_mallocs--;
current_arg->function_pointer_arguments = 0;
current_arg->function_pointer_arguments = new;
}
else {
current_arg->function_pointer_arguments = strdup_or_exit (yytext);
}
}
/* Add a word. */
void
do_word (cfparse_t * cfp, const char * yytext, int leng)
{
if (cfp->s.saw_word) {
pop_state ();
}
function_save (cfp, yytext, leng);
cfp->s.saw_word = TRUE;
}
/* This is triggered by a "typedef" statement. The typedef statement
means that what follows it is not to be exported into the header
file unless the user requests that, unlike struct or enum, where we
need to put the extern declaration of the struct or enum or union
into the header file. */
void
do_typedef (cfparse_t * cfp, const char * yytext, int leng)
{
if (cfp->verbatiming) {
function_save (cfp, yytext, leng);
}
else {
cfp->s.seen_typedef = TRUE;
}
}
/* Copy an entire typedef, up to the final semicolon, if in verbatim
mode. */
void
do_copy_typedef (cfparse_t * cfp, const char * yytext, int leng)
{
if (cfp->verbatiming) {
fprintf (cfp->outfile, "%s\n", yytext);
}
}
/* If we are in verbatim copying mode, print whatever is in "x" to the
output file, otherwise discard it. */
void
inline_print (cfparse_t * cfp, const char * yytext, int yyleng)
{
count_lines (cfp, yytext, yyleng);
if (cfp->verbatiming) {
if (! cfp->in_typedef) {
fprintf (cfp->outfile, "%s", yytext);
}
}
}
/* Deal with preprocessor '#line' directives. */
void
line_change (cfparse_t * cfp, const char * yytext, int yyleng)
{
char * first_quote;
unsigned name_length;
char * line_at;
int line;
char * end;
first_quote = strchr (yytext, '\"');
if (! first_quote) {
line_warning ("could not find the file name in line directive %s",
yytext);
push_in_cpp ();
return;
}
name_length = strlen (first_quote);
if (name_length > MAX_LINE_NAME) {
line_warning ("very long source name '%s': truncating",
first_quote);
strncpy (cfp->line_source_name, first_quote + 1,
MAX_LINE_NAME);
cfp->line_source_name[MAX_LINE_NAME - 1] = '\0';
}
else {
strcpy (cfp->line_source_name, first_quote + 1);
cfp->line_source_name[name_length - 2] = '\0';
}
line_at = strstr (yytext, "line");
if (! line_at) {
line_at = strstr (yytext, "#");
if (! line_at) {
bug (HERE, "peculiar mismatch in strstr: "
"no 'line' or '#' in '%s'", yytext);
}
line_at += 1;
}
else {
line_at += 4;
}
line = strtol (line_at, & end, 10);
if (line < 1) {
line_warning ("Invalid line number %d in directive",
line);
}
if (end == line_at || line == 0) {
line_warning ("line number in %s could not be parsed",
yytext);
}
else {
cfp->ln = line - 1;
yylineno = line - 1;
set_source_name (cfp, cfp->line_source_name);
}
push_in_cpp ();
}
/* This copies 'c-extensions.h' into the current header file. */
void
copy_c_extensions (cfparse_t * cfp)
{
static BOOL c_ex_searched = FALSE;
static const char * c_ex_file_name = NULL;
if (! c_ex_searched) {
c_ex_searched = TRUE;
if (fexists (C_EXTENSIONS_FILE)) {
c_ex_file_name = C_EXTENSIONS_FILE;
}
else if (fexists (C_EX_LOCAL)) {
c_ex_file_name = C_EX_LOCAL;
}
else {
warning ("cannot find master copy of '%s'", C_EX_FILE_NAME);
}
}
if (c_ex_file_name) {
fprintf (cfp->outfile, "#line 1 \"%s\"\n", c_ex_file_name);
fcopy (cfp->outfile, c_ex_file_name);
}
}
/* Copy the file "c-extensions.h" into the output header file if
necessary. */
void
check_extensions (cfparse_t * cfp)
{
static unsigned written_c_extensions;
if (written_c_extensions) {
return;
}
copy_c_extensions (cfp);
written_c_extensions = 1;
}
/* Names of various preprocessor conditions. These are used to find
substrings in a Cpp statement. "ZAP" means mark for destruction. */
static const char * cpp_if_names[N_CPP_IF_TYPES] =
{
"ZAP", "if", "else", "elif", "endif"
};
/* The string lengths of 'cpp_if_names'. */
static unsigned cpp_if_len[N_CPP_IF_TYPES] =
{
3, 2, 4, 4, 5
};
/*
Given a '#endif', find the matching '#if'.
Return value: stack position of matching '#if'.
Side effects: If this routine cannot find the matching '#if' it
aborts with an error message.
Deficiency: this routine is a duplicate of code in the
cpp_stack_tidy routine.
*/
static int
cpp_stack_find_if (int i)
{
unsigned endif_level = 1;
int depth;
depth = i - 1;
while (1) {
if (depth < 0) {
line_error ("can't find matching #if");
}
switch (cpp_if_stack[depth].type) {
case CPP_IF:
endif_level--;
if (! endif_level) {
return depth;
}
break;
case CPP_ENDIF:
endif_level++;
break;
case CPP_ELSE:
case CPP_ELIF:
case CPP_ZAP:
break;
default:
bug (HERE, "bad number %d in switch",
cpp_if_stack[depth].type);
}
depth--;
}
}
/* Stack position of an '#if' statement which started a 'verbatim
copying' routine. */
static unsigned verbatim_limit;
/* Compact the CPP stack such that zapped entries are deleted and the
remaining ones are made contiguous. */
static void
cpp_fill_holes (cfparse_t * cfp)
{
unsigned i;
unsigned j;
j = 0;
for (i = 0; i < cfp->cpp_if_now; i++) {
if (cpp_if_stack[i].type != CPP_ZAP) {
if (i != j) {
cpp_if_stack[j] = cpp_if_stack[i];
if (verbatim_limit && i == verbatim_limit) {
verbatim_limit = j;
}
}
j++;
}
else {
if (cpp_if_stack [i].text) {
CALLX (free_or_exit (cpp_if_stack[i].text));
wt_n_mallocs--;
cpp_if_stack [i].text = 0;
}
}
}
/* Reset the top of the stack. */
for (i = j; i < cfp->cpp_if_now; i++) {
cpp_if_stack[i] = empty_cpp_if;
if (cpp_if_stack [i].text) {
CALLX (free_or_exit (cpp_if_stack[i].text));
wt_n_mallocs--;
cpp_if_stack [i].text = 0;
}
}
cfp->cpp_if_now = j;
}
/*
Find all matching #if and #endif pairs in 'cpp_if_stack' and remove
them, then move all the remaining entries downwards.
*/
static void
cpp_stack_tidy (cfparse_t * cfp)
{
int i;
/* Find matching pairs of '#if' and '#endif' and mark them for
deletion. */
for (i = cfp->cpp_if_now - 1; i >= 0; i--) {
if (cpp_if_stack[i].type == CPP_ENDIF) {
/* Cfunctions has found an '#endif', and it will now descend
the stack looking for the partner '#if'. */
unsigned endif_level = 1;
int depth;
BOOL printed = cpp_if_stack[i].printed;
depth = i - 1;
while (1) {
if (depth < 0) {
line_error ("too many '#endif's");
}
switch (cpp_if_stack[depth].type) {
case CPP_IF:
endif_level--;
if (! endif_level) {
if (! printed && cpp_if_stack[depth].printed) {
cpp_eject (cfp, i);
}
else if (printed && ! cpp_if_stack[depth].printed) {
bug (HERE, "#endif printed but #if not printed");
}
cpp_if_stack[depth].type = CPP_ZAP;
goto zapped;
}
break;
case CPP_ENDIF:
endif_level++;
break;
case CPP_ELIF:
case CPP_ELSE:
if (endif_level == 1) {
cpp_if_stack[depth].type = CPP_ZAP;
}
break;
case CPP_ZAP:
break;
default:
bug (HERE, "bad number %d at depth %d in switch",
cpp_if_stack[depth].type, depth);
;
}
depth--;
}
zapped:
/* Cfunctions has found a matching '#if' and '#endif' pair. */
cpp_if_stack[i].type = CPP_ZAP;
}
}
cpp_fill_holes (cfp);
/* Set everything to 'external'. Now Cfunctions knows that the C
preprocessor stuff currently on the stack is not entangled with
the currently parsed function or variable. */
for (i = 0; i < cfp->cpp_if_now; i++) {
if (! cpp_if_stack[i].printed) {
cpp_if_stack [i].external = 1;
}
}
}
/* Push a new thing onto the preprocessor conditional stack. This is
called by the lexer as it encounters things like #ifdef or
#endif. */
void
cpp_add (cfparse_t * cfp, char * yytext, Cpp_If_Type type)
{
char * x;
unsigned leng;
char cpp_word[9];
x = strstr (yytext, cpp_if_names[type]);
if (! x) {
bug (HERE, "bad string '%s' in cpp_add at %s:%d: should contain '%s'",
yytext, get_source_name (cfp), cfp->ln,
cpp_if_names[type]);
}
x += cpp_if_len[type];
leng = strlen (x);
if (cpp_stack_top.text) {
CALLX (free_or_exit (cpp_stack_top.text));
wt_n_mallocs--;
cpp_stack_top.text = 0;
#if 0
bug (HERE, "Unfreed memory %s at the top of the CPP stack at %s:%d",
cpp_stack_top.text, get_source_name (cfp), cfp->ln);
#endif
}
if (leng) {
cpp_stack_top.text = malloc_or_exit (leng + 1);
wt_n_mallocs++;
strcpy (cpp_stack_top.text, x);
}
cpp_stack_top.type = type;
cpp_stack_top.printed = FALSE;
/* Check for the verbatim copying conditions. */
if (type == CPP_IF) {
if (! cfp->verbatiming) {
/* Look for the text HEADER. */
if (strstr (x, global_macro)) {
cfp->verbatiming = TRUE;
verbatim_limit = cfp->cpp_if_now;
/* Mark this as "printed" although we don't actually
print it. */
cpp_stack_top.printed = TRUE;
/* Add a line directive containing the current line
number of the C file to the output to show that
this is a section copied verbatim from the C
file. */
print_line_number (cfp);
goto verbatim_started;
}
}
}
if (cfp->verbatiming) {
if (type == CPP_ENDIF &&
cpp_stack_find_if (cfp->cpp_if_now) == verbatim_limit) {
/* Cfunctions has hit the final '#endif' of a verbatim copying
area. */
cpp_stack_top.printed = TRUE;
cpp_stack_tidy (cfp);
cfp->verbatiming = FALSE;
}
else {
cpp_eject (cfp, cfp->cpp_if_now);
}
}
else {
/* The following string acts as a marker to other routines that they
should call 'cpp_eject' with the number printed at the end of the
string.
Each client routine has to call 'atoi' to get the number and call
'cpp_eject'. */
leng = sprintf (cpp_word, "@CPP%u", cfp->cpp_if_now);
if (initial_state ()) {
function_save (cfp, cpp_word, leng);
}
else if (argument_state ()) {
argument_save (cfp, cpp_word, leng);
}
else {
/* Even if the statement is ignored here, it remains on the
stack until the next call to 'cpp_stack_tidy'. The 'printed'
flag in 'cpp_ifstack' ensures that an '#endif' will be
printed if a corresponding '#if' has been emitted. */
;
}
}
verbatim_started:
cfp->cpp_if_now++;
if (cfp->cpp_if_now > MAX_CPP_IFS) {
line_error ("too many '#if's: limit is %d", MAX_CPP_IFS);
}
}
/* Write out the preprocessor statement on the stack at position 'u'. */
void
cpp_eject (cfparse_t * cfp, unsigned u)
{
if (cpp_if_stack[u].printed) {
bug (HERE, "Attempt to eject already printed statement %d '%s'",
u, cpp_if_stack[u].text);
}
if (cpp_if_stack[u].type == CPP_ZAP) {
return;
}
/* Start a new line. */
fprintf (cfp->outfile, "\n");
if (cpp_if_stack[u].type == CPP_ENDIF) {
int matching_if;
matching_if = cpp_stack_find_if (u);
/* Cfunctions currently prints almost every conditional so the
following is not executed. */
if (! cpp_if_stack [matching_if].printed) {
return;
}
/* Deficiency: depending on the previous statement, this sometimes
prints too many '\n's. If the last statement ended with '\n'
there will be two consecutive '\n's */
if (cpp_if_stack[u].text) {
fprintf (cfp->outfile, "#%s%s\n",
cpp_if_names[cpp_if_stack[u].type],
cpp_if_stack[u].text);
}
else {
fprintf (cfp->outfile, "#%s\n", cpp_if_names[cpp_if_stack[u].type]);
}
}
else if (cpp_if_stack[u].text) {
fprintf (cfp->outfile, "#%s%s\n", cpp_if_names[cpp_if_stack[u].type],
cpp_if_stack[u].text);
}
else {
if (cpp_if_stack[u].type == CPP_IF ||
cpp_if_stack[u].type == CPP_ELIF) {
bug (HERE, "%s with no condition",
cpp_if_names [cpp_if_stack[u].type]);
}
fprintf (cfp->outfile, "#%s\n", cpp_if_names [cpp_if_stack[u].type]);
}
cpp_if_stack[u].printed = TRUE;
/* Print a line number to indicate where a particular statement
was printed. */
cpp_if_stack[u].print_line = cfp->cpp_prints;
cfp->cpp_prints++;
}
/* This is triggered by # at the start of a line. */
void
do_start_cpp (cfparse_t * cfp, const char * yytext, int yyleng)
{
if (initial_state ()) {
function_reset (cfp);
}
push_in_cpp ();
inline_print (cfp, yytext, yyleng);
}
/* This deals with the unusual case of '{' and '}' in the C program
text. */
void
do_escaped_brace (cfparse_t * cfp, const char * yytext, int yyleng)
{
inline_print (cfp, yytext, yyleng);
}
/* This is triggered by the word "extern" in the C program text. */
void
do_extern (cfparse_t * cfp, const char * yytext, int leng)
{
if (cfp->verbatiming) {
function_save (cfp, yytext, leng);
}
else {
cfp->s.seen_extern = TRUE;
}
}
/* This is triggered by cfunctions' special macro "NO_RETURN". */
void
do_NO_RETURN (cfparse_t * cfp, const char * yytext, int yyleng)
{
check_extensions (cfp);
cfp->s.c_return_value = VOID;
cfp->s.c_no_return = TRUE;
function_save (cfp, "void", 5);
}
/* This is triggered by ")" in function arguments. */
void
do_arguments_close_bracket (cfparse_t * cfp, const char * yytext, int leng)
{
cfp->arg_br_depth--;
if (cfp->arg_br_depth == 0) {
/* This was the last ) in the argument list, so revert to the
"initial" state. */
start_initial ();
}
else if (cfp->arg_br_depth < 0) {
bug (HERE, "underflow %d\n", cfp->arg_br_depth);
}