OSDN Git Service

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