]> gcc.gnu.org Git - gcc.git/blame - gcc/cpphash.c
typeck.c (build_prim_array_type): Correctly set the high word too.
[gcc.git] / gcc / cpphash.c
CommitLineData
6de1e2a9 1/* Part of CPP library. (Macro handling.)
5e7b4e25
JL
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000 Free Software Foundation, Inc.
7f2935c7 4 Written by Per Bothner, 1994.
38e01259 5 Based on CCCP program by Paul Rubin, June 1986
7f2935c7
PB
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
940d9d63 20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7f2935c7
PB
21
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
25
b04cd507
KG
26#include "config.h"
27#include "system.h"
7f2935c7
PB
28#include "cpplib.h"
29#include "cpphash.h"
d35364d1 30#include "hashtab.h"
34ca9541 31#undef abort
7f2935c7 32
d35364d1
ZW
33static unsigned int hash_HASHNODE PARAMS ((const void *));
34static int eq_HASHNODE PARAMS ((const void *, const void *));
35static void del_HASHNODE PARAMS ((void *));
36static int dump_hash_helper PARAMS ((void *, void *));
37
6de1e2a9
ZW
38static int comp_def_part PARAMS ((int, U_CHAR *, int, U_CHAR *,
39 int, int));
6de1e2a9
ZW
40static void push_macro_expansion PARAMS ((cpp_reader *,
41 U_CHAR *, int, HASHNODE *));
455d2586 42static int unsafe_chars PARAMS ((cpp_reader *, int, int));
bcc5cac9
KG
43static int macro_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
44static enum cpp_token macarg PARAMS ((cpp_reader *, int));
45static struct tm *timestamp PARAMS ((cpp_reader *));
46static void special_symbol PARAMS ((HASHNODE *, cpp_reader *));
47
6de1e2a9
ZW
48#define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
49#define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
ba412f14 50#define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
6de1e2a9 51
d35364d1
ZW
52/* Initial hash table size. (It can grow if necessary - see hashtab.c.) */
53#define HASHSIZE 500
54
6de1e2a9
ZW
55/* The arglist structure is built by create_definition to tell
56 collect_expansion where the argument names begin. That
57 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
58 would contain pointers to the strings x, y, and z.
59 collect_expansion would then build a DEFINITION node,
60 with reflist nodes pointing to the places x, y, and z had
61 appeared. So the arglist is just convenience data passed
62 between these two routines. It is not kept around after
63 the current #define has been processed and entered into the
64 hash table. */
65
ba412f14 66struct arg
6de1e2a9 67{
6de1e2a9 68 U_CHAR *name;
ba412f14
ZW
69 int len;
70 char rest_arg;
71};
72
73struct arglist
74{
75 U_CHAR *namebuf;
76 struct arg *argv;
77 int argc;
6de1e2a9
ZW
78};
79
ba412f14
ZW
80
81static DEFINITION *collect_expansion PARAMS ((cpp_reader *, struct arglist *));
82static struct arglist *collect_formal_parameters PARAMS ((cpp_reader *));
bcc5cac9 83
6de1e2a9
ZW
84/* This structure represents one parsed argument in a macro call.
85 `raw' points to the argument text as written (`raw_length' is its length).
86 `expanded' points to the argument's macro-expansion
87 (its length is `expand_length').
88 `stringified_length' is the length the argument would have
4571dc27 89 if stringified. */
6de1e2a9
ZW
90
91/* raw and expanded are relative to ARG_BASE */
92#define ARG_BASE ((pfile)->token_buffer)
93
94struct argdata
95{
96 /* Strings relative to pfile->token_buffer */
97 long raw, expanded, stringified;
98 int raw_length, expand_length;
99 int stringified_length;
6de1e2a9
ZW
100};
101
d35364d1 102/* Calculate hash of a HASHNODE structure. */
bb52fa7f 103static unsigned int
d35364d1
ZW
104hash_HASHNODE (x)
105 const void *x;
7f2935c7 106{
d35364d1
ZW
107 HASHNODE *h = (HASHNODE *)x;
108 const U_CHAR *s = h->name;
109 unsigned int len = h->length;
110 unsigned int n = len, r = 0;
7f2935c7 111
d35364d1
ZW
112 if (h->hash != (unsigned long)-1)
113 return h->hash;
114
bb52fa7f
ZW
115 do
116 r = r * 67 + (*s++ - 113);
117 while (--n);
d35364d1 118 h->hash = r + len;
bb52fa7f 119 return r + len;
7f2935c7
PB
120}
121
d35364d1
ZW
122/* Compare two HASHNODE structures. */
123static int
124eq_HASHNODE (x, y)
125 const void *x;
126 const void *y;
127{
128 const HASHNODE *a = (const HASHNODE *)x;
129 const HASHNODE *b = (const HASHNODE *)y;
130
131 return (a->length == b->length
132 && !strncmp (a->name, b->name, a->length));
133}
134
135/* Destroy a HASHNODE. */
136static void
137del_HASHNODE (x)
138 void *x;
139{
140 HASHNODE *h = (HASHNODE *)x;
141
142 if (h->type == T_MACRO)
143 _cpp_free_definition (h->value.defn);
144 free ((void *) h->name);
145 free (h);
146}
147
148/* Allocate and initialize a HASHNODE structure.
149 Caller must fill in the value field. */
150
151HASHNODE *
152_cpp_make_hashnode (name, len, type, hash)
153 const U_CHAR *name;
154 size_t len;
155 enum node_type type;
156 unsigned long hash;
157{
158 HASHNODE *hp = (HASHNODE *) xmalloc (sizeof (HASHNODE));
159 U_CHAR *p = xmalloc (len + 1);
160
161 hp->type = type;
162 hp->length = len;
163 hp->name = p;
164 hp->hash = hash;
165
166 memcpy (p, name, len);
167 p[len] = 0;
168
169 return hp;
170}
171
172/* Find the hash node for name "name", which ends at the first
173 non-identifier char.
0f41302f
MS
174
175 If LEN is >= 0, it is the length of the name.
d35364d1 176 Otherwise, compute the length now. */
0f41302f 177
7f2935c7 178HASHNODE *
b0699dad 179_cpp_lookup (pfile, name, len)
bb52fa7f 180 cpp_reader *pfile;
7f2935c7
PB
181 const U_CHAR *name;
182 int len;
7f2935c7 183{
d35364d1
ZW
184 const U_CHAR *bp;
185 HASHNODE dummy;
7f2935c7
PB
186
187 if (len < 0)
188 {
203588e7 189 for (bp = name; is_idchar (*bp); bp++);
7f2935c7
PB
190 len = bp - name;
191 }
192
d35364d1
ZW
193 dummy.name = name;
194 dummy.length = len;
195 dummy.hash = -1;
196
197 return (HASHNODE *) htab_find (pfile->hashtab, (void *)&dummy);
198}
199
200/* Find the hashtable slot for name "name". Used to insert or delete. */
201HASHNODE **
202_cpp_lookup_slot (pfile, name, len, insert, hash)
203 cpp_reader *pfile;
204 const U_CHAR *name;
205 int len;
206 int insert;
207 unsigned long *hash;
208{
209 const U_CHAR *bp;
210 HASHNODE dummy;
211 HASHNODE **slot;
7f2935c7 212
d35364d1 213 if (len < 0)
6de1e2a9 214 {
d35364d1
ZW
215 for (bp = name; is_idchar (*bp); bp++);
216 len = bp - name;
6de1e2a9 217 }
d35364d1
ZW
218
219 dummy.name = name;
220 dummy.length = len;
221 dummy.hash = -1;
222
223 slot = (HASHNODE **) htab_find_slot (pfile->hashtab, (void *)&dummy, insert);
224 if (insert)
225 *hash = dummy.hash;
226 return slot;
227}
228
229/* Init the hash table. In here so it can see the hash and eq functions. */
230void
231_cpp_init_macro_hash (pfile)
232 cpp_reader *pfile;
233{
234 pfile->hashtab = htab_create (HASHSIZE, hash_HASHNODE,
235 eq_HASHNODE, del_HASHNODE);
7f2935c7
PB
236}
237
cf4ed945
ZW
238/* Free a DEFINITION structure. Used by delete_macro, and by
239 do_define when redefining macros. */
240
241void
b0699dad 242_cpp_free_definition (d)
cf4ed945
ZW
243 DEFINITION *d;
244{
245 struct reflist *ap, *nextap;
246
247 for (ap = d->pattern; ap != NULL; ap = nextap)
248 {
249 nextap = ap->next;
250 free (ap);
251 }
252 if (d->nargs >= 0)
253 free (d->argnames);
254 free (d);
255}
256
6de1e2a9
ZW
257static int
258macro_cleanup (pbuf, pfile)
259 cpp_buffer *pbuf;
260 cpp_reader *pfile ATTRIBUTE_UNUSED;
261{
262 HASHNODE *macro = (HASHNODE *) pbuf->data;
263 if (macro->type == T_DISABLED)
264 macro->type = T_MACRO;
265 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
7ceb3598 266 free ((PTR) pbuf->buf);
6de1e2a9
ZW
267 return 0;
268}
269
ba412f14
ZW
270/* Read a replacement list for a macro, and build the DEFINITION
271 structure. ARGLIST specifies the formal parameters to look for in
272 the text of the definition. If ARGLIST is null, this is an
273 object-like macro; if it points to an empty arglist, this is a
274 function-like macro with no arguments.
275
276 A good half of this is devoted to supporting -traditional.
277 Kill me now. */
6de1e2a9
ZW
278
279static DEFINITION *
ba412f14 280collect_expansion (pfile, arglist)
6de1e2a9 281 cpp_reader *pfile;
6de1e2a9
ZW
282 struct arglist *arglist;
283{
284 DEFINITION *defn;
ba412f14
ZW
285 struct reflist *pat = 0, *endpat = 0;
286 enum cpp_token token;
287 long start, here, last;
288 int i;
289 int argc;
290 size_t len;
291 struct arg *argv;
292 U_CHAR *tok, *exp;
293 enum { START = 0, NORM, ARG, STRIZE, PASTE } last_token = START;
294
295 if (arglist)
34ca9541 296 {
ba412f14
ZW
297 argv = arglist->argv;
298 argc = arglist->argc;
34ca9541 299 }
ba412f14 300 else
6de1e2a9 301 {
ba412f14
ZW
302 argv = 0;
303 argc = 0;
6de1e2a9
ZW
304 }
305
ba412f14
ZW
306 last = start = CPP_WRITTEN (pfile);
307 last -= 2; /* two extra chars for the leading escape */
308 for (;;)
6de1e2a9 309 {
ba412f14
ZW
310 /* We use cpp_get_token because get_directive_token would
311 discard whitespace and we can't cope with that yet. Macro
312 expansion is off, so we are guaranteed not to see POP or EOF. */
6de1e2a9 313
ba412f14 314 while (PEEKC () == '\r')
6de1e2a9 315 {
ba412f14
ZW
316 FORWARD (1);
317 CPP_BUMP_LINE (pfile);
6de1e2a9 318 }
ba412f14
ZW
319 if (PEEKC () == '\n')
320 goto done;
321 here = CPP_WRITTEN (pfile);
322 token = cpp_get_token (pfile);
323 tok = pfile->token_buffer + here;
324 switch (token)
6de1e2a9 325 {
ba412f14
ZW
326 case CPP_POP:
327 case CPP_EOF:
328 case CPP_VSPACE:
329 cpp_ice (pfile, "EOF or VSPACE in collect_expansion");
330 goto done;
331
332 case CPP_HSPACE:
333 if (last_token == STRIZE || last_token == PASTE
334 || last_token == START)
335 CPP_SET_WRITTEN (pfile, here);
336 break;
6de1e2a9 337
ba412f14
ZW
338 case CPP_STRINGIZE:
339 if (last_token == PASTE)
340 /* Not really a stringifier. */
341 goto norm;
342 last_token = STRIZE;
343 CPP_SET_WRITTEN (pfile, here); /* delete from replacement text */
344 break;
6de1e2a9 345
ba412f14
ZW
346 case CPP_TOKPASTE:
347 /* If the last token was an argument, discard this token and
348 any hspace between it and the argument's position. Then
349 mark the arg raw_after. */
350 if (last_token == ARG)
351 {
352 endpat->raw_after = 1;
353 last_token = PASTE;
354 CPP_SET_WRITTEN (pfile, last);
6de1e2a9
ZW
355 break;
356 }
ba412f14
ZW
357 else if (last_token == PASTE)
358 /* ## ## - the second ## is ordinary. */
359 goto norm;
7d26bd16
JM
360 else if (last_token == START)
361 cpp_error (pfile, "`##' at start of macro definition");
ba412f14
ZW
362
363 /* Discard the token and any hspace before it. */
364 while (is_hspace (pfile->token_buffer[here-1]))
365 here--;
366 CPP_SET_WRITTEN (pfile, here);
367
368 if (last_token == STRIZE)
369 /* Oops - that wasn't a stringify operator. */
370 CPP_PUTC (pfile, '#');
371 last_token = PASTE;
372 break;
6de1e2a9 373
ba412f14
ZW
374 case CPP_COMMENT:
375 /* We must be in -traditional mode. Pretend this was a
376 token paste, but only if there was no leading or
377 trailing space. */
378 CPP_SET_WRITTEN (pfile, here);
379 if (is_hspace (pfile->token_buffer[here-1]))
380 break;
381 if (is_hspace (PEEKC ()))
382 break;
383 if (last_token == ARG)
384 endpat->raw_after = 1;
385 last_token = PASTE;
386 break;
6de1e2a9 387
ba412f14
ZW
388 case CPP_STRING:
389 case CPP_CHAR:
390 if (last_token == STRIZE)
391 cpp_error (pfile, "`#' is not followed by a macro argument name");
6de1e2a9 392
ba412f14
ZW
393 if (CPP_TRADITIONAL (pfile) || CPP_OPTIONS (pfile)->warn_stringify)
394 goto maybe_trad_stringify;
395 else
396 goto norm;
397
398 case CPP_NAME:
399 for (i = 0; i < argc; i++)
400 if (!strncmp (tok, argv[i].name, argv[i].len)
401 && ! is_idchar (tok[argv[i].len]))
402 goto addref;
403
404 /* fall through */
405 default:
406 norm:
407 if (last_token == STRIZE)
408 cpp_error (pfile, "`#' is not followed by a macro argument name");
409 last_token = NORM;
410 break;
411 }
412 continue;
6de1e2a9 413
ba412f14
ZW
414 addref:
415 {
416 struct reflist *tpat;
417
418 /* Make a pat node for this arg and add it to the pat list */
419 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
420 tpat->next = NULL;
421 tpat->raw_before = (last_token == PASTE);
422 tpat->raw_after = 0;
423 tpat->stringify = (last_token == STRIZE);
424 tpat->rest_args = argv[i].rest_arg;
425 tpat->argno = i;
426 tpat->nchars = here - last;
427
428 if (endpat == NULL)
429 pat = tpat;
430 else
431 endpat->next = tpat;
432 endpat = tpat;
433 last = here;
434 }
435 CPP_SET_WRITTEN (pfile, here); /* discard arg name */
436 last_token = ARG;
437 continue;
6de1e2a9 438
ba412f14 439 maybe_trad_stringify:
c45da1ca 440 last_token = NORM;
ba412f14
ZW
441 {
442 U_CHAR *base, *p, *limit;
443 struct reflist *tpat;
6de1e2a9 444
ba412f14
ZW
445 base = p = pfile->token_buffer + here;
446 limit = CPP_PWRITTEN (pfile);
447
448 while (++p < limit)
449 {
450 if (is_idstart (*p))
451 continue;
452 for (i = 0; i < argc; i++)
453 if (!strncmp (tok, argv[i].name, argv[i].len)
454 && ! is_idchar (tok[argv[i].len]))
455 goto mts_addref;
456 continue;
457
458 mts_addref:
459 if (!CPP_TRADITIONAL (pfile))
460 {
461 /* Must have got here because of -Wtraditional. */
462 cpp_warning (pfile,
463 "macro argument `%.*s' would be stringified with -traditional",
464 (int) argv[i].len, argv[i].name);
465 continue;
466 }
467 if (CPP_OPTIONS (pfile)->warn_stringify)
468 cpp_warning (pfile, "macro argument `%.*s' is stringified",
469 (int) argv[i].len, argv[i].name);
470
471 /* Remove the argument from the string. */
472 memmove (p, p + argv[i].len, limit - (p + argv[i].len));
473 limit -= argv[i].len;
474
475 /* Make a pat node for this arg and add it to the pat list */
476 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
477 tpat->next = NULL;
478
479 /* Don't attempt to paste this with anything. */
480 tpat->raw_before = 0;
481 tpat->raw_after = 0;
482 tpat->stringify = 1;
483 tpat->rest_args = argv[i].rest_arg;
484 tpat->argno = i;
485 tpat->nchars = (p - base) + here - last;
486
487 if (endpat == NULL)
488 pat = tpat;
489 else
490 endpat->next = tpat;
491 endpat = tpat;
492 last = (p - base) + here;
493 }
494 CPP_ADJUST_WRITTEN (pfile, CPP_PWRITTEN (pfile) - limit);
495 }
496 }
497 done:
498
499 if (last_token == STRIZE)
500 cpp_error (pfile, "`#' is not followed by a macro argument name");
501 else if (last_token == PASTE)
502 cpp_error (pfile, "`##' at end of macro definition");
503
c45da1ca
ZW
504 if (last_token == START)
505 {
506 /* Empty macro definition. */
7ceb3598 507 exp = (U_CHAR *) xstrdup ("\r \r ");
c45da1ca
ZW
508 len = 1;
509 }
510 else
511 {
512 /* Trim trailing white space from definition. */
513 here = CPP_WRITTEN (pfile);
514 while (here > last && is_hspace (pfile->token_buffer [here-1]))
515 here--;
516 CPP_SET_WRITTEN (pfile, here);
c45da1ca
ZW
517 CPP_NUL_TERMINATE (pfile);
518 len = CPP_WRITTEN (pfile) - start + 1;
7ceb3598
NB
519 /* space for no-concat markers at either end */
520 exp = (U_CHAR *) xmalloc (len + 4);
c45da1ca
ZW
521 exp[0] = '\r';
522 exp[1] = ' ';
523 exp[len + 1] = '\r';
524 exp[len + 2] = ' ';
525 exp[len + 3] = '\0';
526 memcpy (&exp[2], pfile->token_buffer + start, len - 1);
527 }
528
ba412f14
ZW
529 CPP_SET_WRITTEN (pfile, start);
530
531 defn = (DEFINITION *) xmalloc (sizeof (DEFINITION));
532 defn->length = len + 3;
533 defn->expansion = exp;
534 defn->pattern = pat;
535 defn->rest_args = 0;
536 if (arglist)
537 {
538 defn->nargs = argc;
539 defn->argnames = arglist->namebuf;
540 if (argv)
541 {
542 defn->rest_args = argv[argc - 1].rest_arg;
543 free (argv);
6de1e2a9 544 }
ba412f14 545 free (arglist);
6de1e2a9 546 }
ba412f14 547 else
6de1e2a9 548 {
ba412f14
ZW
549 defn->nargs = -1;
550 defn->argnames = 0;
551 defn->rest_args = 0;
6de1e2a9 552 }
6de1e2a9
ZW
553 return defn;
554}
555
ba412f14
ZW
556static struct arglist *
557collect_formal_parameters (pfile)
6de1e2a9 558 cpp_reader *pfile;
6de1e2a9 559{
ba412f14
ZW
560 struct arglist *result = 0;
561 struct arg *argv = 0;
38b24ee2 562 U_CHAR *namebuf = (U_CHAR *) xstrdup ("");
6de1e2a9 563
ba412f14
ZW
564 U_CHAR *name, *tok;
565 size_t argslen = 1;
566 int len;
567 int argc = 0;
568 int i;
569 enum cpp_token token;
570 long old_written;
6de1e2a9 571
ba412f14
ZW
572 old_written = CPP_WRITTEN (pfile);
573 token = get_directive_token (pfile);
574 if (token != CPP_LPAREN)
575 {
576 cpp_ice (pfile, "first token = %d not %d in collect_formal_parameters",
577 token, CPP_LPAREN);
578 goto invalid;
579 }
6de1e2a9 580
ba412f14
ZW
581 argv = (struct arg *) xmalloc (sizeof (struct arg));
582 argv[argc].len = 0;
583 argv[argc].rest_arg = 0;
584 for (;;)
585 {
586 CPP_SET_WRITTEN (pfile, old_written);
587 token = get_directive_token (pfile);
588 switch (token)
589 {
590 case CPP_NAME:
591 tok = pfile->token_buffer + old_written;
592 len = CPP_PWRITTEN (pfile) - tok;
593 if (namebuf
7ceb3598 594 && (name = (U_CHAR *) strstr (namebuf, tok))
ba412f14
ZW
595 && name[len] == ','
596 && (name == namebuf || name[-1] == ','))
597 {
598 cpp_error (pfile, "duplicate macro argument name `%s'", tok);
599 continue;
600 }
38b24ee2 601 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->c99
8051b178 602 && !strncmp (tok, "__VA_ARGS__", sizeof "__VA_ARGS__" - 1))
38b24ee2
ZW
603 cpp_pedwarn (pfile,
604 "C99 does not permit use of `__VA_ARGS__' as a macro argument name");
7ceb3598 605 namebuf = (U_CHAR *) xrealloc (namebuf, argslen + len + 1);
ba412f14
ZW
606 name = &namebuf[argslen - 1];
607 argslen += len + 1;
608
609 memcpy (name, tok, len);
610 name[len] = ',';
611 name[len+1] = '\0';
612 argv[argc].len = len;
613 argv[argc].rest_arg = 0;
614 break;
6de1e2a9 615
ba412f14
ZW
616 case CPP_COMMA:
617 argc++;
7ceb3598 618 argv = (struct arg *) xrealloc (argv, (argc + 1)*sizeof(struct arg));
ba412f14
ZW
619 argv[argc].len = 0;
620 break;
6de1e2a9 621
ba412f14
ZW
622 case CPP_RPAREN:
623 goto done;
6de1e2a9 624
ba412f14
ZW
625 case CPP_3DOTS:
626 goto rest_arg;
6de1e2a9 627
ba412f14
ZW
628 case CPP_VSPACE:
629 cpp_error (pfile, "missing right paren in macro argument list");
630 goto invalid;
6de1e2a9 631
ba412f14
ZW
632 default:
633 cpp_error (pfile, "syntax error in #define");
634 goto invalid;
635 }
636 }
6de1e2a9 637
ba412f14
ZW
638 rest_arg:
639 /* There are two possible styles for a vararg macro:
640 the C99 way: #define foo(a, ...) a, __VA_ARGS__
641 the gnu way: #define foo(a, b...) a, b
642 The C99 way can be considered a special case of the gnu way.
643 There are also some constraints to worry about, but we'll handle
644 those elsewhere. */
645 if (argv[argc].len == 0)
646 {
647 if (CPP_PEDANTIC (pfile) && ! CPP_OPTIONS (pfile)->c99)
648 cpp_pedwarn (pfile, "C89 does not permit varargs macros");
6de1e2a9 649
ba412f14 650 len = sizeof "__VA_ARGS__" - 1;
7ceb3598 651 namebuf = (U_CHAR *) xrealloc (namebuf, argslen + len + 1);
ba412f14
ZW
652 name = &namebuf[argslen - 1];
653 argslen += len;
654 memcpy (name, "__VA_ARGS__", len);
ba412f14
ZW
655 argv[argc].len = len;
656 }
657 else
658 if (CPP_PEDANTIC (pfile))
659 cpp_pedwarn (pfile, "ISO C does not permit named varargs macros");
660
661 argv[argc].rest_arg = 1;
ba412f14
ZW
662
663 token = get_directive_token (pfile);
664 if (token != CPP_RPAREN)
665 {
666 cpp_error (pfile, "another parameter follows `...'");
667 goto invalid;
668 }
6de1e2a9 669
ba412f14
ZW
670 done:
671 /* Go through argv and fix up the pointers. */
672 len = 0;
673 for (i = 0; i <= argc; i++)
674 {
675 argv[i].name = namebuf + len;
676 len += argv[i].len + 1;
38b24ee2 677 namebuf[len - 1] = '\0';
ba412f14 678 }
6de1e2a9 679
ba412f14 680 CPP_SET_WRITTEN (pfile, old_written);
6de1e2a9 681
ba412f14
ZW
682 result = (struct arglist *) xmalloc (sizeof (struct arglist));
683 if (namebuf[0] != '\0')
684 {
685 result->namebuf = namebuf;
686 result->argc = argc + 1;
687 result->argv = argv;
6de1e2a9
ZW
688 }
689 else
690 {
ba412f14
ZW
691 free (namebuf);
692 result->namebuf = 0;
693 result->argc = 0;
694 result->argv = 0;
695 }
6de1e2a9 696
ba412f14
ZW
697 return result;
698
699 invalid:
700 if (argv)
701 free (argv);
702 if (namebuf)
703 free (namebuf);
704 return 0;
705}
706
707/* Create a DEFINITION node for a macro. The reader's point is just
708 after the macro name. If FUNLIKE is true, this is a function-like
709 macro. */
710
711DEFINITION *
b0699dad 712_cpp_create_definition (pfile, funlike)
ba412f14
ZW
713 cpp_reader *pfile;
714 int funlike;
715{
716 struct arglist *args = 0;
717 long line, col;
718 const char *file;
719 DEFINITION *defn;
720
721 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
722 file = CPP_BUFFER (pfile)->nominal_fname;
723
724 pfile->no_macro_expand++;
725 pfile->parsing_define_directive++;
726 CPP_OPTIONS (pfile)->discard_comments++;
c45da1ca 727 CPP_OPTIONS (pfile)->no_line_commands++;
ba412f14
ZW
728
729 if (funlike)
730 {
731 args = collect_formal_parameters (pfile);
732 if (args == 0)
733 goto err;
6de1e2a9
ZW
734 }
735
ba412f14
ZW
736 defn = collect_expansion (pfile, args);
737 if (defn == 0)
738 goto err;
739
6de1e2a9
ZW
740 defn->line = line;
741 defn->file = file;
ba412f14 742 defn->col = col;
6de1e2a9 743
ba412f14
ZW
744 pfile->no_macro_expand--;
745 pfile->parsing_define_directive--;
746 CPP_OPTIONS (pfile)->discard_comments--;
c45da1ca 747 CPP_OPTIONS (pfile)->no_line_commands--;
ba412f14 748 return defn;
6de1e2a9 749
ba412f14
ZW
750 err:
751 pfile->no_macro_expand--;
752 pfile->parsing_define_directive--;
753 CPP_OPTIONS (pfile)->discard_comments--;
c45da1ca 754 CPP_OPTIONS (pfile)->no_line_commands--;
ba412f14 755 return 0;
6de1e2a9
ZW
756}
757
758/*
759 * Parse a macro argument and append the info on PFILE's token_buffer.
760 * REST_ARGS means to absorb the rest of the args.
761 * Return nonzero to indicate a syntax error.
762 */
763
764static enum cpp_token
765macarg (pfile, rest_args)
766 cpp_reader *pfile;
767 int rest_args;
768{
769 int paren = 0;
770 enum cpp_token token;
6de1e2a9
ZW
771
772 /* Try to parse as much of the argument as exists at this
773 input stack level. */
6de1e2a9
ZW
774 for (;;)
775 {
776 token = cpp_get_token (pfile);
777 switch (token)
778 {
779 case CPP_EOF:
564ad5f4 780 return token;
6de1e2a9
ZW
781 case CPP_POP:
782 /* If we've hit end of file, it's an error (reported by caller).
783 Ditto if it's the end of cpp_expand_to_buffer text.
784 If we've hit end of macro, just continue. */
785 if (!CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
564ad5f4 786 return token;
6de1e2a9
ZW
787 break;
788 case CPP_LPAREN:
789 paren++;
790 break;
791 case CPP_RPAREN:
792 if (--paren < 0)
793 goto found;
794 break;
795 case CPP_COMMA:
796 /* if we've returned to lowest level and
797 we aren't absorbing all args */
798 if (paren == 0 && rest_args == 0)
799 goto found;
800 break;
801 found:
802 /* Remove ',' or ')' from argument buffer. */
803 CPP_ADJUST_WRITTEN (pfile, -1);
564ad5f4 804 return token;
6de1e2a9
ZW
805 default:;
806 }
807 }
6de1e2a9
ZW
808}
809\f
6de1e2a9
ZW
810
811static struct tm *
812timestamp (pfile)
813 cpp_reader *pfile;
814{
815 if (!pfile->timebuf)
816 {
817 time_t t = time ((time_t *) 0);
818 pfile->timebuf = localtime (&t);
819 }
820 return pfile->timebuf;
821}
822
bcc5cac9 823static const char * const monthnames[] =
6de1e2a9
ZW
824{
825 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
826 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
827};
828
829/*
830 * expand things like __FILE__. Place the expansion into the output
831 * buffer *without* rescanning.
832 */
833
834static void
835special_symbol (hp, pfile)
836 HASHNODE *hp;
837 cpp_reader *pfile;
838{
839 const char *buf;
840 int len;
841 cpp_buffer *ip;
842
843 switch (hp->type)
844 {
845 case T_FILE:
846 case T_BASE_FILE:
847 {
38b24ee2 848 ip = cpp_file_buffer (pfile);
6de1e2a9
ZW
849 if (hp->type == T_BASE_FILE)
850 {
38b24ee2 851 while (CPP_PREV_BUFFER (ip) != NULL)
6de1e2a9
ZW
852 ip = CPP_PREV_BUFFER (ip);
853 }
854
855 buf = ip->nominal_fname;
856
857 if (!buf)
858 buf = "";
859 CPP_RESERVE (pfile, 3 + 4 * strlen (buf));
860 quote_string (pfile, buf);
861 return;
862 }
863
864 case T_INCLUDE_LEVEL:
865 {
38b24ee2
ZW
866 int true_indepth = 1;
867 ip = cpp_file_buffer (pfile);
868 while ((ip = CPP_PREV_BUFFER (ip)) != NULL)
869 true_indepth++;
6de1e2a9
ZW
870
871 CPP_RESERVE (pfile, 10);
872 sprintf (CPP_PWRITTEN (pfile), "%d", true_indepth);
873 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
874 return;
875 }
876
877 case T_VERSION:
6feb7728 878 len = strlen (hp->value.cpval);
6de1e2a9
ZW
879 CPP_RESERVE (pfile, 3 + len);
880 CPP_PUTC_Q (pfile, '"');
6feb7728 881 CPP_PUTS_Q (pfile, hp->value.cpval, len);
6de1e2a9
ZW
882 CPP_PUTC_Q (pfile, '"');
883 CPP_NUL_TERMINATE_Q (pfile);
884 return;
885
886 case T_CONST:
887 buf = hp->value.cpval;
888 if (!buf)
889 return;
890 if (*buf == '\0')
ed45de98 891 buf = "\r ";
6de1e2a9
ZW
892
893 len = strlen (buf);
894 CPP_RESERVE (pfile, len + 1);
895 CPP_PUTS_Q (pfile, buf, len);
896 CPP_NUL_TERMINATE_Q (pfile);
897 return;
898
899 case T_STDC:
900 CPP_RESERVE (pfile, 2);
901#ifdef STDC_0_IN_SYSTEM_HEADERS
38b24ee2 902 ip = cpp_file_buffer (pfile);
6de1e2a9 903 if (ip->system_header_p
707beebb 904 && !cpp_defined (pfile, (const U_CHAR *) "__STRICT_ANSI__", 15))
6de1e2a9
ZW
905 CPP_PUTC_Q (pfile, '0');
906 else
907#endif
908 CPP_PUTC_Q (pfile, '1');
909 CPP_NUL_TERMINATE_Q (pfile);
910 return;
911
912 case T_SPECLINE:
913 {
914 long line;
5e4df1ae 915 cpp_buf_line_and_col (cpp_file_buffer (pfile), &line, NULL);
6de1e2a9
ZW
916
917 CPP_RESERVE (pfile, 10);
918 sprintf (CPP_PWRITTEN (pfile), "%ld", line);
919 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
920 return;
921 }
922
923 case T_DATE:
924 case T_TIME:
925 {
926 struct tm *timebuf;
927
928 CPP_RESERVE (pfile, 20);
929 timebuf = timestamp (pfile);
930 if (hp->type == T_DATE)
931 sprintf (CPP_PWRITTEN (pfile), "\"%s %2d %4d\"",
932 monthnames[timebuf->tm_mon],
933 timebuf->tm_mday, timebuf->tm_year + 1900);
934 else
935 sprintf (CPP_PWRITTEN (pfile), "\"%02d:%02d:%02d\"",
936 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
937
938 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
939 return;
940 }
941
fc009f96
GK
942 case T_POISON:
943 cpp_error (pfile, "attempt to use poisoned `%s'.", hp->name);
944 CPP_RESERVE (pfile, 1);
945 CPP_PUTC_Q (pfile, '0');
946 CPP_NUL_TERMINATE_Q (pfile);
947 break;
948
6de1e2a9 949 default:
c1212d2f 950 cpp_ice (pfile, "invalid special hash type");
6de1e2a9
ZW
951 return;
952 }
6de1e2a9
ZW
953}
954
955/* Expand a macro call.
956 HP points to the symbol that is the macro being called.
957 Put the result of expansion onto the input stack
958 so that subsequent input by our caller will use it.
959
960 If macro wants arguments, caller has already verified that
961 an argument list follows; arguments come from the input stack. */
962
963void
b0699dad 964_cpp_macroexpand (pfile, hp)
6de1e2a9
ZW
965 cpp_reader *pfile;
966 HASHNODE *hp;
967{
968 int nargs;
969 DEFINITION *defn;
970 register U_CHAR *xbuf;
971 long start_line, start_column;
972 int xbuf_len;
a544cfd2 973 struct argdata *args = 0;
6de1e2a9 974 long old_written = CPP_WRITTEN (pfile);
a544cfd2 975 int rest_args, rest_zero = 0;
6de1e2a9
ZW
976 register int i;
977
6de1e2a9
ZW
978 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
979
980 /* Check for and handle special symbols. */
981 if (hp->type != T_MACRO)
982 {
983 special_symbol (hp, pfile);
984 xbuf_len = CPP_WRITTEN (pfile) - old_written;
985 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
986 CPP_SET_WRITTEN (pfile, old_written);
ba412f14 987 memcpy (xbuf, CPP_PWRITTEN (pfile), xbuf_len + 1);
6de1e2a9
ZW
988 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
989 CPP_BUFFER (pfile)->has_escapes = 1;
990 return;
991 }
992
993 defn = hp->value.defn;
994 nargs = defn->nargs;
995 pfile->output_escapes++;
996
997 if (nargs >= 0)
998 {
564ad5f4 999 enum cpp_token token;
6de1e2a9
ZW
1000
1001 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
1002
1003 for (i = 0; i < nargs; i++)
1004 {
1005 args[i].raw = args[i].expanded = 0;
1006 args[i].raw_length = 0;
1007 args[i].expand_length = args[i].stringified_length = -1;
6de1e2a9
ZW
1008 }
1009
1010 /* Parse all the macro args that are supplied. I counts them.
1011 The first NARGS args are stored in ARGS.
1012 The rest are discarded. If rest_args is set then we assume
1013 macarg absorbed the rest of the args. */
1014 i = 0;
1015 rest_args = 0;
564ad5f4
ZW
1016
1017 /* Skip over the opening parenthesis. */
1018 CPP_OPTIONS (pfile)->discard_comments++;
1019 CPP_OPTIONS (pfile)->no_line_commands++;
1020 pfile->no_macro_expand++;
1021 pfile->no_directives++;
1022
1023 token = cpp_get_non_space_token (pfile);
1024 if (token != CPP_LPAREN)
1025 cpp_ice (pfile, "macroexpand: unexpected token %d (wanted LPAREN)",
1026 token);
1027 CPP_ADJUST_WRITTEN (pfile, -1);
1028
1029 token = CPP_EOF;
6de1e2a9
ZW
1030 do
1031 {
1032 if (rest_args)
1033 continue;
1034 if (i < nargs || (nargs == 0 && i == 0))
1035 {
1036 /* if we are working on last arg which absorbs rest of args... */
1037 if (i == nargs - 1 && defn->rest_args)
1038 rest_args = 1;
1039 args[i].raw = CPP_WRITTEN (pfile);
1040 token = macarg (pfile, rest_args);
1041 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
6de1e2a9
ZW
1042 }
1043 else
1044 token = macarg (pfile, 0);
1045 if (token == CPP_EOF || token == CPP_POP)
564ad5f4
ZW
1046 cpp_error_with_line (pfile, start_line, start_column,
1047 "unterminated macro call");
6de1e2a9
ZW
1048 i++;
1049 }
1050 while (token == CPP_COMMA);
564ad5f4
ZW
1051 CPP_OPTIONS (pfile)->discard_comments--;
1052 CPP_OPTIONS (pfile)->no_line_commands--;
1053 pfile->no_macro_expand--;
1054 pfile->no_directives--;
1055 if (token != CPP_RPAREN)
1056 return;
6de1e2a9
ZW
1057
1058 /* If we got one arg but it was just whitespace, call that 0 args. */
1059 if (i == 1)
1060 {
1061 register U_CHAR *bp = ARG_BASE + args[0].raw;
1062 register U_CHAR *lim = bp + args[0].raw_length;
1063 /* cpp.texi says for foo ( ) we provide one argument.
1064 However, if foo wants just 0 arguments, treat this as 0. */
1065 if (nargs == 0)
a9ae4483 1066 while (bp != lim && is_space(*bp))
6de1e2a9
ZW
1067 bp++;
1068 if (bp == lim)
1069 i = 0;
1070 }
1071
1072 /* Don't output an error message if we have already output one for
1073 a parse error above. */
1074 rest_zero = 0;
1075 if (nargs == 0 && i > 0)
1076 {
1077 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
1078 }
1079 else if (i < nargs)
1080 {
1081 /* traditional C allows foo() if foo wants one argument. */
1082 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
1083 ;
1084 /* the rest args token is allowed to absorb 0 tokens */
1085 else if (i == nargs - 1 && defn->rest_args)
1086 rest_zero = 1;
1087 else if (i == 0)
1088 cpp_error (pfile, "macro `%s' used without args", hp->name);
1089 else if (i == 1)
1090 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
1091 else
1092 cpp_error (pfile, "macro `%s' used with only %d args",
1093 hp->name, i);
1094 }
1095 else if (i > nargs)
1096 {
1097 cpp_error (pfile,
1098 "macro `%s' used with too many (%d) args", hp->name, i);
1099 }
1100 }
1101
1102 /* If macro wants zero args, we parsed the arglist for checking only.
1103 Read directly from the macro definition. */
1104 if (nargs <= 0)
1105 {
1106 xbuf = defn->expansion;
1107 xbuf_len = defn->length;
1108 }
1109 else
1110 {
1111 register U_CHAR *exp = defn->expansion;
1112 register int offset; /* offset in expansion,
1113 copied a piece at a time */
1114 register int totlen; /* total amount of exp buffer filled so far */
1115
1116 register struct reflist *ap, *last_ap;
1117
1118 /* Macro really takes args. Compute the expansion of this call. */
1119
1120 /* Compute length in characters of the macro's expansion.
1121 Also count number of times each arg is used. */
1122 xbuf_len = defn->length;
1123 for (ap = defn->pattern; ap != NULL; ap = ap->next)
1124 {
1125 if (ap->stringify)
1126 {
1127 register struct argdata *arg = &args[ap->argno];
1128 /* Stringify if it hasn't already been */
1129 if (arg->stringified_length < 0)
1130 {
1131 int arglen = arg->raw_length;
1132 int escaped = 0;
1133 int in_string = 0;
1134 int c;
1135 /* Initially need_space is -1. Otherwise, 1 means the
1136 previous character was a space, but we suppressed it;
1137 0 means the previous character was a non-space. */
1138 int need_space = -1;
1139 i = 0;
1140 arg->stringified = CPP_WRITTEN (pfile);
1141 if (!CPP_TRADITIONAL (pfile))
1142 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
1143 for (; i < arglen; i++)
1144 {
1145 c = (ARG_BASE + arg->raw)[i];
1146
1147 if (!in_string)
1148 {
eaefae0e
ZW
1149 /* Delete "\r " and "\r-" escapes. */
1150 if (c == '\r')
1151 {
1152 i++;
1153 continue;
1154 }
6de1e2a9
ZW
1155 /* Internal sequences of whitespace are
1156 replaced by one space except within
1157 a string or char token. */
eaefae0e 1158 else if (is_space(c))
6de1e2a9 1159 {
6de1e2a9
ZW
1160 if (need_space == 0)
1161 need_space = 1;
1162 continue;
1163 }
1164 else if (need_space > 0)
1165 CPP_PUTC (pfile, ' ');
1166 need_space = 0;
1167 }
1168
1169 if (escaped)
1170 escaped = 0;
1171 else
1172 {
1173 if (c == '\\')
1174 escaped = 1;
1175 if (in_string)
1176 {
1177 if (c == in_string)
1178 in_string = 0;
1179 }
1180 else if (c == '\"' || c == '\'')
1181 in_string = c;
1182 }
1183
1184 /* Escape these chars */
1185 if (c == '\"' || (in_string && c == '\\'))
1186 CPP_PUTC (pfile, '\\');
1187 if (ISPRINT (c))
1188 CPP_PUTC (pfile, c);
1189 else
1190 {
1191 CPP_RESERVE (pfile, 4);
1192 sprintf ((char *) CPP_PWRITTEN (pfile), "\\%03o",
1193 (unsigned int) c);
1194 CPP_ADJUST_WRITTEN (pfile, 4);
1195 }
1196 }
1197 if (!CPP_TRADITIONAL (pfile))
1198 CPP_PUTC (pfile, '\"'); /* insert ending quote */
1199 arg->stringified_length
1200 = CPP_WRITTEN (pfile) - arg->stringified;
1201 }
1202 xbuf_len += args[ap->argno].stringified_length;
1203 }
1204 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
4571dc27 1205 /* Add 4 for two \r-space markers to prevent
6de1e2a9
ZW
1206 token concatenation. */
1207 xbuf_len += args[ap->argno].raw_length + 4;
1208 else
1209 {
1210 /* We have an ordinary (expanded) occurrence of the arg.
1211 So compute its expansion, if we have not already. */
1212 if (args[ap->argno].expand_length < 0)
1213 {
1214 args[ap->argno].expanded = CPP_WRITTEN (pfile);
1215 cpp_expand_to_buffer (pfile,
1216 ARG_BASE + args[ap->argno].raw,
1217 args[ap->argno].raw_length);
1218
1219 args[ap->argno].expand_length
1220 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
1221 }
1222
4571dc27 1223 /* Add 4 for two \r-space markers to prevent
6de1e2a9
ZW
1224 token concatenation. */
1225 xbuf_len += args[ap->argno].expand_length + 4;
1226 }
6de1e2a9
ZW
1227 }
1228
1229 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
1230
1231 /* Generate in XBUF the complete expansion
1232 with arguments substituted in.
1233 TOTLEN is the total size generated so far.
1234 OFFSET is the index in the definition
1235 of where we are copying from. */
1236 offset = totlen = 0;
1237 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
1238 last_ap = ap, ap = ap->next)
1239 {
1240 register struct argdata *arg = &args[ap->argno];
1241 int count_before = totlen;
1242
1243 /* Add chars to XBUF. */
ba412f14
ZW
1244 i = ap->nchars;
1245 memcpy (&xbuf[totlen], &exp[offset], i);
1246 totlen += i;
1247 offset += i;
6de1e2a9
ZW
1248
1249 /* If followed by an empty rest arg with concatenation,
1250 delete the last run of nonwhite chars. */
1251 if (rest_zero && totlen > count_before
1252 && ((ap->rest_args && ap->raw_before)
1253 || (last_ap != NULL && last_ap->rest_args
1254 && last_ap->raw_after)))
1255 {
1256 /* Delete final whitespace. */
a9ae4483 1257 while (totlen > count_before && is_space(xbuf[totlen - 1]))
6de1e2a9
ZW
1258 totlen--;
1259
1260 /* Delete the nonwhites before them. */
a9ae4483 1261 while (totlen > count_before && !is_space(xbuf[totlen - 1]))
6de1e2a9
ZW
1262 totlen--;
1263 }
1264
1265 if (ap->stringify != 0)
1266 {
ba412f14
ZW
1267 memcpy (xbuf + totlen, ARG_BASE + arg->stringified,
1268 arg->stringified_length);
6de1e2a9
ZW
1269 totlen += arg->stringified_length;
1270 }
1271 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
1272 {
1273 U_CHAR *p1 = ARG_BASE + arg->raw;
1274 U_CHAR *l1 = p1 + arg->raw_length;
1275 if (ap->raw_before)
1276 {
5d83f44b
ZW
1277 /* Arg is concatenated before: delete leading whitespace,
1278 whitespace markers, and no-reexpansion markers. */
1279 while (p1 != l1)
1280 {
a9ae4483 1281 if (is_space(p1[0]))
5d83f44b
ZW
1282 p1++;
1283 else if (p1[0] == '\r')
1284 p1 += 2;
1285 else
1286 break;
1287 }
6de1e2a9
ZW
1288 }
1289 if (ap->raw_after)
1290 {
1291 /* Arg is concatenated after: delete trailing whitespace,
1292 whitespace markers, and no-reexpansion markers. */
1293 while (p1 != l1)
1294 {
a9ae4483 1295 if (is_space(l1[-1]))
6de1e2a9 1296 l1--;
ed45de98
ZW
1297 else if (l1[-1] == '\r')
1298 l1--;
6de1e2a9
ZW
1299 else if (l1[-1] == '-')
1300 {
ed45de98 1301 if (l1 != p1 + 1 && l1[-2] == '\r')
6de1e2a9
ZW
1302 l1 -= 2;
1303 else
1304 break;
1305 }
1306 else
1307 break;
1308 }
1309 }
1310
1311 /* Delete any no-reexpansion marker that precedes
1312 an identifier at the beginning of the argument. */
ed45de98 1313 if (p1[0] == '\r' && p1[1] == '-')
6de1e2a9
ZW
1314 p1 += 2;
1315
ba412f14 1316 memcpy (xbuf + totlen, p1, l1 - p1);
6de1e2a9
ZW
1317 totlen += l1 - p1;
1318 }
1319 else
1320 {
1321 U_CHAR *expanded = ARG_BASE + arg->expanded;
1322 if (!ap->raw_before && totlen > 0 && arg->expand_length
1323 && !CPP_TRADITIONAL (pfile)
455d2586 1324 && unsafe_chars (pfile, xbuf[totlen - 1], expanded[0]))
6de1e2a9 1325 {
ed45de98 1326 xbuf[totlen++] = '\r';
6de1e2a9
ZW
1327 xbuf[totlen++] = ' ';
1328 }
1329
ba412f14 1330 memcpy (xbuf + totlen, expanded, arg->expand_length);
6de1e2a9
ZW
1331 totlen += arg->expand_length;
1332
1333 if (!ap->raw_after && totlen > 0 && offset < defn->length
1334 && !CPP_TRADITIONAL (pfile)
455d2586 1335 && unsafe_chars (pfile, xbuf[totlen - 1], exp[offset]))
6de1e2a9 1336 {
ed45de98 1337 xbuf[totlen++] = '\r';
6de1e2a9
ZW
1338 xbuf[totlen++] = ' ';
1339 }
6de1e2a9
ZW
1340 }
1341
1342 if (totlen > xbuf_len)
34ca9541 1343 {
c1212d2f 1344 cpp_ice (pfile, "buffer overrun in macroexpand");
34ca9541
ZW
1345 return;
1346 }
6de1e2a9
ZW
1347 }
1348
1349 /* if there is anything left of the definition
1350 after handling the arg list, copy that in too. */
1351
1352 for (i = offset; i < defn->length; i++)
1353 {
1354 /* if we've reached the end of the macro */
1355 if (exp[i] == ')')
1356 rest_zero = 0;
1357 if (!(rest_zero && last_ap != NULL && last_ap->rest_args
1358 && last_ap->raw_after))
1359 xbuf[totlen++] = exp[i];
1360 }
1361
1362 xbuf[totlen] = 0;
1363 xbuf_len = totlen;
1364
1365 }
1366
1367 pfile->output_escapes--;
1368
1369 /* Now put the expansion on the input stack
1370 so our caller will commence reading from it. */
1371 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
1372 CPP_BUFFER (pfile)->has_escapes = 1;
1373
1374 /* Pop the space we've used in the token_buffer for argument expansion. */
1375 CPP_SET_WRITTEN (pfile, old_written);
1376
1377 /* Recursive macro use sometimes works traditionally.
1378 #define foo(x,y) bar (x (y,0), y)
1379 foo (foo, baz) */
1380
1381 if (!CPP_TRADITIONAL (pfile))
1382 hp->type = T_DISABLED;
1383}
1384
1385/* Return 1 iff a token ending in C1 followed directly by a token C2
1386 could cause mis-tokenization. */
1387
1388static int
455d2586
ZW
1389unsafe_chars (pfile, c1, c2)
1390 cpp_reader *pfile;
6de1e2a9
ZW
1391 int c1, c2;
1392{
1393 switch (c1)
1394 {
5d83f44b 1395 case '+': case '-':
6de1e2a9
ZW
1396 if (c2 == c1 || c2 == '=')
1397 return 1;
1398 goto letter;
1399
5d83f44b 1400 case 'e': case 'E': case 'p': case 'P':
6de1e2a9
ZW
1401 if (c2 == '-' || c2 == '+')
1402 return 1; /* could extend a pre-processing number */
1403 goto letter;
1404
455d2586
ZW
1405 case '$':
1406 if (CPP_OPTIONS (pfile)->dollars_in_ident)
1407 goto letter;
1408 return 0;
1409
6de1e2a9
ZW
1410 case 'L':
1411 if (c2 == '\'' || c2 == '\"')
1412 return 1; /* Could turn into L"xxx" or L'xxx'. */
1413 goto letter;
1414
5d83f44b
ZW
1415 case '.': case '0': case '1': case '2': case '3':
1416 case '4': case '5': case '6': case '7': case '8': case '9':
6de1e2a9
ZW
1417 case '_': case 'a': case 'b': case 'c': case 'd': case 'f':
1418 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1419 case 'm': case 'n': case 'o': case 'q': case 'r': case 's':
1420 case 't': case 'u': case 'v': case 'w': case 'x': case 'y':
1421 case 'z': case 'A': case 'B': case 'C': case 'D': case 'F':
1422 case 'G': case 'H': case 'I': case 'J': case 'K': case 'M':
1423 case 'N': case 'O': case 'Q': case 'R': case 'S': case 'T':
1424 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
1425 letter:
1426 /* We're in the middle of either a name or a pre-processing number. */
a9ae4483 1427 return (is_idchar(c2) || c2 == '.');
6de1e2a9
ZW
1428
1429 case '<': case '>': case '!': case '%': case '#': case ':':
1430 case '^': case '&': case '|': case '*': case '/': case '=':
1431 return (c2 == c1 || c2 == '=');
1432 }
1433 return 0;
1434}
1435
1436static void
1437push_macro_expansion (pfile, xbuf, xbuf_len, hp)
1438 cpp_reader *pfile;
1439 register U_CHAR *xbuf;
1440 int xbuf_len;
1441 HASHNODE *hp;
1442{
1443 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
1444 if (mbuf == NULL)
1445 return;
1446 mbuf->cleanup = macro_cleanup;
1447 mbuf->data = hp;
1448
ed45de98 1449 /* The first chars of the expansion should be a "\r " added by
6de1e2a9
ZW
1450 collect_expansion. This is to prevent accidental token-pasting
1451 between the text preceding the macro invocation, and the macro
1452 expansion text.
1453
1454 We would like to avoid adding unneeded spaces (for the sake of
1455 tools that use cpp, such as imake). In some common cases we can
1456 tell that it is safe to omit the space.
1457
1458 The character before the macro invocation cannot have been an
1459 idchar (or else it would have been pasted with the idchars of
1460 the macro name). Therefore, if the first non-space character
1461 of the expansion is an idchar, we do not need the extra space
1462 to prevent token pasting.
1463
1464 Also, we don't need the extra space if the first char is '(',
1465 or some other (less common) characters. */
1466
ed45de98 1467 if (xbuf[0] == '\r' && xbuf[1] == ' '
a9ae4483 1468 && (is_idchar(xbuf[2]) || xbuf[2] == '(' || xbuf[2] == '\''
6de1e2a9
ZW
1469 || xbuf[2] == '\"'))
1470 mbuf->cur += 2;
1471
1472 /* Likewise, avoid the extra space at the end of the macro expansion
1473 if this is safe. We can do a better job here since we can know
1474 what the next char will be. */
1475 if (xbuf_len >= 3
ed45de98 1476 && mbuf->rlimit[-2] == '\r'
6de1e2a9
ZW
1477 && mbuf->rlimit[-1] == ' ')
1478 {
1479 int c1 = mbuf->rlimit[-3];
1480 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
455d2586 1481 if (c2 == EOF || !unsafe_chars (pfile, c1, c2))
6de1e2a9
ZW
1482 mbuf->rlimit -= 2;
1483 }
1484}
1485
1486/* Return zero if two DEFINITIONs are isomorphic. */
1487
1488int
b0699dad 1489_cpp_compare_defs (pfile, d1, d2)
6de1e2a9
ZW
1490 cpp_reader *pfile;
1491 DEFINITION *d1, *d2;
1492{
38b24ee2
ZW
1493 struct reflist *a1, *a2;
1494 U_CHAR *p1 = d1->expansion;
1495 U_CHAR *p2 = d2->expansion;
6de1e2a9
ZW
1496 int first = 1;
1497
1498 if (d1->nargs != d2->nargs)
1499 return 1;
1500 if (CPP_PEDANTIC (pfile)
38b24ee2
ZW
1501 && d1->argnames && d2->argnames)
1502 {
1503 U_CHAR *arg1 = d1->argnames;
1504 U_CHAR *arg2 = d2->argnames;
1505 size_t len;
1506 int i = d1->nargs;
1507 while (i--)
1508 {
8051b178 1509 len = strlen (arg1) + 1;
38b24ee2
ZW
1510 if (strcmp (arg1, arg2))
1511 return 1;
1512 arg1 += len;
1513 arg2 += len;
1514 }
1515 }
6de1e2a9
ZW
1516 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1517 a1 = a1->next, a2 = a2->next)
1518 {
1519 if (!((a1->nchars == a2->nchars && !strncmp (p1, p2, a1->nchars))
1520 || !comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1521 || a1->argno != a2->argno
1522 || a1->stringify != a2->stringify
1523 || a1->raw_before != a2->raw_before
1524 || a1->raw_after != a2->raw_after)
1525 return 1;
1526 first = 0;
1527 p1 += a1->nchars;
1528 p2 += a2->nchars;
1529 }
1530 if (a1 != a2)
1531 return 1;
1532
1533 return comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1534 p2, d2->length - (p2 - d2->expansion), 1);
1535}
1536
1537/* Return 1 if two parts of two macro definitions are effectively different.
1538 One of the parts starts at BEG1 and has LEN1 chars;
1539 the other has LEN2 chars at BEG2.
1540 Any sequence of whitespace matches any other sequence of whitespace.
1541 FIRST means these parts are the first of a macro definition;
1542 so ignore leading whitespace entirely.
1543 LAST means these parts are the last of a macro definition;
1544 so ignore trailing whitespace entirely. */
1545
1546static int
1547comp_def_part (first, beg1, len1, beg2, len2, last)
1548 int first;
1549 U_CHAR *beg1, *beg2;
1550 int len1, len2;
1551 int last;
1552{
1553 register U_CHAR *end1 = beg1 + len1;
1554 register U_CHAR *end2 = beg2 + len2;
1555 if (first)
1556 {
a9ae4483 1557 while (beg1 != end1 && is_space(*beg1))
6de1e2a9 1558 beg1++;
a9ae4483 1559 while (beg2 != end2 && is_space(*beg2))
6de1e2a9
ZW
1560 beg2++;
1561 }
1562 if (last)
1563 {
a9ae4483 1564 while (beg1 != end1 && is_space(end1[-1]))
6de1e2a9 1565 end1--;
a9ae4483 1566 while (beg2 != end2 && is_space(end2[-1]))
6de1e2a9
ZW
1567 end2--;
1568 }
1569 while (beg1 != end1 && beg2 != end2)
1570 {
a9ae4483 1571 if (is_space(*beg1) && is_space(*beg2))
6de1e2a9 1572 {
a9ae4483 1573 while (beg1 != end1 && is_space(*beg1))
6de1e2a9 1574 beg1++;
a9ae4483 1575 while (beg2 != end2 && is_space(*beg2))
6de1e2a9
ZW
1576 beg2++;
1577 }
1578 else if (*beg1 == *beg2)
1579 {
1580 beg1++;
1581 beg2++;
1582 }
1583 else
1584 break;
1585 }
1586 return (beg1 != end1) || (beg2 != end2);
1587}
3caee4a8
ZW
1588
1589/* Dump the definition of macro MACRO on stdout. The format is suitable
1590 to be read back in again. */
1591
1592void
b0699dad 1593_cpp_dump_definition (pfile, sym, len, defn)
3caee4a8 1594 cpp_reader *pfile;
a2a76ce7
ZW
1595 const U_CHAR *sym;
1596 long len;
1597 DEFINITION *defn;
3caee4a8 1598{
c45da1ca
ZW
1599 if (pfile->lineno == 0)
1600 output_line_command (pfile, same_file);
1601
a2a76ce7 1602 CPP_RESERVE (pfile, len + sizeof "#define ");
3caee4a8 1603 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " -1);
a2a76ce7 1604 CPP_PUTS_Q (pfile, sym, len);
3caee4a8
ZW
1605
1606 if (defn->nargs == -1)
1607 {
1608 CPP_PUTC_Q (pfile, ' ');
1609
1610 /* The first and last two characters of a macro expansion are
1611 always "\r "; this needs to be trimmed out.
1612 So we need length-4 chars of space, plus one for the NUL. */
1613 CPP_RESERVE (pfile, defn->length - 4 + 1);
1614 CPP_PUTS_Q (pfile, defn->expansion + 2, defn->length - 4);
3caee4a8
ZW
1615 }
1616 else
1617 {
1618 struct reflist *r;
e7553be5
DB
1619 unsigned char **argv = (unsigned char **) alloca (defn->nargs *
1620 sizeof(char *));
1621 int *argl = (int *) alloca (defn->nargs * sizeof(int));
3caee4a8
ZW
1622 unsigned char *x;
1623 int i;
1624
1625 /* First extract the argument list. */
38b24ee2
ZW
1626 x = defn->argnames;
1627 for (i = 0; i < defn->nargs; i++)
3caee4a8
ZW
1628 {
1629 argv[i] = x;
38b24ee2
ZW
1630 argl[i] = strlen (x);
1631 x += argl[i] + 1;
3caee4a8
ZW
1632 }
1633
1634 /* Now print out the argument list. */
1635 CPP_PUTC_Q (pfile, '(');
1636 for (i = 0; i < defn->nargs; i++)
1637 {
1638 CPP_RESERVE (pfile, argl[i] + 2);
38b24ee2
ZW
1639 if (!(i == defn->nargs-1 && defn->rest_args
1640 && !strcmp (argv[i], "__VA_ARGS__")))
1641 CPP_PUTS_Q (pfile, argv[i], argl[i]);
3caee4a8
ZW
1642 if (i < defn->nargs-1)
1643 CPP_PUTS_Q (pfile, ", ", 2);
1644 }
3caee4a8 1645 if (defn->rest_args)
38b24ee2
ZW
1646 CPP_PUTS (pfile, "...", 3);
1647 CPP_PUTS (pfile, ") ", 2);
3caee4a8
ZW
1648
1649 /* Now the definition. */
1650 x = defn->expansion;
1651 for (r = defn->pattern; r; r = r->next)
1652 {
1653 i = r->nchars;
1654 if (*x == '\r') x += 2, i -= 2;
1655 /* i chars for macro text, plus the length of the macro
1656 argument name, plus one for a stringify marker, plus two for
1657 each concatenation marker. */
1658 CPP_RESERVE (pfile,
1659 i + argl[r->argno] + r->stringify
1660 + (r->raw_before + r->raw_after) * 2);
1661
1662 if (i > 0) CPP_PUTS_Q (pfile, x, i);
1663 if (r->raw_before)
1664 CPP_PUTS_Q (pfile, "##", 2);
1665 if (r->stringify)
1666 CPP_PUTC_Q (pfile, '#');
1667 CPP_PUTS_Q (pfile, argv[r->argno], argl[r->argno]);
1668 if (r->raw_after && !(r->next && r->next->nchars == 0
1669 && r->next->raw_before))
1670 CPP_PUTS_Q (pfile, "##", 2);
1671
1672 x += i;
1673 }
1674
1675 i = defn->length - (x - defn->expansion) - 2;
1676 if (*x == '\r') x += 2, i -= 2;
1677 if (i > 0) CPP_PUTS (pfile, x, i);
3caee4a8 1678 }
c45da1ca
ZW
1679 if (pfile->lineno == 0)
1680 CPP_PUTC (pfile, '\n');
1681 CPP_NUL_TERMINATE (pfile);
3caee4a8 1682}
d35364d1
ZW
1683
1684/* Dump out the hash table. */
1685static int
1686dump_hash_helper (h, p)
1687 void *h;
1688 void *p;
1689{
1690 HASHNODE *hp = (HASHNODE *)h;
1691 cpp_reader *pfile = (cpp_reader *)p;
1692
1693 _cpp_dump_definition (pfile, hp->name, hp->length, hp->value.defn);
1694 return 1;
1695}
1696
1697void
1698_cpp_dump_macro_hash (pfile)
1699 cpp_reader *pfile;
1700{
1701 htab_traverse (pfile->hashtab, dump_hash_helper, pfile);
1702}
This page took 0.697352 seconds and 5 git commands to generate.