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