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