OSDN Git Service

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