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