oops - omitted from previous delta fixing UNIQUE_SECTION
[official-gcc.git] / gcc / cpphash.c
blob8a0c887e5803ec8b00f36f2a9dce991da64d81a9
1 /* Part of CPP library. (Macro handling.)
2 Copyright (C) 1986, 87, 89, 92-96, 98, 99, 2000 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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! */
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "cpphash.h"
29 #undef abort
31 static int comp_def_part PARAMS ((int, U_CHAR *, int, U_CHAR *,
32 int, int));
33 static void push_macro_expansion PARAMS ((cpp_reader *,
34 U_CHAR *, int, HASHNODE *));
35 static int unsafe_chars PARAMS ((int, int));
36 static int macro_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
37 static enum cpp_token macarg PARAMS ((cpp_reader *, int));
38 static struct tm *timestamp PARAMS ((cpp_reader *));
39 static void special_symbol PARAMS ((HASHNODE *, cpp_reader *));
42 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
43 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
44 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
46 extern char *version_string;
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. */
59 struct arglist
61 struct arglist *next;
62 U_CHAR *name;
63 int length;
64 int argno;
65 char rest_args;
68 static DEFINITION *collect_expansion PARAMS ((cpp_reader *, U_CHAR *, U_CHAR *,
69 int, struct arglist *));
71 /* This structure represents one parsed argument in a macro call.
72 `raw' points to the argument text as written (`raw_length' is its length).
73 `expanded' points to the argument's macro-expansion
74 (its length is `expand_length').
75 `stringified_length' is the length the argument would have
76 if stringified. */
78 /* raw and expanded are relative to ARG_BASE */
79 #define ARG_BASE ((pfile)->token_buffer)
81 struct argdata
83 /* Strings relative to pfile->token_buffer */
84 long raw, expanded, stringified;
85 int raw_length, expand_length;
86 int stringified_length;
90 /* Return hash function on name. must be compatible with the one
91 computed a step at a time, elsewhere */
93 int
94 hashf (name, len, hashsize)
95 register const U_CHAR *name;
96 register int len;
97 int hashsize;
99 register int r = 0;
101 while (len--)
102 r = HASHSTEP (r, *name++);
104 return MAKE_POS (r) % hashsize;
107 /* Find the most recent hash node for name "name" (ending with first
108 non-identifier char) installed by cpp_install
110 If LEN is >= 0, it is the length of the name.
111 Otherwise, compute the length by scanning the entire name.
113 If HASH is >= 0, it is the precomputed hash code.
114 Otherwise, compute the hash code. */
116 HASHNODE *
117 cpp_lookup (pfile, name, len, hash)
118 cpp_reader *pfile ATTRIBUTE_UNUSED;
119 const U_CHAR *name;
120 int len;
121 int hash;
123 register const U_CHAR *bp;
124 register HASHNODE *bucket;
126 if (len < 0)
128 for (bp = name; is_idchar (*bp); bp++);
129 len = bp - name;
132 if (hash < 0)
133 hash = hashf (name, len, HASHSIZE);
135 bucket = pfile->hashtab[hash];
136 while (bucket)
138 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
139 return bucket;
140 bucket = bucket->next;
142 return (HASHNODE *) 0;
146 * Delete a hash node. Some weirdness to free junk from macros.
147 * More such weirdness will have to be added if you define more hash
148 * types that need it.
151 /* Note that the DEFINITION of a macro is removed from the hash table
152 but its storage is not freed. This would be a storage leak
153 except that it is not reasonable to keep undefining and redefining
154 large numbers of macros many times.
155 In any case, this is necessary, because a macro can be #undef'd
156 in the middle of reading the arguments to a call to it.
157 If #undef freed the DEFINITION, that would crash. */
159 void
160 delete_macro (hp)
161 HASHNODE *hp;
164 if (hp->prev != NULL)
165 hp->prev->next = hp->next;
166 if (hp->next != NULL)
167 hp->next->prev = hp->prev;
169 /* make sure that the bucket chain header that
170 the deleted guy was on points to the right thing afterwards. */
171 if (hp == *hp->bucket_hdr)
172 *hp->bucket_hdr = hp->next;
174 if (hp->type == T_MACRO)
176 DEFINITION *d = hp->value.defn;
177 struct reflist *ap, *nextap;
179 for (ap = d->pattern; ap != NULL; ap = nextap)
181 nextap = ap->next;
182 free (ap);
184 if (d->nargs >= 0)
185 free (d->args.argnames);
186 free (d);
189 free (hp);
192 /* Install a name in the main hash table, even if it is already there.
193 Name stops with first non alphanumeric, except leading '#'.
194 Caller must check against redefinition if that is desired.
195 delete_macro () removes things installed by cpp_install () in fifo order.
196 this is important because of the `defined' special symbol used
197 in #if, and also if pushdef/popdef directives are ever implemented.
199 If LEN is >= 0, it is the length of the name.
200 Otherwise, compute the length by scanning the entire name.
202 If HASH is >= 0, it is the precomputed hash code.
203 Otherwise, compute the hash code. */
205 HASHNODE *
206 cpp_install (pfile, name, len, type, value, hash)
207 cpp_reader *pfile;
208 const U_CHAR *name;
209 int len;
210 enum node_type type;
211 const char *value;
212 int hash;
214 register HASHNODE *hp;
215 register int i, bucket;
216 register const U_CHAR *p;
218 if (len < 0)
220 p = name;
221 while (is_idchar(*p))
222 p++;
223 len = p - name;
226 if (hash < 0)
227 hash = hashf (name, len, HASHSIZE);
229 i = sizeof (HASHNODE) + len + 1;
230 hp = (HASHNODE *) xmalloc (i);
231 bucket = hash;
232 hp->bucket_hdr = &pfile->hashtab[bucket];
233 hp->next = pfile->hashtab[bucket];
234 pfile->hashtab[bucket] = hp;
235 hp->prev = NULL;
236 if (hp->next != NULL)
237 hp->next->prev = hp;
238 hp->type = type;
239 hp->length = len;
240 hp->value.cpval = value;
241 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
242 bcopy (name, hp->name, len);
243 hp->name[len] = 0;
244 return hp;
247 static int
248 macro_cleanup (pbuf, pfile)
249 cpp_buffer *pbuf;
250 cpp_reader *pfile ATTRIBUTE_UNUSED;
252 HASHNODE *macro = (HASHNODE *) pbuf->data;
253 if (macro->type == T_DISABLED)
254 macro->type = T_MACRO;
255 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
256 free (pbuf->buf);
257 return 0;
261 /* Read a replacement list for a macro with parameters.
262 Build the DEFINITION structure.
263 Reads characters of text starting at BUF until END.
264 ARGLIST specifies the formal parameters to look for
265 in the text of the definition; NARGS is the number of args
266 in that list, or -1 for a macro name that wants no argument list.
267 MACRONAME is the macro name itself (so we can avoid recursive expansion)
268 and NAMELEN is its length in characters.
270 Note that comments, backslash-newlines, and leading white space
271 have already been deleted from the argument. */
273 static DEFINITION *
274 collect_expansion (pfile, buf, limit, nargs, arglist)
275 cpp_reader *pfile;
276 U_CHAR *buf, *limit;
277 int nargs;
278 struct arglist *arglist;
280 DEFINITION *defn;
281 register U_CHAR *p, *lastp, *exp_p;
282 struct reflist *endpat = NULL;
283 /* Pointer to first nonspace after last ## seen. */
284 U_CHAR *concat = 0;
285 /* Pointer to first nonspace after last single-# seen. */
286 U_CHAR *stringify = 0;
287 int maxsize;
288 int expected_delimiter = '\0';
290 /* Scan thru the replacement list, ignoring comments and quoted
291 strings, picking up on the macro calls. It does a linear search
292 thru the arg list on every potential symbol. Profiling might say
293 that something smarter should happen. */
295 if (limit < buf)
297 cpp_fatal (pfile, "internal error: limit < buf in collect_expansion");
298 limit = buf; /* treat it like a null defn */
301 /* Find the beginning of the trailing whitespace. */
302 p = buf;
303 while (p < limit && is_space(limit[-1]))
304 limit--;
306 /* Allocate space for the text in the macro definition.
307 Leading and trailing whitespace chars need 2 bytes each.
308 Each other input char may or may not need 1 byte,
309 so this is an upper bound. The extra 5 are for invented
310 leading and trailing escape-marker and final null. */
311 maxsize = (sizeof (DEFINITION)
312 + (limit - p) + 5);
313 defn = (DEFINITION *) xcalloc (1, maxsize);
315 defn->nargs = nargs;
316 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
317 lastp = exp_p;
319 p = buf;
321 /* Add one initial space escape-marker to prevent accidental
322 token-pasting (often removed by macroexpand). */
323 *exp_p++ = '\r';
324 *exp_p++ = ' ';
326 if (limit - p >= 2 && p[0] == '#' && p[1] == '#')
328 cpp_error (pfile, "`##' at start of macro definition");
329 p += 2;
332 /* Process the main body of the definition. */
333 while (p < limit)
335 int skipped_arg = 0;
336 register U_CHAR c = *p++;
338 *exp_p++ = c;
340 if (!CPP_TRADITIONAL (pfile))
342 switch (c)
344 case '\'':
345 case '\"':
346 if (expected_delimiter != '\0')
348 if (c == expected_delimiter)
349 expected_delimiter = '\0';
351 else
352 expected_delimiter = c;
353 break;
355 case '\\':
356 if (p < limit && expected_delimiter)
358 /* In a string, backslash goes through
359 and makes next char ordinary. */
360 *exp_p++ = *p++;
362 break;
364 case '#':
365 /* # is ordinary inside a string. */
366 if (expected_delimiter)
367 break;
368 if (p < limit && *p == '#')
370 /* ##: concatenate preceding and following tokens. */
371 /* Take out the first #, discard preceding whitespace. */
372 exp_p--;
373 while (exp_p > lastp && is_hspace(exp_p[-1]))
374 --exp_p;
375 /* Skip the second #. */
376 p++;
377 /* Discard following whitespace. */
378 SKIP_WHITE_SPACE (p);
379 concat = p;
380 if (p == limit)
381 cpp_error (pfile, "`##' at end of macro definition");
383 else if (nargs >= 0)
385 /* Single #: stringify following argument ref.
386 Don't leave the # in the expansion. */
387 exp_p--;
388 SKIP_WHITE_SPACE (p);
389 if (p == limit || !is_idstart(*p)
390 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' ||
391 p[1] == '"')))
392 cpp_error (pfile,
393 "`#' operator is not followed by a macro argument name");
394 else
395 stringify = p;
397 break;
400 else
402 /* In -traditional mode, recognize arguments inside strings and
403 character constants, and ignore special properties of #.
404 Arguments inside strings are considered "stringified", but no
405 extra quote marks are supplied. */
406 switch (c)
408 case '\'':
409 case '\"':
410 if (expected_delimiter != '\0')
412 if (c == expected_delimiter)
413 expected_delimiter = '\0';
415 else
416 expected_delimiter = c;
417 break;
419 case '\\':
420 /* Backslash quotes delimiters and itself,
421 but not macro args. */
422 if (expected_delimiter != 0 && p < limit
423 && (*p == expected_delimiter || *p == '\\'))
425 *exp_p++ = *p++;
426 continue;
428 break;
430 case '/':
431 if (expected_delimiter != '\0')
432 /* No comments inside strings. */
433 break;
434 if (*p == '*')
436 /* If we find a comment that wasn't removed by
437 handle_directive, this must be -traditional.
438 So replace the comment with nothing at all. */
439 exp_p--;
440 p += 1;
441 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
442 p++;
443 #if 0
444 /* Mark this as a concatenation-point,
445 as if it had been ##. */
446 concat = p;
447 #endif
449 break;
453 /* Handle the start of a symbol. */
454 if (is_idchar(c) && nargs > 0)
456 U_CHAR *id_beg = p - 1;
457 int id_len;
459 --exp_p;
460 while (p != limit && is_idchar(*p))
461 p++;
462 id_len = p - id_beg;
464 if (is_idstart(c)
465 && !(id_len == 1 && c == 'L' && (*p == '\'' || *p == '"')))
467 register struct arglist *arg;
469 for (arg = arglist; arg != NULL; arg = arg->next)
471 struct reflist *tpat;
473 if (arg->name[0] == c
474 && arg->length == id_len
475 && strncmp (arg->name, id_beg, id_len) == 0)
477 if (expected_delimiter && CPP_OPTIONS
478 (pfile)->warn_stringify)
480 if (CPP_TRADITIONAL (pfile))
482 cpp_warning (pfile,
483 "macro argument `%.*s' is stringified.",
484 id_len, arg->name);
486 else
488 cpp_warning (pfile,
489 "macro arg `%.*s' would be stringified with -traditional.",
490 id_len, arg->name);
493 /* If ANSI, don't actually substitute
494 inside a string. */
495 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
496 break;
497 /* make a pat node for this arg and append it
498 to the end of the pat list */
499 tpat = (struct reflist *)
500 xmalloc (sizeof (struct reflist));
501 tpat->next = NULL;
502 tpat->raw_before = concat == id_beg;
503 tpat->raw_after = 0;
504 tpat->rest_args = arg->rest_args;
505 tpat->stringify = (CPP_TRADITIONAL (pfile)
506 ? expected_delimiter != '\0'
507 : stringify == id_beg);
509 if (endpat == NULL)
510 defn->pattern = tpat;
511 else
512 endpat->next = tpat;
513 endpat = tpat;
515 tpat->argno = arg->argno;
516 tpat->nchars = exp_p - lastp;
518 register U_CHAR *p1 = p;
519 SKIP_WHITE_SPACE (p1);
520 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
521 tpat->raw_after = 1;
523 lastp = exp_p;
524 skipped_arg = 1;
525 break;
530 /* If this was not a macro arg, copy it into the expansion. */
531 if (!skipped_arg)
533 register U_CHAR *lim1 = p;
534 p = id_beg;
535 while (p != lim1)
536 *exp_p++ = *p++;
537 if (stringify == id_beg)
538 cpp_error (pfile,
539 "`#' operator should be followed by a macro argument name");
544 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
546 /* If ANSI, put in a "\r " marker to prevent token pasting.
547 But not if "inside a string" (which in ANSI mode
548 happens only for -D option). */
549 *exp_p++ = '\r';
550 *exp_p++ = ' ';
553 *exp_p = '\0';
555 defn->length = exp_p - defn->expansion;
557 /* Crash now if we overrun the allocated size. */
558 if (defn->length + 1 > maxsize)
559 abort ();
561 #if 0
562 /* This isn't worth the time it takes. */
563 /* give back excess storage */
564 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
565 #endif
567 return defn;
571 * special extension string that can be added to the last macro argument to
572 * allow it to absorb the "rest" of the arguments when expanded. Ex:
573 * #define wow(a, b...) process (b, a, b)
574 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
575 * { wow (one, two); } -> { process (two, one, two); }
576 * if this "rest_arg" is used with the concat token '##' and if it is not
577 * supplied then the token attached to with ## will not be outputted. Ex:
578 * #define wow (a, b...) process (b ## , a, ## b)
579 * { wow (1, 2); } -> { process (2, 1, 2); }
580 * { wow (one); } -> { process (one); {
582 static char rest_extension[] = "...";
583 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
585 /* Create a DEFINITION node from a #define directive. Arguments are
586 as for do_define. */
588 MACRODEF
589 create_definition (buf, limit, pfile, predefinition)
590 U_CHAR *buf, *limit;
591 cpp_reader *pfile;
592 int predefinition;
594 U_CHAR *bp; /* temp ptr into input buffer */
595 U_CHAR *symname; /* remember where symbol name starts */
596 int sym_length; /* and how long it is */
597 int rest_args = 0;
598 long line, col;
599 const char *file =
600 CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
601 DEFINITION *defn;
602 int arglengths = 0; /* Accumulate lengths of arg names
603 plus number of args. */
604 MACRODEF mdef;
605 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
607 bp = buf;
609 while (is_hspace(*bp))
610 bp++;
612 symname = bp; /* remember where it starts */
614 sym_length = check_macro_name (pfile, bp);
615 bp += sym_length;
617 /* Lossage will occur if identifiers or control keywords are broken
618 across lines using backslash. This is not the right place to take
619 care of that. */
621 if (*bp == '(')
623 struct arglist *arg_ptrs = NULL;
624 int argno = 0;
626 bp++; /* skip '(' */
627 SKIP_WHITE_SPACE (bp);
629 /* Loop over macro argument names. */
630 while (*bp != ')')
632 struct arglist *temp;
634 temp = (struct arglist *) alloca (sizeof (struct arglist));
635 temp->name = bp;
636 temp->next = arg_ptrs;
637 temp->argno = argno++;
638 temp->rest_args = 0;
639 arg_ptrs = temp;
641 if (rest_args)
642 cpp_pedwarn (pfile, "another parameter follows `%s'",
643 rest_extension);
645 if (!is_idstart(*bp))
646 cpp_pedwarn (pfile, "invalid character in macro parameter name");
648 /* Find the end of the arg name. */
649 while (is_idchar(*bp))
651 bp++;
652 /* do we have a "special" rest-args extension here? */
653 if ((size_t) (limit - bp) > REST_EXTENSION_LENGTH
654 && !strncmp (rest_extension, bp, REST_EXTENSION_LENGTH))
656 rest_args = 1;
657 temp->rest_args = 1;
658 break;
661 temp->length = bp - temp->name;
662 if (rest_args == 1)
663 bp += REST_EXTENSION_LENGTH;
664 arglengths += temp->length + 2;
665 SKIP_WHITE_SPACE (bp);
666 if (temp->length == 0 || (*bp != ',' && *bp != ')'))
668 cpp_error (pfile,
669 "badly punctuated parameter list in `#define'");
670 goto nope;
672 if (*bp == ',')
674 bp++;
675 SKIP_WHITE_SPACE (bp);
677 if (bp >= limit)
679 cpp_error (pfile, "unterminated parameter list in `#define'");
680 goto nope;
683 struct arglist *otemp;
685 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
686 if (temp->length == otemp->length
687 && strncmp (temp->name, otemp->name, temp->length) == 0)
689 U_CHAR *name;
691 name = (U_CHAR *) alloca (temp->length + 1);
692 (void) strncpy (name, temp->name, temp->length);
693 name[temp->length] = '\0';
694 cpp_error (pfile,
695 "duplicate argument name `%s' in `#define'",
696 name);
697 goto nope;
702 ++bp; /* skip paren */
703 SKIP_WHITE_SPACE (bp);
704 /* now everything from bp before limit is the definition. */
705 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
706 defn->rest_args = rest_args;
708 /* Now set defn->args.argnames to the result of concatenating
709 the argument names in reverse order
710 with comma-space between them. */
711 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
713 struct arglist *temp;
714 int i = 0;
715 for (temp = arg_ptrs; temp; temp = temp->next)
717 bcopy (temp->name, &defn->args.argnames[i], temp->length);
718 i += temp->length;
719 if (temp->next != 0)
721 defn->args.argnames[i++] = ',';
722 defn->args.argnames[i++] = ' ';
725 defn->args.argnames[i] = 0;
728 else
730 /* Simple expansion or empty definition. */
732 if (bp < limit)
734 if (is_hspace(*bp))
736 bp++;
737 SKIP_WHITE_SPACE (bp);
739 else
740 /* Per C9x, missing white space after the name in a #define
741 of an object-like macro is always a constraint violation. */
742 cpp_pedwarn (pfile,
743 "missing white space after `#define %.*s'",
744 sym_length, symname);
746 /* now everything from bp before limit is the definition. */
747 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
748 defn->args.argnames = (U_CHAR *) "";
751 defn->line = line;
752 defn->file = file;
754 /* OP is null if this is a predefinition */
755 defn->predefined = predefinition;
756 mdef.defn = defn;
757 mdef.symnam = symname;
758 mdef.symlen = sym_length;
760 return mdef;
762 nope:
763 mdef.defn = 0;
764 return mdef;
768 * Parse a macro argument and append the info on PFILE's token_buffer.
769 * REST_ARGS means to absorb the rest of the args.
770 * Return nonzero to indicate a syntax error.
773 static enum cpp_token
774 macarg (pfile, rest_args)
775 cpp_reader *pfile;
776 int rest_args;
778 int paren = 0;
779 enum cpp_token token;
780 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
781 CPP_OPTIONS (pfile)->put_out_comments = 0;
783 /* Try to parse as much of the argument as exists at this
784 input stack level. */
785 pfile->no_macro_expand++;
786 CPP_OPTIONS (pfile)->no_line_commands++;
787 for (;;)
789 token = cpp_get_token (pfile);
790 switch (token)
792 case CPP_EOF:
793 goto done;
794 case CPP_POP:
795 /* If we've hit end of file, it's an error (reported by caller).
796 Ditto if it's the end of cpp_expand_to_buffer text.
797 If we've hit end of macro, just continue. */
798 if (!CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
799 goto done;
800 break;
801 case CPP_LPAREN:
802 paren++;
803 break;
804 case CPP_RPAREN:
805 if (--paren < 0)
806 goto found;
807 break;
808 case CPP_COMMA:
809 /* if we've returned to lowest level and
810 we aren't absorbing all args */
811 if (paren == 0 && rest_args == 0)
812 goto found;
813 break;
814 found:
815 /* Remove ',' or ')' from argument buffer. */
816 CPP_ADJUST_WRITTEN (pfile, -1);
817 goto done;
818 default:;
822 done:
823 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
824 CPP_OPTIONS (pfile)->no_line_commands--;
825 pfile->no_macro_expand--;
827 return token;
831 static struct tm *
832 timestamp (pfile)
833 cpp_reader *pfile;
835 if (!pfile->timebuf)
837 time_t t = time ((time_t *) 0);
838 pfile->timebuf = localtime (&t);
840 return pfile->timebuf;
843 static const char * const monthnames[] =
845 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
846 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
850 * expand things like __FILE__. Place the expansion into the output
851 * buffer *without* rescanning.
854 static void
855 special_symbol (hp, pfile)
856 HASHNODE *hp;
857 cpp_reader *pfile;
859 const char *buf;
860 int len;
861 cpp_buffer *ip;
863 switch (hp->type)
865 case T_FILE:
866 case T_BASE_FILE:
868 ip = CPP_BUFFER (pfile);
869 if (hp->type == T_BASE_FILE)
871 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
872 ip = CPP_PREV_BUFFER (ip);
874 else
876 ip = CPP_BUFFER (pfile);
877 while (!ip->nominal_fname && ip != CPP_NULL_BUFFER (pfile))
878 ip = CPP_PREV_BUFFER (ip);
881 buf = ip->nominal_fname;
883 if (!buf)
884 buf = "";
885 CPP_RESERVE (pfile, 3 + 4 * strlen (buf));
886 quote_string (pfile, buf);
887 return;
890 case T_INCLUDE_LEVEL:
892 int true_indepth = 0;
893 ip = CPP_BUFFER (pfile);
894 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
895 if (ip->fname != NULL)
896 true_indepth++;
898 CPP_RESERVE (pfile, 10);
899 sprintf (CPP_PWRITTEN (pfile), "%d", true_indepth);
900 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
901 return;
904 case T_VERSION:
905 len = strlen (version_string);
906 CPP_RESERVE (pfile, 3 + len);
907 CPP_PUTC_Q (pfile, '"');
908 CPP_PUTS_Q (pfile, version_string, len);
909 CPP_PUTC_Q (pfile, '"');
910 CPP_NUL_TERMINATE_Q (pfile);
911 return;
913 case T_CONST:
914 buf = hp->value.cpval;
915 if (!buf)
916 return;
917 if (*buf == '\0')
918 buf = "\r ";
920 len = strlen (buf);
921 CPP_RESERVE (pfile, len + 1);
922 CPP_PUTS_Q (pfile, buf, len);
923 CPP_NUL_TERMINATE_Q (pfile);
924 return;
926 case T_STDC:
927 CPP_RESERVE (pfile, 2);
928 #ifdef STDC_0_IN_SYSTEM_HEADERS
929 ip = CPP_BUFFER (pfile);
930 while (!ip->nominal_fname && ip != CPP_NULL_BUFFER (pfile))
931 ip = CPP_PREV_BUFFER (ip);
932 if (ip->system_header_p
933 && !cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", 15, -1))
934 CPP_PUTC_Q (pfile, '0');
935 else
936 #endif
937 CPP_PUTC_Q (pfile, '1');
938 CPP_NUL_TERMINATE_Q (pfile);
939 return;
941 case T_SPECLINE:
943 long line;
944 cpp_buf_line_and_col (cpp_file_buffer (pfile), &line, NULL);
946 CPP_RESERVE (pfile, 10);
947 sprintf (CPP_PWRITTEN (pfile), "%ld", line);
948 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
949 return;
952 case T_DATE:
953 case T_TIME:
955 struct tm *timebuf;
957 CPP_RESERVE (pfile, 20);
958 timebuf = timestamp (pfile);
959 if (hp->type == T_DATE)
960 sprintf (CPP_PWRITTEN (pfile), "\"%s %2d %4d\"",
961 monthnames[timebuf->tm_mon],
962 timebuf->tm_mday, timebuf->tm_year + 1900);
963 else
964 sprintf (CPP_PWRITTEN (pfile), "\"%02d:%02d:%02d\"",
965 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
967 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
968 return;
971 case T_POISON:
972 cpp_error (pfile, "attempt to use poisoned `%s'.", hp->name);
973 CPP_RESERVE (pfile, 1);
974 CPP_PUTC_Q (pfile, '0');
975 CPP_NUL_TERMINATE_Q (pfile);
976 break;
978 default:
979 cpp_fatal (pfile, "cpplib internal error: invalid special hash type");
980 return;
984 /* Expand a macro call.
985 HP points to the symbol that is the macro being called.
986 Put the result of expansion onto the input stack
987 so that subsequent input by our caller will use it.
989 If macro wants arguments, caller has already verified that
990 an argument list follows; arguments come from the input stack. */
992 void
993 macroexpand (pfile, hp)
994 cpp_reader *pfile;
995 HASHNODE *hp;
997 int nargs;
998 DEFINITION *defn;
999 register U_CHAR *xbuf;
1000 long start_line, start_column;
1001 int xbuf_len;
1002 struct argdata *args = 0;
1003 long old_written = CPP_WRITTEN (pfile);
1004 #if 0
1005 int start_line = instack[indepth].lineno;
1006 #endif
1007 int rest_args, rest_zero = 0;
1008 register int i;
1010 #if 0
1011 /* This macro is being used inside a #if, which means it must be */
1012 /* recorded as a precondition. */
1013 if (pcp_inside_if && pcp_outfile && defn->predefined)
1014 dump_single_macro (hp, pcp_outfile);
1015 #endif
1017 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
1019 /* Check for and handle special symbols. */
1020 if (hp->type != T_MACRO)
1022 special_symbol (hp, pfile);
1023 xbuf_len = CPP_WRITTEN (pfile) - old_written;
1024 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
1025 CPP_SET_WRITTEN (pfile, old_written);
1026 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
1027 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
1028 CPP_BUFFER (pfile)->has_escapes = 1;
1029 return;
1032 defn = hp->value.defn;
1033 nargs = defn->nargs;
1034 pfile->output_escapes++;
1036 if (nargs >= 0)
1038 enum cpp_token token = CPP_EOF;
1040 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
1042 for (i = 0; i < nargs; i++)
1044 args[i].raw = args[i].expanded = 0;
1045 args[i].raw_length = 0;
1046 args[i].expand_length = args[i].stringified_length = -1;
1049 /* Parse all the macro args that are supplied. I counts them.
1050 The first NARGS args are stored in ARGS.
1051 The rest are discarded. If rest_args is set then we assume
1052 macarg absorbed the rest of the args. */
1053 i = 0;
1054 rest_args = 0;
1055 rest_args = 0;
1056 FORWARD (1); /* Discard open-parenthesis before first arg. */
1059 if (rest_args)
1060 continue;
1061 if (i < nargs || (nargs == 0 && i == 0))
1063 /* if we are working on last arg which absorbs rest of args... */
1064 if (i == nargs - 1 && defn->rest_args)
1065 rest_args = 1;
1066 args[i].raw = CPP_WRITTEN (pfile);
1067 token = macarg (pfile, rest_args);
1068 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
1070 else
1071 token = macarg (pfile, 0);
1072 if (token == CPP_EOF || token == CPP_POP)
1074 cpp_error_with_line (pfile, start_line, start_column,
1075 "unterminated macro call");
1076 return;
1078 i++;
1080 while (token == CPP_COMMA);
1082 /* If we got one arg but it was just whitespace, call that 0 args. */
1083 if (i == 1)
1085 register U_CHAR *bp = ARG_BASE + args[0].raw;
1086 register U_CHAR *lim = bp + args[0].raw_length;
1087 /* cpp.texi says for foo ( ) we provide one argument.
1088 However, if foo wants just 0 arguments, treat this as 0. */
1089 if (nargs == 0)
1090 while (bp != lim && is_space(*bp))
1091 bp++;
1092 if (bp == lim)
1093 i = 0;
1096 /* Don't output an error message if we have already output one for
1097 a parse error above. */
1098 rest_zero = 0;
1099 if (nargs == 0 && i > 0)
1101 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
1103 else if (i < nargs)
1105 /* traditional C allows foo() if foo wants one argument. */
1106 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
1108 /* the rest args token is allowed to absorb 0 tokens */
1109 else if (i == nargs - 1 && defn->rest_args)
1110 rest_zero = 1;
1111 else if (i == 0)
1112 cpp_error (pfile, "macro `%s' used without args", hp->name);
1113 else if (i == 1)
1114 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
1115 else
1116 cpp_error (pfile, "macro `%s' used with only %d args",
1117 hp->name, i);
1119 else if (i > nargs)
1121 cpp_error (pfile,
1122 "macro `%s' used with too many (%d) args", hp->name, i);
1126 /* If macro wants zero args, we parsed the arglist for checking only.
1127 Read directly from the macro definition. */
1128 if (nargs <= 0)
1130 xbuf = defn->expansion;
1131 xbuf_len = defn->length;
1133 else
1135 register U_CHAR *exp = defn->expansion;
1136 register int offset; /* offset in expansion,
1137 copied a piece at a time */
1138 register int totlen; /* total amount of exp buffer filled so far */
1140 register struct reflist *ap, *last_ap;
1142 /* Macro really takes args. Compute the expansion of this call. */
1144 /* Compute length in characters of the macro's expansion.
1145 Also count number of times each arg is used. */
1146 xbuf_len = defn->length;
1147 for (ap = defn->pattern; ap != NULL; ap = ap->next)
1149 if (ap->stringify)
1151 register struct argdata *arg = &args[ap->argno];
1152 /* Stringify if it hasn't already been */
1153 if (arg->stringified_length < 0)
1155 int arglen = arg->raw_length;
1156 int escaped = 0;
1157 int in_string = 0;
1158 int c;
1159 /* Initially need_space is -1. Otherwise, 1 means the
1160 previous character was a space, but we suppressed it;
1161 0 means the previous character was a non-space. */
1162 int need_space = -1;
1163 i = 0;
1164 arg->stringified = CPP_WRITTEN (pfile);
1165 if (!CPP_TRADITIONAL (pfile))
1166 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
1167 for (; i < arglen; i++)
1169 c = (ARG_BASE + arg->raw)[i];
1171 if (!in_string)
1173 /* Internal sequences of whitespace are
1174 replaced by one space except within
1175 a string or char token. */
1176 if (is_space(c))
1178 if (CPP_WRITTEN (pfile) > (unsigned) arg->stringified
1179 && (CPP_PWRITTEN (pfile))[-1] == '\r')
1181 /* "\r " escape markers are removed */
1182 CPP_ADJUST_WRITTEN (pfile, -1);
1183 continue;
1185 if (need_space == 0)
1186 need_space = 1;
1187 continue;
1189 else if (need_space > 0)
1190 CPP_PUTC (pfile, ' ');
1191 need_space = 0;
1194 if (escaped)
1195 escaped = 0;
1196 else
1198 if (c == '\\')
1199 escaped = 1;
1200 if (in_string)
1202 if (c == in_string)
1203 in_string = 0;
1205 else if (c == '\"' || c == '\'')
1206 in_string = c;
1209 /* Escape these chars */
1210 if (c == '\"' || (in_string && c == '\\'))
1211 CPP_PUTC (pfile, '\\');
1212 if (ISPRINT (c))
1213 CPP_PUTC (pfile, c);
1214 else
1216 CPP_RESERVE (pfile, 4);
1217 sprintf ((char *) CPP_PWRITTEN (pfile), "\\%03o",
1218 (unsigned int) c);
1219 CPP_ADJUST_WRITTEN (pfile, 4);
1222 if (!CPP_TRADITIONAL (pfile))
1223 CPP_PUTC (pfile, '\"'); /* insert ending quote */
1224 arg->stringified_length
1225 = CPP_WRITTEN (pfile) - arg->stringified;
1227 xbuf_len += args[ap->argno].stringified_length;
1229 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
1230 /* Add 4 for two \r-space markers to prevent
1231 token concatenation. */
1232 xbuf_len += args[ap->argno].raw_length + 4;
1233 else
1235 /* We have an ordinary (expanded) occurrence of the arg.
1236 So compute its expansion, if we have not already. */
1237 if (args[ap->argno].expand_length < 0)
1239 args[ap->argno].expanded = CPP_WRITTEN (pfile);
1240 cpp_expand_to_buffer (pfile,
1241 ARG_BASE + args[ap->argno].raw,
1242 args[ap->argno].raw_length);
1244 args[ap->argno].expand_length
1245 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
1248 /* Add 4 for two \r-space markers to prevent
1249 token concatenation. */
1250 xbuf_len += args[ap->argno].expand_length + 4;
1254 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
1256 /* Generate in XBUF the complete expansion
1257 with arguments substituted in.
1258 TOTLEN is the total size generated so far.
1259 OFFSET is the index in the definition
1260 of where we are copying from. */
1261 offset = totlen = 0;
1262 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
1263 last_ap = ap, ap = ap->next)
1265 register struct argdata *arg = &args[ap->argno];
1266 int count_before = totlen;
1268 /* Add chars to XBUF. */
1269 for (i = 0; i < ap->nchars; i++, offset++)
1270 xbuf[totlen++] = exp[offset];
1272 /* If followed by an empty rest arg with concatenation,
1273 delete the last run of nonwhite chars. */
1274 if (rest_zero && totlen > count_before
1275 && ((ap->rest_args && ap->raw_before)
1276 || (last_ap != NULL && last_ap->rest_args
1277 && last_ap->raw_after)))
1279 /* Delete final whitespace. */
1280 while (totlen > count_before && is_space(xbuf[totlen - 1]))
1281 totlen--;
1283 /* Delete the nonwhites before them. */
1284 while (totlen > count_before && !is_space(xbuf[totlen - 1]))
1285 totlen--;
1288 if (ap->stringify != 0)
1290 bcopy (ARG_BASE + arg->stringified,
1291 xbuf + totlen, arg->stringified_length);
1292 totlen += arg->stringified_length;
1294 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
1296 U_CHAR *p1 = ARG_BASE + arg->raw;
1297 U_CHAR *l1 = p1 + arg->raw_length;
1298 if (ap->raw_before)
1300 /* Arg is concatenated before: delete leading whitespace,
1301 whitespace markers, and no-reexpansion markers. */
1302 while (p1 != l1)
1304 if (is_space(p1[0]))
1305 p1++;
1306 else if (p1[0] == '\r')
1307 p1 += 2;
1308 else
1309 break;
1312 if (ap->raw_after)
1314 /* Arg is concatenated after: delete trailing whitespace,
1315 whitespace markers, and no-reexpansion markers. */
1316 while (p1 != l1)
1318 if (is_space(l1[-1]))
1319 l1--;
1320 else if (l1[-1] == '\r')
1321 l1--;
1322 else if (l1[-1] == '-')
1324 if (l1 != p1 + 1 && l1[-2] == '\r')
1325 l1 -= 2;
1326 else
1327 break;
1329 else
1330 break;
1334 /* Delete any no-reexpansion marker that precedes
1335 an identifier at the beginning of the argument. */
1336 if (p1[0] == '\r' && p1[1] == '-')
1337 p1 += 2;
1339 bcopy (p1, xbuf + totlen, l1 - p1);
1340 totlen += l1 - p1;
1342 else
1344 U_CHAR *expanded = ARG_BASE + arg->expanded;
1345 if (!ap->raw_before && totlen > 0 && arg->expand_length
1346 && !CPP_TRADITIONAL (pfile)
1347 && unsafe_chars (xbuf[totlen - 1], expanded[0]))
1349 xbuf[totlen++] = '\r';
1350 xbuf[totlen++] = ' ';
1353 bcopy (expanded, xbuf + totlen, arg->expand_length);
1354 totlen += arg->expand_length;
1356 if (!ap->raw_after && totlen > 0 && offset < defn->length
1357 && !CPP_TRADITIONAL (pfile)
1358 && unsafe_chars (xbuf[totlen - 1], exp[offset]))
1360 xbuf[totlen++] = '\r';
1361 xbuf[totlen++] = ' ';
1365 if (totlen > xbuf_len)
1367 cpp_fatal (pfile, "internal_error: buffer overrun in macroexpand");
1368 return;
1372 /* if there is anything left of the definition
1373 after handling the arg list, copy that in too. */
1375 for (i = offset; i < defn->length; i++)
1377 /* if we've reached the end of the macro */
1378 if (exp[i] == ')')
1379 rest_zero = 0;
1380 if (!(rest_zero && last_ap != NULL && last_ap->rest_args
1381 && last_ap->raw_after))
1382 xbuf[totlen++] = exp[i];
1385 xbuf[totlen] = 0;
1386 xbuf_len = totlen;
1390 pfile->output_escapes--;
1392 /* Now put the expansion on the input stack
1393 so our caller will commence reading from it. */
1394 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
1395 CPP_BUFFER (pfile)->has_escapes = 1;
1397 /* Pop the space we've used in the token_buffer for argument expansion. */
1398 CPP_SET_WRITTEN (pfile, old_written);
1400 /* Recursive macro use sometimes works traditionally.
1401 #define foo(x,y) bar (x (y,0), y)
1402 foo (foo, baz) */
1404 if (!CPP_TRADITIONAL (pfile))
1405 hp->type = T_DISABLED;
1408 /* Return 1 iff a token ending in C1 followed directly by a token C2
1409 could cause mis-tokenization. */
1411 static int
1412 unsafe_chars (c1, c2)
1413 int c1, c2;
1415 switch (c1)
1417 case '+': case '-':
1418 if (c2 == c1 || c2 == '=')
1419 return 1;
1420 goto letter;
1422 case 'e': case 'E': case 'p': case 'P':
1423 if (c2 == '-' || c2 == '+')
1424 return 1; /* could extend a pre-processing number */
1425 goto letter;
1427 case 'L':
1428 if (c2 == '\'' || c2 == '\"')
1429 return 1; /* Could turn into L"xxx" or L'xxx'. */
1430 goto letter;
1432 case '.': case '0': case '1': case '2': case '3':
1433 case '4': case '5': case '6': case '7': case '8': case '9':
1434 case '_': case 'a': case 'b': case 'c': case 'd': case 'f':
1435 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1436 case 'm': case 'n': case 'o': case 'q': case 'r': case 's':
1437 case 't': case 'u': case 'v': case 'w': case 'x': case 'y':
1438 case 'z': case 'A': case 'B': case 'C': case 'D': case 'F':
1439 case 'G': case 'H': case 'I': case 'J': case 'K': case 'M':
1440 case 'N': case 'O': case 'Q': case 'R': case 'S': case 'T':
1441 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
1442 letter:
1443 /* We're in the middle of either a name or a pre-processing number. */
1444 return (is_idchar(c2) || c2 == '.');
1446 case '<': case '>': case '!': case '%': case '#': case ':':
1447 case '^': case '&': case '|': case '*': case '/': case '=':
1448 return (c2 == c1 || c2 == '=');
1450 return 0;
1453 static void
1454 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
1455 cpp_reader *pfile;
1456 register U_CHAR *xbuf;
1457 int xbuf_len;
1458 HASHNODE *hp;
1460 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
1461 if (mbuf == NULL)
1462 return;
1463 mbuf->cleanup = macro_cleanup;
1464 mbuf->data = hp;
1466 /* The first chars of the expansion should be a "\r " added by
1467 collect_expansion. This is to prevent accidental token-pasting
1468 between the text preceding the macro invocation, and the macro
1469 expansion text.
1471 We would like to avoid adding unneeded spaces (for the sake of
1472 tools that use cpp, such as imake). In some common cases we can
1473 tell that it is safe to omit the space.
1475 The character before the macro invocation cannot have been an
1476 idchar (or else it would have been pasted with the idchars of
1477 the macro name). Therefore, if the first non-space character
1478 of the expansion is an idchar, we do not need the extra space
1479 to prevent token pasting.
1481 Also, we don't need the extra space if the first char is '(',
1482 or some other (less common) characters. */
1484 if (xbuf[0] == '\r' && xbuf[1] == ' '
1485 && (is_idchar(xbuf[2]) || xbuf[2] == '(' || xbuf[2] == '\''
1486 || xbuf[2] == '\"'))
1487 mbuf->cur += 2;
1489 /* Likewise, avoid the extra space at the end of the macro expansion
1490 if this is safe. We can do a better job here since we can know
1491 what the next char will be. */
1492 if (xbuf_len >= 3
1493 && mbuf->rlimit[-2] == '\r'
1494 && mbuf->rlimit[-1] == ' ')
1496 int c1 = mbuf->rlimit[-3];
1497 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
1498 if (c2 == EOF || !unsafe_chars (c1, c2))
1499 mbuf->rlimit -= 2;
1503 /* Return zero if two DEFINITIONs are isomorphic. */
1506 compare_defs (pfile, d1, d2)
1507 cpp_reader *pfile;
1508 DEFINITION *d1, *d2;
1510 register struct reflist *a1, *a2;
1511 register U_CHAR *p1 = d1->expansion;
1512 register U_CHAR *p2 = d2->expansion;
1513 int first = 1;
1515 if (d1->nargs != d2->nargs)
1516 return 1;
1517 if (CPP_PEDANTIC (pfile)
1518 && strcmp ((char *) d1->args.argnames, (char *) d2->args.argnames))
1519 return 1;
1520 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1521 a1 = a1->next, a2 = a2->next)
1523 if (!((a1->nchars == a2->nchars && !strncmp (p1, p2, a1->nchars))
1524 || !comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1525 || a1->argno != a2->argno
1526 || a1->stringify != a2->stringify
1527 || a1->raw_before != a2->raw_before
1528 || a1->raw_after != a2->raw_after)
1529 return 1;
1530 first = 0;
1531 p1 += a1->nchars;
1532 p2 += a2->nchars;
1534 if (a1 != a2)
1535 return 1;
1537 return comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1538 p2, d2->length - (p2 - d2->expansion), 1);
1541 /* Return 1 if two parts of two macro definitions are effectively different.
1542 One of the parts starts at BEG1 and has LEN1 chars;
1543 the other has LEN2 chars at BEG2.
1544 Any sequence of whitespace matches any other sequence of whitespace.
1545 FIRST means these parts are the first of a macro definition;
1546 so ignore leading whitespace entirely.
1547 LAST means these parts are the last of a macro definition;
1548 so ignore trailing whitespace entirely. */
1550 static int
1551 comp_def_part (first, beg1, len1, beg2, len2, last)
1552 int first;
1553 U_CHAR *beg1, *beg2;
1554 int len1, len2;
1555 int last;
1557 register U_CHAR *end1 = beg1 + len1;
1558 register U_CHAR *end2 = beg2 + len2;
1559 if (first)
1561 while (beg1 != end1 && is_space(*beg1))
1562 beg1++;
1563 while (beg2 != end2 && is_space(*beg2))
1564 beg2++;
1566 if (last)
1568 while (beg1 != end1 && is_space(end1[-1]))
1569 end1--;
1570 while (beg2 != end2 && is_space(end2[-1]))
1571 end2--;
1573 while (beg1 != end1 && beg2 != end2)
1575 if (is_space(*beg1) && is_space(*beg2))
1577 while (beg1 != end1 && is_space(*beg1))
1578 beg1++;
1579 while (beg2 != end2 && is_space(*beg2))
1580 beg2++;
1582 else if (*beg1 == *beg2)
1584 beg1++;
1585 beg2++;
1587 else
1588 break;
1590 return (beg1 != end1) || (beg2 != end2);
1593 /* Dump the definition of macro MACRO on stdout. The format is suitable
1594 to be read back in again. */
1596 void
1597 dump_definition (pfile, macro)
1598 cpp_reader *pfile;
1599 MACRODEF macro;
1601 DEFINITION *defn = macro.defn;
1603 CPP_RESERVE (pfile, macro.symlen + sizeof "#define ");
1604 CPP_PUTS_Q (pfile, "#define ", sizeof "#define " -1);
1605 CPP_PUTS_Q (pfile, macro.symnam, macro.symlen);
1607 if (defn->nargs == -1)
1609 CPP_PUTC_Q (pfile, ' ');
1611 /* The first and last two characters of a macro expansion are
1612 always "\r "; this needs to be trimmed out.
1613 So we need length-4 chars of space, plus one for the NUL. */
1614 CPP_RESERVE (pfile, defn->length - 4 + 1);
1615 CPP_PUTS_Q (pfile, defn->expansion + 2, defn->length - 4);
1616 CPP_NUL_TERMINATE_Q (pfile);
1618 else
1620 struct reflist *r;
1621 unsigned char *argnames = (unsigned char *) xstrdup (defn->args.argnames);
1622 unsigned char **argv = (unsigned char **) alloca (defn->nargs *
1623 sizeof(char *));
1624 int *argl = (int *) alloca (defn->nargs * sizeof(int));
1625 unsigned char *x;
1626 int i;
1628 /* First extract the argument list. */
1629 x = argnames;
1630 i = defn->nargs;
1631 while (i--)
1633 argv[i] = x;
1634 while (*x != ',' && *x != '\0') x++;
1635 argl[i] = x - argv[i];
1636 if (*x == ',')
1638 *x = '\0';
1639 x += 2; /* skip the space after the comma */
1643 /* Now print out the argument list. */
1644 CPP_PUTC_Q (pfile, '(');
1645 for (i = 0; i < defn->nargs; i++)
1647 CPP_RESERVE (pfile, argl[i] + 2);
1648 CPP_PUTS_Q (pfile, argv[i], argl[i]);
1649 if (i < defn->nargs-1)
1650 CPP_PUTS_Q (pfile, ", ", 2);
1653 if (defn->rest_args)
1654 CPP_PUTS (pfile, "...) ", 5);
1655 else
1656 CPP_PUTS (pfile, ") ", 2);
1658 /* Now the definition. */
1659 x = defn->expansion;
1660 for (r = defn->pattern; r; r = r->next)
1662 i = r->nchars;
1663 if (*x == '\r') x += 2, i -= 2;
1664 /* i chars for macro text, plus the length of the macro
1665 argument name, plus one for a stringify marker, plus two for
1666 each concatenation marker. */
1667 CPP_RESERVE (pfile,
1668 i + argl[r->argno] + r->stringify
1669 + (r->raw_before + r->raw_after) * 2);
1671 if (i > 0) CPP_PUTS_Q (pfile, x, i);
1672 if (r->raw_before)
1673 CPP_PUTS_Q (pfile, "##", 2);
1674 if (r->stringify)
1675 CPP_PUTC_Q (pfile, '#');
1676 CPP_PUTS_Q (pfile, argv[r->argno], argl[r->argno]);
1677 if (r->raw_after && !(r->next && r->next->nchars == 0
1678 && r->next->raw_before))
1679 CPP_PUTS_Q (pfile, "##", 2);
1681 x += i;
1684 i = defn->length - (x - defn->expansion) - 2;
1685 if (*x == '\r') x += 2, i -= 2;
1686 if (i > 0) CPP_PUTS (pfile, x, i);
1687 CPP_NUL_TERMINATE (pfile);