OSDN Git Service

* cpphash.h (POOL_ALIGN, POOL_FRONT, POOL_LIMIT, POOL_BASE,
[pf3gnuchains/gcc-fork.git] / gcc / cppmacro.c
1 /* Part of CPP library.  (Macro and #define handling.)
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3    1999, 2000, 2001 Free Software Foundation, Inc.
4    Written by Per Bothner, 1994.
5    Based on CCCP program by Paul Rubin, June 1986
6    Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22  In other words, you are welcome to use, share and improve this program.
23  You are forbidden to forbid anyone else to use, share and improve
24  what you give them.   Help stamp out software-hoarding!  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "intl.h"               /* for _("<command line>") below.  */
29 #include "cpplib.h"
30 #include "cpphash.h"
31
32 struct cpp_macro
33 {
34   cpp_hashnode **params;        /* Parameters, if any.  */
35   cpp_token *expansion;         /* First token of replacement list.   */
36   unsigned int line;            /* Starting line number.  */
37   unsigned int count;           /* Number of tokens in expansion.  */
38   unsigned short paramc;        /* Number of parameters.  */
39   unsigned int fun_like : 1;    /* If a function-like macro.  */
40   unsigned int variadic : 1;    /* If a variadic macro.  */
41   unsigned int disabled : 1;    /* If macro is disabled.  */
42   unsigned int syshdr   : 1;    /* If macro defined in system header.  */
43 };
44
45 typedef struct macro_arg macro_arg;
46 struct macro_arg
47 {
48   const cpp_token **first;      /* First token in unexpanded argument.  */
49   const cpp_token **expanded;   /* Macro-expanded argument.   */
50   const cpp_token *stringified; /* Stringified argument.  */
51   unsigned int count;           /* # of tokens in argument.  */
52   unsigned int expanded_count;  /* # of tokens in expanded argument.  */
53 };
54
55 /* Macro expansion.  */
56
57 static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
58 static const cpp_token *builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
59 static void push_token_context
60   PARAMS ((cpp_reader *, cpp_macro *, const cpp_token *, unsigned int));
61 static void push_ptoken_context
62   PARAMS ((cpp_reader *, cpp_macro *, _cpp_buff *,
63            const cpp_token **, unsigned int));
64 static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
65 static cpp_context *next_context PARAMS ((cpp_reader *));
66 static const cpp_token *padding_token
67   PARAMS ((cpp_reader *, const cpp_token *));
68 static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
69 static unsigned char *quote_string PARAMS ((unsigned char *,
70                                             const unsigned char *,
71                                             unsigned int));
72 static const cpp_token *new_string_token PARAMS ((cpp_reader *, U_CHAR *,
73                                                   unsigned int));
74 static const cpp_token *new_number_token PARAMS ((cpp_reader *, int));
75 static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
76 static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
77 static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
78                                   const cpp_token *));
79 static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *));
80 static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *));
81
82 /* #define directive parsing and handling.  */
83
84 static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
85 static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
86 static int warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
87                                          const cpp_macro *));
88 static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
89 static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
90 static void check_trad_stringification PARAMS ((cpp_reader *,
91                                                 const cpp_macro *,
92                                                 const cpp_string *));
93
94 /* Allocates and returns a CPP_STRING token, containing TEXT of length
95    LEN, after null-terminating it.  TEXT must be in permanent storage.  */
96 static const cpp_token *
97 new_string_token (pfile, text, len)
98      cpp_reader *pfile;
99      unsigned char *text;
100      unsigned int len;
101 {
102   cpp_token *token = _cpp_temp_token (pfile);
103
104   text[len] = '\0';
105   token->type = CPP_STRING;
106   token->val.str.len = len;
107   token->val.str.text = text;
108   token->flags = 0;
109   return token;
110 }
111
112 /* Allocates and returns a CPP_NUMBER token evaluating to NUMBER.  */
113 static const cpp_token *
114 new_number_token (pfile, number)
115      cpp_reader *pfile;
116      int number;
117 {
118   cpp_token *token = _cpp_temp_token (pfile);
119   unsigned char *buf = _cpp_unaligned_alloc (pfile, 20);
120
121   sprintf ((char *) buf, "%d", number);
122   token->type = CPP_NUMBER;
123   token->val.str.text = buf;
124   token->val.str.len = ustrlen (buf);
125   token->flags = 0;
126   return token;
127 }
128
129 static const char * const monthnames[] =
130 {
131   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
132   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
133 };
134
135 /* Handle builtin macros like __FILE__.  */
136 static const cpp_token *
137 builtin_macro (pfile, node)
138      cpp_reader *pfile;
139      cpp_hashnode *node;
140 {
141   switch (node->value.builtin)
142     {
143     default:
144       cpp_ice (pfile, "invalid builtin macro \"%s\"", NODE_NAME (node));
145       return new_number_token (pfile, 1);
146
147     case BT_FILE:
148     case BT_BASE_FILE:
149       {
150         unsigned int len;
151         const char *name;
152         U_CHAR *buf;
153         const struct line_map *map = pfile->map;
154
155         if (node->value.builtin == BT_BASE_FILE)
156           while (! MAIN_FILE_P (map))
157             map = INCLUDED_FROM (&pfile->line_maps, map);
158
159         name = map->to_file;
160         len = strlen (name);
161         buf = _cpp_unaligned_alloc (pfile, len * 4 + 1);
162         len = quote_string (buf, (const unsigned char *) name, len) - buf;
163
164         return new_string_token (pfile, buf, len);
165       }
166         
167     case BT_INCLUDE_LEVEL:
168       /* The line map depth counts the primary source as level 1, but
169          historically __INCLUDE_DEPTH__ has called the primary source
170          level 0.  */
171       return new_number_token (pfile, pfile->line_maps.depth - 1);
172
173     case BT_SPECLINE:
174       /* If __LINE__ is embedded in a macro, it must expand to the
175          line of the macro's invocation, not its definition.
176          Otherwise things like assert() will not work properly.  */
177       return new_number_token (pfile, SOURCE_LINE (pfile->map,
178                                                    pfile->cur_token[-1].line));
179
180     case BT_STDC:
181       {
182         int stdc = (!CPP_IN_SYSTEM_HEADER (pfile)
183                     || pfile->spec_nodes.n__STRICT_ANSI__->type != NT_VOID);
184         return new_number_token (pfile, stdc);
185       }
186
187     case BT_DATE:
188     case BT_TIME:
189       if (pfile->date.type == CPP_EOF)
190         {
191           /* Allocate __DATE__ and __TIME__ strings from permanent
192              storage.  We only do this once, and don't generate them
193              at init time, because time() and localtime() are very
194              slow on some systems.  */
195           time_t tt = time (NULL);
196           struct tm *tb = localtime (&tt);
197
198           pfile->date.val.str.text =
199             _cpp_unaligned_alloc (pfile, sizeof ("Oct 11 1347"));
200           pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
201           pfile->date.type = CPP_STRING;
202           pfile->date.flags = 0;
203           sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
204                    monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
205
206           pfile->time.val.str.text =
207             _cpp_unaligned_alloc (pfile, sizeof ("12:34:56"));
208           pfile->time.val.str.len = sizeof ("12:34:56") - 1;
209           pfile->time.type = CPP_STRING;
210           pfile->time.flags = 0;
211           sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
212                    tb->tm_hour, tb->tm_min, tb->tm_sec);
213         }
214
215       return node->value.builtin == BT_DATE ? &pfile->date: &pfile->time;
216     }
217 }
218
219 /* Adds backslashes before all backslashes and double quotes appearing
220    in strings.  Non-printable characters are converted to octal.  */
221 static U_CHAR *
222 quote_string (dest, src, len)
223      U_CHAR *dest;
224      const U_CHAR *src;
225      unsigned int len;
226 {
227   while (len--)
228     {
229       U_CHAR c = *src++;
230
231       if (c == '\\' || c == '"')
232         {
233           *dest++ = '\\';
234           *dest++ = c;
235         }
236       else
237         {
238           if (ISPRINT (c))
239             *dest++ = c;
240           else
241             {
242               sprintf ((char *) dest, "\\%03o", c);
243               dest += 4;
244             }
245         }
246     }
247
248   return dest;
249 }
250
251 /* Convert a token sequence to a single string token according to the
252    rules of the ISO C #-operator.  */
253 static const cpp_token *
254 stringify_arg (pfile, arg)
255      cpp_reader *pfile;
256      macro_arg *arg;
257 {
258   unsigned char *dest = BUFF_FRONT (pfile->u_buff);
259   unsigned int i, escape_it, backslash_count = 0;
260   const cpp_token *source = NULL;
261   size_t len;
262
263   /* Loop, reading in the argument's tokens.  */
264   for (i = 0; i < arg->count; i++)
265     {
266       const cpp_token *token = arg->first[i];
267
268       if (token->type == CPP_PADDING)
269         {
270           if (source == NULL)
271             source = token->val.source;
272           continue;
273         }
274
275       escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
276                    || token->type == CPP_CHAR || token->type == CPP_WCHAR);
277
278       /* Room for each char being written in octal, initial space and
279          final NUL.  */
280       len = cpp_token_len (token);
281       if (escape_it)
282         len *= 4;
283       len += 2;
284
285       if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
286         {
287           size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
288           _cpp_extend_buff (pfile, &pfile->u_buff, len);
289           dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
290         }
291
292       /* Leading white space?  */
293       if (dest != BUFF_FRONT (pfile->u_buff))
294         {
295           if (source == NULL)
296             source = token;
297           if (source->flags & PREV_WHITE)
298             *dest++ = ' ';
299         }
300       source = NULL;
301
302       if (escape_it)
303         {
304           _cpp_buff *buff = _cpp_get_buff (pfile, len);
305           unsigned char *buf = BUFF_FRONT (buff);
306           len = cpp_spell_token (pfile, token, buf) - buf;
307           dest = quote_string (dest, buf, len);
308           _cpp_release_buff (pfile, buff);
309         }
310       else
311         dest = cpp_spell_token (pfile, token, dest);
312
313       if (token->type == CPP_OTHER && token->val.c == '\\')
314         backslash_count++;
315       else
316         backslash_count = 0;
317     }
318
319   /* Ignore the final \ of invalid string literals.  */
320   if (backslash_count & 1)
321     {
322       cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
323       dest--;
324     }
325
326   /* Commit the memory, including NUL, and return the token.  */
327   len = dest - BUFF_FRONT (pfile->u_buff);
328   BUFF_FRONT (pfile->u_buff) = dest + 1;
329   return new_string_token (pfile, dest - len, len);
330 }
331
332 /* Try to paste two tokens.  On success, return non-zero.  In any
333    case, PLHS is updated to point to the pasted token, which is
334    guaranteed to not have the PASTE_LEFT flag set.  */
335 static bool
336 paste_tokens (pfile, plhs, rhs)
337      cpp_reader *pfile;
338      const cpp_token **plhs, *rhs;
339 {
340   unsigned char *buf, *end;
341   const cpp_token *lhs;
342   unsigned int len;
343   bool valid;
344
345   lhs = *plhs;
346   len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
347   buf = (unsigned char *) alloca (len);
348   end = cpp_spell_token (pfile, lhs, buf);
349
350   /* Avoid comment headers, since they are still processed in stage 3.
351      It is simpler to insert a space here, rather than modifying the
352      lexer to ignore comments in some circumstances.  Simply returning
353      false doesn't work, since we want to clear the PASTE_LEFT flag.  */
354   if (lhs->type == CPP_DIV
355       && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
356     *end++ = ' ';
357   end = cpp_spell_token (pfile, rhs, end);
358
359   cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
360
361   /* Tweak the column number the lexer will report.  */
362   pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
363
364   /* We don't want a leading # to be interpreted as a directive.  */
365   pfile->buffer->saved_flags = 0;
366
367   /* Set pfile->cur_token as required by _cpp_lex_direct.  */
368   pfile->cur_token = _cpp_temp_token (pfile);
369   *plhs = _cpp_lex_direct (pfile);
370   valid = (pfile->buffer->cur == pfile->buffer->rlimit
371            && pfile->buffer->read_ahead == EOF);
372   _cpp_pop_buffer (pfile);
373
374   return valid;
375 }
376
377 /* Handles an arbitrarily long sequence of ## operators.  This
378    implementation is left-associative, non-recursive, and finishes a
379    paste before handling succeeding ones.  If the paste fails, we back
380    up a token to just after the ## operator, with the effect that it
381    appears in the output stream normally.  */
382 static void
383 paste_all_tokens (pfile, lhs)
384      cpp_reader *pfile;
385      const cpp_token *lhs;
386 {
387   const cpp_token *rhs;
388   cpp_context *context = pfile->context;
389
390   do
391     {
392       /* Take the token directly from the current context.  We can do
393          this, because we are in the replacement list of either an
394          object-like macro, or a function-like macro with arguments
395          inserted.  In either case, the constraints to #define
396          guarantee we have at least one more token.  */
397       if (context->direct_p)
398         rhs = context->first.token++;
399       else
400         rhs = *context->first.ptoken++;
401
402       if (rhs->type == CPP_PADDING)
403         abort ();
404
405       if (!paste_tokens (pfile, &lhs, rhs))
406         {
407           _cpp_backup_tokens (pfile, 1);
408
409           /* Mandatory warning for all apart from assembler.  */
410           if (CPP_OPTION (pfile, lang) != CLK_ASM)
411             cpp_warning (pfile,
412          "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
413                          cpp_token_as_text (pfile, lhs),
414                          cpp_token_as_text (pfile, rhs));
415           break;
416         }
417     }
418   while (rhs->flags & PASTE_LEFT);
419
420   /* Put the resulting token in its own context.  */
421   push_token_context (pfile, NULL, lhs, 1);
422 }
423
424 /* Reads and returns the arguments to a function-like macro invocation.
425    Assumes the opening parenthesis has been processed.  If there is an
426    error, emits an appropriate diagnostic and returns NULL.  */
427 static _cpp_buff *
428 collect_args (pfile, node)
429      cpp_reader *pfile;
430      const cpp_hashnode *node;
431 {
432   _cpp_buff *buff, *base_buff;
433   cpp_macro *macro;
434   macro_arg *args, *arg;
435   const cpp_token *token;
436   unsigned int argc;
437   bool error = false;
438
439   macro = node->value.macro;
440   if (macro->paramc)
441     argc = macro->paramc;
442   else
443     argc = 1;
444   buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
445                                        + sizeof (macro_arg)));
446   base_buff = buff;
447   args = (macro_arg *) buff->base;
448   memset (args, 0, argc * sizeof (macro_arg));
449   buff->cur = (unsigned char *) &args[argc];
450   arg = args, argc = 0;
451
452   /* Collect the tokens making up each argument.  We don't yet know
453      how many arguments have been supplied, whether too many or too
454      few.  Hence the slightly bizarre usage of "argc" and "arg".  */
455   do
456     {
457       unsigned int paren_depth = 0;
458       unsigned int ntokens = 0;
459
460       argc++;
461       arg->first = (const cpp_token **) buff->cur;
462
463       for (;;)
464         {
465           /* Require space for 2 new tokens (including a CPP_EOF).  */
466           if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
467             {
468               buff = _cpp_append_extend_buff (pfile, buff,
469                                               1000 * sizeof (cpp_token *));
470               arg->first = (const cpp_token **) buff->cur;
471             }
472
473           token = cpp_get_token (pfile);
474
475           if (token->type == CPP_PADDING)
476             {
477               /* Drop leading padding.  */
478               if (ntokens == 0)
479                 continue;
480             }
481           else if (token->type == CPP_OPEN_PAREN)
482             paren_depth++;
483           else if (token->type == CPP_CLOSE_PAREN)
484             {
485               if (paren_depth-- == 0)
486                 break;
487             }
488           else if (token->type == CPP_COMMA)
489             {
490               /* A comma does not terminate an argument within
491                  parentheses or as part of a variable argument.  */
492               if (paren_depth == 0
493                   && ! (macro->variadic && argc == macro->paramc))
494                 break;
495             }
496           else if (token->type == CPP_EOF
497                    || (token->type == CPP_HASH && token->flags & BOL))
498             break;
499
500           arg->first[ntokens++] = token;
501         }
502
503       /* Drop trailing padding.  */
504       while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
505         ntokens--;
506
507       arg->count = ntokens;
508       arg->first[ntokens] = &pfile->eof;
509
510       /* Terminate the argument.  Excess arguments loop back and
511          overwrite the final legitimate argument, before failing.  */
512       if (argc <= macro->paramc)
513         {
514           buff->cur = (unsigned char *) &arg->first[ntokens + 1];
515           if (argc != macro->paramc)
516             arg++;
517         }
518     }
519   while (token->type != CPP_CLOSE_PAREN
520          && token->type != CPP_EOF
521          && token->type != CPP_HASH);
522
523   if (token->type == CPP_EOF || token->type == CPP_HASH)
524     {
525       bool step_back = false;
526
527       /* 6.10.3 paragraph 11: If there are sequences of preprocessing
528          tokens within the list of arguments that would otherwise act
529          as preprocessing directives, the behavior is undefined.
530
531          This implementation will report a hard error, terminate the
532          macro invocation, and proceed to process the directive.  */
533       if (token->type == CPP_HASH)
534         {
535           cpp_error (pfile,
536                      "directives may not be used inside a macro argument");
537           step_back = true;
538         }
539       else
540         step_back = (pfile->context->prev || pfile->state.in_directive);
541
542       /* We still need the CPP_EOF to end directives, and to end
543          pre-expansion of a macro argument.  Step back is not
544          unconditional, since we don't want to return a CPP_EOF to our
545          callers at the end of an -include-d file.  */
546       if (step_back)
547         _cpp_backup_tokens (pfile, 1);
548       cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
549                  NODE_NAME (node));
550       error = true;
551     }
552   else if (argc < macro->paramc)
553     {
554       /* As an extension, a rest argument is allowed to not appear in
555          the invocation at all.
556          e.g. #define debug(format, args...) something
557          debug("string");
558          
559          This is exactly the same as if there had been an empty rest
560          argument - debug("string", ).  */
561
562       if (argc + 1 == macro->paramc && macro->variadic)
563         {
564           if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
565             cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
566         }
567       else
568         {
569           cpp_error (pfile,
570                      "macro \"%s\" requires %u arguments, but only %u given",
571                      NODE_NAME (node), macro->paramc, argc);
572           error = true;
573         }
574     }
575   else if (argc > macro->paramc)
576     {
577       /* Empty argument to a macro taking no arguments is OK.  */
578       if (argc != 1 || arg->count)
579         {
580           cpp_error (pfile,
581                      "macro \"%s\" passed %u arguments, but takes just %u",
582                      NODE_NAME (node), argc, macro->paramc);
583           error = true;
584         }
585     }
586
587   if (!error)
588     return base_buff;
589
590   _cpp_release_buff (pfile, base_buff);
591   return NULL;
592 }
593
594 static int
595 funlike_invocation_p (pfile, node)
596      cpp_reader *pfile;
597      const cpp_hashnode *node;
598 {
599   const cpp_token *maybe_paren;
600   _cpp_buff *buff = NULL;
601
602   pfile->state.prevent_expansion++;
603   pfile->keep_tokens++;
604
605   pfile->state.parsing_args = 1;
606   do
607     maybe_paren = cpp_get_token (pfile);
608   while (maybe_paren->type == CPP_PADDING);
609   pfile->state.parsing_args = 2;
610
611   if (maybe_paren->type == CPP_OPEN_PAREN)
612     buff = collect_args (pfile, node);
613   else
614     {
615       _cpp_backup_tokens (pfile, 1);
616       if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
617         cpp_warning (pfile,
618  "function-like macro \"%s\" must be used with arguments in traditional C",
619                      NODE_NAME (node));
620     }
621
622   pfile->state.parsing_args = 0;
623   pfile->keep_tokens--;
624   pfile->state.prevent_expansion--;
625
626   if (buff)
627     {
628       if (node->value.macro->paramc > 0)
629         replace_args (pfile, node->value.macro, (macro_arg *) buff->base);
630       _cpp_release_buff (pfile, buff);
631     }
632
633   return buff != 0;
634 }
635
636 /* Push the context of a macro onto the context stack.  TOKEN is the
637    macro name.  If we can successfully start expanding the macro,
638    TOKEN is replaced with the first token of the expansion, and we
639    return non-zero.  */
640 static int
641 enter_macro_context (pfile, node)
642      cpp_reader *pfile;
643      cpp_hashnode *node;
644 {
645   if (node->flags & NODE_BUILTIN)
646     push_token_context (pfile, NULL, builtin_macro (pfile, node), 1);
647   else
648     {
649       cpp_macro *macro = node->value.macro;
650
651       if (macro->fun_like && !funlike_invocation_p (pfile, node))
652         return 0;
653
654       /* Disable the macro within its expansion.  */
655       macro->disabled = 1;
656
657       if (macro->paramc == 0)
658         push_token_context (pfile, macro, macro->expansion, macro->count);
659     }
660  
661   return 1;
662 }
663
664 /* Take the expansion of a function-like MACRO, replacing parameters
665    with the actual arguments.  Each argument is macro-expanded before
666    replacement, unless operated upon by the # or ## operators.  */
667 static void
668 replace_args (pfile, macro, args)
669      cpp_reader *pfile;
670      cpp_macro *macro;
671      macro_arg *args;
672 {
673   unsigned int i, total;
674   const cpp_token *src, *limit;
675   const cpp_token **dest, **first;
676   macro_arg *arg;
677   _cpp_buff *buff;
678
679   /* First, fully macro-expand arguments, calculating the number of
680      tokens in the final expansion as we go.  The ordering of the if
681      statements below is subtle; we must handle stringification before
682      pasting.  */
683   total = macro->count;
684   limit = macro->expansion + macro->count;
685
686   for (src = macro->expansion; src < limit; src++)
687     if (src->type == CPP_MACRO_ARG)
688       {
689         /* Leading and trailing padding tokens.  */
690         total += 2;
691
692         /* We have an argument.  If it is not being stringified or
693            pasted it is macro-replaced before insertion.  */
694         arg = &args[src->val.arg_no - 1];
695
696         if (src->flags & STRINGIFY_ARG)
697           {
698             if (!arg->stringified)
699               arg->stringified = stringify_arg (pfile, arg);
700           }
701         else if ((src->flags & PASTE_LEFT)
702                  || (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
703           total += arg->count - 1;
704         else
705           {
706             if (!arg->expanded)
707               expand_arg (pfile, arg);
708             total += arg->expanded_count - 1;
709           }
710       }
711
712   /* Now allocate space for the expansion, copy the tokens and replace
713      the arguments.  */
714   buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
715   first = (const cpp_token **) buff->base;
716   dest = first;
717
718   for (src = macro->expansion; src < limit; src++)
719     {
720       unsigned int count;
721       const cpp_token **from, **paste_flag;
722
723       if (src->type != CPP_MACRO_ARG)
724         {
725           *dest++ = src;
726           continue;
727         }
728
729       paste_flag = 0;
730       arg = &args[src->val.arg_no - 1];
731       if (src->flags & STRINGIFY_ARG)
732         count = 1, from = &arg->stringified;
733       else if (src->flags & PASTE_LEFT)
734         count = arg->count, from = arg->first;
735       else if (src != macro->expansion && (src[-1].flags & PASTE_LEFT))
736         {
737           count = arg->count, from = arg->first;
738           if (dest != first)
739             {
740               /* GCC has special semantics for , ## b where b is a
741                  varargs parameter: the comma disappears if b was
742                  given no actual arguments (not merely if b is an
743                  empty argument); otherwise the paste flag is removed.  */
744               if (dest[-1]->type == CPP_COMMA
745                   && macro->variadic
746                   && src->val.arg_no == macro->paramc)
747                 {
748                   if (count == 0)
749                     dest--;
750                   else
751                     paste_flag = dest - 1;
752                 }
753               /* Remove the paste flag if the RHS is a placemarker.  */
754               else if (count == 0)
755                 paste_flag = dest - 1;
756             }
757         }
758       else
759         count = arg->expanded_count, from = arg->expanded;
760
761       /* Padding on the left of an argument (unless RHS of ##).  */
762       if (!pfile->state.in_directive
763           && src != macro->expansion && !(src[-1].flags & PASTE_LEFT))
764         *dest++ = padding_token (pfile, src);
765
766       if (count)
767         {
768           memcpy (dest, from, count * sizeof (cpp_token *));
769           dest += count;
770
771           /* With a non-empty argument on the LHS of ##, the last
772              token should be flagged PASTE_LEFT.  */
773           if (src->flags & PASTE_LEFT)
774             paste_flag = dest - 1;
775         }
776
777       /* Avoid paste on RHS (even case count == 0).  */
778       if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
779         *dest++ = &pfile->avoid_paste;
780
781       /* Add a new paste flag, or remove an unwanted one.  */
782       if (paste_flag)
783         {
784           cpp_token *token = _cpp_temp_token (pfile);
785           token->type = (*paste_flag)->type;
786           token->val.str = (*paste_flag)->val.str;
787           if (src->flags & PASTE_LEFT)
788             token->flags = (*paste_flag)->flags | PASTE_LEFT;
789           else
790             token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
791           *paste_flag = token;
792         }
793     }
794
795   /* Free the expanded arguments.  */
796   for (i = 0; i < macro->paramc; i++)
797     if (args[i].expanded)
798       free (args[i].expanded);
799
800   push_ptoken_context (pfile, macro, buff, first, dest - first);
801 }
802
803 /* Return a special padding token, with padding inherited from SOURCE.  */
804 static const cpp_token *
805 padding_token (pfile, source)
806      cpp_reader *pfile;
807      const cpp_token *source;
808 {
809   cpp_token *result = _cpp_temp_token (pfile);
810
811   result->type = CPP_PADDING;
812   result->val.source = source;
813   result->flags = 0;
814   return result;
815 }
816
817 /* Move to the next context.  Create one if there is none.  */
818 static cpp_context *
819 next_context (pfile)
820      cpp_reader *pfile;
821 {
822   cpp_context *result = pfile->context->next;
823
824   if (result == 0)
825     {
826       result = xnew (cpp_context);
827       result->prev = pfile->context;
828       result->next = 0;
829       pfile->context->next = result;
830     }
831
832   pfile->context = result;
833   return result;
834 }
835
836 /* Push a list of pointers to tokens.  */
837 static void
838 push_ptoken_context (pfile, macro, buff, first, count)
839      cpp_reader *pfile;
840      cpp_macro *macro;
841      _cpp_buff *buff;
842      const cpp_token **first;
843      unsigned int count;
844 {
845   cpp_context *context = next_context (pfile);
846
847   context->direct_p = false;
848   context->macro = macro;
849   context->buff = buff;
850   context->first.ptoken = first;
851   context->last.ptoken = first + count;
852 }
853
854 /* Push a list of tokens.  */
855 static void
856 push_token_context (pfile, macro, first, count)
857      cpp_reader *pfile;
858      cpp_macro *macro;
859      const cpp_token *first;
860      unsigned int count;
861 {
862   cpp_context *context = next_context (pfile);
863
864   context->direct_p = true;
865   context->macro = macro;
866   context->buff = NULL;
867   context->first.token = first;
868   context->last.token = first + count;
869 }
870
871 static void
872 expand_arg (pfile, arg)
873      cpp_reader *pfile;
874      macro_arg *arg;
875 {
876   unsigned int capacity;
877
878   if (arg->count == 0)
879     return;
880
881   /* Loop, reading in the arguments.  */
882   capacity = 256;
883   arg->expanded = (const cpp_token **)
884     xmalloc (capacity * sizeof (cpp_token *));
885
886   push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
887   for (;;)
888     {
889       const cpp_token *token;
890
891       if (arg->expanded_count + 1 >= capacity)
892         {
893           capacity *= 2;
894           arg->expanded = (const cpp_token **)
895             xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
896         }
897
898       token = cpp_get_token (pfile);
899
900       if (token->type == CPP_EOF)
901         break;
902
903       arg->expanded[arg->expanded_count++] = token;
904     }
905
906   _cpp_pop_context (pfile);
907 }
908
909 void
910 _cpp_pop_context (pfile)
911      cpp_reader *pfile;
912 {
913   cpp_context *context = pfile->context;
914
915   /* Re-enable a macro when leaving its expansion.  */
916   if (context->macro)
917     context->macro->disabled = 0;
918
919   if (context->buff)
920     _cpp_release_buff (pfile, context->buff);
921
922   pfile->context = context->prev;
923 }
924
925 /* Eternal routine to get a token.  Also used nearly everywhere
926    internally, except for places where we know we can safely call
927    the lexer directly, such as lexing a directive name.
928
929    Macro expansions and directives are transparently handled,
930    including entering included files.  Thus tokens are post-macro
931    expansion, and after any intervening directives.  External callers
932    see CPP_EOF only at EOF.  Internal callers also see it when meeting
933    a directive inside a macro call, when at the end of a directive and
934    state.in_directive is still 1, and at the end of argument
935    pre-expansion.  */
936 const cpp_token *
937 cpp_get_token (pfile)
938      cpp_reader *pfile;
939 {
940   const cpp_token *result;
941
942   for (;;)
943     {
944       cpp_hashnode *node;
945       cpp_context *context = pfile->context;
946
947       /* Context->prev == 0 <=> base context.  */
948       if (!context->prev)
949         result = _cpp_lex_token (pfile);
950       else if (context->first.token != context->last.token)
951         {
952           if (context->direct_p)
953             result = context->first.token++;
954           else
955             result = *context->first.ptoken++;
956
957           if (result->flags & PASTE_LEFT)
958             {
959               paste_all_tokens (pfile, result);
960               if (pfile->state.in_directive)
961                 continue;
962               return padding_token (pfile, result);
963             }
964         }
965       else
966         {
967           _cpp_pop_context (pfile);
968           if (pfile->state.in_directive)
969             continue;
970           return &pfile->avoid_paste;
971         }
972
973       if (result->type != CPP_NAME)
974         break;
975
976       node = result->val.node;
977
978       /* Handle macros and the _Pragma operator.  */
979       if (node->type == NT_MACRO && !(result->flags & NO_EXPAND))
980         {
981           /* Macros invalidate controlling macros.  */
982           pfile->mi_valid = false;
983
984           if (!(node->flags & NODE_BUILTIN) && node->value.macro->disabled)
985             {
986               /* Flag this token as always unexpandable.  */
987               cpp_token *t = _cpp_temp_token (pfile);
988               t->type = result->type;
989               t->flags = result->flags | NO_EXPAND;
990               t->val.str = result->val.str;
991               result = t;
992             }
993           else if (!pfile->state.prevent_expansion
994                    && enter_macro_context (pfile, node))
995             {
996               if (pfile->state.in_directive)
997                 continue;
998               return padding_token (pfile, result);
999             }
1000         }
1001
1002       /* Don't interpret _Pragma within directives.  The standard is
1003          not clear on this, but to me this makes most sense.  */
1004       if (node != pfile->spec_nodes.n__Pragma
1005           || pfile->state.in_directive)
1006         break;
1007
1008       /* Handle it, and loop back for another token.  MI is cleared
1009          since this token came from either the lexer or a macro.  */
1010       _cpp_do__Pragma (pfile);
1011     }
1012
1013   return result;
1014 }
1015
1016 /* Returns true if we're expanding an object-like macro that was
1017    defined in a system header.  Just checks the macro at the top of
1018    the stack.  Used for diagnostic suppression.  */
1019 int
1020 cpp_sys_macro_p (pfile)
1021      cpp_reader *pfile;
1022 {
1023   cpp_macro *macro = pfile->context->macro;
1024
1025   return macro && macro->syshdr;
1026 }
1027
1028 /* Read each token in, until EOF.  Directives are transparently
1029    processed.  */
1030 void
1031 cpp_scan_nooutput (pfile)
1032      cpp_reader *pfile;
1033 {
1034   while (cpp_get_token (pfile)->type != CPP_EOF)
1035     ;
1036 }
1037
1038 /* Step back one (or more) tokens.  Can only step mack more than 1 if
1039    they are from the lexer, and not from macro expansion.  */
1040 void
1041 _cpp_backup_tokens (pfile, count)
1042      cpp_reader *pfile;
1043      unsigned int count;
1044 {
1045   if (pfile->context->prev == NULL)
1046     {
1047       pfile->lookaheads += count;
1048       while (count--)
1049         {
1050           pfile->cur_token--;
1051           if (pfile->cur_token == pfile->cur_run->base)
1052             {
1053               pfile->cur_run = pfile->cur_run->prev;
1054               pfile->cur_token = pfile->cur_run->limit;
1055             }
1056         }
1057     }
1058   else
1059     {
1060       if (count != 1)
1061         abort ();
1062       if (pfile->context->direct_p)
1063         pfile->context->first.token--;
1064       else
1065         pfile->context->first.ptoken--;
1066     }
1067 }
1068
1069 /* #define directive parsing and handling.  */
1070
1071 /* Returns non-zero if a macro redefinition warning is required.  */
1072 static int
1073 warn_of_redefinition (pfile, node, macro2)
1074      cpp_reader *pfile;
1075      const cpp_hashnode *node;
1076      const cpp_macro *macro2;
1077 {
1078   const cpp_macro *macro1;
1079   unsigned int i;
1080
1081   /* Some redefinitions need to be warned about regardless.  */
1082   if (node->flags & NODE_WARN)
1083     return 1;
1084
1085   if (! CPP_PEDANTIC (pfile))
1086     return 0;
1087
1088   /* Redefinition of a macro is allowed if and only if the old and new
1089      definitions are the same.  (6.10.3 paragraph 2).  */
1090   macro1 = node->value.macro;
1091
1092   /* The quick failures.  */
1093   if (macro1->count != macro2->count
1094       || macro1->paramc != macro2->paramc
1095       || macro1->fun_like != macro2->fun_like
1096       || macro1->variadic != macro2->variadic)
1097     return 1;
1098
1099   /* Check each token.  */
1100   for (i = 0; i < macro1->count; i++)
1101     if (! _cpp_equiv_tokens (&macro1->expansion[i], &macro2->expansion[i]))
1102       return 1;
1103
1104   /* Check parameter spellings.  */
1105   for (i = 0; i < macro1->paramc; i++)
1106     if (macro1->params[i] != macro2->params[i])
1107       return 1;
1108
1109   return 0;
1110 }
1111
1112 /* Free the definition of hashnode H.  */
1113
1114 void
1115 _cpp_free_definition (h)
1116      cpp_hashnode *h;
1117 {
1118   /* Macros and assertions no longer have anything to free.  */
1119   h->type = NT_VOID;
1120   /* Clear builtin flag in case of redefinition.  */
1121   h->flags &= ~NODE_BUILTIN;
1122 }
1123
1124 /* Save parameter NODE to the parameter list of macro MACRO.  Returns
1125    zero on success, non-zero if the paramter is a duplicate.  */
1126 static int
1127 save_parameter (pfile, macro, node)
1128      cpp_reader *pfile;
1129      cpp_macro *macro;
1130      cpp_hashnode *node;
1131 {
1132   /* Constraint 6.10.3.6 - duplicate parameter names.  */
1133   if (node->arg_index)
1134     {
1135       cpp_error (pfile, "duplicate macro parameter \"%s\"", NODE_NAME (node));
1136       return 1;
1137     }
1138
1139   if (BUFF_ROOM (pfile->a_buff)
1140       < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1141     _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1142
1143   ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1144   node->arg_index = macro->paramc;
1145   return 0;
1146 }
1147
1148 /* Check the syntax of the parameters in a MACRO definition.  */
1149 static int
1150 parse_params (pfile, macro)
1151      cpp_reader *pfile;
1152      cpp_macro *macro;
1153 {
1154   unsigned int prev_ident = 0;
1155
1156   for (;;)
1157     {
1158       const cpp_token *token = _cpp_lex_token (pfile);
1159
1160       switch (token->type)
1161         {
1162         default:
1163           cpp_error (pfile, "\"%s\" may not appear in macro parameter list",
1164                      cpp_token_as_text (pfile, token));
1165           return 0;
1166
1167         case CPP_NAME:
1168           if (prev_ident)
1169             {
1170               cpp_error (pfile, "macro parameters must be comma-separated");
1171               return 0;
1172             }
1173           prev_ident = 1;
1174
1175           if (save_parameter (pfile, macro, token->val.node))
1176             return 0;
1177           continue;
1178
1179         case CPP_CLOSE_PAREN:
1180           if (prev_ident || macro->paramc == 0)
1181             return 1;
1182
1183           /* Fall through to pick up the error.  */
1184         case CPP_COMMA:
1185           if (!prev_ident)
1186             {
1187               cpp_error (pfile, "parameter name missing");
1188               return 0;
1189             }
1190           prev_ident = 0;
1191           continue;
1192
1193         case CPP_ELLIPSIS:
1194           macro->variadic = 1;
1195           if (!prev_ident)
1196             {
1197               save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__);
1198               pfile->state.va_args_ok = 1;
1199               if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1200                 cpp_pedwarn (pfile,
1201                      "anonymous variadic macros were introduced in C99");
1202             }
1203           else if (CPP_OPTION (pfile, pedantic))
1204             cpp_pedwarn (pfile, "ISO C does not permit named variadic macros");
1205
1206           /* We're at the end, and just expect a closing parenthesis.  */
1207           token = _cpp_lex_token (pfile);
1208           if (token->type == CPP_CLOSE_PAREN)
1209             return 1;
1210           /* Fall through.  */
1211
1212         case CPP_EOF:
1213           cpp_error (pfile, "missing ')' in macro parameter list");
1214           return 0;
1215         }
1216     }
1217 }
1218
1219 /* Allocate room for a token from a macro's replacement list.  */
1220 static cpp_token *
1221 alloc_expansion_token (pfile, macro)
1222      cpp_reader *pfile;
1223      cpp_macro *macro;
1224 {
1225   if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1226     _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1227
1228   return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1229 }
1230
1231 static cpp_token *
1232 lex_expansion_token (pfile, macro)
1233      cpp_reader *pfile;
1234      cpp_macro *macro;
1235 {
1236   cpp_token *token;
1237
1238   pfile->cur_token = alloc_expansion_token (pfile, macro);
1239   token = _cpp_lex_direct (pfile);
1240
1241   /* Is this an argument?  */
1242   if (token->type == CPP_NAME && token->val.node->arg_index)
1243     {
1244       token->type = CPP_MACRO_ARG;
1245       token->val.arg_no = token->val.node->arg_index;
1246     }
1247   else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1248            && (token->type == CPP_STRING || token->type == CPP_CHAR))
1249     check_trad_stringification (pfile, macro, &token->val.str);
1250
1251   return token;
1252 }
1253
1254 /* Parse a macro and save its expansion.  Returns non-zero on success.  */
1255 int
1256 _cpp_create_definition (pfile, node)
1257      cpp_reader *pfile;
1258      cpp_hashnode *node;
1259 {
1260   cpp_macro *macro;
1261   cpp_token *token, *saved_cur_token;
1262   const cpp_token *ctoken;
1263   unsigned int i, ok = 1;
1264
1265   macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1266   macro->line = pfile->directive_line;
1267   macro->params = 0;
1268   macro->paramc = 0;
1269   macro->variadic = 0;
1270   macro->count = 0;
1271   macro->fun_like = 0;
1272
1273   /* Get the first token of the expansion (or the '(' of a
1274      function-like macro).  */
1275   ctoken = _cpp_lex_token (pfile);
1276
1277   if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1278     {
1279       ok = parse_params (pfile, macro);
1280       macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1281       if (!ok)
1282         goto cleanup2;
1283
1284       /* Success.  Commit the parameter array.  */
1285       BUFF_FRONT (pfile->a_buff) = (U_CHAR *) &macro->params[macro->paramc];
1286       macro->fun_like = 1;
1287     }
1288   else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1289     cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
1290
1291   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
1292   saved_cur_token = pfile->cur_token;
1293
1294   if (macro->fun_like)
1295     token = lex_expansion_token (pfile, macro);
1296   else
1297     {
1298       token = alloc_expansion_token (pfile, macro);
1299       *token = *ctoken;
1300     }
1301
1302   for (;;)
1303     {
1304       /* Check the stringifying # constraint 6.10.3.2.1 of
1305          function-like macros when lexing the subsequent token.  */
1306       if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1307         {
1308           if (token->type == CPP_MACRO_ARG)
1309             {
1310               token->flags &= ~PREV_WHITE;
1311               token->flags |= STRINGIFY_ARG;
1312               token->flags |= token[-1].flags & PREV_WHITE;
1313               token[-1] = token[0];
1314               macro->count--;
1315             }
1316           /* Let assembler get away with murder.  */
1317           else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1318             {
1319               ok = 0;
1320               cpp_error (pfile, "'#' is not followed by a macro parameter");
1321               goto cleanup1;
1322             }
1323         }
1324
1325       if (token->type == CPP_EOF)
1326         break;
1327
1328       /* Paste operator constraint 6.10.3.3.1.  */
1329       if (token->type == CPP_PASTE)
1330         {
1331           /* Token-paste ##, can appear in both object-like and
1332              function-like macros, but not at the ends.  */
1333           if (--macro->count > 0)
1334             token = lex_expansion_token (pfile, macro);
1335
1336           if (macro->count == 0 || token->type == CPP_EOF)
1337             {
1338               ok = 0;
1339               cpp_error (pfile,
1340                          "'##' cannot appear at either end of a macro expansion");
1341               goto cleanup1;
1342             }
1343
1344           token[-1].flags |= PASTE_LEFT;
1345         }
1346
1347       token = lex_expansion_token (pfile, macro);
1348     }
1349
1350   macro->expansion = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1351
1352   /* Don't count the CPP_EOF.  */
1353   macro->count--;
1354
1355   /* Clear whitespace on first token for macro equivalence purposes.  */
1356   if (macro->count)
1357     macro->expansion[0].flags &= ~PREV_WHITE;
1358
1359   /* Commit the memory.  */
1360   BUFF_FRONT (pfile->a_buff) = (U_CHAR *) &macro->expansion[macro->count];
1361
1362   /* Implement the macro-defined-to-itself optimisation.  */
1363   macro->disabled = (macro->count == 1 && !macro->fun_like
1364                      && macro->expansion[0].type == CPP_NAME
1365                      && macro->expansion[0].val.node == node);
1366
1367   /* To suppress some diagnostics.  */
1368   macro->syshdr = pfile->map->sysp != 0;
1369
1370   if (node->type != NT_VOID)
1371     {
1372       if (warn_of_redefinition (pfile, node, macro))
1373         {
1374           cpp_pedwarn_with_line (pfile, pfile->directive_line, 0,
1375                                  "\"%s\" redefined", NODE_NAME (node));
1376
1377           if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1378             cpp_pedwarn_with_line (pfile, node->value.macro->line, 0,
1379                             "this is the location of the previous definition");
1380         }
1381       _cpp_free_definition (node);
1382     }
1383
1384   /* Enter definition in hash table.  */
1385   node->type = NT_MACRO;
1386   node->value.macro = macro;
1387   if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1388     node->flags |= NODE_WARN;
1389
1390  cleanup1:
1391
1392   /* Set type for SEEN_EOL() in cpplib.c, restore the lexer position.  */
1393   saved_cur_token[-1].type = pfile->cur_token[-1].type;
1394   pfile->cur_token = saved_cur_token;
1395
1396  cleanup2:
1397
1398   /* Stop the lexer accepting __VA_ARGS__.  */
1399   pfile->state.va_args_ok = 0;
1400
1401   /* Clear the fast argument lookup indices.  */
1402   for (i = macro->paramc; i-- > 0; )
1403     macro->params[i]->arg_index = 0;
1404
1405   return ok;
1406 }
1407
1408 /* Warn if a token in `string' matches one of the function macro
1409    arguments in `info'.  This function assumes that the macro is a
1410    function macro and not an object macro.  */
1411 static void
1412 check_trad_stringification (pfile, macro, string)
1413      cpp_reader *pfile;
1414      const cpp_macro *macro;
1415      const cpp_string *string;
1416 {
1417   unsigned int i, len;
1418   const U_CHAR *p, *q, *limit = string->text + string->len;
1419   
1420   /* Loop over the string.  */
1421   for (p = string->text; p < limit; p = q)
1422     {
1423       /* Find the start of an identifier.  */
1424       while (p < limit && !is_idstart (*p))
1425         p++;
1426
1427       /* Find the end of the identifier.  */
1428       q = p;
1429       while (q < limit && is_idchar (*q))
1430         q++;
1431
1432       len = q - p;
1433
1434       /* Loop over the function macro arguments to see if the
1435          identifier inside the string matches one of them.  */
1436       for (i = 0; i < macro->paramc; i++)
1437         {
1438           const cpp_hashnode *node = macro->params[i];
1439
1440           if (NODE_LEN (node) == len
1441               && !memcmp (p, NODE_NAME (node), len))
1442             {
1443               cpp_warning (pfile,
1444            "macro argument \"%s\" would be stringified with -traditional.",
1445                            NODE_NAME (node));
1446               break;
1447             }
1448         }
1449     }
1450 }
1451
1452 /* Returns the name, arguments and expansion of a macro, in a format
1453    suitable to be read back in again, and therefore also for DWARF 2
1454    debugging info.  e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1455    Caller is expected to generate the "#define" bit if needed.  The
1456    returned text is temporary, and automatically freed later.  */
1457
1458 const unsigned char *
1459 cpp_macro_definition (pfile, node)
1460      cpp_reader *pfile;
1461      const cpp_hashnode *node;
1462 {
1463   unsigned int i, len;
1464   const cpp_macro *macro = node->value.macro;
1465   unsigned char *buffer;
1466
1467   if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1468     {
1469       cpp_ice (pfile, "invalid hash type %d in cpp_macro_definition", node->type);
1470       return 0;
1471     }
1472
1473   /* Calculate length.  */
1474   len = NODE_LEN (node) + 1;                    /* ' ' */
1475   if (macro->fun_like)
1476     {
1477       len += 3;         /* "()" plus possible final "." of named
1478                            varargs (we have + 2 below).  */
1479       for (i = 0; i < macro->paramc; i++)
1480         len += NODE_LEN (macro->params[i]) + 2; /* ", " */
1481     }
1482
1483   for (i = 0; i < macro->count; i++)
1484     {
1485       cpp_token *token = &macro->expansion[i];
1486
1487       if (token->type == CPP_MACRO_ARG)
1488         len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1489       else
1490         len += cpp_token_len (token); /* Includes room for ' '.  */
1491       if (token->flags & STRINGIFY_ARG)
1492         len++;                  /* "#" */
1493       if (token->flags & PASTE_LEFT)
1494         len += 3;               /* " ##" */
1495     }
1496
1497   if (len > pfile->macro_buffer_len)
1498     {
1499       pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len);
1500       pfile->macro_buffer_len = len;
1501     }
1502
1503   /* Fill in the buffer.  Start with the macro name.  */
1504   buffer = pfile->macro_buffer;
1505   memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1506   buffer += NODE_LEN (node);
1507
1508   /* Parameter names.  */
1509   if (macro->fun_like)
1510     {
1511       *buffer++ = '(';
1512       for (i = 0; i < macro->paramc; i++)
1513         {
1514           cpp_hashnode *param = macro->params[i];
1515
1516           if (param != pfile->spec_nodes.n__VA_ARGS__)
1517             {
1518               memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1519               buffer += NODE_LEN (param);
1520             }
1521
1522           if (i + 1 < macro->paramc)
1523             *buffer++ = ',', *buffer++ = ' ';
1524           else if (macro->variadic)
1525             *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1526         }
1527       *buffer++ = ')';
1528     }
1529
1530   /* Expansion tokens.  */
1531   if (macro->count)
1532     {
1533       *buffer++ = ' ';
1534       for (i = 0; i < macro->count; i++)
1535         {
1536           cpp_token *token = &macro->expansion[i];
1537
1538           if (token->flags & PREV_WHITE)
1539             *buffer++ = ' ';
1540           if (token->flags & STRINGIFY_ARG)
1541             *buffer++ = '#';
1542
1543           if (token->type == CPP_MACRO_ARG)
1544             {
1545               len = NODE_LEN (macro->params[token->val.arg_no - 1]);
1546               memcpy (buffer,
1547                       NODE_NAME (macro->params[token->val.arg_no - 1]), len);
1548               buffer += len;
1549             }
1550           else
1551             buffer = cpp_spell_token (pfile, token, buffer);
1552
1553           if (token->flags & PASTE_LEFT)
1554             {
1555               *buffer++ = ' ';
1556               *buffer++ = '#';
1557               *buffer++ = '#';
1558               /* Next has PREV_WHITE; see _cpp_create_definition.  */
1559             }
1560         }
1561     }
1562
1563   *buffer = '\0';
1564   return pfile->macro_buffer;
1565 }