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