This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmacro.c
1921 lines (1821 loc) · 46.8 KB
/
macro.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
/*
* (c) Thomas Pornin 1999 - 2002
*
* 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.
* 4. The name of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT 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 AUTHORS OR CONTRIBUTORS 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.
*
*/
#include "tune.h"
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <limits.h>
#include "ucppi.h"
#include "mem.h"
#include "nhash.h"
/*
* we store macros in a hash table, and retrieve them using their name
* as identifier.
*/
static HTT macros;
static int macros_init_done = 0;
static void del_macro(void *m)
{
struct macro *n = m;
size_t i;
for (i = 0; (int)i < n->narg; i ++) freemem(n->arg[i]);
if (n->narg > 0) freemem(n->arg);
#ifdef LOW_MEM
if (n->cval.length) freemem(n->cval.t);
#else
if (n->val.nt) {
for (i = 0; i < n->val.nt; i ++)
if (S_TOKEN(n->val.t[i].type))
freemem(n->val.t[i].name);
freemem(n->val.t);
}
#endif
freemem(n);
}
static inline struct macro *new_macro(void)
{
struct macro *m = getmem(sizeof(struct macro));
m->narg = -1;
m->nest = 0;
#ifdef LOW_MEM
m->cval.length = 0;
#else
m->val.nt = m->val.art = 0;
#endif
m->vaarg = 0;
return m;
}
/*
* for special macros, and the "defined" operator
*/
enum {
MAC_NONE, MAC_DEFINED,
MAC_LINE, MAC_FILE, MAC_DATE, MAC_TIME, MAC_STDC, MAC_PRAGMA
};
#define MAC_SPECIAL MAC_LINE
/*
* returns 1 for "defined"
* returns x > 1 for a special macro such as __FILE__
* returns 0 otherwise
*/
static inline int check_special_macro(char *name)
{
if (!strcmp(name, "defined")) return MAC_DEFINED;
if (*name != '_') return MAC_NONE;
if (*(name + 1) == 'P') {
if (!strcmp(name, "_Pragma")) return MAC_PRAGMA;
return MAC_NONE;
} else if (*(name + 1) != '_') return MAC_NONE;
if (no_special_macros) return MAC_NONE;
if (!strcmp(name, "__LINE__")) return MAC_LINE;
else if (!strcmp(name, "__FILE__")) return MAC_FILE;
else if (!strcmp(name, "__DATE__")) return MAC_DATE;
else if (!strcmp(name, "__TIME__")) return MAC_TIME;
else if (!strcmp(name, "__STDC__")) return MAC_STDC;
return MAC_NONE;
}
int c99_compliant = 1;
int c99_hosted = 1;
/*
* add the special macros to the macro table
*/
static void add_special_macros(void)
{
struct macro *m;
HTT_put(¯os, new_macro(), "__LINE__");
HTT_put(¯os, new_macro(), "__FILE__");
HTT_put(¯os, new_macro(), "__DATE__");
HTT_put(¯os, new_macro(), "__TIME__");
HTT_put(¯os, new_macro(), "__STDC__");
m = new_macro(); m->narg = 1;
m->arg = getmem(sizeof(char *)); m->arg[0] = sdup("foo");
HTT_put(¯os, m, "_Pragma");
if (c99_compliant) {
#ifndef LOW_MEM
struct token t;
#endif
m = new_macro();
#ifdef LOW_MEM
m->cval.t = getmem(9);
m->cval.t[0] = NUMBER;
mmv(m->cval.t + 1, "199901L", 8);
m->cval.length = 9;
#else
t.type = NUMBER;
t.line = 0;
t.name = sdup("199901L");
aol(m->val.t, m->val.nt, t, TOKEN_LIST_MEMG);
#endif
HTT_put(¯os, m, "__STDC_VERSION__");
}
if (c99_hosted) {
#ifndef LOW_MEM
struct token t;
#endif
m = new_macro();
#ifdef LOW_MEM
m->cval.t = getmem(3);
m->cval.t[0] = NUMBER;
mmv(m->cval.t + 1, "1", 2);
m->cval.length = 3;
#else
t.type = NUMBER;
t.line = 0;
t.name = sdup("1");
aol(m->val.t, m->val.nt, t, TOKEN_LIST_MEMG);
#endif
HTT_put(¯os, m, "__STDC_HOSTED__");
}
}
#ifdef LOW_MEM
/*
* We store macro arguments as a single-byte token MACROARG, followed
* by the argument number as a one or two-byte value. If the argument
* number is between 0 and 127 (inclusive), it is stored as such in
* a single byte. Otherwise, it is supposed to be a 14-bit number, with
* the 7 upper bits stored in the first byte (with the high bit set to 1)
* and the 7 lower bits in the second byte.
*/
#endif
/*
* print the content of a macro, in #define form
*/
static void print_macro(void *vm)
{
struct macro *m = vm;
char *mname = HASH_ITEM_NAME(m);
int x = check_special_macro(mname);
size_t i;
if (x != MAC_NONE) {
fprintf(emit_output, "/* #define %s */ /* special */\n",
mname);
return;
}
fprintf(emit_output, "#define %s", mname);
if (m->narg >= 0) {
fprintf(emit_output, "(");
for (i = 0; i < (size_t)(m->narg); i ++) {
fprintf(emit_output, i ? ", %s" : "%s", m->arg[i]);
}
if (m->vaarg) {
fputs(m->narg ? ", ..." : "...", emit_output);
}
fprintf(emit_output, ")");
}
#ifdef LOW_MEM
if (m->cval.length == 0) {
fputc('\n', emit_output);
return;
}
fputc(' ', emit_output);
for (i = 0; i < m->cval.length;) {
int tt = m->cval.t[i ++];
if (tt == MACROARG) {
unsigned anum = m->cval.t[i];
if (anum >= 128) anum = ((anum & 127U) << 8)
| m->cval.t[++ i];
if (anum == (unsigned)m->narg)
fputs("__VA_ARGS__", emit_output);
else
fputs(m->arg[anum], emit_output);
i ++;
}
else if (S_TOKEN(tt)) {
fputs((char *)(m->cval.t + i), emit_output);
i += 1 + strlen((char *)(m->cval.t + i));
} else fputs(operators_name[tt], emit_output);
}
#else
if (m->val.nt == 0) {
fputc('\n', emit_output);
return;
}
fputc(' ', emit_output);
for (i = 0; i < m->val.nt; i ++) {
if (m->val.t[i].type == MACROARG) {
if (m->val.t[i].line == m->narg)
fputs("__VA_ARGS__", emit_output);
else
fputs(m->arg[(size_t)(m->val.t[i].line)],
emit_output);
} else fputs(token_name(m->val.t + i), emit_output);
}
#endif
fputc('\n', emit_output);
}
/*
* Send a token to the output (a token_fifo in lexer mode, the output
* buffer in stand alone mode).
*/
void print_token(struct lexer_state *ls, struct token *t, long uz_line)
{
char *x = t->name;
if (uz_line && t->line < 0) t->line = uz_line;
if (ls->flags & LEXER) {
struct token at;
at = *t;
if (S_TOKEN(t->type)) {
at.name = sdup(at.name);
throw_away(ls->gf, at.name);
}
aol(ls->output_fifo->t, ls->output_fifo->nt, at,
TOKEN_LIST_MEMG);
return;
}
if (ls->flags & KEEP_OUTPUT) {
for (; ls->oline < ls->line;) put_char(ls, '\n');
}
if (!S_TOKEN(t->type)) x = operators_name[t->type];
for (; *x; x ++) put_char(ls, *x);
}
/*
* Send a token to the output at a given line (this is for text output
* and unreplaced macros due to lack of arguments).
*/
static void print_token_nailed(struct lexer_state *ls, struct token *t,
long nail_line)
{
char *x = t->name;
if (ls->flags & LEXER) {
print_token(ls, t, 0);
return;
}
if (ls->flags & KEEP_OUTPUT) {
for (; ls->oline < nail_line;) put_char(ls, '\n');
}
if (!S_TOKEN(t->type)) x = operators_name[t->type];
for (; *x; x ++) put_char(ls, *x);
}
/*
* send a reduced whitespace token to the output
*/
#define print_space(ls) do { \
struct token lt; \
lt.type = OPT_NONE; \
lt.line = (ls)->line; \
print_token((ls), <, 0); \
} while (0)
/*
* We found a #define directive; parse the end of the line, perform
* sanity checks, store the new macro into the "macros" hash table.
*
* In case of a redefinition of a macro: we enforce the rule that a
* macro should be redefined identically, including the spelling of
* parameters. We emit an error on offending code; dura lex, sed lex.
* After all, it is easy to avoid such problems, with a #undef directive.
*/
int handle_define(struct lexer_state *ls)
{
struct macro *m = 0, *n;
#ifdef LOW_MEM
struct token_fifo mv;
#endif
int ltwws = 1, redef = 0;
char *mname = 0;
int narg;
size_t nt;
long l = ls->line;
#ifdef LOW_MEM
mv.art = mv.nt = 0;
#endif
/* find the next non-white token on the line, this should be
the macro name */
while (!next_token(ls) && ls->ctok->type != NEWLINE) {
if (ttMWS(ls->ctok->type)) continue;
if (ls->ctok->type == NAME) mname = sdup(ls->ctok->name);
break;
}
if (mname == 0) {
error(l, "missing macro name");
return 1;
}
if (check_special_macro(mname)) {
error(l, "trying to redefine the special macro %s", mname);
goto warp_error;
}
/*
* If a macro with this name was already defined: the K&R
* states that the new macro should be identical to the old one
* (with some arcane rule of equivalence of whitespace); otherwise,
* redefining the macro is an error. Most preprocessors would
* only emit a warning (or nothing at all) on an unidentical
* redefinition.
*
* Since it is easy to avoid this error (with a #undef directive),
* we choose to enforce the rule and emit an error.
*/
if ((n = HTT_get(¯os, mname)) != 0) {
/* redefinition of a macro: we must check that we define
it identical */
redef = 1;
#ifdef LOW_MEM
n->cval.rp = 0;
#endif
freemem(mname);
mname = 0;
}
if (!redef) {
m = new_macro();
m->narg = -1;
#ifdef LOW_MEM
#define mval mv
#else
#define mval (m->val)
#endif
}
if (next_token(ls)) goto define_end;
/*
* Check if the token immediately following the macro name is
* a left parenthesis; if so, then this is a macro with arguments.
* Collect their names and try to match the next parenthesis.
*/
if (ls->ctok->type == LPAR) {
int i, j;
int need_comma = 0, saw_mdots = 0;
narg = 0;
while (!next_token(ls)) {
if (ls->ctok->type == NEWLINE) {
error(l, "truncated macro definition");
goto define_error;
}
if (ls->ctok->type == COMMA) {
if (saw_mdots) {
error(l, "'...' must end the macro "
"argument list");
goto warp_error;
}
if (!need_comma) {
error(l, "void macro argument");
goto warp_error;
}
need_comma = 0;
continue;
} else if (ls->ctok->type == NAME) {
if (saw_mdots) {
error(l, "'...' must end the macro "
"argument list");
goto warp_error;
}
if (need_comma) {
error(l, "missing comma in "
"macro argument list");
goto warp_error;
}
if (!redef) {
aol(m->arg, narg,
sdup(ls->ctok->name), 8);
/* we must keep track of m->narg
so that cleanup in case of
error works. */
m->narg = narg;
if (narg == 128
&& (ls->flags & WARN_STANDARD))
warning(l, "more arguments to "
"macro than the ISO "
"limit (127)");
#ifdef LOW_MEM
if (narg == 32767) {
error(l, "too many arguments "
"in macro definition "
"(max 32766)");
goto warp_error;
}
#endif
} else {
/* this is a redefinition of the
macro; check equality between
old and new definitions */
if (narg >= n->narg) goto redef_error;
if (strcmp(ls->ctok->name,
n->arg[narg ++]))
goto redef_error;
}
need_comma = 1;
continue;
} else if ((ls->flags & MACRO_VAARG)
&& ls->ctok->type == MDOTS) {
if (need_comma) {
error(l, "missing comma before '...'");
goto warp_error;
}
if (redef && !n->vaarg) goto redef_error;
if (!redef) m->vaarg = 1;
saw_mdots = 1;
need_comma = 1;
continue;
} else if (ls->ctok->type == RPAR) {
if (narg > 0 && !need_comma) {
error(l, "void macro argument");
goto warp_error;
}
if (redef && n->vaarg && !saw_mdots)
goto redef_error;
break;
} else if (ttMWS(ls->ctok->type)) {
continue;
}
error(l, "invalid macro argument");
goto warp_error;
}
if (!redef) {
for (i = 1; i < narg; i ++) for (j = 0; j < i; j ++)
if (!strcmp(m->arg[i], m->arg[j])) {
error(l, "duplicate macro "
"argument");
goto warp_error;
}
}
if (!redef) m->narg = narg;
} else {
if (!ttWHI(ls->ctok->type) && (ls->flags & WARN_STANDARD))
warning(ls->line, "identifier not followed by "
"whitespace in #define");
ls->flags |= READ_AGAIN;
narg = 0;
}
if (redef) nt = 0;
/* now, we have the arguments. Let's get the macro contents. */
while (!next_token(ls) && ls->ctok->type != NEWLINE) {
struct token t;
t.type = ls->ctok->type;
if (ltwws && ttMWS(t.type)) continue;
t.line = 0;
if (t.type == NAME) {
int i;
if ((ls->flags & MACRO_VAARG)
&& !strcmp(ls->ctok->name, "__VA_ARGS__")) {
if (redef) {
if (!n->vaarg) goto redef_error;
} else if (!m->vaarg) {
error(l, "'__VA_ARGS__' is forbidden "
"in macros with a fixed "
"number of arguments");
goto warp_error;
}
t.type = MACROARG;
t.line = redef ? n->narg : m->narg;
}
for (i = 0; i < narg; i ++)
if (!strcmp(redef ? n->arg[i] : m->arg[i],
ls->ctok->name)) {
t.type = MACROARG;
/* this is a hack: we store the
argument number in the line field */
t.line = i;
break;
}
}
if (!redef && S_TOKEN(t.type)) t.name = sdup(ls->ctok->name);
if (ttMWS(t.type)) {
if (ltwws) continue;
#ifdef SEMPER_FIDELIS
t.type = OPT_NONE;
#else
t.type = NONE;
#endif
ltwws = 1;
} else ltwws = 0;
if (!redef) {
/* we ensure that each macro token has a correct
line number */
if (t.type != MACROARG) t.line = 1;
aol(mval.t, mval.nt, t, TOKEN_LIST_MEMG);
} else {
#ifdef LOW_MEM
int tt;
if (n->cval.rp >= n->cval.length) {
#ifdef SEMPER_FIDELIS
if (t.type != OPT_NONE) goto redef_error;
#else
if (t.type != NONE) goto redef_error;
#endif
} else if (t.type != n->cval.t[n->cval.rp]) {
goto redef_error;
} else if (t.type == MACROARG) {
unsigned anum = n->cval.t[n->cval.rp + 1];
if (anum >= 128U) anum = ((anum & 127U) << 8)
| m->cval.t[n->cval.rp + 2];
if (anum != (unsigned)t.line) goto redef_error;
} else if (S_TOKEN(t.type) && strcmp(ls->ctok->name,
(char *)(n->cval.t + n->cval.rp + 1))) {
goto redef_error;
}
tt = n->cval.t[n->cval.rp ++];
if (S_TOKEN(tt)) n->cval.rp += 1
+ strlen((char *)(n->cval.t + n->cval.rp));
else if (tt == MACROARG) {
if (n->cval.t[++ n->cval.rp] >= 128)
n->cval.rp ++;
}
#else
if (nt >= n->val.nt) {
#ifdef SEMPER_FIDELIS
if (t.type != OPT_NONE) goto redef_error;
#else
if (t.type != NONE) goto redef_error;
#endif
} else if (t.type != n->val.t[nt].type
|| (t.type == MACROARG
&& t.line != n->val.t[nt].line)
|| (S_TOKEN(t.type) && strcmp(ls->ctok->name,
n->val.t[nt].name))) {
goto redef_error;
}
#endif
nt ++;
}
}
if (redef) {
#ifdef LOW_MEM
if (n->cval.rp < n->cval.length) goto redef_error_2;
#else
if (nt < n->val.nt) goto redef_error_2;
#endif
return 0;
}
/* now we have the complete macro; perform some checks about
the operators # and ##, and, if everything is ok,
store the macro into the hash table */
define_end:
#ifdef SEMPER_FIDELIS
if (mval.nt && mval.t[mval.nt - 1].type == OPT_NONE) {
#else
if (mval.nt && mval.t[mval.nt - 1].type == NONE) {
#endif
mval.nt --;
if (mval.nt == 0) freemem(mval.t);
}
if (mval.nt != 0) {
size_t i;
/* some checks about the macro */
if (mval.t[0].type == DSHARP
|| mval.t[0].type == DIG_DSHARP
|| mval.t[mval.nt - 1].type == DSHARP
|| mval.t[mval.nt - 1].type == DIG_DSHARP) {
error(l, "operator '##' may neither begin "
"nor end a macro");
goto define_error;
}
if (m->narg >= 0) for (i = 0; i < mval.nt; i ++)
if ((mval.t[i].type == SHARP
|| mval.t[i].type == DIG_SHARP) &&
(i == (mval.nt - 1)
|| (ttMWS(mval.t[i + 1].type) &&
(i == mval.nt - 2
|| mval.t[i + 2].type != MACROARG))
|| (!ttMWS(mval.t[i + 1].type)
&& mval.t[i + 1].type != MACROARG))) {
error(l, "operator '#' not followed "
"by a macro argument");
goto define_error;
}
}
#ifdef LOW_MEM
{
size_t i, l;
for (i = 0, l = 0; i < mval.nt; i ++) {
l ++;
if (S_TOKEN(mval.t[i].type))
l += 1 + strlen(mval.t[i].name);
else if (mval.t[i].type == MACROARG) {
l ++;
if (mval.t[i].line >= 128) l ++;
}
}
m->cval.length = l;
if (l) m->cval.t = getmem(l);
for (i = 0, l = 0; i < mval.nt; i ++) {
m->cval.t[l ++] = mval.t[i].type;
if (S_TOKEN(mval.t[i].type)) {
size_t x = 1 + strlen(mval.t[i].name);
mmv(m->cval.t + l, mval.t[i].name, x);
l += x;
freemem(mval.t[i].name);
}
else if (mval.t[i].type == MACROARG) {
unsigned anum = mval.t[i].line;
if (anum >= 128) {
m->cval.t[l ++] = 128 | (anum >> 8);
m->cval.t[l ++] = anum & 0xFF;
} else {
m->cval.t[l ++] = anum;
}
}
}
if (mval.nt) freemem(mval.t);
}
#endif
HTT_put(¯os, m, mname);
freemem(mname);
if (emit_defines) print_macro(m);
return 0;
redef_error:
while (ls->ctok->type != NEWLINE && !next_token(ls));
redef_error_2:
error(l, "macro '%s' redefined unidentically", HASH_ITEM_NAME(n));
return 1;
warp_error:
while (ls->ctok->type != NEWLINE && !next_token(ls));
define_error:
if (m) del_macro(m);
if (mname) freemem(mname);
#ifdef LOW_MEM
if (mv.nt) {
size_t i;
for (i = 0; i < mv.nt; i ++)
if (S_TOKEN(mv.t[i].type)) freemem(mv.t[i].name);
freemem(mv.t);
}
#endif
return 1;
#undef mval
}
/*
* Get the arguments for a macro. This code is tricky because there can
* be multiple sources for these arguments, if we are in the middle of
* a macro replacement; arguments are macro-replaced before inclusion
* into the macro replacement.
*
* return value:
* 1 no argument (last token read from next_token())
* 2 no argument (last token read from tfi)
* 3 no argument (nothing read)
* 4 error
*
* Void arguments are allowed in C99.
*/
static int collect_arguments(struct lexer_state *ls, struct token_fifo *tfi,
int penury, struct token_fifo *atl, int narg, int vaarg, int *wr)
{
int ltwws = 1, npar = 0, i;
struct token *ct = 0;
int read_from_fifo = 0;
long begin_line = ls->line;
#define unravel(ls) (read_from_fifo = 0, !((tfi && tfi->art < tfi->nt \
&& (read_from_fifo = 1) != 0 && (ct = tfi->t + (tfi->art ++))) \
|| ((!tfi || penury) && !next_token(ls) && (ct = (ls)->ctok))))
/*
* collect_arguments() is assumed to setup correctly atl
* (this is not elegant, but it works)
*/
for (i = 0; i < narg; i ++) atl[i].art = atl[i].nt = 0;
if (vaarg) atl[narg].art = atl[narg].nt = 0;
*wr = 0;
while (!unravel(ls)) {
if (!read_from_fifo && ct->type == NEWLINE) ls->ltwnl = 1;
if (ttWHI(ct->type)) {
*wr = 1;
continue;
}
if (ct->type == LPAR) {
npar = 1;
}
break;
}
if (!npar) {
if (ct == ls->ctok) return 1;
if (read_from_fifo) return 2;
return 3;
}
if (!read_from_fifo && ct == ls->ctok) ls->ltwnl = 0;
i = 0;
if ((narg + vaarg) == 0) {
while(!unravel(ls)) {
if (ttWHI(ct->type)) continue;
if (ct->type == RPAR) goto harvested;
npar = 1;
goto too_many_args;
}
}
while (!unravel(ls)) {
struct token t;
if (ct->type == LPAR) npar ++;
else if (ct->type == RPAR && (-- npar) == 0) {
if (atl[i].nt != 0
&& ttMWS(atl[i].t[atl[i].nt - 1].type))
atl[i].nt --;
i ++;
/*
* C99 standard states that at least one argument
* should be present for the ... part; to relax
* this behaviour, change 'narg + vaarg' to 'narg'.
*/
if (i < (narg + vaarg)) {
error(begin_line, "not enough arguments "
"to macro");
return 4;
}
if (i > narg) {
if (!(ls->flags & MACRO_VAARG) || !vaarg)
goto too_many_args;
}
goto harvested;
} else if (ct->type == COMMA && npar <= 1 && i < narg) {
if (atl[i].nt != 0
&& ttMWS(atl[i].t[atl[i].nt - 1].type))
atl[i].nt --;
if (++ i == narg) {
if (!(ls->flags & MACRO_VAARG) || !vaarg)
goto too_many_args;
}
if (i > 30000) goto too_many_args;
ltwws = 1;
continue;
} else if (ltwws && ttWHI(ct->type)) continue;
t.type = ct->type;
if (!read_from_fifo) t.line = ls->line; else t.line = ct->line;
/*
* Stringification applies only to macro arguments;
* so we handle here OPT_NONE.
* OPT_NONE is kept, but does not count as whitespace,
* and merges with other whitespace to give a fully
* qualified NONE token. Two OPT_NONE tokens merge.
* Initial and final OPT_NONE are discarded (initial
* is already done, as OPT_NONE is matched by ttWHI).
*/
if (ttWHI(t.type)) {
if (t.type != OPT_NONE) {
t.type = NONE;
#ifdef SEMPER_FIDELIS
t.name = sdup(" ");
throw_away(ls->gf, t.name);
#endif
ltwws = 1;
}
if (atl[i].nt > 0
&& atl[i].t[atl[i].nt - 1].type == OPT_NONE)
atl[i].nt --;
} else {
ltwws = 0;
if (S_TOKEN(t.type)) {
t.name = ct->name;
if (ct == (ls)->ctok) {
t.name = sdup(t.name);
throw_away(ls->gf, t.name);
}
}
}
aol(atl[i].t, atl[i].nt, t, TOKEN_LIST_MEMG);
}
error(begin_line, "unfinished macro call");
return 4;
too_many_args:
error(begin_line, "too many arguments to macro");
while (npar && !unravel(ls)) {
if (ct->type == LPAR) npar ++;
else if (ct->type == RPAR) npar --;
}
return 4;
harvested:
if (i > 127 && (ls->flags & WARN_STANDARD))
warning(begin_line, "macro call with %d arguments (ISO "
"specifies 127 max)", i);
return 0;
#undef unravel
}
/*
* concat_token() is called when the ## operator is used. It uses
* the struct lexer_state dsharp_lexer to parse the result of the
* concatenation.
*
* Law enforcement: if the whole string does not produce a valid
* single token, an error (non-zero result) is returned.
*/
struct lexer_state dsharp_lexer;
static inline int concat_token(struct token *t1, struct token *t2)
{
char *n1 = token_name(t1), *n2 = token_name(t2);
size_t l1 = strlen(n1), l2 = strlen(n2);
unsigned char *x = getmem(l1 + l2 + 1);
int r;
mmv(x, n1, l1);
mmv(x + l1, n2, l2);
x[l1 + l2] = 0;
dsharp_lexer.input = 0;
dsharp_lexer.input_string = x;
dsharp_lexer.pbuf = 0;
dsharp_lexer.ebuf = l1 + l2;
dsharp_lexer.discard = 1;
dsharp_lexer.flags = DEFAULT_LEXER_FLAGS;
dsharp_lexer.pending_token = 0;
r = next_token(&dsharp_lexer);
freemem(x);
return (r == 1 || dsharp_lexer.pbuf < (l1 + l2)
|| dsharp_lexer.pending_token
|| (dsharp_lexer.pbuf == (l1 + l2) && !dsharp_lexer.discard));
}
#ifdef PRAGMA_TOKENIZE
/*
* tokenize_string() takes a string as input, and split it into tokens,
* reassembling the tokens into a single compressed string generated by
* compress_token_list(); this function is used for _Pragma processing.
*/
struct lexer_state tokenize_lexer;
static char *tokenize_string(struct lexer_state *ls, char *buf)
{
struct token_fifo tf;
size_t bl = strlen(buf);
int r;
tokenize_lexer.input = 0;
tokenize_lexer.input_string = (unsigned char *)buf;
tokenize_lexer.pbuf = 0;
tokenize_lexer.ebuf = bl;
tokenize_lexer.discard = 1;
tokenize_lexer.flags = ls->flags | LEXER;
tokenize_lexer.pending_token = 0;
tf.art = tf.nt = 0;
while (!(r = next_token(&tokenize_lexer))) {
struct token t, *ct = tokenize_lexer.ctok;
if (ttWHI(ct->type)) continue;
t = *ct;
if (S_TOKEN(t.type)) t.name = sdup(t.name);
aol(tf.t, tf.nt, t, TOKEN_LIST_MEMG);
}
if (tokenize_lexer.pbuf < bl) goto tokenize_error;
return (char *)((compress_token_list(&tf)).t);
tokenize_error:
if (tf.nt) {
for (tf.art = 0; tf.art < tf.nt; tf.art ++)
if (S_TOKEN(tf.t[tf.art].type))
freemem(tf.t[tf.art].name);
freemem(tf.t);
}
return 0;
}
#endif
/*
* stringify_string() has a self-explanatory name. It is called when
* the # operator is used in a macro and a string constant must be
* stringified.
*/
static inline char *stringify_string(char *x)
{
size_t l;
int i, inside_str = 0, inside_cc = 0, must_quote, has_quoted = 0;
char *y, *d;
for (i = 0; i < 2; i ++) {
if (i) d[0] = '"';
for (l = 1, y = x; *y; y ++, l ++) {
must_quote = 0;
if (inside_cc) {
if (*y == '\\') {
must_quote = 1;
has_quoted = 1;
} else if (!has_quoted && *y == '\'')
inside_cc = 0;
} else if (inside_str) {
if (*y == '"' || *y == '\\') must_quote = 1;
if (*y == '\\') has_quoted = 1;
else if (!has_quoted && *y == '"')
inside_str = 0;
} else if (*y == '"') {
inside_str = 1;
must_quote = 1;
} else if (*y == '\'') {
inside_cc = 1;
}
if (must_quote) {
if (i) d[l] = '\\';
l ++;
}
if (i) d[l] = *y;
}
if (!i) d = getmem(l + 2);
if (i) {
d[l] = '"';
d[l + 1] = 0;
}
}
return d;
}
/*
* stringify() produces a constant string, result of the # operator
* on a list of tokens.
*/
static char *stringify(struct token_fifo *tf)
{
size_t tlen;
size_t i;
char *x, *y;
for (tlen = 0, i = 0; i < tf->nt; i ++)
if (tf->t[i].type < CPPERR && tf->t[i].type != OPT_NONE)
tlen += strlen(token_name(tf->t + i));
if (tlen == 0) return sdup("\"\"");
x = getmem(tlen + 1);
for (tlen = 0, i = 0; i < tf->nt; i ++) {
if (tf->t[i].type >= CPPERR || tf->t[i].type == OPT_NONE)
continue;
strcpy(x + tlen, token_name(tf->t + i));
tlen += strlen(token_name(tf->t + i));
}
/* no need to add a trailing 0: strcpy() did that (and the string
is not empty) */
y = stringify_string(x);
freemem(x);
return y;
}