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