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