OSDN Git Service

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