OSDN Git Service

* cpphash.c (collect_funlike_expansion): Make "# is not
[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 /* Structure allocated for every #define.  For a simple replacement
35    such as
36         #define foo bar
37
38    we allocate an object_defn structure; the expansion field points
39    to the replacement text.  For a function-like macro we allocate a
40    funct_defn structure; nargs is the number of arguments - it can be zero,
41    e.g.
42        #define getchar() getc (stdin)
43
44    When there are args, the expansion is the replacement text with the
45    args squashed out, and the reflist is a list describing how to
46    build the output from the input: e.g., "3 chars, then the 1st arg,
47    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
48    The chars here come from the expansion.  Whatever is left of the
49    expansion after the last arg-occurrence is copied after that arg.
50    Note that the reflist can be arbitrarily long---
51    its length depends on the number of times the arguments appear in
52    the replacement text, not how many args there are.  Example:
53    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
54    pattern list
55      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
56    where (x, y) means (nchars, argno).
57
58    Note that EMPTY and IDENTITY macros have object_defn structures too,
59    but they're just used to hold the file, line, and column.  The
60    expansion field will be NULL.  */
61
62 struct object_defn
63 {
64   const U_CHAR *expansion;
65   unsigned int length;
66
67   const char *file;             /* File, line, column of definition */
68   int line;
69   int col;
70 };  
71
72 struct reflist
73 {
74   const struct reflist *next;
75   char stringify;               /* nonzero if this arg was preceded by a
76                                    # operator. */
77   char raw_before;              /* Nonzero if a ## operator before arg. */
78   char raw_after;               /* Nonzero if a ## operator after arg. */
79   char rest_args;               /* Nonzero if this arg. absorbs the rest */
80   int nchars;                   /* Number of literal chars to copy before
81                                    this arg occurrence.  */
82   int argno;                    /* Number of arg to substitute (origin-0) */
83 };
84
85 struct funct_defn
86 {
87   int nargs;
88   int length;                   /* length of expansion string */
89   const U_CHAR *expansion;
90   char rest_args;               /* Nonzero if last arg. absorbs the rest */
91   const struct reflist *pattern;
92
93   /* Names of macro args, concatenated in order with \0 between
94      them.  The only use of this is that we warn on redefinition if
95      this differs between the old and new definitions.  */
96   U_CHAR *argnames;
97
98   const char *file;             /* File, line, column of definition */
99   int line;
100   int col;
101 };
102
103 /* This is the second argument to eq_HASHNODE.  */
104 struct hashdummy
105 {
106   const U_CHAR *name;
107   unsigned short length;
108 };
109
110 static unsigned int hash_HASHNODE PARAMS ((const void *));
111 static int eq_HASHNODE            PARAMS ((const void *, const void *));
112 static void del_HASHNODE          PARAMS ((void *));
113 static HASHNODE *make_HASHNODE    PARAMS ((const U_CHAR *, size_t,
114                                            enum node_type, unsigned int));
115
116 static void dump_funlike_macro    PARAMS ((cpp_reader *,
117                                            const struct funct_defn *));
118 static int dump_hash_helper       PARAMS ((void **, void *));
119
120 static void push_macro_expansion PARAMS ((cpp_reader *, const U_CHAR *,
121                                           int, HASHNODE *));
122 static int unsafe_chars          PARAMS ((cpp_reader *, int, int));
123 static enum cpp_ttype macarg     PARAMS ((cpp_reader *, int));
124 static void special_symbol       PARAMS ((cpp_reader *, HASHNODE *));
125 static int compare_defs          PARAMS ((cpp_reader *,
126                                           const struct funct_defn *,
127                                           const struct funct_defn *));
128
129 /* Initial hash table size.  (It can grow if necessary - see hashtab.c.)  */
130 #define HASHSIZE 500
131
132 /* The arglist structure is built by collect_params to tell
133    collect_funlike_expansion where the argument names begin.  That is,
134    for a define like "#define f(x,y,z) foo+x-bar*y", the arglist would
135    contain pointers to the strings x, y, and z.
136    collect_funlike_expansion would then build a funct_defn node, with
137    reflist nodes pointing to the places x, y, and z had appeared.
138
139    The arglist is just convenience data passed between these two
140    routines.  It is not kept around after the current #define has been
141    processed and entered into the hash table.  */
142
143 struct arg
144 {
145   const U_CHAR *name;
146   unsigned int len;
147   char rest_arg;
148 };
149
150 struct arglist
151 {
152   U_CHAR *namebuf;
153   const struct arg *argv;
154   int argc;
155 };
156
157 static struct object_defn *
158 collect_objlike_expansion PARAMS ((cpp_reader *, cpp_toklist *));
159 static struct funct_defn *
160 collect_funlike_expansion PARAMS ((cpp_reader *, cpp_toklist *,
161                                    struct arglist *, unsigned int));
162 static unsigned int collect_params PARAMS ((cpp_reader *, cpp_toklist *,
163                                             struct arglist *));
164
165 static void warn_trad_stringify PARAMS ((cpp_reader *, U_CHAR *, size_t,
166                                          unsigned int, const struct arg *));
167 static unsigned int trad_stringify PARAMS ((cpp_reader *, U_CHAR *, size_t,
168                                             unsigned int, const struct arg *,
169                                             struct reflist **,
170                                             struct reflist **, unsigned int));
171 static int duplicate_arg_p PARAMS ((U_CHAR *, U_CHAR *));
172 static void add_pat PARAMS ((struct reflist **, struct reflist **,
173                              unsigned int, unsigned int, int, int, int, int));
174
175 /* This structure represents one parsed argument in a macro call.
176    `raw' points to the argument text as written (`raw_length' is its length).
177    `expanded' points to the argument's macro-expansion
178    (its length is `expand_length').
179    `stringified_length' is the length the argument would have
180    if stringified.  */
181
182 /* raw and expanded are relative to ARG_BASE */
183 #define ARG_BASE ((pfile)->token_buffer)
184
185 struct argdata
186 {
187   /* Strings relative to pfile->token_buffer */
188   long raw, expanded, stringified;
189   int raw_length, expand_length;
190   int stringified_length;
191 };
192
193 static void scan_arguments      PARAMS ((cpp_reader *,
194                                          const struct funct_defn *,
195                                          struct argdata *, const U_CHAR *));
196 static void stringify           PARAMS ((cpp_reader *, struct argdata *));
197 static void funlike_macroexpand PARAMS ((cpp_reader *, HASHNODE *,
198                                          struct argdata *));
199
200 /* Calculate hash of a string of length LEN.  */
201 unsigned int
202 _cpp_calc_hash (str, len)
203      const U_CHAR *str;
204      size_t len;
205 {
206   size_t n = len;
207   unsigned int r = 0;
208
209   do
210     r = r * 67 + (*str++ - 113);
211   while (--n);
212   return r + len;
213 }
214
215 /* Calculate hash of a HASHNODE structure.  */
216 static unsigned int
217 hash_HASHNODE (x)
218      const void *x;
219 {
220   const HASHNODE *h = (const HASHNODE *)x;
221   return h->hash;
222 }
223
224 /* Compare a HASHNODE structure (already in the table) with a
225    hashdummy structure (not yet in the table).  This relies on the
226    rule that the existing entry is the first argument, the potential
227    entry the second.  It also relies on the comparison function never
228    being called except as a direct consequence of a call to
229    htab_find(_slot)_with_hash.  */
230 static int
231 eq_HASHNODE (x, y)
232      const void *x;
233      const void *y;
234 {
235   const HASHNODE *a = (const HASHNODE *)x;
236   const struct hashdummy *b = (const struct hashdummy *)y;
237
238   return (a->length == b->length
239           && !ustrncmp (a->name, b->name, a->length));
240 }
241
242 /* Destroy a HASHNODE.  */
243 static void
244 del_HASHNODE (x)
245      void *x;
246 {
247   HASHNODE *h = (HASHNODE *)x;
248
249   _cpp_free_definition (h);
250   free (h);
251 }
252
253 /* Allocate and initialize a HASHNODE structure.
254    Caller must fill in the value field.  */
255
256 static HASHNODE *
257 make_HASHNODE (name, len, type, hash)
258      const U_CHAR *name;
259      size_t len;
260      enum node_type type;
261      unsigned int hash;
262 {
263   HASHNODE *hp = (HASHNODE *) xmalloc (sizeof (HASHNODE) + len);
264   U_CHAR *p = (U_CHAR *)hp + offsetof (HASHNODE, name);
265
266   hp->type = type;
267   hp->length = len;
268   hp->hash = hash;
269   hp->disabled = 0;
270
271   memcpy (p, name, len);
272   p[len] = 0;
273
274   return hp;
275 }
276
277 /* Find the hash node for name "name", of length LEN.  */
278
279 HASHNODE *
280 _cpp_lookup (pfile, name, len)
281      cpp_reader *pfile;
282      const U_CHAR *name;
283      int len;
284 {
285   struct hashdummy dummy;
286   HASHNODE *new, **slot;
287   unsigned int hash;
288
289   dummy.name = name;
290   dummy.length = len;
291   hash = _cpp_calc_hash (name, len);
292
293   slot = (HASHNODE **)
294     htab_find_slot_with_hash (pfile->hashtab, (void *)&dummy, hash, INSERT);
295   if (*slot)
296     return *slot;
297
298   new = make_HASHNODE (name, len, T_VOID, hash);
299   new->value.cpval = NULL;
300   *slot = new;
301   return new;
302 }
303
304 /* Init the hash table.  In here so it can see the hash and eq functions.  */
305 void
306 _cpp_init_macro_hash (pfile)
307      cpp_reader *pfile;
308 {
309   pfile->hashtab = htab_create (HASHSIZE, hash_HASHNODE,
310                                 eq_HASHNODE, del_HASHNODE);
311 }
312
313 /* Free the definition of macro H.  */
314
315 void
316 _cpp_free_definition (h)
317      HASHNODE *h;
318 {
319   if (h->type == T_XCONST)
320     free ((PTR) h->value.cpval);
321   else if (h->type == T_MACRO)
322     {
323       if (h->value.odefn->expansion)
324         free ((PTR) h->value.odefn->expansion);
325       free ((PTR) h->value.odefn);
326     }
327   else if (h->type == T_FMACRO)
328     {
329       const struct funct_defn *d = h->value.fdefn;
330       const struct reflist *ap, *nextap;
331     
332       for (ap = d->pattern; ap != NULL; ap = nextap)
333         {
334           nextap = ap->next;
335           free ((PTR) ap);
336         }
337       if (d->argnames)
338         free ((PTR) d->argnames);
339       free ((PTR) d);
340     }
341   h->value.cpval = NULL;
342 }
343
344 /* Create pat nodes.  */
345
346 static void
347 add_pat (pat, endpat, nchars, argno, raw_before, raw_after, strize, rest)
348      struct reflist **pat, **endpat;
349      unsigned int nchars;
350      unsigned int argno;
351      int raw_before, raw_after, strize, rest;
352 {
353   struct reflist *tpat;
354   tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
355   tpat->next = NULL;
356   tpat->raw_before = raw_before;
357   tpat->raw_after = raw_after;
358   tpat->stringify = strize;
359   tpat->rest_args = rest;
360   tpat->argno = argno;
361   tpat->nchars = nchars;
362
363   if (*endpat == NULL)
364     *pat = tpat;
365   else
366     (*endpat)->next = tpat;
367   *endpat = tpat;
368 }  
369
370 /* Issue warnings for macro argument names seen inside strings.  */
371 static void
372 warn_trad_stringify (pfile, p, len, argc, argv)
373      cpp_reader *pfile;
374      U_CHAR *p;
375      size_t len;
376      unsigned int argc;
377      const struct arg *argv;
378 {
379   U_CHAR *limit;
380   unsigned int i;
381
382   limit = p + len;
383   for (;;)
384     {
385       while (p < limit && !is_idstart (*p)) p++;
386       if (p >= limit)
387         break;
388
389       for (i = 0; i < argc; i++)
390         if (!ustrncmp (p, argv[i].name, argv[i].len)
391             && ! is_idchar (p[argv[i].len]))
392           {
393             cpp_warning (pfile,
394                 "macro argument \"%s\" would be stringified in traditional C",
395                          argv[i].name);
396             break;
397           }
398       p++;
399       while (p < limit && is_idchar (*p)) p++;
400       if (p >= limit)
401         break;
402     }
403 }
404
405 /* Generate pat nodes for macro arguments seen inside strings.  */
406 static unsigned int
407 trad_stringify (pfile, base, len, argc, argv, pat, endpat, last)
408      cpp_reader *pfile;
409      U_CHAR *base;
410      size_t len;
411      unsigned int argc;
412      const struct arg *argv;
413      struct reflist **pat, **endpat;
414      unsigned int last;
415 {
416   U_CHAR *p, *limit;
417   unsigned int i;
418
419   p = base;
420   limit = base + len;
421   for (;;)
422     {
423     proceed:
424       while (p < limit && !is_idstart (*p)) p++;
425       if (p >= limit)
426         break;
427
428       for (i = 0; i < argc; i++)
429         if (!ustrncmp (p, argv[i].name, argv[i].len)
430             && ! is_idchar (p[argv[i].len]))
431           {
432             if (CPP_WTRADITIONAL (pfile))
433               cpp_warning (pfile, "macro argument \"%s\" is stringified",
434                            argv[i].name);
435             /* Write out the string up to this point, and add a pat
436                node for the argument.  Note that the argument is NOT
437                stringified.  */
438             CPP_PUTS (pfile, base, p - base);
439             add_pat (pat, endpat, CPP_WRITTEN (pfile) - last, i /* argno */,
440                      !is_hspace (p[-1]) /* raw_before */,
441                      !is_hspace (p[argv[i].len]) /* raw_after */,
442                      0 /* strize */,
443                      argv[i].rest_arg);
444             last = CPP_WRITTEN (pfile);
445             base = p + argv[i].len;
446             p = base;
447             goto proceed;
448           }
449       p++;
450       while (p < limit && is_idchar (*p)) p++;
451       if (p >= limit)
452         break;
453     }
454   CPP_PUTS (pfile, base, p - base);
455   return last;
456 }
457
458 /* Read a replacement list for an object-like macro, and build the
459    object_defn structure.  LIST contains the replacement list,
460    beginning at 1.  */
461 static struct object_defn *
462 collect_objlike_expansion (pfile, list)
463      cpp_reader *pfile;
464      cpp_toklist *list;
465 {
466   struct object_defn *defn;
467   unsigned int i;
468   unsigned int start;
469   int last_was_paste = 0;
470   U_CHAR *exp;
471   size_t len;
472
473   /* We copy the expansion text into the token_buffer, then out to
474      its proper home.  */
475   start = CPP_WRITTEN (pfile);
476   CPP_PUTS (pfile, "\r ", 2);
477
478   for (i = 1; i < list->tokens_used; i++)
479     {
480       switch (TOK_TYPE (list, i))
481         {
482         case CPP_EOF:
483           cpp_ice (pfile, "EOF in collect_expansion");
484           /* fall through */
485         case CPP_VSPACE:
486           goto done;
487
488         case CPP_PASTE:
489           /* ## is not special if it appears right after another ##;
490              nor is it special if -traditional.  */
491           if (last_was_paste || CPP_TRADITIONAL (pfile))
492             break;
493           if (i == 1)
494             cpp_error (pfile, "`##' at start of macro definition");
495
496           last_was_paste = 1;
497           continue;
498
499         default:;
500         }
501
502       if (i > 1 && !last_was_paste && TOK_PREV_WHITE (list, i))
503         CPP_PUTC (pfile, ' ');
504
505       CPP_PUTS (pfile, TOK_NAME (list, i), TOK_LEN (list, i));
506       last_was_paste = 0;
507     }
508  done:
509
510   if (last_was_paste)
511     cpp_error (pfile, "`##' at end of macro definition");
512
513   CPP_PUTS (pfile, "\r ", 2);
514   len = CPP_WRITTEN (pfile) - start;
515   CPP_SET_WRITTEN (pfile, start);
516
517   if (len <= 4)
518     cpp_ice (pfile, "empty object-like macro went through full #define");
519
520   exp = (U_CHAR *) xmalloc (len + 1);
521   memcpy (exp, pfile->token_buffer + start, len);
522   exp[len] = '\0';
523
524   defn = (struct object_defn *) xmalloc (sizeof (struct object_defn));
525   defn->length = len;
526   defn->expansion = exp;
527
528   return defn;
529 }
530
531 /* Read a replacement list for a function-like macro, and build the
532    funct_defn structure.  LIST contains the replacement list,
533    beginning at REPLACEMENT.  ARGLIST specifies the formal parameters
534    to look for in the text of the definition.  */
535
536 static struct funct_defn *
537 collect_funlike_expansion (pfile, list, arglist, replacement)
538      cpp_reader *pfile;
539      cpp_toklist *list;
540      struct arglist *arglist;
541      unsigned int replacement;
542 {
543   struct funct_defn *defn;
544   struct reflist *pat = 0, *endpat = 0;
545   enum cpp_ttype token;
546   unsigned int start, last;
547   unsigned int i;
548   int j, argc;
549   size_t len;
550   const struct arg *argv;
551   U_CHAR *tok, *exp;
552   enum { START = 0, NORM, ARG, STRIZE, PASTE } last_token = START;
553
554   argv = arglist->argv;
555   argc = arglist->argc;
556
557   /* We copy the expansion text into the token_buffer, then out to
558      its proper home.  */
559   last = start = CPP_WRITTEN (pfile);
560   CPP_PUTS (pfile, "\r ", 2);
561
562   for (i = replacement; i < list->tokens_used; i++)
563     {
564       token = TOK_TYPE (list, i);
565       tok = TOK_NAME (list, i);
566       len = TOK_LEN (list, i);
567       switch (token)
568         {
569         case CPP_EOF:
570           cpp_ice (pfile, "EOF in collect_expansion");
571           /* fall through */
572         case CPP_VSPACE:
573           goto done;
574
575         case CPP_HASH:
576           /* # is special in function-like macros with no args.
577              (6.10.3.2 para 1.)  However, it is not special after
578              PASTE. (Implied by 6.10.3.3 para 4.)  Nor is it special
579              if -traditional.  */
580           if (last_token == PASTE || CPP_TRADITIONAL (pfile))
581             break;
582           last_token = STRIZE;
583           continue;
584
585         case CPP_PASTE:
586           /* ## is not special if it appears right after another ##;
587              nor is it special if -traditional.  */
588           if (last_token == PASTE || CPP_TRADITIONAL (pfile))
589             break;
590
591           if (last_token == START)
592             cpp_error (pfile, "`##' at start of macro definition");
593           else if (last_token == ARG)
594             /* If the last token was an argument, mark it raw_after.  */
595             endpat->raw_after = 1;
596           else if (last_token == STRIZE)
597             /* Oops - that wasn't a stringify operator.  */
598             CPP_PUTC (pfile, '#');
599
600           last_token = PASTE;
601           continue;
602
603         default:;
604         }
605
606       if (last_token != PASTE && last_token != START
607           && TOK_PREV_WHITE (list, i))
608         CPP_PUTC (pfile, ' ');
609       if (last_token == ARG && CPP_TRADITIONAL (pfile)
610           && !TOK_PREV_WHITE (list, i))
611         endpat->raw_after = 1;
612
613       switch (token)
614         {
615         case CPP_STRING:
616         case CPP_CHAR:
617           if (argc == 0)
618             goto norm;
619           if (CPP_TRADITIONAL (pfile))
620             {
621               last = trad_stringify (pfile, tok, len, argc, argv,
622                                      &pat, &endpat, last);
623               break;
624             }
625           else
626             {
627               if (CPP_WTRADITIONAL (pfile))
628                 warn_trad_stringify (pfile, tok, len, argc, argv);
629               goto norm;
630             }
631
632         case CPP_NAME:
633           for (j = 0; j < argc; j++)
634             if (argv[j].len == len
635                 && !ustrncmp (tok, argv[j].name, argv[j].len))
636               goto addref;
637
638           /* fall through */
639         default:
640         norm:
641           if (last_token == STRIZE)
642             {
643               /* This is a mandatory diagnostic (6.10.3.2 para 1), but
644                  in assembly language # may have some other
645                  significance we don't know about, so suppress the
646                  warning. */
647               if (! CPP_OPTION (pfile, lang_asm))
648                 cpp_pedwarn (pfile,
649                              "# is not followed by a macro argument name");
650               if (TOK_PREV_WHITE (list, i))
651                 CPP_ADJUST_WRITTEN (pfile, -1);
652               if (TOK_PREV_WHITE (list, i-1))
653                 CPP_PUTC (pfile, ' ');
654               CPP_PUTC (pfile, '#');
655               if (TOK_PREV_WHITE (list, i))
656                 CPP_PUTC (pfile, ' ');
657             }
658           CPP_PUTS (pfile, tok, len);
659           last_token = NORM;
660         }
661       continue;
662
663     addref:
664       {
665         int raw_before = (last_token == PASTE
666                           || (CPP_TRADITIONAL (pfile)
667                               && ! TOK_PREV_WHITE (list, j)));
668       
669         add_pat (&pat, &endpat,
670                  CPP_WRITTEN (pfile) - last /* nchars */, j /* argno */,
671                  raw_before, 0 /* raw_after */,
672                  (last_token == STRIZE), argv[j].rest_arg);
673       
674         last = CPP_WRITTEN (pfile);
675       }
676       last_token = ARG;
677     }
678  done:
679
680   if (last_token == STRIZE)
681     cpp_error (pfile, "`#' is not followed by a macro argument name");
682   else if (last_token == PASTE)
683     cpp_error (pfile, "`##' at end of macro definition");
684
685     CPP_PUTS (pfile, "\r ", 2);
686   len = CPP_WRITTEN (pfile) - start;
687   CPP_SET_WRITTEN (pfile, start);
688
689   exp = (U_CHAR *) xmalloc (len + 1);
690   memcpy (exp, pfile->token_buffer + start, len);
691   exp[len] = '\0';
692
693   defn = (struct funct_defn *) xmalloc (sizeof (struct funct_defn));
694   defn->length = len;
695   defn->expansion = exp;
696   defn->pattern = pat;
697   defn->rest_args = argc && argv[argc - 1].rest_arg;
698   defn->nargs = argc;
699   defn->argnames = arglist->namebuf;
700   if (argv)
701     free ((PTR) argv);
702   return defn;
703 }
704
705 /* Is argument NEW, which has just been added to the argument list,
706    a duplicate of a previous argument name?  */
707 static int
708 duplicate_arg_p (args, new)
709      U_CHAR *args, *new;
710 {
711   size_t newlen = ustrlen (new) + 1;
712   size_t oldlen;
713
714   while (args < new)
715     {
716       oldlen = ustrlen (args) + 1;
717       if (!memcmp (args, new, MIN (oldlen, newlen)))
718         return 1;
719       args += oldlen;
720     }
721   return 0;
722 }
723
724 static unsigned int
725 collect_params (pfile, list, arglist)
726      cpp_reader *pfile;
727      cpp_toklist *list;
728      struct arglist *arglist;
729 {
730   struct arg *argv = 0;
731   U_CHAR *namebuf, *p, *tok;
732   unsigned int len, argslen;
733   unsigned int argc, a, i, j;
734
735   /* The formal parameters list starts at token 1.  */
736   if (TOK_TYPE (list, 1) != CPP_OPEN_PAREN)
737     {
738       cpp_ice (pfile, "first token = %d not %d in collect_formal_parameters",
739                TOK_TYPE (list, 1), CPP_OPEN_PAREN);
740       return 0;
741     }
742
743   /* Scan once and count the number of parameters; also check for
744      syntax errors here.  */
745   argc = 0;
746   argslen = 0;
747   for (i = 2; i < list->tokens_used; i++)
748     switch (TOK_TYPE (list, i))
749       {
750       case CPP_NAME:
751         argslen += TOK_LEN (list, i) + 1;
752         argc++;
753         break;
754       case CPP_COMMA:
755         break;
756       case CPP_CLOSE_PAREN:
757         goto scanned;
758       case CPP_VSPACE:
759         cpp_error_with_line (pfile, list->line, TOK_COL (list, i),
760                              "missing right paren in macro argument list");
761         return 0;
762
763       default:
764         cpp_error_with_line (pfile, list->line, TOK_COL (list, i),
765                              "illegal token in macro argument list");
766         return 0;
767
768       case CPP_ELLIPSIS:
769         if (TOK_TYPE (list, i-1) != CPP_NAME)
770           {
771             argslen += sizeof "__VA_ARGS__";
772             argc++;
773           }
774         i++;
775         if (TOK_TYPE (list, i) != CPP_CLOSE_PAREN)
776           {
777             cpp_error_with_line (pfile, list->line, TOK_COL (list, i),
778                                  "another parameter follows \"...\"");
779             return 0;
780           }
781         goto scanned;
782       }
783
784   cpp_ice (pfile, "collect_params: unreachable - i=%d, ntokens=%d, type=%d",
785            i, list->tokens_used, TOK_TYPE (list, i-1));
786   return 0;
787
788  scanned:
789   if (argc == 0)        /* function-like macro, no arguments */
790     {
791       arglist->argc = 0;
792       arglist->argv = 0;
793       arglist->namebuf = 0;
794       return i + 1;
795     }
796   if (argslen == 0)
797     {
798       cpp_ice (pfile, "collect_params: argc=%d argslen=0", argc);
799       return 0;
800     }
801
802   /* Now allocate space and copy the suckers.  */
803   argv = (struct arg *) xmalloc (argc * sizeof (struct arg));
804   namebuf = (U_CHAR *) xmalloc (argslen);
805   p = namebuf;
806   a = 0;
807   for (j = 2; j < i; j++)
808     switch (TOK_TYPE (list, j))
809       {
810       case CPP_NAME:
811         tok = TOK_NAME (list, j);
812         len = TOK_LEN (list, j);
813         memcpy (p, tok, len);
814         p[len] = '\0';
815         if (duplicate_arg_p (namebuf, p))
816           {
817             cpp_error (pfile, "duplicate macro argument name \"%s\"", tok);
818             a++;
819             break;
820           }
821         if (CPP_PEDANTIC (pfile) && CPP_OPTION (pfile, c99)
822             && len == sizeof "__VA_ARGS__" - 1
823             && !ustrcmp (p, U"__VA_ARGS__"))
824           cpp_pedwarn (pfile,
825         "C99 does not permit use of __VA_ARGS__ as a macro argument name");
826         argv[a].len = len;
827         argv[a].name = p;
828         argv[a].rest_arg = 0;
829         p += len + 1;
830         a++;
831         break;
832
833       case CPP_COMMA:
834         break;
835
836       case CPP_ELLIPSIS:
837         if (TOK_TYPE (list, j-1) != CPP_NAME)
838           {
839             if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
840               cpp_pedwarn (pfile, "C89 does not permit varargs macros");
841
842             argv[a].len = sizeof "__VA_ARGS__" - 1;
843             argv[a].name = p;
844             argv[a].rest_arg = 1;
845             strcpy ((char *)p, "__VA_ARGS__");
846           }
847         else
848           {
849             if (CPP_PEDANTIC (pfile))
850               cpp_pedwarn (pfile,
851                            "ISO C does not permit named varargs macros");
852             argv[a-1].rest_arg = 1;
853           }
854         break;
855
856       default:
857         cpp_ice (pfile, "collect_params: impossible token type %d",
858                  TOK_TYPE (list, j));
859       }
860
861   arglist->argc = argc;
862   arglist->argv = argv;
863   arglist->namebuf = namebuf;
864   return i + 1;
865 }
866
867 /* Create a definition for a macro.  The replacement text (including
868    formal parameters if present) is in LIST.  If FUNLIKE is true, this
869    is a function-like macro.  */
870
871 int
872 _cpp_create_definition (pfile, list, hp)
873      cpp_reader *pfile;
874      cpp_toklist *list;
875      HASHNODE *hp;
876 {
877   struct funct_defn *fdefn = 0;
878   struct object_defn *odefn = 0;
879   enum node_type ntype;
880   int ok;
881
882   /* Special-case a few simple and common idioms:
883      #define TOKEN   // nothing
884      #define TOKEN TOKEN
885
886      Might also be good to special-case these:
887
888      #define FUNC()  // nothing
889      #define FUNC(a, b, ...) // nothing
890      #define FUNC(a, b, c) FUNC(a, b, c)  */
891
892   if (list->tokens_used == 2)
893     ntype = T_EMPTY;    /* Empty definition of object-like macro.  */
894   else if (list->tokens_used == 3 && TOK_TYPE (list, 1) == CPP_NAME
895            && TOK_LEN (list, 0) == TOK_LEN (list, 1)
896            && !ustrncmp (TOK_NAME (list, 0), TOK_NAME (list, 1),
897                          TOK_LEN (list, 0)))
898     ntype = T_IDENTITY;  /* Object like macro defined to itself.  */
899
900   /* The macro is function-like only if the next character,
901      with no intervening whitespace, is '('.  */
902   else if (TOK_TYPE (list, 1) == CPP_OPEN_PAREN
903            && ! TOK_PREV_WHITE (list, 1))
904     {
905       struct arglist args;
906       int replacement;
907
908       replacement = collect_params (pfile, list, &args);
909       if (replacement == 0)
910         return 0;
911       fdefn = collect_funlike_expansion (pfile, list, &args, replacement);
912       if (fdefn == 0)
913         return 0;
914
915       ntype = T_FMACRO;
916     }
917
918   /* Otherwise it is an object-like macro, and C99 requires
919      whitespace after the name (6.10.3 para 3).  */
920   else
921     {
922       if (! TOK_PREV_WHITE (list, 1))
923         cpp_pedwarn (pfile,
924                      "The C standard requires whitespace after #define %s",
925                      hp->name);
926
927       odefn = collect_objlike_expansion (pfile, list);
928       if (odefn == 0)
929         return 0;
930
931       ntype = T_MACRO;
932     }
933
934   if (ntype == T_EMPTY || ntype == T_IDENTITY)
935     {
936       odefn = xmalloc (sizeof (struct object_defn));
937       odefn->length = 0;
938       odefn->expansion = 0;
939     }
940
941   /* Check for a redefinition, and its legality.  Redefining a macro
942      of whatever stripe is ok if the definitions are the same.
943      Redefining a built-in _constant_ (T_CONST or T_XCONST) is ok only
944      with -D.  Otherwise a redefinition is not ok.  */
945
946   switch (hp->type)
947     {
948     case T_VOID:  ok = 1; break;
949     default:      ok = 0; break;
950
951     case T_MACRO:
952       ok = (ntype == hp->type
953             && odefn->length == hp->value.odefn->length
954             && !ustrncmp (odefn->expansion, hp->value.odefn->expansion,
955                           odefn->length));
956       break;
957     case T_FMACRO:
958       ok = (ntype == hp->type
959             && !compare_defs (pfile, fdefn, hp->value.fdefn));
960       break;
961     case T_IDENTITY:
962     case T_EMPTY:
963       ok = (ntype == hp->type);
964       break;
965     case T_CONST:
966     case T_XCONST:
967       ok = ! pfile->done_initializing;
968       break;
969     }
970
971   /* Print the warning or error if it's not ok.  */
972   if (! ok)
973     {
974       cpp_pedwarn (pfile, "\"%s\" redefined", hp->name);
975       if (pfile->done_initializing)
976         {
977           const char *file;
978           unsigned int line, col;
979           if (hp->type == T_FMACRO)
980             {
981               file = hp->value.fdefn->file;
982               line = hp->value.fdefn->line;
983               col  = hp->value.fdefn->col;
984             }
985           else
986             {
987               file = hp->value.odefn->file;
988               line = hp->value.odefn->line;
989               col  = hp->value.odefn->col;
990             }
991         cpp_pedwarn_with_file_and_line (pfile, file, line, col,
992                         "this is the location of the previous definition");
993         }
994     }
995
996   /* And replace the old definition (if any).  */
997
998   _cpp_free_definition (hp);
999
1000   hp->type = ntype;
1001   if (ntype == T_FMACRO)
1002     {
1003       fdefn->file = CPP_BUFFER (pfile)->nominal_fname;
1004       fdefn->line = list->line;
1005       fdefn->col  = TOK_COL (list, 0);
1006       hp->value.fdefn = fdefn;
1007     }
1008   else
1009     {
1010       odefn->file = CPP_BUFFER (pfile)->nominal_fname;
1011       odefn->line = list->line;
1012       odefn->col  = TOK_COL (list, 0);
1013       hp->value.odefn = odefn;
1014     }
1015   return 1;
1016 }
1017
1018 /*
1019  * Parse a macro argument and append the info on PFILE's token_buffer.
1020  * REST_ARGS means to absorb the rest of the args.
1021  * Return nonzero to indicate a syntax error.
1022  */
1023
1024 static enum cpp_ttype
1025 macarg (pfile, rest_args)
1026      cpp_reader *pfile;
1027      int rest_args;
1028 {
1029   int paren = 0;
1030   enum cpp_ttype token;
1031
1032   /* Try to parse as much of the argument as exists at this
1033      input stack level.  */
1034   for (;;)
1035     {
1036       token = cpp_get_token (pfile);
1037       switch (token)
1038         {
1039         case CPP_EOF:
1040           /* We've hit end of file; this is an error.
1041              Caller will report it.  */
1042           return token;
1043         case CPP_OPEN_PAREN:
1044           paren++;
1045           break;
1046         case CPP_CLOSE_PAREN:
1047           if (--paren < 0)
1048             goto found;
1049           break;
1050         case CPP_COMMA:
1051           /* if we've returned to lowest level and
1052              we aren't absorbing all args */
1053           if (paren == 0 && rest_args == 0)
1054             goto found;
1055           break;
1056         found:
1057           /* Remove ',' or ')' from argument buffer.  */
1058           CPP_ADJUST_WRITTEN (pfile, -1);
1059           return token;
1060         default:;
1061         }
1062     }
1063 }
1064 \f
1065
1066 static const char * const monthnames[] =
1067 {
1068   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1069   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
1070 };
1071
1072 /* Place into PFILE a quoted string representing the string SRC.
1073    Caller must reserve enough space in pfile->token_buffer.  */
1074
1075 void
1076 _cpp_quote_string (pfile, src)
1077      cpp_reader *pfile;
1078      const U_CHAR *src;
1079 {
1080   U_CHAR c;
1081
1082   CPP_PUTC_Q (pfile, '\"');
1083   for (;;)
1084     switch ((c = *src++))
1085       {
1086       default:
1087         if (ISPRINT (c))
1088           CPP_PUTC_Q (pfile, c);
1089         else
1090           {
1091             sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
1092             CPP_ADJUST_WRITTEN (pfile, 4);
1093           }
1094         break;
1095
1096       case '\"':
1097       case '\\':
1098         CPP_PUTC_Q (pfile, '\\');
1099         CPP_PUTC_Q (pfile, c);
1100         break;
1101       
1102       case '\0':
1103         CPP_PUTC_Q (pfile, '\"');
1104         return;
1105       }
1106 }
1107
1108 /*
1109  * expand things like __FILE__.  Place the expansion into the output
1110  * buffer *without* rescanning.
1111  */
1112
1113 #define DSC(str) (const U_CHAR *)str, sizeof str - 1
1114 static void
1115 special_symbol (pfile, hp)
1116      cpp_reader *pfile;
1117      HASHNODE *hp;
1118 {
1119   const U_CHAR *buf;
1120   cpp_buffer *ip;
1121   size_t len;
1122
1123   switch (hp->type)
1124     {
1125     case T_FILE:
1126     case T_BASE_FILE:
1127       ip = cpp_file_buffer (pfile);
1128       if (ip == NULL)
1129         {
1130           CPP_PUTS (pfile, "\"\"", 2);
1131           return;
1132         }
1133       if (hp->type == T_BASE_FILE)
1134         while (CPP_PREV_BUFFER (ip) != NULL)
1135           ip = CPP_PREV_BUFFER (ip);
1136
1137       buf = (const U_CHAR *) ip->nominal_fname;
1138       len = ustrlen (buf);
1139       CPP_RESERVE (pfile, 3 + 4 * len);
1140       _cpp_quote_string (pfile, buf);
1141       return;
1142
1143     case T_INCLUDE_LEVEL:
1144       {
1145         int true_indepth = 0;
1146         ip = cpp_file_buffer (pfile);
1147         while (ip)
1148           {
1149             true_indepth++;
1150             ip = CPP_PREV_BUFFER (ip);
1151           }
1152
1153         CPP_RESERVE (pfile, 10);
1154         sprintf ((char *)CPP_PWRITTEN (pfile), "%d", true_indepth);
1155         len = ustrlen (CPP_PWRITTEN (pfile));
1156         CPP_ADJUST_WRITTEN (pfile, len);
1157         return;
1158       }
1159
1160     case T_STDC:
1161 #ifdef STDC_0_IN_SYSTEM_HEADERS
1162       ip = cpp_file_buffer (pfile);
1163       if (ip && ip->system_header_p
1164           && !cpp_defined (pfile, DSC("__STRICT_ANSI__")))
1165         {
1166           CPP_PUTC (pfile, '0');
1167           return;
1168         }
1169 #endif
1170     constant:
1171       buf = hp->value.cpval;
1172       if (!buf || *buf == '\0')
1173         return;
1174
1175       len = ustrlen (buf);
1176       CPP_PUTS (pfile, buf, len);
1177       return;
1178
1179     case T_SPECLINE:
1180       ip = cpp_file_buffer (pfile);
1181       if (ip == NULL)
1182         {
1183           CPP_PUTC (pfile, '0');
1184           return;
1185         }
1186       CPP_RESERVE (pfile, 10);
1187       sprintf ((char *)CPP_PWRITTEN (pfile), "%u", CPP_BUF_LINE (ip));
1188       len = ustrlen (CPP_PWRITTEN (pfile));
1189       CPP_ADJUST_WRITTEN (pfile, len);
1190       return;
1191
1192     case T_DATE:
1193     case T_TIME:
1194       /* Generate both __DATE__ and __TIME__, stuff them into their
1195          respective hash nodes, and mark the nodes T_XCONST so we
1196          don't have to do this again.  We don't generate these strings
1197          at init time because time() and localtime() are very slow on
1198          some systems.  */
1199       {
1200         time_t tt = time (NULL);
1201         struct tm *tb = localtime (&tt);
1202         HASHNODE *d, *t;
1203
1204         if (hp->type == T_DATE)
1205           d = hp, t = _cpp_lookup (pfile, DSC("__TIME__"));
1206         else
1207           t = hp, d = _cpp_lookup (pfile, DSC("__DATE__"));
1208
1209         d->value.cpval = xmalloc (sizeof "'Oct 11 1347'");
1210         sprintf ((char *)d->value.cpval, "\"%s %2d %4d\"",
1211                  monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
1212         d->type = T_XCONST;
1213
1214         t->value.cpval = xmalloc (sizeof "'12:34:56'");
1215         sprintf ((char *)t->value.cpval, "\"%02d:%02d:%02d\"",
1216                  tb->tm_hour, tb->tm_min, tb->tm_sec);
1217         t->type = T_XCONST;
1218         goto constant;
1219       }
1220
1221     case T_POISON:
1222       cpp_error (pfile, "attempt to use poisoned `%s'.", hp->name);
1223       CPP_PUTC (pfile, '0');
1224       break;
1225
1226     default:
1227       cpp_ice (pfile, "invalid special hash type");
1228       return;
1229     }
1230 }
1231 #undef DSC
1232
1233 /* Expand a macro call.
1234    HP points to the symbol that is the macro being called.
1235    Put the result of expansion onto the input stack
1236    so that subsequent input by our caller will use it.
1237
1238    If macro wants arguments, caller has already verified that
1239    an argument list follows; arguments come from the input stack.  */
1240
1241 void
1242 _cpp_macroexpand (pfile, hp)
1243      cpp_reader *pfile;
1244      HASHNODE *hp;
1245 {
1246   const struct funct_defn *defn;
1247   struct argdata *args;
1248   unsigned int old_written;
1249   int i;
1250
1251   /* Object like macro - most common case.  */
1252   if (hp->type == T_MACRO)
1253     {
1254       push_macro_expansion (pfile, hp->value.odefn->expansion,
1255                             hp->value.odefn->length, hp);
1256       return;
1257     }
1258
1259   /* Or might it be a constant string?  */
1260   if (hp->type == T_CONST || hp->type == T_XCONST)
1261     {
1262       const U_CHAR *cpval = hp->value.cpval;
1263       if (cpval && *cpval != '\0')
1264         push_macro_expansion (pfile, cpval, ustrlen (cpval), hp);
1265       return;
1266     }
1267
1268   /* Or a special symbol?  */
1269   if (hp->type != T_FMACRO)
1270     {
1271       U_CHAR *xbuf;
1272       unsigned int len;
1273
1274       old_written = CPP_WRITTEN (pfile);
1275       special_symbol (pfile, hp);
1276       len = CPP_WRITTEN (pfile) - old_written;
1277       CPP_SET_WRITTEN (pfile, old_written);
1278       if (len == 0)
1279         return;
1280
1281       xbuf = (U_CHAR *) xmalloc (len + 1);
1282       memcpy (xbuf, CPP_PWRITTEN (pfile), len);
1283       xbuf[len] = '\0';
1284       push_macro_expansion (pfile, xbuf, len, hp);
1285       return;
1286     }
1287
1288   /* Okay, it's a full-on function-like macro...  */
1289   old_written = CPP_WRITTEN (pfile);
1290   defn = hp->value.fdefn;
1291
1292   args = alloca (MAX (defn->nargs, 1) * sizeof (struct argdata));
1293   for (i = 0; i < MAX (defn->nargs, 1); i++)
1294     {
1295       args[i].raw = args[i].expanded = 0;
1296       args[i].raw_length = 0;
1297       args[i].expand_length = args[i].stringified_length = -1;
1298     }
1299
1300   pfile->output_escapes++;
1301   scan_arguments (pfile, defn, args, hp->name);
1302
1303   /* If macro wants zero args, we parsed the arglist for checking only.
1304      Read directly from the macro definition.  */
1305   if (defn->nargs == 0 || defn->pattern == 0)
1306     {
1307       /* If the defn is the empty string, don't bother pushing it.  */
1308       if (defn->length > 4)
1309         push_macro_expansion (pfile, defn->expansion, defn->length, hp);
1310     }
1311   else
1312     funlike_macroexpand (pfile, hp, args);
1313
1314   CPP_SET_WRITTEN (pfile, old_written);
1315   pfile->output_escapes--;
1316 }
1317
1318 static void
1319 scan_arguments (pfile, defn, args, name)
1320      cpp_reader *pfile;
1321      const struct funct_defn *defn;
1322      struct argdata *args;
1323      const U_CHAR *name;
1324 {
1325   enum cpp_ttype token;
1326   unsigned int start_line, start_column;
1327   unsigned int nargs = defn->nargs;
1328   unsigned int i;
1329   
1330   cpp_buffer *ip = cpp_file_buffer (pfile);
1331   if (ip)
1332     {
1333       start_line = CPP_BUF_LINE (ip);
1334       start_column = CPP_BUF_COL (ip);
1335     }
1336   else
1337     start_line = start_column = 0;
1338
1339   /* Parse all the macro args that are supplied.  I counts them.  The
1340      first NARGS args are stored in ARGS.  The rest are discarded.  If
1341      rest_args is set then we assume macarg absorbed the rest of the
1342      args.  */
1343   i = 0;
1344
1345   /* Skip over the opening parenthesis.  */
1346   CPP_OPTION (pfile, discard_comments)++;
1347   pfile->no_macro_expand++;
1348   pfile->no_directives++;
1349
1350   token = cpp_get_non_space_token (pfile);
1351   if (token != CPP_OPEN_PAREN)
1352     cpp_ice (pfile, "macroexpand: unexpected token %d (wanted LPAREN)",
1353              token);
1354   CPP_ADJUST_WRITTEN (pfile, -1);
1355
1356   token = CPP_EOF;
1357   do
1358     {
1359       if (i < MAX (nargs, 1))
1360         {
1361           args[i].raw = CPP_WRITTEN (pfile);
1362           token = macarg (pfile, (i == nargs - 1 && defn->rest_args));
1363           args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
1364         }
1365       else
1366         token = macarg (pfile, 0);
1367       if (token == CPP_EOF)
1368         cpp_error_with_line (pfile, start_line, start_column,
1369                              "unterminated macro call");
1370       i++;
1371     }
1372   while (token == CPP_COMMA);
1373   CPP_OPTION (pfile, discard_comments)--;
1374   pfile->no_macro_expand--;
1375   pfile->no_directives--;
1376   if (token != CPP_CLOSE_PAREN)
1377     return;
1378
1379   /* foo ( ) is equivalent to foo () unless foo takes exactly one
1380      argument, in which case the former is allowed and the latter
1381      is not.  XXX C99 is silent on this rule, but it seems
1382      inconsistent to me.  */
1383   if (i == 1 && nargs == 0)
1384     {
1385       register U_CHAR *bp = ARG_BASE + args[0].raw;
1386       register U_CHAR *lim = bp + args[0].raw_length;
1387       while (bp != lim && is_space(*bp))
1388         bp++;
1389       if (bp == lim)
1390         i = 0;
1391     }
1392
1393   /* Don't output an error message if we have already output one for
1394      a parse error above.  */
1395   if (nargs == 0 && i > 0)
1396     {
1397       cpp_error (pfile, "arguments given to macro `%s'", name);
1398     }
1399   else if (i < nargs)
1400     {
1401       /* traditional C allows foo() if foo wants one argument.  */
1402       if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
1403         ;
1404       /* the rest args token is allowed to absorb 0 tokens */
1405       else if (i == nargs - 1 && defn->rest_args)
1406         ;
1407       else if (i == 0)
1408         cpp_error (pfile, "macro `%s' used without args", name);
1409       else if (i == 1)
1410         cpp_error (pfile, "macro `%s' used with just one arg", name);
1411       else
1412         cpp_error (pfile, "macro `%s' used with only %d args", name, i);
1413     }
1414   else if (i > nargs)
1415     {
1416       cpp_error (pfile, "macro `%s' used with too many (%d) args", name, i);
1417     }
1418 }
1419
1420 static void
1421 stringify (pfile, arg)
1422      cpp_reader *pfile;
1423      struct argdata *arg;
1424 {
1425   int arglen = arg->raw_length;
1426   int escaped = 0;
1427   int in_string = 0;
1428   int c;
1429   int i;
1430   /* Initially need_space is -1.  Otherwise, 1 means the previous
1431      character was a space, but we suppressed it; 0 means the previous
1432      character was a non-space.  */
1433   int need_space = -1;
1434   i = 0;
1435   arg->stringified = CPP_WRITTEN (pfile);
1436   CPP_PUTC (pfile, '\"');       /* insert beginning quote */
1437   for (; i < arglen; i++)
1438     {
1439       c = (ARG_BASE + arg->raw)[i];
1440
1441       if (!in_string)
1442         {
1443           /* Delete "\r " and "\r-" escapes.  */
1444           if (c == '\r')
1445             {
1446               i++;
1447               continue;
1448             }
1449           /* Internal sequences of whitespace are replaced by one
1450              space except within a string or char token. */
1451           else if (is_space(c))
1452             {
1453               if (need_space == 0)
1454                 need_space = 1;
1455               continue;
1456             }
1457           else if (need_space > 0)
1458             CPP_PUTC (pfile, ' ');
1459           need_space = 0;
1460         }
1461
1462       if (escaped)
1463         escaped = 0;
1464       else
1465         {
1466           if (c == '\\')
1467             escaped = 1;
1468           if (in_string)
1469             {
1470               if (c == in_string)
1471                 in_string = 0;
1472             }
1473           else if (c == '\"' || c == '\'')
1474             in_string = c;
1475         }
1476
1477       /* Escape these chars */
1478       if (c == '\"' || (in_string && c == '\\'))
1479         CPP_PUTC (pfile, '\\');
1480       if (ISPRINT (c))
1481         CPP_PUTC (pfile, c);
1482       else
1483         {
1484           CPP_RESERVE (pfile, 4);
1485           sprintf ((char *) CPP_PWRITTEN (pfile), "\\%03o", (unsigned int) c);
1486           CPP_ADJUST_WRITTEN (pfile, 4);
1487         }
1488     }
1489   CPP_PUTC (pfile, '\"');       /* insert ending quote */
1490   arg->stringified_length  = CPP_WRITTEN (pfile) - arg->stringified;
1491 }
1492
1493 static void
1494 funlike_macroexpand (pfile, hp, args)
1495      cpp_reader *pfile;
1496      HASHNODE *hp;
1497      struct argdata *args;
1498 {
1499   const struct funct_defn *defn = hp->value.fdefn;
1500   register U_CHAR *xbuf;
1501   int xbuf_len;
1502   const U_CHAR *exp = defn->expansion;
1503   int offset;   /* offset in expansion, copied a piece at a time */
1504   int totlen;   /* total amount of exp buffer filled so far */
1505   const struct reflist *ap, *last_ap;
1506   int i;
1507
1508   /* Compute length in characters of the macro's expansion.
1509      Also count number of times each arg is used.  */
1510   xbuf_len = defn->length;
1511   for (ap = defn->pattern; ap != NULL; ap = ap->next)
1512     {
1513       if (ap->stringify)
1514         {
1515           /* Stringify if it hasn't already been */
1516           if (args[ap->argno].stringified_length < 0)
1517             stringify (pfile, &args[ap->argno]);
1518           xbuf_len += args[ap->argno].stringified_length;
1519         }
1520       else if (ap->raw_before || ap->raw_after)
1521         /* Add 4 for two \r-space markers to prevent
1522            token concatenation.  */
1523         xbuf_len += args[ap->argno].raw_length + 4;
1524       else
1525         {
1526           /* We have an ordinary (expanded) occurrence of the arg.
1527              So compute its expansion, if we have not already.  */
1528           if (args[ap->argno].expand_length < 0)
1529             {
1530               args[ap->argno].expanded = CPP_WRITTEN (pfile);
1531               _cpp_expand_to_buffer (pfile, ARG_BASE + args[ap->argno].raw,
1532                                      args[ap->argno].raw_length);
1533
1534               args[ap->argno].expand_length
1535                 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
1536             }
1537
1538           /* Add 4 for two \r-space markers to prevent
1539              token concatenation.  */
1540           xbuf_len += args[ap->argno].expand_length + 4;
1541         }
1542     }
1543
1544   xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
1545
1546   /* Generate in XBUF the complete expansion with arguments
1547      substituted in.  TOTLEN is the total size generated so far.
1548      OFFSET is the index in the definition of where we are copying
1549      from.  */
1550   offset = totlen = 0;
1551   for (last_ap = NULL, ap = defn->pattern; ap != NULL;
1552        last_ap = ap, ap = ap->next)
1553     {
1554       register struct argdata *arg = &args[ap->argno];
1555       int count_before = totlen;
1556
1557       /* Add chars to XBUF.  */
1558       i = ap->nchars;
1559       memcpy (&xbuf[totlen], &exp[offset], i);
1560       totlen += i;
1561       offset += i;
1562
1563       /* If followed by an empty rest arg with concatenation,
1564          delete the last run of nonwhite chars.  */
1565       if (arg->raw_length == 0 && totlen > count_before
1566           && ((ap->rest_args && ap->raw_before)
1567               || (last_ap != NULL && last_ap->rest_args
1568                   && last_ap->raw_after)))
1569         {
1570           /* Delete final whitespace.  */
1571           while (totlen > count_before && is_space(xbuf[totlen - 1]))
1572             totlen--;
1573
1574           /* Delete the nonwhites before them.  */
1575           while (totlen > count_before && !is_space(xbuf[totlen - 1]))
1576             totlen--;
1577         }
1578
1579       if (ap->stringify != 0)
1580         {
1581           memcpy (xbuf + totlen, ARG_BASE + arg->stringified,
1582                   arg->stringified_length);
1583           totlen += arg->stringified_length;
1584         }
1585       else if (ap->raw_before || ap->raw_after)
1586         {
1587           U_CHAR *p1 = ARG_BASE + arg->raw;
1588           U_CHAR *l1 = p1 + arg->raw_length;
1589           if (ap->raw_before)
1590             {
1591               /* Arg is concatenated before: delete leading whitespace,
1592                  whitespace markers, and no-reexpansion markers.  */
1593               while (p1 != l1)
1594                 {
1595                   if (is_space(p1[0]))
1596                     p1++;
1597                   else if (p1[0] == '\r')
1598                     p1 += 2;
1599                   else
1600                     break;
1601                 }
1602             }
1603           if (ap->raw_after)
1604             {
1605               /* Arg is concatenated after: delete trailing whitespace,
1606                  whitespace markers, and no-reexpansion markers.  */
1607               while (p1 != l1)
1608                 {
1609                   if (is_space(l1[-1]))
1610                     l1--;
1611                   else if (l1[-1] == '\r')
1612                     l1--;
1613                   else if (l1[-1] == '-')
1614                     {
1615                       if (l1 != p1 + 1 && l1[-2] == '\r')
1616                         l1 -= 2;
1617                       else
1618                         break;
1619                     }
1620                   else
1621                     break;
1622                 }
1623             }
1624
1625           /* Delete any no-reexpansion marker that precedes
1626              an identifier at the beginning of the argument. */
1627           if (p1[0] == '\r' && p1[1] == '-')
1628             p1 += 2;
1629
1630           memcpy (xbuf + totlen, p1, l1 - p1);
1631           totlen += l1 - p1;
1632         }
1633       else
1634         {
1635           U_CHAR *expanded = ARG_BASE + arg->expanded;
1636           if (!ap->raw_before && totlen > 0 && arg->expand_length
1637               && unsafe_chars (pfile, xbuf[totlen - 1], expanded[0]))
1638             {
1639               xbuf[totlen++] = '\r';
1640               xbuf[totlen++] = ' ';
1641             }
1642
1643           memcpy (xbuf + totlen, expanded, arg->expand_length);
1644           totlen += arg->expand_length;
1645
1646           if (!ap->raw_after && totlen > 0 && offset < defn->length
1647               && unsafe_chars (pfile, xbuf[totlen - 1], exp[offset]))
1648             {
1649               xbuf[totlen++] = '\r';
1650               xbuf[totlen++] = ' ';
1651             }
1652         }
1653     }
1654
1655   /* if there is anything left of the definition
1656      after handling the arg list, copy that in too.  */
1657
1658   for (i = offset; i < defn->length; i++)
1659     xbuf[totlen++] = exp[i];
1660   xbuf[totlen] = 0;
1661
1662   if (totlen > xbuf_len)
1663     /* Just die - we've trashed the heap at this point.  */
1664     abort ();
1665   
1666   /* Now put the expansion on the input stack
1667      so our caller will commence reading from it.  */
1668   push_macro_expansion (pfile, xbuf, totlen, hp);
1669
1670   /* Overload buffer->mapped to indicate that xbuf needs to be freed.  */
1671   CPP_BUFFER (pfile)->mapped = 1;
1672 }
1673
1674 /* Return 1 iff a token ending in C1 followed directly by a token C2
1675    could cause mis-tokenization.  */
1676
1677 static int
1678 unsafe_chars (pfile, c1, c2)
1679      cpp_reader *pfile;
1680      int c1, c2;
1681 {
1682   /* If c2 is EOF, that's always safe.  */
1683   if (c2 == EOF)
1684     return 0;
1685
1686   switch (c1)
1687     {
1688     case EOF:
1689       /* We don't know what the previous character was.  We do know
1690          that it can't have been an idchar (or else it would have been
1691          pasted with the idchars of the macro name), and there are a
1692          number of second characters for which it doesn't matter what
1693          the first was.  */
1694       if (is_idchar (c2) || c2 == '\'' || c2 == '\"'
1695           || c2 == '(' || c2 == '[' || c2 == '{'
1696           || c2 == ')' || c2 == ']' || c2 == '}')
1697         return 0;
1698       return 1;
1699
1700     case '+':  case '-':
1701       if (c2 == c1 || c2 == '=')
1702         return 1;
1703       goto letter;
1704
1705     case 'e':  case 'E':  case 'p':  case 'P':
1706       if (c2 == '-' || c2 == '+')
1707         return 1;               /* could extend a pre-processing number */
1708       goto letter;
1709
1710     case '$':
1711       if (CPP_OPTION (pfile, dollars_in_ident))
1712         goto letter;
1713       return 0;
1714
1715     case 'L':
1716       if (c2 == '\'' || c2 == '\"')
1717         return 1;               /* Could turn into L"xxx" or L'xxx'.  */
1718       goto letter;
1719
1720     case '.':  case '0':  case '1':  case '2':  case '3':
1721     case '4':  case '5':  case '6':  case '7':  case '8':  case '9':
1722     case '_':  case 'a':  case 'b':  case 'c':  case 'd':  case 'f':
1723     case 'g':  case 'h':  case 'i':  case 'j':  case 'k':  case 'l':
1724     case 'm':  case 'n':  case 'o':  case 'q':  case 'r':  case 's':
1725     case 't':  case 'u':  case 'v':  case 'w':  case 'x':  case 'y':
1726     case 'z':  case 'A':  case 'B':  case 'C':  case 'D':  case 'F':
1727     case 'G':  case 'H':  case 'I':  case 'J':  case 'K':  case 'M':
1728     case 'N':  case 'O':  case 'Q':  case 'R':  case 'S':  case 'T':
1729     case 'U':  case 'V':  case 'W':  case 'X':  case 'Y':  case 'Z':
1730     letter:
1731     /* We're in the middle of either a name or a pre-processing number.  */
1732       return (is_idchar(c2) || c2 == '.');
1733
1734     case '<':  case '>':  case '!':  case '%':  case '#':  case ':':
1735     case '^':  case '&':  case '|':  case '*':  case '/':  case '=':
1736       return (c2 == c1 || c2 == '=');
1737     }
1738   return 0;
1739 }
1740
1741 static void
1742 push_macro_expansion (pfile, xbuf, len, hp)
1743      cpp_reader *pfile;
1744      const U_CHAR *xbuf;
1745      int len;
1746      HASHNODE *hp;
1747 {
1748   cpp_buffer *mbuf;
1749   int advance_cur = 0;
1750
1751   /* The first chars of the expansion should be a "\r " added by
1752      collect_expansion.  This is to prevent accidental token-pasting
1753      between the text preceding the macro invocation, and the macro
1754      expansion text.
1755
1756      We would like to avoid adding unneeded spaces (for the sake of
1757      tools that use cpp, such as imake).  In some common cases we can
1758      tell that it is safe to omit the space.  */
1759
1760   if (xbuf[0] == '\r' && xbuf[1] == ' '
1761       && !unsafe_chars (pfile, EOF, xbuf[2]))
1762     advance_cur = 1;
1763
1764   /* Likewise, avoid the extra space at the end of the macro expansion
1765      if this is safe.  We can do a better job here since we can know
1766      what the next char will be.  */
1767   if (len >= 3 && xbuf[len-2] == '\r' && xbuf[len-1] == ' '
1768       && !unsafe_chars (pfile, xbuf[len-3], CPP_BUF_PEEK (CPP_BUFFER (pfile))))
1769     len -= 2;
1770
1771   /* If the total expansion is "\r \r ", we must not trim both escapes.  */
1772   if (len == 2 && advance_cur)
1773     advance_cur = 0;
1774
1775   mbuf = cpp_push_buffer (pfile, xbuf, len);
1776   if (mbuf == NULL)
1777     return;
1778   if (advance_cur)
1779     mbuf->cur += 2;
1780   mbuf->macro = hp;
1781   mbuf->has_escapes = 1;
1782
1783   /* In C89, a macro cannot be expanded recursively.  Traditional C
1784      permits it, but any use in an object-like macro must lead to
1785      infinite recursion, so always follow C89 in object-like macros.
1786      Likewise, in a function-like macro it must cause infinite
1787      recursion unless we are actually doing something with the
1788      arguments.
1789
1790      Even that criterion is too weak.  The only example known where
1791      macro recursion isn't infinite is:
1792         #define bar(x,y) foo(x(y, 0))
1793         bar(bar, baz)
1794      which expands to foo(bar(baz, 0)) in C89 and
1795      foo(foo(baz(0, 0)) in K+R.  This looks pathological to me.
1796      If someone has a real-world example I would love to see it.  */
1797   if (hp->type != T_FMACRO
1798       || hp->value.fdefn->nargs == 0
1799       || hp->value.fdefn->pattern == 0
1800       || !CPP_TRADITIONAL (pfile))
1801     hp->disabled = 1;
1802 }
1803
1804 /* Return zero if two funct_defns are isomorphic.  */
1805
1806 static int
1807 compare_defs (pfile, d1, d2)
1808      cpp_reader *pfile;
1809      const struct funct_defn *d1, *d2;
1810 {
1811   const struct reflist *a1, *a2;
1812
1813   if (d1->nargs != d2->nargs)
1814     return 1;
1815   if (ustrcmp (d1->expansion, d2->expansion))
1816     return 1;
1817   if (CPP_PEDANTIC (pfile)
1818       && d1->argnames && d2->argnames)
1819     {
1820       U_CHAR *arg1 = d1->argnames;
1821       U_CHAR *arg2 = d2->argnames;
1822       size_t len;
1823       int i = d1->nargs;
1824       while (i--)
1825         {
1826           len = ustrlen (arg1) + 1;
1827           if (ustrcmp (arg1, arg2))
1828             return 1;
1829           arg1 += len;
1830           arg2 += len;
1831         }
1832     }
1833   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1834        a1 = a1->next, a2 = a2->next)
1835     {
1836       if (a1->nchars != a2->nchars
1837           || a1->argno != a2->argno
1838           || a1->stringify != a2->stringify
1839           || a1->raw_before != a2->raw_before
1840           || a1->raw_after != a2->raw_after)
1841         return 1;
1842     }
1843   if (a1 != a2)
1844     return 1;
1845
1846   return 0;
1847 }
1848
1849 /* Dump the definition of macro MACRO on stdout.  The format is suitable
1850    to be read back in again. */
1851
1852 void
1853 _cpp_dump_definition (pfile, hp)
1854      cpp_reader *pfile;
1855      HASHNODE *hp;
1856 {
1857   CPP_RESERVE (pfile, hp->length + sizeof "#define ");
1858   CPP_PUTS_Q (pfile, "#define ", sizeof "#define " - 1);
1859   CPP_PUTS_Q (pfile, hp->name, hp->length);
1860
1861   if (hp->type == T_EMPTY)
1862     /* do nothing */;
1863   else if (hp->type == T_FMACRO)
1864     dump_funlike_macro (pfile, hp->value.fdefn);
1865   else
1866     {
1867       CPP_PUTC_Q (pfile, ' ');
1868
1869       if (hp->type == T_IDENTITY)
1870         CPP_PUTS (pfile, hp->name, hp->length);
1871       else if (hp->type == T_MACRO)
1872         {
1873           /* The first and last two characters of a macro expansion are
1874              always "\r "; this needs to be trimmed out.
1875              So we need length-4 chars of space, plus one for the NUL.  */
1876           CPP_RESERVE (pfile, hp->value.odefn->length - 4 + 1);
1877           CPP_PUTS_Q (pfile, hp->value.odefn->expansion + 2,
1878                       hp->value.odefn->length - 4);
1879         }
1880       else
1881         cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
1882     }
1883   if (CPP_BUFFER (pfile) == 0 || ! pfile->done_initializing)
1884     CPP_PUTC (pfile, '\n');
1885 }
1886
1887 static void
1888 dump_funlike_macro (pfile, defn)
1889      cpp_reader *pfile;
1890      const struct funct_defn *defn;
1891 {
1892   const struct reflist *r;
1893   const U_CHAR **argv = (const U_CHAR **) alloca (defn->nargs *
1894                                                   sizeof(const U_CHAR *));
1895   int *argl = (int *) alloca (defn->nargs * sizeof(int));
1896   const U_CHAR *x;
1897   int i;
1898
1899   /* First extract the argument list. */
1900   x = defn->argnames;
1901   for (i = 0; i < defn->nargs; i++)
1902     {
1903       argv[i] = x;
1904       argl[i] = ustrlen (x);
1905       x += argl[i] + 1;
1906     }
1907       
1908   /* Now print out the argument list. */
1909   CPP_PUTC_Q (pfile, '(');
1910   for (i = 0; i < defn->nargs; i++)
1911     {
1912       CPP_RESERVE (pfile, argl[i] + 2);
1913       if (!(i == defn->nargs-1 && defn->rest_args
1914             && !ustrcmp (argv[i], U"__VA_ARGS__")))
1915         CPP_PUTS_Q (pfile, argv[i], argl[i]);
1916       if (i < defn->nargs-1)
1917         CPP_PUTS_Q (pfile, ", ", 2);
1918     }
1919   if (defn->rest_args)
1920     CPP_PUTS (pfile, "...", 3);
1921   CPP_PUTS (pfile, ") ", 2);
1922
1923   /* Now the definition. */
1924   x = defn->expansion;
1925   for (r = defn->pattern; r; r = r->next)
1926     {
1927       i = r->nchars;
1928       if (*x == '\r') x += 2, i -= 2;
1929       /* i chars for macro text, plus the length of the macro
1930          argument name, plus one for a stringify marker, plus two for
1931          each concatenation marker. */
1932       CPP_RESERVE (pfile,
1933                    i + argl[r->argno] + r->stringify
1934                    + (r->raw_before + r->raw_after) * 2);
1935
1936       if (i > 0) CPP_PUTS_Q (pfile, x, i);
1937       if (r->raw_before)
1938         CPP_PUTS_Q (pfile, "##", 2);
1939       if (r->stringify)
1940         CPP_PUTC_Q (pfile, '#');
1941       CPP_PUTS_Q (pfile, argv[r->argno], argl[r->argno]);
1942       if (r->raw_after && !(r->next && r->next->nchars == 0
1943                             && r->next->raw_before))
1944         CPP_PUTS_Q (pfile, "##", 2);
1945
1946       x += i;
1947     }
1948
1949   i = defn->length - (x - defn->expansion) - 2;
1950   if (*x == '\r') x += 2, i -= 2;
1951   if (i > 0) CPP_PUTS (pfile, x, i);
1952 }
1953
1954 /* Dump out the hash table.  */
1955 static int
1956 dump_hash_helper (h, p)
1957      void **h;
1958      void *p;
1959 {
1960   HASHNODE *hp = (HASHNODE *)*h;
1961   cpp_reader *pfile = (cpp_reader *)p;
1962
1963   if (hp->type == T_MACRO || hp->type == T_FMACRO
1964       || hp->type == T_IDENTITY || hp->type == T_EMPTY)
1965     _cpp_dump_definition (pfile, hp);
1966   return 1;
1967 }
1968
1969 void
1970 _cpp_dump_macro_hash (pfile)
1971      cpp_reader *pfile;
1972 {
1973   htab_traverse (pfile->hashtab, dump_hash_helper, pfile);
1974 }