-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathexpand.c
2283 lines (2153 loc) · 91.1 KB
/
expand.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.
*/
/*
* E X P A N D . C
* M a c r o E x p a n s i o n
*
* The macro expansion routines are placed here.
*/
#include "system.H"
#include "internal.H"
#define ARG_ERROR (-255)
#define CERROR 1
#define CWARN 2
typedef struct location { /* Where macro or arg locate */
long start_line; /* Beginning at 1 */
size_t start_col; /* Beginning at 0 */
long end_line;
size_t end_col;
} LOCATION;
typedef struct magic_seq { /* Data of a sequence inserted between tokens */
char * magic_start; /* First MAC_INF sequence */
char * magic_end; /* End of last MAC_INF seq */
int space; /* Space succeeds or not */
} MAGIC_SEQ;
static int compat_mode;
static char * expand_std( DEFBUF * defp, char * out, char * out_end
, LINE_COL line_col, int * pragma_op);
/* Expand a macro completely (for Standard modes) */
static DEFBUF * is_macro_call( DEFBUF * defp, char ** cp, char ** endf
, MAGIC_SEQ * mgc_seq); /* Is this really a macro call ? */
static int collect_args( const DEFBUF * defp, char ** arglist, int m_num);
/* Collect arguments of a macro call*/
static int get_an_arg( int c, char ** argpp, char * arg_end
, char ** seqp, int var_arg, int nargs, LOCATION ** locp, int m_num
, MAGIC_SEQ * mgc_prefix); /* Get an argument */
static int squeeze_ws( char ** out, char ** endf, MAGIC_SEQ * mgc_seq);
/* Squeeze white spaces to a space */
static void skip_macro( void);
/* Skip the rest of macro call */
static void diag_macro( int severity, const char * format
, const char * arg1, long arg2, const char * arg3, const DEFBUF * defp1
, const DEFBUF * defp2) ;
/* Supplement diagnostic information*/
static void dump_args( const char * why, int nargs, const char ** arglist);
/* Dump arguments list */
static int rescan_level; /* Times of macro rescan */
static const char * const macbuf_overflow
= "Buffer overflow expanding macro \"%s\" at %.0ld\"%s\""; /* _E_ */
static const char * const empty_arg
= "Empty argument in macro call \"%s\""; /* _W2_ */
static const char * const unterm_macro
= "Unterminated macro call \"%s\""; /* _E_ */
static const char * const narg_error
= "%s than necessary %ld argument(s) in macro call \"%s\""; /* _E_ _W1_ */
void expand_init(
int compat /* "Compatible" to GNUC expansion of recursive macro*/
)
/* Set expand_macro() function */
{
expand_macro = expand_std;
compat_mode = compat;
}
DEFBUF * is_macro(
char ** cp
)
/*
* The name is already in 'identifier', the next token is not yet read.
* Return the definition info if the name is a macro call, else return NULL.
*/
{
DEFBUF * defp;
if ((defp = look_id( identifier)) != NULL) /* Is a macro name */
return is_macro_call( defp, cp, NULL, NULL);
else
return NULL;
}
static DEFBUF * is_macro_call(
DEFBUF * defp,
char ** cp, /* Pointer to output buffer */
char ** endf, /* Pointer to indicate end of infile buffer */
MAGIC_SEQ * mgc_seq /* Infs on MAC_INF sequences and space */
)
/*
* Return DEFBUF if the defp->name is a macro call, else return NULL.
*/
{
int c;
if (defp->nargs >= 0 /* Function-like macro */
|| defp->nargs == DEF_PRAGMA) { /* _Pragma() pseudo-macro */
c = squeeze_ws( cp, endf, mgc_seq); /* See the next char. */
if (c == CHAR_EOF) /* End of file */
unget_string( "\n", NULL); /* Restore skipped '\n' */
else if (c != RT_END)
/* Still in the file and rescan boundary ? */
unget_ch(); /* To see it again */
if (c != '(') { /* Only the name of function-like macro */
return NULL;
}
}
return defp; /* Really a macro call */
}
/*
* expand_macro() expands a macro call completely, and writes out the result
* to the specified output buffer and returns the advanced pointer.
*/
/*
* T h e S T A N D A R D C o n f o r m i n g M o d e
* o f M a c r o E x p a n s i o n
*
* 1998/08 First released. kmatsui
*/
/* For debug of -K option: should be turned off on release version. */
#define DEBUG_MACRO_ANN FALSE
/* Return value of is_able_repl() */
#define NO 0 /* "Blue-painted" */
#define YES 1 /* Not blue-painted */
#define READ_OVER 2
/* Still "blue-painted", yet has read over repl-list */
/*
* Macros related to macro notification mode.
* The macro notification routines are hacks on the normal processing
* routines, and very complicated and cumbersome. Be sure to keep symmetry
* of starting and closing magic sequences. Enable the routines enclosed
* by #if 0 - #endif for debugging.
* Any byte in the sequences beginning with MAC_INF can coincide with any
* other character. Hence, the data stream should be read from the top,
* not from the tail, in principle.
*/
/* Length of sequence of MAC_INF, MAC_CALL_START, mac_num-1, mac_num-2 */
#define MAC_S_LEN 4
/* Length of sequence of MAC_INF, MAC_ARG_START, mac_num1, mac_num2, arg-num*/
#define ARG_S_LEN 5
#define MAC_E_LEN 2 /* Length of MAC_INF, MAC_CALL_END sequence */
#define ARG_E_LEN MAC_E_LEN /* Lenght of MAC_INF, MAC_ARG_END sequence */
#define MAC_E_LEN_V 4 /* Length of macro closing sequence in verbose mode */
/* MAC_INF, MAC_CALL_END, mac_num1, mac_num2 */
#define ARG_E_LEN_V 5 /* Length of argument closing sequence in verbose */
/* MAC_INF, MAC_ARG_END, mac_num1, mac_num2, arg_num */
#define IN_SRC_LEN 3 /* Length of sequence of IN_SRC, num-1, num-2 */
#define INIT_MAC_INF 0x100 /* Initial num of elements in mac_inf[] */
#define MAX_MAC_INF 0x1000 /* Maximum num of elements in mac_inf[] */
#define INIT_IN_SRC_NUM 0x100 /* Initial num of elements in in_src[] */
#define MAX_IN_SRC_NUM 0x1000 /* Maximum num of elements in in_src[] */
/* Variables for macro notification mode */
typedef struct macro_inf { /* Informations of a macro */
const DEFBUF * defp; /* Definition of the macro */
char * args; /* Arguments, if any */
int num_args; /* Number of real arguments */
int recur; /* Recurrence of this macro */
LOCATION locs; /* Location of macro call */
LOCATION * loc_args; /* Location of arguments */
} MACRO_INF;
static MACRO_INF * mac_inf;
static int max_mac_num; /* Current num of elements in mac_inf[] */
static int mac_num; /* Index into mac_inf[] */
static LOCATION * in_src; /* Location of identifiers in macro arguments */
static int max_in_src_num; /* Current num of elements in in_src[] */
static int in_src_num; /* Index into in_src[] */
static int trace_macro; /* Enable to trace macro infs */
static struct {
const DEFBUF * def; /* Macro definition */
int read_over; /* Has read over repl-list */
/* 'read_over' is never used in POST_STD mode and in compat_mode*/
} replacing[ RESCAN_LIMIT]; /* Macros currently replacing */
static int has_pragma = FALSE; /* Flag of _Pragma() operator */
static int print_macro_inf( int c, char ** cpp, char ** opp);
/* Embed macro infs into comments */
static char * print_macro_arg( char *out, MACRO_INF * m_inf, int argn
, int real_arg, int start);
/* Embed macro arg inf into comments*/
static char * replace( DEFBUF * defp, char * out, char * out_end
, const DEFBUF * outer, FILEINFO * rt_file, LINE_COL line_col
, int in_src_n);
/* Replace a macro recursively */
static char * close_macro_inf( char * out_p, int m_num, int in_src_n);
/* Put closing mark for a macro call*/
static DEFBUF * def_special( DEFBUF * defp);
/* Re-define __LINE__, __FILE__ */
static int prescan( const DEFBUF * defp, const char ** arglist
, char * out, char * out_end);
/* Process #, ## operator */
static char * catenate( const DEFBUF * defp, const char ** arglist
, char * out, char * out_end, char ** token_p);
/* Catenate tokens */
static char * remove_magics( const char * argp, int from_last);
/* Remove pair of magic characters */
#if DEBUG_MACRO_ANN
static void chk_symmetry( char * start_id, char * end_id, size_t len);
/* Check if a pair of magics are symmetrical */
#endif
static char * stringize( const DEFBUF * defp, const char * argp, char * out);
/* Stringize an argument */
static char * substitute( const DEFBUF * defp, const char ** arglist
, const char * in, char * out, char * out_end);
/* Substitute parms with arguments */
static char * rescan( const DEFBUF * outer, const char * in, char * out
, char * out_end);
/* Rescan once replaced sequences */
static int disable_repl( const DEFBUF * defp);
/* Disable the macro once replaced */
static void enable_repl( const DEFBUF * defp, int done);
/* Enable the macro for later use */
static int is_able_repl( const DEFBUF * defp);
/* Is the macro allowed to replace? */
static char * insert_to_bptr( char * ins, size_t len);
/* Insert a sequence into infile->bptr */
static char * expand_std(
DEFBUF * defp, /* Macro definition */
char * out, /* Output buffer */
char * out_end, /* End of output buffer */
LINE_COL line_col, /* Location of macro */
int * pragma_op /* _Pragma() is found ? */
)
/*
* Expand a macro call completely, write the results to the specified buffer
* and return the advanced output pointer.
*/
{
char macrobuf[ NMACWORK + IDMAX]; /* Buffer for replace() */
char * out_p = out;
size_t len;
int c, c1;
char * cp;
has_pragma = FALSE; /* Have to re-initialize*/
macro_line = src_line; /* Line number for diag */
macro_name = defp->name;
rescan_level = 0;
trace_macro = (mcpp_debug & MACRO_CALL)
&& ! in_directive;
if (trace_macro) {
max_mac_num = INIT_MAC_INF;
mac_inf = (MACRO_INF *) xmalloc( sizeof (MACRO_INF) * max_mac_num);
memset( mac_inf, 0, sizeof (MACRO_INF) * max_mac_num);
max_in_src_num = INIT_IN_SRC_NUM;
in_src = (LOCATION *) xmalloc( sizeof (LOCATION) * max_in_src_num);
memset( in_src, 0, sizeof (LOCATION) * max_in_src_num);
mac_num = in_src_num = 0; /* Initialize */
}
if (replace( defp, macrobuf, macrobuf + NMACWORK, NULL, infile, line_col
, 0) == NULL) { /* Illegal macro call */
skip_macro();
macro_line = MACRO_ERROR;
goto exp_end;
}
len = (size_t) (out_end - out);
if (strlen( macrobuf) > len) {
cerror( macbuf_overflow, macro_name, 0, macrobuf);
memcpy( out, macrobuf, len);
out_p = out + len;
macro_line = MACRO_ERROR;
goto exp_end;
}
#if DEBUG_MACRO_ANN
chk_magic_balance( macrobuf, macrobuf + strlen( macrobuf), FALSE, TRUE);
#endif
cp = macrobuf;
c1 = '\0'; /* The char previous to 'c' */
while ((c = *cp++) != EOS) {
if (c == DEF_MAGIC)
continue; /* Skip DEF_MAGIC */
if (c == IN_SRC) { /* Skip IN_SRC */
if (trace_macro)
cp += 2; /* Skip also the number (coded in 2 bytes) */
continue;
} else if (c == TOK_SEP) {
/* Remove redundant token separator */
if ((char_type[ c1 & UCHARMAX] & HSP)
|| (char_type[ *cp & UCHARMAX] & HSP)
|| in_include
|| (*cp == MAC_INF && *(cp + 1) == MAC_CALL_END)
|| (c1 == MAC_CALL_END))
continue;
/* Skip separator just after ' ', '\t' */
/* and just after MAC_CALL_END. */
/* Also skip this in lang_asm mode, #include */
/* Skip just before another TOK_SEP, ' ', '\t' */
/* Skip just before MAC_INF,MAC_CALL_END seq too*/
else
c = ' '; /* Else convert to ' ' */
} else if (trace_macro && (c == MAC_INF)) {
/* Embed macro expansion informations into comments */
c = *cp++;
c1 = print_macro_inf( c, &cp, &out_p);
if (out_end <= out_p) {
cerror( macbuf_overflow, macro_name, 0, out);
macro_line = MACRO_ERROR;
goto exp_end;
}
continue;
}
*out_p++ = c1 = c;
}
macro_line = 0;
exp_end:
*out_p = EOS;
if (mcpp_debug & EXPAND)
dump_string( "expand_std exit", out);
macro_name = NULL;
clear_exp_mac(); /* Clear the information for diagnostic */
if (trace_macro) { /* Clear macro informations */
int num;
for (num = 1; num < mac_num; num++) { /* 'num' start at 1 */
if (mac_inf[ num].num_args >= 0) { /* Macro with args */
free( mac_inf[ num].args); /* Saved arguments */
free( mac_inf[ num].loc_args); /* Location of args */
}
}
free( mac_inf);
free( in_src);
}
*pragma_op = has_pragma;
return out_p;
}
static int print_macro_inf(
int c,
char ** cpp, /* Magic character sequence */
char ** opp /* Output for macro information */
)
/*
* Embed macro expansion information into comments.
* Enabled by '#pragma MCPP debug macro_call' or -K option in STD mode.
*/
{
MACRO_INF * m_inf = NULL;
int num;
int num_args; /* Number of actual args (maybe less than expected) */
int i;
if (*((*opp) - 1) == '/' && *((*opp) - 2) != '*')
/* Immediately preceding token is '/' (not '*' and '/') */
*((*opp)++) = ' ';
if (c == MAC_CALL_START || c == MAC_ARG_START) {
num = ((*(*cpp)++ & UCHARMAX) - 1) * UCHARMAX;
num += (*(*cpp)++ & UCHARMAX) - 1;
m_inf = & mac_inf[ num]; /* Saved information */
}
switch (c) {
case MAC_CALL_START : /* Start of a macro expansion */
*opp += sprintf( *opp, "/*<%s", m_inf->defp->name); /* Macro name */
if (m_inf->locs.start_line) {
/* Location of the macro call in source file */
*opp += sprintf( *opp, " %ld:%d-%ld:%d"
, m_inf->locs.start_line, (int) m_inf->locs.start_col
, m_inf->locs.end_line, (int) m_inf->locs.end_col);
}
*opp = stpcpy( *opp, "*/");
if ((num_args = m_inf->num_args) >= 1) {
/* The macro has arguments. Show the locations. */
for (i = 0; i < num_args; i++) /* Arg num begins at 0 */
*opp = print_macro_arg( *opp, m_inf, i, TRUE, TRUE);
}
break;
case MAC_ARG_START : /* Start of an argument */
i = (*(*cpp)++ & UCHARMAX) - 1; /* Argument number */
*opp = print_macro_arg( *opp, m_inf, i, FALSE, TRUE);
break;
case MAC_CALL_END : /* End of a macro expansion */
/* Else fall through */
case MAC_ARG_END : /* End of an argument */
*opp = stpcpy( *opp, "/*>*/");
break;
}
return **cpp & UCHARMAX;
}
static char * print_macro_arg(
char * out, /* Output buffer */
MACRO_INF * m_inf, /* &mac_inf[ m_num] */
int argn, /* Argument number */
int real_arg, /* Real argument or expanded argument ? */
int start /* Start of an argument or end ? */
)
/*
* Embed an argument information into a comment.
* This routine is only called from above print_macro_inf().
*/
{
LOCATION * loc = m_inf->loc_args + argn;
out += sprintf( out, "/*%s%s:%d-%d", real_arg ? "!" : (start ? "<" : "")
, m_inf->defp->name, m_inf->recur, argn);
if (real_arg && m_inf->loc_args && loc->start_line) {
/* Location of the argument in source file */
out += sprintf( out, " %ld:%d-%ld:%d", loc->start_line
, (int) loc->start_col, loc->end_line, (int) loc->end_col);
}
if (! start) /* End of an argument in verbose mode */
out = stpcpy( out, ">");
out = stpcpy( out, "*/");
return out;
}
static char * replace(
DEFBUF * defp, /* Macro to be replaced */
char * out, /* Output Buffer */
char * out_end, /* End of output buffer */
const DEFBUF * outer, /* Outer macro replacing*/
FILEINFO * rt_file, /* Repl-text "file" */
LINE_COL line_col, /* Location of macro */
int in_src_n /* Index into in_src[] */
)
/*
* Replace a possibly nested macro recursively.
* replace() and rescan() call each other recursively.
* Return the advanced output pointer or NULL on error.
*/
{
char ** arglist = NULL; /* Pointers to arguments*/
int nargs; /* Number of arguments expected */
char * catbuf; /* Buffer for prescan() */
char * expbuf; /* Buffer for substitute() */
char * out_p; /* Output pointer */
char * cur_out = out; /* One more output pointer */
int num_args;
/* Number of actual arguments (maybe less than expected) */
int enable_trace_macro; /* To exclude _Pragma() pseudo macro */
int m_num = 0; /* 'mac_num' of current macro */
MACRO_INF * m_inf = NULL; /* Pointer into mac_inf[] */
if (mcpp_debug & EXPAND) {
dump_a_def( "replace entry", defp, FALSE, TRUE, fp_debug);
dump_unget( "replace entry");
}
if ((mcpp_debug & MACRO_CALL) && in_if)
mcpp_fprintf( OUT, "/*%s*/", defp->name);
enable_trace_macro = trace_macro && defp->nargs != DEF_PRAGMA;
if (enable_trace_macro) {
int num;
int recurs;
if (mac_num >= MAX_MAC_INF - 1) {
cerror( "Too many nested macros in tracing %s" /* _E_ */
, defp->name, 0L, NULL);
return NULL;
} else if (mac_num >= max_mac_num - 1) {
size_t len = sizeof (MACRO_INF) * max_mac_num;
/* Enlarge the array */
mac_inf = (MACRO_INF *) xrealloc( (char *) mac_inf, len * 2);
memset( mac_inf + max_mac_num, 0, len);
/* Clear the latter half */
max_mac_num *= 2;
}
m_num = ++mac_num; /* Remember this number */
/* Note 'mac_num' starts at 1 */
*cur_out++ = MAC_INF; /* Embed a magic char */
*cur_out++ = MAC_CALL_START; /* A macro call */
/* Its index number, can be greater than UCHARMAX */
/* We represent the number by 2 bytes where each byte is not '\0' */
*cur_out++ = (m_num / UCHARMAX) + 1;
*cur_out++ = (m_num % UCHARMAX) + 1;
*cur_out = EOS;
m_inf = & mac_inf[ m_num];
m_inf->defp = defp; /* The macro definition */
m_inf->num_args = 0; /* Default num of args */
if (line_col.line) {
get_src_location( & line_col);
m_inf->locs.start_line = line_col.line;
m_inf->locs.start_col = line_col.col;
} else {
m_inf->locs.start_col = m_inf->locs.start_line = 0L;
}
m_inf->args = NULL;
m_inf->loc_args = NULL; /* Default args */
for (num = 1, recurs = 0; num < m_num; num++)
if (mac_inf[ num].defp == defp)
recurs++; /* Recursively nested macro */
m_inf->recur = recurs;
}
nargs = (defp->nargs == DEF_PRAGMA) ? 1 : (defp->nargs & ~AVA_ARGS);
if (nargs < DEF_NOARGS_DYNAMIC) { /* __FILE__, __LINE__ */
defp = def_special( defp); /* These are redefined dynamically */
/* Wrap repl-text with token separators to prevent token merging */
*cur_out++ = TOK_SEP;
cur_out = stpcpy( cur_out, defp->repl);
*cur_out++ = TOK_SEP;
*cur_out = EOS;
if (enable_trace_macro) {
m_inf->defp = defp; /* Redefined dynamically*/
cur_out = close_macro_inf( cur_out, m_num, in_src_n);
}
return cur_out;
} else if (nargs == DEF_NOARGS_PREDEF_OLD
&& (warn_level & 1)) { /* Some macros on GCC */
cwarn( "Old style predefined macro \"%s\" is used", /* _W2_ */
defp->name, 0L, NULL);
} else if (nargs >= 0) { /* Function-like macro */
squeeze_ws( NULL, NULL, NULL); /* Skip to '(' */
/* Magic sequences are already read over by is_macro_call() */
arglist = (char **) xmalloc( (nargs + 1) * sizeof (char *));
arglist[ 0] = xmalloc( (size_t) (NMACWORK + IDMAX * 2));
/* Note: arglist[ n] may be reallocated */
/* and re-written by collect_args() */
if ((num_args = collect_args( defp, arglist, m_num)) == ARG_ERROR) {
free( arglist[ 0]); /* Syntax error */
free( arglist);
return NULL;
}
if (enable_trace_macro) {
/* Save the arglist for later informations */
m_inf->args = arglist[ 0];
m_inf->num_args = num_args; /* Number of actual args*/
}
if (outer && rt_file != infile) {
/* Has read over replacement-text */
if (compat_mode) {
enable_repl( outer, FALSE); /* Enable re-expansion */
if (mcpp_debug & EXPAND)
dump_string( "enabled re-expansion"
, outer ? outer->name : "<arg>");
} else {
replacing[ rescan_level-1].read_over = READ_OVER;
}
}
}
catbuf = xmalloc( (size_t) (NMACWORK + IDMAX));
if (mcpp_debug & EXPAND) {
mcpp_fprintf( DBG, "(%s)", defp->name);
dump_string( "prescan entry", defp->repl);
}
if (prescan( defp, (const char **) arglist, catbuf, catbuf + NMACWORK)
== FALSE) { /* Process #, ## operators */
diag_macro( CERROR, macbuf_overflow, defp->name, 0L, catbuf, defp
, NULL);
if (nargs >= 0) {
if (! enable_trace_macro)
/* arglist[0] is needed for macro infs */
free( arglist[ 0]);
free( arglist);
}
free( catbuf);
return NULL;
}
catbuf = xrealloc( catbuf, strlen( catbuf) + 1);
/* Use memory sparingly */
if (mcpp_debug & EXPAND) {
mcpp_fprintf( DBG, "(%s)", defp->name);
dump_string( "prescan exit", catbuf);
}
if (nargs > 0) { /* Function-like macro with any argument */
expbuf = xmalloc( (size_t) (NMACWORK + IDMAX));
if (mcpp_debug & EXPAND) {
mcpp_fprintf( DBG, "(%s)", defp->name);
dump_string( "substitute entry", catbuf);
}
out_p = substitute( defp, (const char **) arglist, catbuf, expbuf
, expbuf + NMACWORK); /* Expand each arguments */
if (! enable_trace_macro)
free( arglist[ 0]);
free( arglist);
free( catbuf);
expbuf = xrealloc( expbuf, strlen( expbuf) + 1);
/* Use memory sparingly */
if (mcpp_debug & EXPAND) {
mcpp_fprintf( DBG, "(%s)", defp->name);
dump_string( "substitute exit", expbuf);
}
} else { /* Object-like macro or */
if (nargs == 0 && ! enable_trace_macro)
/* Function-like macro with no argument */
free( arglist[ 0]);
free( arglist);
out_p = expbuf = catbuf;
}
if (out_p)
out_p = rescan( defp, expbuf, cur_out, out_end);
if (out_p && defp->nargs == DEF_PRAGMA)
has_pragma = TRUE;
/* Inform mcpp_main() that _Pragma() was found */
free( expbuf);
if (enable_trace_macro && out_p)
out_p = close_macro_inf( out_p, m_num, in_src_n);
if (mcpp_debug & EXPAND)
dump_string( "replace exit", out);
if (trace_macro && defp->nargs == DEF_PRAGMA) {
/* Remove intervening magics if the macro is _Pragma pseudo-macro */
/* These magics have been inserted by macros in _Pragma()'s args */
int c;
cur_out = out_p = out;
while ((c = *cur_out++) != EOS) {
if (c == MAC_INF) {
switch (*cur_out) {
case MAC_ARG_START :
cur_out++;
/* Fall through */
case MAC_CALL_START :
cur_out++;
cur_out++;
/* Fall through */
default:
cur_out++;
break;
}
} else {
*out_p++ = c;
}
}
*out_p = EOS;
}
return out_p;
}
static char * close_macro_inf(
char * out_p, /* Current output pointer */
int m_num, /* 'mac_num' of this macro */
int in_src_n /* Location of macro in arg */
)
/*
* Mark up closing of a macro expansion.
* Note that 'm_num' argument is necessary rather than 'm_inf' from replace(),
* because mac_inf[] may have been reallocated while rescanning.
*/
{
MACRO_INF * m_inf;
LINE_COL e_line_col;
m_inf = & mac_inf[ m_num];
*out_p++ = MAC_INF; /* Magic for end of macro expansion */
*out_p++ = MAC_CALL_END;
*out_p = EOS;
get_ch(); /* Clear the garbage */
unget_ch();
if (infile->fp || in_src_n) {
if (infile->fp) { /* Macro call on source file */
e_line_col.line = src_line;
e_line_col.col = infile->bptr - infile->buffer;
} else { /* Macro in argument of parent macro and from source */
e_line_col.line = in_src[ in_src_n].end_line;
e_line_col.col = in_src[ in_src_n].end_col;
}
/* Get the location before line splicing by <backslash><newline> */
/* or by a line-crossing comment */
get_src_location( & e_line_col);
m_inf->locs.end_line = e_line_col.line;
m_inf->locs.end_col = e_line_col.col;
} else {
m_inf->locs.end_col = m_inf->locs.end_line = 0L;
}
return out_p;
}
static DEFBUF * def_special(
DEFBUF * defp /* Macro definition */
)
/*
* Re-define __LINE__, __FILE__.
* Return the new definition.
*/
{
const FILEINFO * file;
DEFBUF ** prevp;
int cmp;
switch (defp->nargs) {
case DEF_NOARGS_DYNAMIC - 1: /* __LINE__ */
if ((src_line > std_limits.line_num || src_line <= 0)
&& (warn_level & 1))
diag_macro( CWARN
, "Line number %.0s\"%ld\" is out of range" /* _W1_ */
, NULL, src_line, NULL, defp, NULL);
sprintf( defp->repl, "%ld", src_line); /* Re-define */
break;
case DEF_NOARGS_DYNAMIC - 2: /* __FILE__ */
for (file = infile; file != NULL; file = file->parent) {
if (file->fp != NULL) {
sprintf( work_buf, "\"%s\"", file->filename);
if (str_eq( work_buf, defp->repl))
break; /* No change */
defp->nargs = DEF_NOARGS; /* Enable to redefine */
prevp = look_prev( defp->name, &cmp);
defp = install_macro( "__FILE__", DEF_NOARGS_DYNAMIC - 2, ""
, work_buf, prevp, cmp, 0); /* Re-define */
break;
}
}
break;
}
return defp;
}
static int prescan(
const DEFBUF * defp, /* Definition of the macro */
const char ** arglist, /* Pointers to actual arguments */
char * out, /* Output buffer */
char * out_end /* End of output buffer */
)
/*
* Concatenate the tokens surounding ## by catenate(), and stringize the
* argument following # by stringize().
*/
{
FILEINFO * file;
char * prev_token = NULL; /* Preceding token */
char * horiz_space = NULL; /* Horizontal white space */
int c; /* Value of a character */
/*
* The replacement lists are --
* stuff1<SEP>stuff2
* or stuff1<SEP>stuff2<SEP>stuff3...
* where <SEP> is CAT, maybe with preceding space and following space,
* stuff might be
* ordinary-token
* MAC_PARM<n>
* or <QUO>MAC_PARM<n>
* where <QUO> is ST_QUO, possibly with following space.
*
* DEF_MAGIC may has been inserted sometimes.
* In other than POST_STD modes, TOK_SEP and IN_SRC may have been
* inserted, and TOK_SEPs are inserted also in this routine.
* In trace_macro mode, many magic character sequences may have been
* inserted here and there.
*/
*out++ = TOK_SEP; /* Wrap replacement */
workp = work_buf; /* text with token */
workp = stpcpy( workp, defp->repl); /* separators to */
*workp++ = TOK_SEP; /* prevent unintended*/
*workp = EOS; /* token merging. */
file = unget_string( work_buf, defp->name);
while (c = get_ch(), file == infile) { /* To the end of repl */
switch (c) {
case ST_QUOTE:
skip_ws(); /* Skip spaces and the returned MAC_PARM*/
c = get_ch() - 1; /* Parameter number */
prev_token = out; /* Remember the token */
out = stringize( defp, arglist[ c], out);
/* Stringize without expansion */
horiz_space = NULL;
break;
case CAT:
if (*prev_token == DEF_MAGIC || *prev_token == IN_SRC) {
/* Rare case yet possible after catenate() */
size_t len = 1;
/* Remove trailing white space prior to removing DEF_MAGIC */
if (horiz_space == out - 1) {
*--out = EOS;
horiz_space = NULL;
}
if (*prev_token == IN_SRC && trace_macro)
len = IN_SRC_LEN;
memmove( prev_token, prev_token + len
, strlen( prev_token + len));
out -= len;
*out = EOS; /* Remove DEF_MAGIC, IN_SRC */
}
if (horiz_space == out - 1) {
*--out = EOS; /* Remove trailing white space */
horiz_space = NULL;
}
out = catenate( defp, arglist, out, out_end, &prev_token);
if (char_type[ *(out - 1) & UCHARMAX] & HSP)
horiz_space = out - 1; /* TOK_SEP has been appended */
break;
case MAC_PARM:
prev_token = out;
*out++ = MAC_PARM;
*out++ = get_ch(); /* Parameter number */
break;
case TOK_SEP:
case ' ':
case '\t' :
if (out - 1 == horiz_space)
continue; /* Squeeze white spaces */
horiz_space = out;
*out++ = c;
break;
default:
prev_token = out;
scan_token( c, &out, out_end); /* Ordinary token */
break;
}
*out = EOS; /* Ensure termination */
if (out_end <= out) /* Buffer overflow */
return FALSE;
}
*out = EOS; /* Ensure terminatation in case of no token */
unget_ch();
return TRUE;
}
static char * catenate(
const DEFBUF * defp, /* The macro definition */
const char ** arglist, /* Pointers to actual arguments */
char * out, /* Output buffer */
char * out_end, /* End of output buffer */
char ** token_p /* Address of preceding token pointer */
)
/*
* Concatenate the previous and the following tokens.
* Note: The parameter codes may coincide with white spaces or any
* other characters.
*/
{
char * prev_prev_token = NULL;
const char * invalid_token
= "Not a valid preprocessing token \"%s\""; /* _E_ _W2_ */
const char * argp; /* Pointer to an actual argument*/
char * prev_token = *token_p; /* Preceding token */
int in_arg = FALSE;
int c; /* Value of a character */
/* Get the previous token */
if (*prev_token == MAC_PARM) { /* Formal parameter */
c = (*(prev_token + 1) & UCHARMAX) - 1; /* Parm number */
argp = arglist[ c]; /* Actual argument */
out = prev_token; /* To overwrite */
if (trace_macro)
argp = remove_magics( argp, TRUE); /* Remove pair of magics */
if (argp == (char*)RT_END) {
*out = EOS; /* An empty argument */
} else {
unget_string( argp, NULL);
if (trace_macro)
free( (char *) argp);
/* malloc()ed in remove_magics() */
while ((c = get_ch()) != RT_END) {
prev_prev_token = prev_token;
prev_token = out; /* Remember the last token */
scan_token( c, &out, out_end);
} /* Copy actual argument without expansion */
if (*prev_token == TOK_SEP) {
out = prev_token;
prev_token = prev_prev_token; /* Skip separator */
}
if (*prev_token == DEF_MAGIC
|| (*prev_token == IN_SRC)) {
size_t len = 1;
if (trace_macro && *prev_token == IN_SRC)
len = IN_SRC_LEN;
memmove( prev_token, prev_token + len
, (size_t) ((out -= len) - prev_token));
/* Remove DEF_MAGIC enabling the name to replace later */
}
}
} /* Else the previous token is an ordinary token, not an argument */
c = skip_ws();
/* Catenate */
switch (c) {
case ST_QUOTE: /* First stringize and then catenate */
skip_ws(); /* Skip MAC_PARM, ST_QUOTE */
c = get_ch() - 1;
out = stringize( defp, arglist[ c], out);
break;
case MAC_PARM:
c = get_ch() - 1; /* Parameter number */
argp = arglist[ c]; /* Actual argument */
if (trace_macro)
argp = remove_magics( argp, FALSE); /* Remove pair of magics */
if (*argp == RT_END) {
*out = EOS; /* An empty argument */
} else {
unget_string( argp, NULL);
if (trace_macro)
free( (char *) argp);
if ((c = get_ch()) == DEF_MAGIC) { /* Remove DEF_MAGIC */
c = get_ch(); /* enabling to replace */
} else if (c == IN_SRC) { /* Remove IN_SRC */
if (trace_macro) {
get_ch(); /* Also its number */
get_ch();
}
c = get_ch();
}
scan_token( c, &out, out_end); /* The first token */
if (*infile->bptr) /* There are more tokens*/
in_arg = TRUE;
}
break;
case IN_SRC:
if (trace_macro) {
get_ch();
get_ch();
}
/* Fall through */
case DEF_MAGIC:
c = get_ch(); /* Skip DEF_MAGIC, IN_SRC */
/* Fall through */
default:
scan_token( c, &out, out_end); /* Copy the token */
break;
}
/* The generated sequence is a valid preprocessing-token ? */
if (*prev_token) { /* There is any token */
unget_string( prev_token, NULL); /* Scan once more */
c = get_ch(); /* This line should be before the next line. */
infile->fp = (FILE *)-1; /* To check token length*/
if (mcpp_debug & EXPAND)
dump_string( "checking generated token", infile->buffer);
scan_token( c, (workp = work_buf, &workp), work_end);
infile->fp = NULL;
if (*infile->bptr != EOS) { /* More than a token */
diag_macro( CERROR, invalid_token, prev_token, 0L, NULL, defp
, NULL);
infile->bptr += strlen( infile->bptr);
}
get_ch(); /* To the parent "file" */
unget_ch();
}
*out++ = TOK_SEP; /* Prevent token merging*/
*out = EOS;
if (in_arg) { /* There are more tokens after the generated one */
while ((c = get_ch()) != RT_END) {
if (c == TOK_SEP)
continue; /* Skip separator */
prev_token = out; /* Remember the last token */
scan_token( c, &out, out_end);
} /* Copy rest of argument without expansion */
}
*token_p = prev_token; /* Report back the last token */
return out;
}
static char * remove_magics(
const char * argp, /* The argument list */
int from_last /* token is the last or first? */
)
/*
* Remove pair of magic character sequences in an argument in order to catenate
* the last or first token to another.
* Or remove pair of magic character sequences surrounding an argument in order
* to keep symmetry of magics.
*/
{
#define INIT_MAGICS 128