OSDN Git Service

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