OSDN Git Service

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