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