OSDN Git Service

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