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