OSDN Git Service

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