-
Notifications
You must be signed in to change notification settings - Fork 29
/
directive.c
1404 lines (1309 loc) · 51 KB
/
directive.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
/*-
* Copyright (c) 1998, 2002-2008 Kiyoshi Matsui <[email protected]>
* All rights reserved.
*
* Some parts of this code are derived from the public domain software
* DECUS cpp (1984,1985) written by Martin Minow.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* D I R E C T I V E . C
* P r o c e s s D i r e c t i v e L i n e s
*
* The routines to handle directives other than #include and #pragma
* are placed here.
*/
#include "system.H"
#include "internal.H"
static int do_if( int hash, const char * directive_name);
/* #if, #elif, #ifdef, #ifndef */
static void sync_linenum( void);
/* Synchronize number of newlines */
static long do_line( void);
/* Process #line directive */
static int get_parm( void);
/* Get parameters of macro, its nargs, names, lengths */
static int get_repl( const char * macroname);
/* Get replacement text embedding parameter number */
static char * is_formal( const char * name, int conv);
/* If formal parameter, save the number */
static char * def_stringization( char * repl_cur);
/* Define stringization */
static char * mgtoken_save( const char * macroname);
/* Prefix DEF_MAGIC to macro name in repl-text */
static void do_undef( void);
/* Process #undef directive */
static void dump_repl( const DEFBUF * dp, FILE * fp, int gcc2_va);
/* Dump replacement text */
/*
* Generate (by hand-inspection) a set of unique values for each directive.
* MCPP won't compile if there are hash conflicts.
*/
#define L_if ('i' ^ (EOS << 1))
#define L_ifdef ('i' ^ ('d' << 1))
#define L_ifndef ('i' ^ ('n' << 1))
#define L_elif ('e' ^ ('i' << 1))
#define L_else ('e' ^ ('s' << 1))
#define L_endif ('e' ^ ('d' << 1))
#define L_define ('d' ^ ('f' << 1))
#define L_undef ('u' ^ ('d' << 1))
#define L_line ('l' ^ ('n' << 1))
#define L_include ('i' ^ ('c' << 1))
#if SYSTEM == SYS_MAC
#define L_import ('i' ^ ('p' << 1))
#endif
#define L_error ('e' ^ ('r' << 1))
#define L_pragma ('p' ^ ('a' << 1))
static const char * const not_ident
= "Not an identifier \"%s\""; /* _E_ */
static const char * const no_arg = "No argument"; /* _E_ */
static const char * const excess
= "Excessive token sequence \"%s\""; /* _E_ _W1_ */
void directive( void)
/*
* Process #directive lines. Each directive have their own subroutines.
*/
{
const char * const many_nesting =
"More than %.0s%ld nesting of #if (#ifdef) sections%s"; /* _F_ _W4_ _W8_ */
const char * const not_in_section
= "Not in a #if (#ifdef) section in a source file"; /* _E_ _W1_ */
const char * const illeg_dir
= "Illegal #directive \"%s%.0ld%s\""; /* _E_ _W1_ _W8_ */
const char * const in_skipped = " (in skipped block)"; /* _W8_ */
FILEINFO * file;
int token_type;
int hash;
int c;
char * tp;
in_directive = TRUE;
if (keep_comments) {
mcpp_fputc( '\n', OUT); /* Possibly flush out comments */
newlines--;
}
c = skip_ws();
if (c == '\n') /* 'null' directive */
goto ret;
token_type = scan_token( c, (workp = work_buf, &workp), work_end);
if (token_type != NAM) {
if (compiling) {
cerror( illeg_dir, work_buf, 0L, NULL);
} else if (warn_level & 8) {
cwarn( illeg_dir, work_buf, 0L, in_skipped);
}
goto skip_line;
}
hash = (identifier[ 1] == EOS) ? identifier[ 0]
: (identifier[ 0] ^ (identifier[ 2] << 1));
if (strlen( identifier) > 7)
hash ^= (identifier[ 7] << 1);
/* hash is set to a unique value corresponding to the directive.*/
switch (hash) {
case L_if: tp = "if"; break;
case L_ifdef: tp = "ifdef"; break;
case L_ifndef: tp = "ifndef"; break;
case L_elif: tp = "elif"; break;
case L_else: tp = "else"; break;
case L_endif: tp = "endif"; break;
case L_define: tp = "define"; break;
case L_undef: tp = "undef"; break;
case L_line: tp = "line"; break;
case L_include: tp = "include"; break;
#if SYSTEM == SYS_MAC
case L_import: tp = "import"; break;
#endif
case L_error: tp = "error"; break;
case L_pragma: tp = "pragma"; break;
default: tp = NULL; break;
}
if (tp != NULL && ! str_eq( identifier, tp)) { /* Hash conflict*/
hash = 0; /* Unknown directive, will */
tp = NULL; /* be handled by do_old() */
}
if (! compiling) { /* Not compiling now */
switch (hash) {
case L_elif :
case L_else : /* Test the #if's nest, if 0, compile */
case L_endif: /* Un-nest #if */
break;
case L_if : /* These can't turn */
case L_ifdef: /* compilation on, but */
case L_ifndef : /* we must nest #if's.*/
if (&ifstack[ BLK_NEST] < ++ifptr)
goto if_nest_err;
if ((warn_level & 8)
&& &ifstack[ std_limits.blk_nest + 1] == ifptr)
cwarn( many_nesting, NULL, (long) std_limits.blk_nest
, in_skipped);
ifptr->stat = 0; /* !WAS_COMPILING */
ifptr->ifline = src_line; /* Line at section start*/
goto skip_line;
default : /* Other directives */
if (tp == NULL && (warn_level & 8))
do_old(); /* Unknown directive ? */
goto skip_line; /* Skip the line */
}
}
macro_line = 0; /* Reset error flag */
file = infile; /* Remember the current file */
switch (hash) {
case L_if:
case L_ifdef:
case L_ifndef:
if (&ifstack[ BLK_NEST] < ++ifptr)
goto if_nest_err;
if ((warn_level & 4) &&
&ifstack[ std_limits.blk_nest + 1] == ifptr)
cwarn( many_nesting, NULL , (long) std_limits.blk_nest, NULL);
ifptr->stat = WAS_COMPILING;
ifptr->ifline = src_line;
goto ifdo;
case L_elif:
if (ifptr == &ifstack[0])
goto nest_err;
if (ifptr == infile->initif) {
goto in_file_nest_err;
}
if (ifptr->stat & ELSE_SEEN)
goto else_seen_err;
if ((ifptr->stat & (WAS_COMPILING | TRUE_SEEN)) != WAS_COMPILING) {
compiling = FALSE; /* Done compiling stuff */
goto skip_line; /* Skip this group */
}
hash = L_if;
ifdo:
c = do_if( hash, tp);
if (mcpp_debug & IF) {
mcpp_fprintf( DBG
, "#if (#elif, #ifdef, #ifndef) evaluate to %s.\n"
, compiling ? "TRUE" : "FALSE");
mcpp_fprintf( DBG, "line %ld: %s", src_line, infile->buffer);
}
if (c == FALSE) { /* Error */
compiling = FALSE; /* Skip this group */
goto skip_line; /* Prevent an extra error message */
}
break;
case L_else:
if (ifptr == &ifstack[0])
goto nest_err;
if (ifptr == infile->initif) {
goto in_file_nest_err;
}
if (ifptr->stat & ELSE_SEEN)
goto else_seen_err;
ifptr->stat |= ELSE_SEEN;
ifptr->elseline = src_line;
if (ifptr->stat & WAS_COMPILING) {
if (compiling || (ifptr->stat & TRUE_SEEN) != 0)
compiling = FALSE;
else
compiling = TRUE;
}
if ((mcpp_debug & MACRO_CALL) && (ifptr->stat & WAS_COMPILING)) {
sync_linenum();
mcpp_fprintf( OUT, "/*else %ld:%c*/\n", src_line
, compiling ? 'T' : 'F'); /* Show that #else is seen */
}
break;
case L_endif:
if (ifptr == &ifstack[0])
goto nest_err;
if (ifptr <= infile->initif) {
goto in_file_nest_err;
}
if (! compiling && (ifptr->stat & WAS_COMPILING))
wrong_line = TRUE;
compiling = (ifptr->stat & WAS_COMPILING);
if ((mcpp_debug & MACRO_CALL) && compiling) {
sync_linenum();
mcpp_fprintf( OUT, "/*endif %ld*/\n", src_line);
/* Show that #if block has ended */
}
--ifptr;
break;
case L_define:
do_define( FALSE, 0);
break;
case L_undef:
do_undef();
break;
case L_line:
if ((c = do_line()) > 0) {
src_line = c;
sharp( NULL, 0); /* Putout the new line number and file name */
infile->line = --src_line; /* Next line number is 'src_line' */
newlines = -1;
} else { /* Error already diagnosed by do_line() */
skip_nl();
}
break;
case L_include:
in_include = TRUE;
if (do_include( FALSE) == TRUE && file != infile)
newlines = -1; /* File has been included. Clear blank lines */
in_include = FALSE;
break;
case L_error:
cerror( infile->buffer, NULL, 0L, NULL); /* _E_ */
break;
case L_pragma:
do_pragma();
newlines = -1; /* Do not putout excessive '\n' */
break;
default: /* Non-Standard or unknown directives */
do_old();
break;
}
switch (hash) {
case L_if :
case L_elif :
case L_define :
case L_line :
goto skip_line; /* To prevent duplicate error message */
#if SYSTEM == SYS_MAC
case L_import :
if (file != infile) /* File has been included */
newlines = -1;
#endif
case L_error :
goto skip_line;
case L_include :
case L_pragma :
break; /* Already read over the line */
default : /* L_else, L_endif, L_undef, etc. */
if (skip_ws() != '\n') {
cerror( excess, infile->bptr-1, 0L, NULL);
}
skip_nl();
}
goto ret;
in_file_nest_err:
cerror( not_in_section, NULL, 0L, NULL);
goto skip_line;
nest_err:
cerror( "Not in a #if (#ifdef) section", NULL, 0L, NULL); /* _E_ */
goto skip_line;
else_seen_err:
cerror( "Already seen #else at line %.0s%ld" /* _E_ */
, NULL, ifptr->elseline, NULL);
skip_line:
skip_nl(); /* Ignore rest of line */
goto ret;
if_nest_err:
cfatal( many_nesting, NULL, (long) BLK_NEST, NULL);
ret:
in_directive = FALSE;
keep_comments = option_flags.c && compiling && !no_output;
/* keep_spaces is on for #define line even if no_output is TRUE */
if (! wrong_line)
newlines++;
}
static int do_if( int hash, const char * directive_name)
/*
* Process an #if (#elif), #ifdef or #ifndef. The latter two are straight-
* forward, while #if needs a subroutine to evaluate the expression.
* do_if() is called only if compiling is TRUE. If false, compilation is
* always supressed, so we don't need to evaluate anything. This supresses
* unnecessary warnings.
*/
{
int c;
int found;
DEFBUF * defp;
if ((c = skip_ws()) == '\n') {
unget_ch();
cerror( no_arg, NULL, 0L, NULL);
return FALSE;
}
if (mcpp_debug & MACRO_CALL) {
sync_linenum();
mcpp_fprintf( OUT, "/*%s %ld*/", directive_name, src_line);
}
if (hash == L_if) { /* #if or #elif */
unget_ch();
found = (eval_if() != 0L); /* Evaluate expression */
if (mcpp_debug & MACRO_CALL)
in_if = FALSE; /* 'in_if' is dynamically set in eval_lex() */
hash = L_ifdef; /* #if is now like #ifdef */
} else { /* #ifdef or #ifndef */
if (scan_token( c, (workp = work_buf, &workp), work_end) != NAM) {
cerror( not_ident, work_buf, 0L, NULL);
return FALSE; /* Next token is not an identifier */
}
found = ((defp = look_id( identifier)) != NULL); /* Look in table*/
if (mcpp_debug & MACRO_CALL) {
if (found)
mcpp_fprintf( OUT, "/*%s*/", defp->name);
}
}
if (found == (hash == L_ifdef)) {
compiling = TRUE;
ifptr->stat |= TRUE_SEEN;
} else {
compiling = FALSE;
}
if (mcpp_debug & MACRO_CALL) {
mcpp_fprintf( OUT, "/*i %c*/\n", compiling ? 'T' : 'F');
/* Report wheather the directive is evaluated TRUE or FALSE */
}
return TRUE;
}
static void sync_linenum( void)
/*
* Put out newlines or #line line to synchronize line number with the
* annotations about #if, #elif, #ifdef, #ifndef, #else or #endif on -K option.
*/
{
if (wrong_line || newlines > 10) {
sharp( NULL, 0);
} else {
while (newlines-- > 0)
mcpp_fputc('\n', OUT);
}
newlines = -1;
}
static long do_line( void)
/*
* Parse the line to update the line number and "filename" field for the next
* input line.
* Values returned are as follows:
* -1: syntax error or out-of-range error (diagnosed by do_line(),
* eval_num()).
* [1,32767]: legal line number for C90, [1,2147483647] for C99.
* Line number [32768,2147483647] in C90 mode is only warned (not an error).
* do_line() always absorbs the line (except the <newline>).
*/
{
const char * const not_digits
= "Line number \"%s\" isn't a decimal digits sequence"; /* _E_ _W1_ */
const char * const out_of_range
= "Line number \"%s\" is out of range of [1,%ld]"; /* _E_ _W1_ */
int token_type;
VAL_SIGN * valp;
char * save;
int c;
if ((c = skip_ws()) == '\n') {
cerror( no_arg, NULL, 0L, NULL);
unget_ch(); /* Push back <newline> */
return -1L; /* Line number is not changed */
}
token_type = get_unexpandable( c, FALSE);
if (macro_line == MACRO_ERROR) /* Unterminated macro */
return -1L; /* already diagnosed. */
if (token_type == NO_TOKEN) /* Macro expanded to 0 token */
goto no_num;
if (token_type != NUM)
goto illeg_num;
for (workp = work_buf; *workp != EOS; workp++) {
if (! isdigit( *workp & UCHARMAX)) {
cerror( not_digits, work_buf, 0L, NULL);
return -1L;
}
}
valp = eval_num( work_buf); /* Evaluate number */
if (valp->sign == VAL_ERROR) { /* Error diagnosed by eval_num()*/
return -1;
} else if ((std_limits.line_num < valp->val || valp->val <= 0L)) {
if (valp->val < LINE99LIMIT && valp->val > 0L) {
if (warn_level & 1)
cwarn( out_of_range, work_buf, std_limits.line_num, NULL);
} else {
cerror( out_of_range, work_buf, std_limits.line_num, NULL);
return -1L;
}
}
token_type = get_unexpandable( skip_ws(), FALSE);
if (macro_line == MACRO_ERROR)
return -1L;
if (token_type != STR) {
if (token_type == NO_TOKEN) { /* Filename is absent */
return (long) valp->val;
} else { /* Expanded macro should be a quoted string */
goto not_fname;
}
}
{
*(workp - 1) = EOS; /* Ignore right '"' */
save = save_string( &work_buf[ 1]); /* Ignore left '"' */
}
if (get_unexpandable( skip_ws(), FALSE) != NO_TOKEN) {
cerror( excess, work_buf, 0L, NULL);
free( save);
return -1L;
}
if (infile->filename)
free( infile->filename);
infile->filename = save; /* New file name */
/* Note that this does not change infile->real_fname */
return (long) valp->val; /* New line number */
no_num:
cerror( "No line number", NULL, 0L, NULL); /* _E_ */
return -1L;
illeg_num:
cerror( "Not a line number \"%s\"", work_buf, 0L, NULL); /* _E_ */
return -1L;
not_fname:
cerror( "Not a file name \"%s\"", work_buf, 0L, NULL); /* _E_ */
return -1L;
}
/*
* M a c r o D e f i n i t i o n s
*/
/*
* look_id() Looks for the name in the defined symbol table. Returns a
* pointer to the definition if found, or NULL if not present.
* install_macro() Installs the definition. Updates the symbol table.
* undefine() Deletes the definition from the symbol table.
*/
/*
* Global work_buf[] are used to store #define parameter lists and
* parms[].name point to them.
* 'nargs' contains the actual number of parameters stored.
*/
typedef struct {
char * name; /* -> Start of each parameter */
size_t len; /* Length of parameter name */
} PARM;
static PARM parms[ NMACPARS];
static int nargs; /* Number of parameters */
static char * token_p; /* Pointer to the token scanned */
static char * repl_base; /* Base of buffer for repl-text */
static char * repl_end; /* End of buffer for repl-text */
static const char * const no_ident = "No identifier"; /* _E_ */
DEFBUF * do_define(
int ignore_redef, /* Do not redefine */
int predefine /* Predefine compiler-specific name */
/*
* Note: The value of 'predefine' should be one of 0, DEF_NOARGS_PREDEF
* or DEF_NOARGS_PREDEF_OLD, the other values cause errors.
*/
)
/*
* Called from directive() when a #define is scanned or called from
* do_options() when a -D option is scanned. This module parses formal
* parameters by get_parm() and the replacement text by get_repl().
*
* There is some special case code to distinguish
* #define foo bar -- object-like macro
* from #define foo() bar -- function-like macro with no parameter
*
* Also, we make sure that
* #define foo foo
* expands to "foo" but doesn't put MCPP into an infinite loop.
*
* A warning is printed if you redefine a symbol with a non-identical
* text. I.e,
* #define foo 123
* #define foo 123
* is ok, but
* #define foo 123
* #define foo +123
* is not.
*
* The following subroutines are called from do_define():
* get_parm() parsing and remembering parameter names.
* get_repl() parsing and remembering replacement text.
*
* The following subroutines are called from get_repl():
* is_formal() is called when an identifier is scanned. It checks through
* the array of formal parameters. If a match is found, the
* identifier is replaced by a control byte which will be used
* to locate the parameter when the macro is expanded.
* def_stringization() is called when '#' operator is scanned. It surrounds
* the token to stringize with magic-codes.
*
* modes other than STD ignore difference of parameter names in macro
* redefinition.
*/
{
const char * const predef = "\"%s\" shouldn't be redefined"; /* _E_ */
char repl_list[ NMACWORK + IDMAX]; /* Replacement text */
char macroname[ IDMAX + 1]; /* Name of the macro defining */
DEFBUF * defp; /* -> Old definition */
DEFBUF ** prevp; /* -> Pointer to previous def in list */
int c;
int redefined; /* TRUE if redefined */
int dnargs = 0; /* defp->nargs */
int cmp; /* Result of name comparison */
size_t def_start, def_end; /* Column of macro definition */
repl_base = repl_list;
repl_end = & repl_list[ NMACWORK];
c = skip_ws();
if ((mcpp_debug & MACRO_CALL) && src_line) /* Start of definition */
def_start = infile->bptr - infile->buffer - 1;
if (c == '\n') {
cerror( no_ident, NULL, 0L, NULL);
unget_ch();
return NULL;
} else if (scan_token( c, (workp = work_buf, &workp), work_end) != NAM) {
cerror( not_ident, work_buf, 0L, NULL);
return NULL;
} else {
prevp = look_prev( identifier, &cmp);
/* Find place in the macro list to insert the definition */
defp = *prevp;
if (cmp || defp->push) { /* Not known or 'pushed' macro */
if (str_eq( identifier, "defined")
|| ((stdc_val)
&& str_eq( identifier, "__VA_ARGS__"))) {
cerror(
"\"%s\" shouldn't be defined", identifier, 0L, NULL); /* _E_ */
return NULL;
}
redefined = FALSE; /* Quite new definition */
} else { /* It's known: */
if (ignore_redef)
return defp;
dnargs = (defp->nargs == DEF_NOARGS_STANDARD
|| defp->nargs == DEF_NOARGS_PREDEF
|| defp->nargs == DEF_NOARGS_PREDEF_OLD)
? DEF_NOARGS : defp->nargs;
if (dnargs <= DEF_NOARGS_DYNAMIC /* __FILE__ and such */
|| dnargs == DEF_PRAGMA /* _Pragma() pseudo-macro */
) {
cerror( predef, identifier, 0L, NULL);
return NULL;
} else {
redefined = TRUE; /* Remember this fact */
}
}
}
strcpy( macroname, identifier); /* Remember the name */
in_define = TRUE; /* Recognize '#', '##' */
if (get_parm() == FALSE) { /* Get parameter list */
in_define = FALSE;
return NULL; /* Syntax error */
}
if (get_repl( macroname) == FALSE) { /* Get replacement text */
in_define = FALSE;
return NULL; /* Syntax error */
}
if ((mcpp_debug & MACRO_CALL) && src_line) {
/* Remember location on source */
char * cp;
cp = infile->bptr - 1; /* Before '\n' */
while (char_type[ *cp & UCHARMAX] & HSP)
cp--; /* Trailing space */
cp++; /* Just after the last token */
def_end = cp - infile->buffer; /* End of definition */
}
in_define = FALSE;
if (redefined) {
if (dnargs != nargs || ! str_eq( defp->repl, repl_list)
|| (! str_eq( defp->parmnames, work_buf))
) { /* Warn if differently redefined */
if (warn_level & 1) {
cwarn(
"The macro is redefined", NULL, 0L, NULL); /* _W1_ */
dump_a_def( " previously macro", defp, FALSE, TRUE
, fp_err);
}
} else { /* Identical redefinition */
return defp;
}
} /* Else new or re-definition*/
defp = install_macro( macroname, nargs, work_buf, repl_list, prevp, cmp
, predefine);
if ((mcpp_debug & MACRO_CALL) && src_line) {
/* Get location on source file */
LINE_COL s_line_col, e_line_col;
s_line_col.line = src_line;
s_line_col.col = def_start;
get_src_location( & s_line_col);
/* Convert to pre-line-splicing data */
e_line_col.line = src_line;
e_line_col.col = def_end;
get_src_location( & e_line_col);
/* Putout the macro definition information embedded in comment */
mcpp_fprintf( OUT, "/*m%s %ld:%d-%ld:%d*/\n", defp->name
, s_line_col.line, s_line_col.col
, e_line_col.line, e_line_col.col);
wrong_line = TRUE; /* Need #line later */
}
return defp;
}
static int get_parm( void)
/*
* Get parameters i.e. numbers into nargs, name into work_buf[], name-length
* into parms[].len. parms[].name point into work_buf.
* Return TRUE if the parameters are legal, else return FALSE.
* In STD mode preprocessor must remember the parameter names, only for
* checking the validity of macro redefinitions. This is required by the
* Standard (what an overhead !).
*/
{
const char * const many_parms
= "More than %.0s%ld parameters"; /* _E_ _W4_ */
const char * const illeg_parm
= "Illegal parameter \"%s\""; /* _E_ */
const char * const misplaced_ellip
= "\"...\" isn't the last parameter"; /* _E_ */
int token_type;
int c;
parms[ 0].name = workp = work_buf;
work_buf[ 0] = EOS;
/* POST_STD mode */
insert_sep = NO_SEP; /* Clear the inserted token separator */
c = get_ch();
if (c == '(') { /* With arguments? */
nargs = 0; /* Init parms counter */
if (skip_ws() == ')')
return TRUE; /* Macro with 0 parm */
else
unget_ch();
do { /* Collect parameters */
if (nargs >= NMACPARS) {
cerror( many_parms, NULL, (long) NMACPARS, NULL);
return FALSE;
}
parms[ nargs].name = workp; /* Save its start */
if ((token_type = scan_token( c = skip_ws(), &workp, work_end))
!= NAM) {
if (c == '\n') {
break;
} else if (c == ',' || c == ')') {
cerror( "Empty parameter", NULL, 0L, NULL); /* _E_ */
return FALSE;
} else if ((stdc_val)
&& token_type == OPE && openum == OP_ELL) {
/*
* Enable variable argument macro which is a feature of
* C99. We enable this even on C90 or C++ for GCC
* compatibility.
*/
if (skip_ws() != ')') {
cerror( misplaced_ellip, NULL, 0L, NULL);
return FALSE;
}
parms[ nargs++].len = 3;
nargs |= VA_ARGS;
goto ret;
} else {
cerror( illeg_parm, parms[ nargs].name, 0L, NULL);
return FALSE; /* Bad parameter syntax */
}
}
if ((stdc_val)
&& str_eq( identifier, "__VA_ARGS__")) {
cerror( illeg_parm, parms[ nargs].name, 0L, NULL);
return FALSE;
/* __VA_ARGS__ should not be used as a parameter */
}
if (is_formal( parms[ nargs].name, FALSE)) {
cerror( "Duplicate parameter name \"%s\"" /* _E_ */
, parms[ nargs].name, 0L, NULL);
return FALSE;
}
parms[ nargs].len = (size_t) (workp - parms[ nargs].name);
/* Save length of param */
*workp++ = ',';
nargs++;
} while ((c = skip_ws()) == ','); /* Get another parameter*/
*--workp = EOS; /* Remove excessive ',' */
if (c != ')') { /* Must end at ) */
unget_ch(); /* Push back '\n' */
cerror(
"Missing \",\" or \")\" in parameter list \"(%s\"" /* _E_ */
, work_buf, 0L, NULL);
return FALSE;
}
} else {
/*
* DEF_NOARGS is needed to distinguish between
* "#define foo" and "#define foo()".
*/
nargs = DEF_NOARGS; /* Object-like macro */
unget_ch();
}
ret:
#if NMACPARS > NMACPARS90MIN
if ((warn_level & 4) && (nargs & ~AVA_ARGS) > std_limits.n_mac_pars)
cwarn( many_parms, NULL , (long) std_limits.n_mac_pars, NULL);
#endif
return TRUE;
}
static int get_repl(
const char * macroname
)
/*
* Get replacement text i.e. names of formal parameters are converted to
* the magic numbers, and operators #, ## is converted to magic characters.
* Return TRUE if replacement list is legal, else return FALSE.
* Any token separator in the text is converted to a single space, no token
* sepatator is inserted by MCPP. Those are required by the Standard for
* stringizing of an argument by # operator.
* In POST_STD mode, inserts a space between any tokens in source (except a
* macro name and the next '(' in macro definition), hence presence or absence
* of token separator makes no difference.
*/
{
const char * const mixed_ops
= "Macro with mixing of ## and # operators isn't portable"; /* _W4_ */
const char * const multiple_cats
= "Macro with multiple ## operators isn't portable"; /* _W4_ */
char * prev_token = NULL; /* Preceding token */
char * prev_prev_token = NULL; /* Pre-preceding token */
int multi_cats = FALSE; /* Multiple ## operators*/
int c;
int token_type; /* Type of token */
char * temp;
char * repl_cur = repl_base; /* Pointer into repl-text buffer*/
*repl_cur = EOS;
token_p = NULL;
c = get_ch();
unget_ch();
if (((char_type[ c] & SPA) == 0) && (nargs < 0) && (warn_level & 1))
cwarn( "No space between macro name \"%s\" and repl-text"/* _W1_ */
, macroname, 0L, NULL);
c = skip_ws(); /* Get to the body */
while (c != '\n') {
prev_prev_token = prev_token;
prev_token = token_p;
token_p = repl_cur; /* Remember the pointer */
token_type = scan_token( c, &repl_cur, repl_end);
switch (token_type) {
case OPE: /* Operator or punctuator */
switch (openum) {
case OP_CAT: /* ## */
if (prev_token == NULL) {
cerror( "No token before ##" /* _E_ */
, NULL, 0L, NULL);
return FALSE;
} else if (*prev_token == CAT) {
cerror( "## after ##", NULL, 0L, NULL); /* _E_ */
return FALSE;
} else if (prev_prev_token && *prev_prev_token == CAT) {
multi_cats = TRUE;
} else if (prev_prev_token && *prev_prev_token == ST_QUOTE
&& (warn_level & 4)) { /* # parm ## */
cwarn( mixed_ops, NULL, 0L, NULL);
}
repl_cur = token_p;
*repl_cur++ = CAT; /* Convert to CAT */
break;
case OP_STR: /* # */
if (nargs < 0) /* In object-like macro */
break; /* '#' is an usual char */
if (prev_token && *prev_token == CAT
&& (warn_level & 4)) /* ## # */
cwarn( mixed_ops, NULL, 0L, NULL);
repl_cur = token_p; /* Overwrite on # */
if ((temp = def_stringization( repl_cur)) == NULL) {
return FALSE; /* Error */
} else {
repl_cur = temp;
}
break;
default: /* Any operator as it is */
break;
}
break;
case NAM:
/*
* Replace this name if it's a parm. Note that the macro name is a
* possible replacement token. We stuff DEF_MAGIC in front of the
* token which is treated as a LETTER by the token scanner and eaten
* by the macro expanding routine. This prevents the macro expander
* from looping if someone writes "#define foo foo".
*/
temp = is_formal( identifier, TRUE);
if (temp == NULL) { /* Not a parameter name */
if ((stdc_val)
&& str_eq( identifier, "__VA_ARGS__")) {
cerror( "\"%s\" without corresponding \"...\"" /* _E_ */
, identifier, 0L, NULL);
return FALSE;
}
if ((temp = mgtoken_save( macroname)) != NULL)
repl_cur = temp; /* Macro name */
} else { /* Parameter name */
repl_cur = temp;
}
break;
case STR: /* String in mac. body */
case CHR: /* Character constant */
break;
case SEP:
break;
default: /* Any token as it is */
break;
}
if ((c = get_ch()) == ' ' || c == '\t') {
*repl_cur++ = ' '; /* Space */
while ((c = get_ch()) == ' ' || c == '\t')
; /* Skip excessive spaces */
}
}
while (repl_base < repl_cur
&& (*(repl_cur - 1) == ' ' || *(repl_cur - 1) == '\t'))
repl_cur--; /* Remove trailing spaces */
*repl_cur = EOS; /* Terminate work */
unget_ch(); /* For syntax check */
if (token_p && *token_p == CAT) {
cerror( "No token after ##", NULL, 0L, NULL); /* _E_ */
return FALSE;
}
if (multi_cats && (warn_level & 4))
cwarn( multiple_cats, NULL, 0L, NULL);
if ((nargs & VA_ARGS) && stdc_ver < 199901L && (warn_level & 2))
/* Variable arg macro is the spec of C99, not C90 nor C++98 */
cwarn( "Variable argument macro is defined", /* _W2_ */
NULL, 0L, NULL);
return TRUE;
}
static char * is_formal(
const char * name,
int conv /* Convert to magic number? */
)
/*
* If the identifier is a formal parameter, save the MAC_PARM and formal
* offset, returning the advanced pointer into the replacement text.
* Else, return NULL.
*/
{
char * repl_cur;
const char * va_arg = "__VA_ARGS__";
PARM parm;
size_t len;
int i;
len = strlen( name);
for (i = 0; i < (nargs & ~AVA_ARGS); i++) { /* For each parameter */
parm = parms[ i];
if ((len == parm.len
/* Note: parms[].name are comma separated */
&& memcmp( name, parm.name, parm.len) == 0)
|| ((nargs & VA_ARGS)
&& i == (nargs & ~AVA_ARGS) - 1 && conv
&& str_eq( name, va_arg))) { /* __VA_ARGS__ */
/* If it's known */
if (conv) {
repl_cur = token_p; /* Overwrite on the name*/
*repl_cur++ = MAC_PARM; /* Save the signal */
*repl_cur++ = i + 1; /* Save the parm number */
return repl_cur; /* Return "gotcha" */
} else {
return parm.name; /* Duplicate parm name */
}
}
}
return NULL; /* Not a formal param */
}
static char * def_stringization( char * repl_cur)
/*
* Define token stringization.
* We store a magic cookie (which becomes surrouding " on expansion) preceding
* the parameter as an operand of # operator.
* Return the current pointer into replacement text if the token following #
* is a parameter name, else return NULL.
*/
{
int c;
char * temp;
*repl_cur++ = ST_QUOTE; /* Prefix */
if (char_type[ c = get_ch()] & HSP) { /* There is a space */
*repl_cur++ = ' ';
while (char_type[ c = get_ch()] & HSP) /* Skip excessive spaces*/
;
}
token_p = repl_cur; /* Remember the pointer */
if (scan_token( c, &repl_cur, repl_end) == NAM) {
if ((temp = is_formal( identifier, TRUE)) != NULL) {
repl_cur = temp;
return repl_cur;
}
}
cerror( "Not a formal parameter \"%s\"", token_p, 0L, NULL); /* _E_ */