PR preprocessor/53229 - Fix diagnostics location when pasting tokens
[platform/upstream/gcc.git] / 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,
4    2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5    Written by Per Bothner, 1994.
6    Based on CCCP program by Paul Rubin, June 1986
7    Adapted to ANSI C, Richard Stallman, Jan 1987
8
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.
22
23  In other words, you are welcome to use, share and improve this program.
24  You are forbidden to forbid anyone else to use, share and improve
25  what you give them.   Help stamp out software-hoarding!  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "cpplib.h"
30 #include "internal.h"
31
32 typedef struct macro_arg macro_arg;
33 /* This structure represents the tokens of a macro argument.  These
34    tokens can be macro themselves, in which case they can be either
35    expanded or unexpanded.  When they are expanded, this data
36    structure keeps both the expanded and unexpanded forms.  */
37 struct macro_arg
38 {
39   const cpp_token **first;      /* First token in unexpanded argument.  */
40   const cpp_token **expanded;   /* Macro-expanded argument.  */
41   const cpp_token *stringified; /* Stringified argument.  */
42   unsigned int count;           /* # of tokens in argument.  */
43   unsigned int expanded_count;  /* # of tokens in expanded argument.  */
44   source_location *virt_locs;   /* Where virtual locations for
45                                    unexpanded tokens are stored.  */
46   source_location *expanded_virt_locs; /* Where virtual locations for
47                                           expanded tokens are
48                                           stored.  */
49 };
50
51 /* The kind of macro tokens which the instance of
52    macro_arg_token_iter is supposed to iterate over.  */
53 enum macro_arg_token_kind {
54   MACRO_ARG_TOKEN_NORMAL,
55   /* This is a macro argument token that got transformed into a string
56      litteral, e.g. #foo.  */
57   MACRO_ARG_TOKEN_STRINGIFIED,
58   /* This is a token resulting from the expansion of a macro
59      argument that was itself a macro.  */
60   MACRO_ARG_TOKEN_EXPANDED
61 };
62
63 /* An iterator over tokens coming from a function-like macro
64    argument.  */
65 typedef struct macro_arg_token_iter macro_arg_token_iter;
66 struct macro_arg_token_iter
67 {
68   /* Whether or not -ftrack-macro-expansion is used.  */
69   bool track_macro_exp_p;
70   /* The kind of token over which we are supposed to iterate.  */
71   enum macro_arg_token_kind kind;
72   /* A pointer to the current token pointed to by the iterator.  */
73   const cpp_token **token_ptr;
74   /* A pointer to the "full" location of the current token.  If
75      -ftrack-macro-expansion is used this location tracks loci accross
76      macro expansion.  */
77   const source_location *location_ptr;
78 #ifdef ENABLE_CHECKING
79   /* The number of times the iterator went forward. This useful only
80      when checking is enabled.  */
81   size_t num_forwards;
82 #endif
83 };
84
85 /* Macro expansion.  */
86
87 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
88                                 const cpp_token *, source_location);
89 static int builtin_macro (cpp_reader *, cpp_hashnode *);
90 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
91                                  const cpp_token **, unsigned int);
92 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
93                                           _cpp_buff *, source_location *,
94                                           const cpp_token **, unsigned int);
95 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
96                                 _cpp_buff **, unsigned *);
97 static cpp_context *next_context (cpp_reader *);
98 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
99 static void expand_arg (cpp_reader *, macro_arg *);
100 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
101 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
102 static void paste_all_tokens (cpp_reader *, const cpp_token *);
103 static bool paste_tokens (cpp_reader *, source_location,
104                           const cpp_token **, const cpp_token *);
105 static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
106 static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
107 static void delete_macro_args (_cpp_buff*, unsigned num_args);
108 static void set_arg_token (macro_arg *, const cpp_token *,
109                            source_location, size_t,
110                            enum macro_arg_token_kind,
111                            bool);
112 static const source_location *get_arg_token_location (const macro_arg *,
113                                                       enum macro_arg_token_kind);
114 static const cpp_token **arg_token_ptr_at (const macro_arg *,
115                                            size_t,
116                                            enum macro_arg_token_kind,
117                                            source_location **virt_location);
118
119 static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
120                                        enum macro_arg_token_kind,
121                                        const macro_arg *,
122                                        const cpp_token **);
123 static const cpp_token *macro_arg_token_iter_get_token
124 (const macro_arg_token_iter *it);
125 static source_location macro_arg_token_iter_get_location
126 (const macro_arg_token_iter *);
127 static void macro_arg_token_iter_forward (macro_arg_token_iter *);
128 static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
129                                    source_location **);
130 static size_t tokens_buff_count (_cpp_buff *);
131 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
132 static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
133                                                           source_location *,
134                                                           const cpp_token *,
135                                                           source_location,
136                                                           source_location,
137                                                           const struct line_map *,
138                                                           unsigned int);
139
140 static const cpp_token **tokens_buff_add_token (_cpp_buff *,
141                                                 source_location *,
142                                                 const cpp_token *,
143                                                 source_location,
144                                                 source_location,
145                                                 const struct line_map *,
146                                                 unsigned int);
147 static inline void tokens_buff_remove_last_token (_cpp_buff *);
148 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
149                           macro_arg *, source_location);
150 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
151                                         _cpp_buff **, unsigned *);
152 static bool create_iso_definition (cpp_reader *, cpp_macro *);
153
154 /* #define directive parsing and handling.  */
155
156 static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
157 static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
158 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
159                                   const cpp_macro *);
160 static bool parse_params (cpp_reader *, cpp_macro *);
161 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
162                                         const cpp_string *);
163 static bool reached_end_of_context (cpp_context *);
164 static void consume_next_token_from_context (cpp_reader *pfile,
165                                              const cpp_token **,
166                                              source_location *);
167 static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
168
169 static cpp_hashnode* macro_of_context (cpp_context *context);
170
171 static bool in_macro_expansion_p (cpp_reader *pfile);
172
173 /* Statistical counter tracking the number of macros that got
174    expanded.  */
175 unsigned num_expanded_macros_counter = 0;
176 /* Statistical counter tracking the total number tokens resulting
177    from macro expansion.  */
178 unsigned num_macro_tokens_counter = 0;
179
180 /* Emits a warning if NODE is a macro defined in the main file that
181    has not been used.  */
182 int
183 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
184                            void *v ATTRIBUTE_UNUSED)
185 {
186   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
187     {
188       cpp_macro *macro = node->value.macro;
189
190       if (!macro->used
191           && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
192         cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
193                                "macro \"%s\" is not used", NODE_NAME (node));
194     }
195
196   return 1;
197 }
198
199 /* Allocates and returns a CPP_STRING token, containing TEXT of length
200    LEN, after null-terminating it.  TEXT must be in permanent storage.  */
201 static const cpp_token *
202 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
203 {
204   cpp_token *token = _cpp_temp_token (pfile);
205
206   text[len] = '\0';
207   token->type = CPP_STRING;
208   token->val.str.len = len;
209   token->val.str.text = text;
210   token->flags = 0;
211   return token;
212 }
213
214 static const char * const monthnames[] =
215 {
216   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
217   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
218 };
219
220 /* Helper function for builtin_macro.  Returns the text generated by
221    a builtin macro. */
222 const uchar *
223 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
224 {
225   const uchar *result = NULL;
226   linenum_type number = 1;
227
228   switch (node->value.builtin)
229     {
230     default:
231       cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
232                  NODE_NAME (node));
233       break;
234
235     case BT_TIMESTAMP:
236       {
237         cpp_buffer *pbuffer = cpp_get_buffer (pfile);
238         if (pbuffer->timestamp == NULL)
239           {
240             /* Initialize timestamp value of the assotiated file. */
241             struct _cpp_file *file = cpp_get_file (pbuffer);
242             if (file)
243               {
244                 /* Generate __TIMESTAMP__ string, that represents 
245                    the date and time of the last modification 
246                    of the current source file. The string constant 
247                    looks like "Sun Sep 16 01:03:52 1973".  */
248                 struct tm *tb = NULL;
249                 struct stat *st = _cpp_get_file_stat (file);
250                 if (st)
251                   tb = localtime (&st->st_mtime);
252                 if (tb)
253                   {
254                     char *str = asctime (tb);
255                     size_t len = strlen (str);
256                     unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
257                     buf[0] = '"';
258                     strcpy ((char *) buf + 1, str);
259                     buf[len] = '"';
260                     pbuffer->timestamp = buf;
261                   }
262                 else
263                   {
264                     cpp_errno (pfile, CPP_DL_WARNING,
265                         "could not determine file timestamp");
266                     pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
267                   }
268               }
269           }
270         result = pbuffer->timestamp;
271       }
272       break;
273     case BT_FILE:
274     case BT_BASE_FILE:
275       {
276         unsigned int len;
277         const char *name;
278         uchar *buf;
279         
280         if (node->value.builtin == BT_FILE)
281           name = linemap_get_expansion_filename (pfile->line_table,
282                                                  pfile->line_table->highest_line);
283         else
284           {
285             name = _cpp_get_file_name (pfile->main_file);
286             if (!name)
287               abort ();
288           }
289         len = strlen (name);
290         buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
291         result = buf;
292         *buf = '"';
293         buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
294         *buf++ = '"';
295         *buf = '\0';
296       }
297       break;
298
299     case BT_INCLUDE_LEVEL:
300       /* The line map depth counts the primary source as level 1, but
301          historically __INCLUDE_DEPTH__ has called the primary source
302          level 0.  */
303       number = pfile->line_table->depth - 1;
304       break;
305
306     case BT_SPECLINE:
307       /* If __LINE__ is embedded in a macro, it must expand to the
308          line of the macro's invocation, not its definition.
309          Otherwise things like assert() will not work properly.  */
310       number = linemap_get_expansion_line (pfile->line_table,
311                                            CPP_OPTION (pfile, traditional)
312                                            ? pfile->line_table->highest_line
313                                            : pfile->cur_token[-1].src_loc);
314       break;
315
316       /* __STDC__ has the value 1 under normal circumstances.
317          However, if (a) we are in a system header, (b) the option
318          stdc_0_in_system_headers is true (set by target config), and
319          (c) we are not in strictly conforming mode, then it has the
320          value 0.  (b) and (c) are already checked in cpp_init_builtins.  */
321     case BT_STDC:
322       if (cpp_in_system_header (pfile))
323         number = 0;
324       else
325         number = 1;
326       break;
327
328     case BT_DATE:
329     case BT_TIME:
330       if (pfile->date == NULL)
331         {
332           /* Allocate __DATE__ and __TIME__ strings from permanent
333              storage.  We only do this once, and don't generate them
334              at init time, because time() and localtime() are very
335              slow on some systems.  */
336           time_t tt;
337           struct tm *tb = NULL;
338
339           /* (time_t) -1 is a legitimate value for "number of seconds
340              since the Epoch", so we have to do a little dance to
341              distinguish that from a genuine error.  */
342           errno = 0;
343           tt = time(NULL);
344           if (tt != (time_t)-1 || errno == 0)
345             tb = localtime (&tt);
346
347           if (tb)
348             {
349               pfile->date = _cpp_unaligned_alloc (pfile,
350                                                   sizeof ("\"Oct 11 1347\""));
351               sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
352                        monthnames[tb->tm_mon], tb->tm_mday,
353                        tb->tm_year + 1900);
354
355               pfile->time = _cpp_unaligned_alloc (pfile,
356                                                   sizeof ("\"12:34:56\""));
357               sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
358                        tb->tm_hour, tb->tm_min, tb->tm_sec);
359             }
360           else
361             {
362               cpp_errno (pfile, CPP_DL_WARNING,
363                          "could not determine date and time");
364                 
365               pfile->date = UC"\"??? ?? ????\"";
366               pfile->time = UC"\"??:??:??\"";
367             }
368         }
369
370       if (node->value.builtin == BT_DATE)
371         result = pfile->date;
372       else
373         result = pfile->time;
374       break;
375
376     case BT_COUNTER:
377       if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
378         cpp_error (pfile, CPP_DL_ERROR,
379             "__COUNTER__ expanded inside directive with -fdirectives-only");
380       number = pfile->counter++;
381       break;
382     }
383
384   if (result == NULL)
385     {
386       /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers.  */
387       result = _cpp_unaligned_alloc (pfile, 21);
388       sprintf ((char *) result, "%u", number);
389     }
390
391   return result;      
392 }
393
394 /* Convert builtin macros like __FILE__ to a token and push it on the
395    context stack.  Also handles _Pragma, for which a new token may not
396    be created.  Returns 1 if it generates a new token context, 0 to
397    return the token to the caller.  */
398 static int
399 builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
400 {
401   const uchar *buf;
402   size_t len;
403   char *nbuf;
404
405   if (node->value.builtin == BT_PRAGMA)
406     {
407       /* Don't interpret _Pragma within directives.  The standard is
408          not clear on this, but to me this makes most sense.  */
409       if (pfile->state.in_directive)
410         return 0;
411
412       return _cpp_do__Pragma (pfile);
413     }
414
415   buf = _cpp_builtin_macro_text (pfile, node);
416   len = ustrlen (buf);
417   nbuf = (char *) alloca (len + 1);
418   memcpy (nbuf, buf, len);
419   nbuf[len]='\n';
420
421   cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
422   _cpp_clean_line (pfile);
423
424   /* Set pfile->cur_token as required by _cpp_lex_direct.  */
425   pfile->cur_token = _cpp_temp_token (pfile);
426   _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
427   if (pfile->buffer->cur != pfile->buffer->rlimit)
428     cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
429                NODE_NAME (node));
430   _cpp_pop_buffer (pfile);
431
432   return 1;
433 }
434
435 /* Copies SRC, of length LEN, to DEST, adding backslashes before all
436    backslashes and double quotes. DEST must be of sufficient size.
437    Returns a pointer to the end of the string.  */
438 uchar *
439 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
440 {
441   while (len--)
442     {
443       uchar c = *src++;
444
445       if (c == '\\' || c == '"')
446         {
447           *dest++ = '\\';
448           *dest++ = c;
449         }
450       else
451           *dest++ = c;
452     }
453
454   return dest;
455 }
456
457 /* Convert a token sequence ARG to a single string token according to
458    the rules of the ISO C #-operator.  */
459 static const cpp_token *
460 stringify_arg (cpp_reader *pfile, macro_arg *arg)
461 {
462   unsigned char *dest;
463   unsigned int i, escape_it, backslash_count = 0;
464   const cpp_token *source = NULL;
465   size_t len;
466
467   if (BUFF_ROOM (pfile->u_buff) < 3)
468     _cpp_extend_buff (pfile, &pfile->u_buff, 3);
469   dest = BUFF_FRONT (pfile->u_buff);
470   *dest++ = '"';
471
472   /* Loop, reading in the argument's tokens.  */
473   for (i = 0; i < arg->count; i++)
474     {
475       const cpp_token *token = arg->first[i];
476
477       if (token->type == CPP_PADDING)
478         {
479           if (source == NULL
480               || (!(source->flags & PREV_WHITE)
481                   && token->val.source == NULL))
482             source = token->val.source;
483           continue;
484         }
485
486       escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
487                    || token->type == CPP_WSTRING || token->type == CPP_WCHAR
488                    || token->type == CPP_STRING32 || token->type == CPP_CHAR32
489                    || token->type == CPP_STRING16 || token->type == CPP_CHAR16
490                    || token->type == CPP_UTF8STRING);
491
492       /* Room for each char being written in octal, initial space and
493          final quote and NUL.  */
494       len = cpp_token_len (token);
495       if (escape_it)
496         len *= 4;
497       len += 3;
498
499       if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
500         {
501           size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
502           _cpp_extend_buff (pfile, &pfile->u_buff, len);
503           dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
504         }
505
506       /* Leading white space?  */
507       if (dest - 1 != BUFF_FRONT (pfile->u_buff))
508         {
509           if (source == NULL)
510             source = token;
511           if (source->flags & PREV_WHITE)
512             *dest++ = ' ';
513         }
514       source = NULL;
515
516       if (escape_it)
517         {
518           _cpp_buff *buff = _cpp_get_buff (pfile, len);
519           unsigned char *buf = BUFF_FRONT (buff);
520           len = cpp_spell_token (pfile, token, buf, true) - buf;
521           dest = cpp_quote_string (dest, buf, len);
522           _cpp_release_buff (pfile, buff);
523         }
524       else
525         dest = cpp_spell_token (pfile, token, dest, true);
526
527       if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
528         backslash_count++;
529       else
530         backslash_count = 0;
531     }
532
533   /* Ignore the final \ of invalid string literals.  */
534   if (backslash_count & 1)
535     {
536       cpp_error (pfile, CPP_DL_WARNING,
537                  "invalid string literal, ignoring final '\\'");
538       dest--;
539     }
540
541   /* Commit the memory, including NUL, and return the token.  */
542   *dest++ = '"';
543   len = dest - BUFF_FRONT (pfile->u_buff);
544   BUFF_FRONT (pfile->u_buff) = dest + 1;
545   return new_string_token (pfile, dest - len, len);
546 }
547
548 /* Try to paste two tokens.  On success, return nonzero.  In any
549    case, PLHS is updated to point to the pasted token, which is
550    guaranteed to not have the PASTE_LEFT flag set.  LOCATION is
551    the virtual location used for error reporting.  */
552 static bool
553 paste_tokens (cpp_reader *pfile, source_location location,
554               const cpp_token **plhs, const cpp_token *rhs)
555 {
556   unsigned char *buf, *end, *lhsend;
557   cpp_token *lhs;
558   unsigned int len;
559
560   len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
561   buf = (unsigned char *) alloca (len);
562   end = lhsend = cpp_spell_token (pfile, *plhs, buf, false);
563
564   /* Avoid comment headers, since they are still processed in stage 3.
565      It is simpler to insert a space here, rather than modifying the
566      lexer to ignore comments in some circumstances.  Simply returning
567      false doesn't work, since we want to clear the PASTE_LEFT flag.  */
568   if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
569     *end++ = ' ';
570   /* In one obscure case we might see padding here.  */
571   if (rhs->type != CPP_PADDING)
572     end = cpp_spell_token (pfile, rhs, end, false);
573   *end = '\n';
574
575   cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
576   _cpp_clean_line (pfile);
577
578   /* Set pfile->cur_token as required by _cpp_lex_direct.  */
579   pfile->cur_token = _cpp_temp_token (pfile);
580   lhs = _cpp_lex_direct (pfile);
581   if (pfile->buffer->cur != pfile->buffer->rlimit)
582     {
583       source_location saved_loc = lhs->src_loc;
584
585       _cpp_pop_buffer (pfile);
586       _cpp_backup_tokens (pfile, 1);
587       *lhsend = '\0';
588
589       /* We have to remove the PASTE_LEFT flag from the old lhs, but
590          we want to keep the new location.  */
591       *lhs = **plhs;
592       *plhs = lhs;
593       lhs->src_loc = saved_loc;
594       lhs->flags &= ~PASTE_LEFT;
595
596       /* Mandatory error for all apart from assembler.  */
597       if (CPP_OPTION (pfile, lang) != CLK_ASM)
598         cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
599          "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
600                    buf, cpp_token_as_text (pfile, rhs));
601       return false;
602     }
603
604   *plhs = lhs;
605   _cpp_pop_buffer (pfile);
606   return true;
607 }
608
609 /* Handles an arbitrarily long sequence of ## operators, with initial
610    operand LHS.  This implementation is left-associative,
611    non-recursive, and finishes a paste before handling succeeding
612    ones.  If a paste fails, we back up to the RHS of the failing ##
613    operator before pushing the context containing the result of prior
614    successful pastes, with the effect that the RHS appears in the
615    output stream after the pasted LHS normally.  */
616 static void
617 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
618 {
619   const cpp_token *rhs = NULL;
620   cpp_context *context = pfile->context;
621   source_location virt_loc = 0;
622
623   /* We are expanding a macro and we must have been called on a token
624      that appears at the left hand side of a ## operator.  */
625   if (macro_of_context (pfile->context) == NULL
626       || (!(lhs->flags & PASTE_LEFT)))
627     abort ();
628
629   if (context->tokens_kind == TOKENS_KIND_EXTENDED)
630     /* The caller must have called consume_next_token_from_context
631        right before calling us.  That has incremented the pointer to
632        the current virtual location.  So it now points to the location
633        of the token that comes right after *LHS.  We want the
634        resulting pasted token to have the location of the current
635        *LHS, though.  */
636     virt_loc = context->c.mc->cur_virt_loc[-1];
637   else
638     /* We are not tracking macro expansion.  So the best virtual
639        location we can get here is the expansion point of the macro we
640        are currently expanding.  */
641     virt_loc = pfile->invocation_location;
642
643   do
644     {
645       /* Take the token directly from the current context.  We can do
646          this, because we are in the replacement list of either an
647          object-like macro, or a function-like macro with arguments
648          inserted.  In either case, the constraints to #define
649          guarantee we have at least one more token.  */
650       if (context->tokens_kind == TOKENS_KIND_DIRECT)
651         rhs = FIRST (context).token++;
652       else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
653         rhs = *FIRST (context).ptoken++;
654       else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
655         {
656           /* So we are in presence of an extended token context, which
657              means that each token in this context has a virtual
658              location attached to it.  So let's not forget to update
659              the pointer to the current virtual location of the
660              current token when we update the pointer to the current
661              token */
662
663           rhs = *FIRST (context).ptoken++;
664           /* context->c.mc must be non-null, as if we were not in a
665              macro context, context->tokens_kind could not be equal to
666              TOKENS_KIND_EXTENDED.  */
667           context->c.mc->cur_virt_loc++;
668         }
669
670       if (rhs->type == CPP_PADDING)
671         {
672           if (rhs->flags & PASTE_LEFT)
673             abort ();
674         }
675       if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
676         break;
677     }
678   while (rhs->flags & PASTE_LEFT);
679
680   /* Put the resulting token in its own context.  */
681   if (context->tokens_kind == TOKENS_KIND_EXTENDED)
682     {
683       source_location *virt_locs = NULL;
684       _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
685       tokens_buff_add_token (token_buf, virt_locs, lhs,
686                              virt_loc, 0, NULL, 0);
687       push_extended_tokens_context (pfile, context->c.mc->macro_node,
688                                     token_buf, virt_locs,
689                                     (const cpp_token **)token_buf->base, 1);
690     }
691   else
692     _cpp_push_token_context (pfile, NULL, lhs, 1);
693 }
694
695 /* Returns TRUE if the number of arguments ARGC supplied in an
696    invocation of the MACRO referenced by NODE is valid.  An empty
697    invocation to a macro with no parameters should pass ARGC as zero.
698
699    Note that MACRO cannot necessarily be deduced from NODE, in case
700    NODE was redefined whilst collecting arguments.  */
701 bool
702 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
703 {
704   if (argc == macro->paramc)
705     return true;
706
707   if (argc < macro->paramc)
708     {
709       /* As an extension, a rest argument is allowed to not appear in
710          the invocation at all.
711          e.g. #define debug(format, args...) something
712          debug("string");
713
714          This is exactly the same as if there had been an empty rest
715          argument - debug("string", ).  */
716
717       if (argc + 1 == macro->paramc && macro->variadic)
718         {
719           if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
720             cpp_error (pfile, CPP_DL_PEDWARN,
721                        "ISO C99 requires rest arguments to be used");
722           return true;
723         }
724
725       cpp_error (pfile, CPP_DL_ERROR,
726                  "macro \"%s\" requires %u arguments, but only %u given",
727                  NODE_NAME (node), macro->paramc, argc);
728     }
729   else
730     cpp_error (pfile, CPP_DL_ERROR,
731                "macro \"%s\" passed %u arguments, but takes just %u",
732                NODE_NAME (node), argc, macro->paramc);
733
734   return false;
735 }
736
737 /* Reads and returns the arguments to a function-like macro
738    invocation.  Assumes the opening parenthesis has been processed.
739    If there is an error, emits an appropriate diagnostic and returns
740    NULL.  Each argument is terminated by a CPP_EOF token, for the
741    future benefit of expand_arg().  If there are any deferred
742    #pragma directives among macro arguments, store pointers to the
743    CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
744
745    What is returned is the buffer that contains the memory allocated
746    to hold the macro arguments.  NODE is the name of the macro this
747    function is dealing with.  If NUM_ARGS is non-NULL, *NUM_ARGS is
748    set to the actual number of macro arguments allocated in the
749    returned buffer.  */
750 static _cpp_buff *
751 collect_args (cpp_reader *pfile, const cpp_hashnode *node,
752               _cpp_buff **pragma_buff, unsigned *num_args)
753 {
754   _cpp_buff *buff, *base_buff;
755   cpp_macro *macro;
756   macro_arg *args, *arg;
757   const cpp_token *token;
758   unsigned int argc;
759   source_location virt_loc;
760   bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
761   unsigned num_args_alloced = 0;
762
763   macro = node->value.macro;
764   if (macro->paramc)
765     argc = macro->paramc;
766   else
767     argc = 1;
768
769 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
770 #define ARG_TOKENS_EXTENT 1000
771
772   buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
773                                        * sizeof (cpp_token *)
774                                        + sizeof (macro_arg)));
775   base_buff = buff;
776   args = (macro_arg *) buff->base;
777   memset (args, 0, argc * sizeof (macro_arg));
778   buff->cur = (unsigned char *) &args[argc];
779   arg = args, argc = 0;
780
781   /* Collect the tokens making up each argument.  We don't yet know
782      how many arguments have been supplied, whether too many or too
783      few.  Hence the slightly bizarre usage of "argc" and "arg".  */
784   do
785     {
786       unsigned int paren_depth = 0;
787       unsigned int ntokens = 0;
788       unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
789       num_args_alloced++;
790
791       argc++;
792       arg->first = (const cpp_token **) buff->cur;
793       if (track_macro_expansion_p)
794         {
795           virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
796           arg->virt_locs = XNEWVEC (source_location,
797                                     virt_locs_capacity);
798         }
799
800       for (;;)
801         {
802           /* Require space for 2 new tokens (including a CPP_EOF).  */
803           if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
804             {
805               buff = _cpp_append_extend_buff (pfile, buff,
806                                               ARG_TOKENS_EXTENT
807                                               * sizeof (cpp_token *));
808               arg->first = (const cpp_token **) buff->cur;
809             }
810           if (track_macro_expansion_p
811               && (ntokens + 2 > virt_locs_capacity))
812             {
813               virt_locs_capacity += ARG_TOKENS_EXTENT;
814               arg->virt_locs = XRESIZEVEC (source_location,
815                                            arg->virt_locs,
816                                            virt_locs_capacity);
817             }
818
819           token = cpp_get_token_1 (pfile, &virt_loc);
820
821           if (token->type == CPP_PADDING)
822             {
823               /* Drop leading padding.  */
824               if (ntokens == 0)
825                 continue;
826             }
827           else if (token->type == CPP_OPEN_PAREN)
828             paren_depth++;
829           else if (token->type == CPP_CLOSE_PAREN)
830             {
831               if (paren_depth-- == 0)
832                 break;
833             }
834           else if (token->type == CPP_COMMA)
835             {
836               /* A comma does not terminate an argument within
837                  parentheses or as part of a variable argument.  */
838               if (paren_depth == 0
839                   && ! (macro->variadic && argc == macro->paramc))
840                 break;
841             }
842           else if (token->type == CPP_EOF
843                    || (token->type == CPP_HASH && token->flags & BOL))
844             break;
845           else if (token->type == CPP_PRAGMA)
846             {
847               cpp_token *newtok = _cpp_temp_token (pfile);
848
849               /* CPP_PRAGMA token lives in directive_result, which will
850                  be overwritten on the next directive.  */
851               *newtok = *token;
852               token = newtok;
853               do
854                 {
855                   if (*pragma_buff == NULL
856                       || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
857                     {
858                       _cpp_buff *next;
859                       if (*pragma_buff == NULL)
860                         *pragma_buff
861                           = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
862                       else
863                         {
864                           next = *pragma_buff;
865                           *pragma_buff
866                             = _cpp_get_buff (pfile,
867                                              (BUFF_FRONT (*pragma_buff)
868                                               - (*pragma_buff)->base) * 2);
869                           (*pragma_buff)->next = next;
870                         }
871                     }
872                   *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
873                   BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
874                   if (token->type == CPP_PRAGMA_EOL)
875                     break;
876                   token = cpp_get_token_1 (pfile, &virt_loc);
877                 }
878               while (token->type != CPP_EOF);
879
880               /* In deferred pragmas parsing_args and prevent_expansion
881                  had been changed, reset it.  */
882               pfile->state.parsing_args = 2;
883               pfile->state.prevent_expansion = 1;
884
885               if (token->type == CPP_EOF)
886                 break;
887               else
888                 continue;
889             }
890           set_arg_token (arg, token, virt_loc,
891                          ntokens, MACRO_ARG_TOKEN_NORMAL,
892                          CPP_OPTION (pfile, track_macro_expansion));
893           ntokens++;
894         }
895
896       /* Drop trailing padding.  */
897       while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
898         ntokens--;
899
900       arg->count = ntokens;
901       set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
902                      ntokens, MACRO_ARG_TOKEN_NORMAL,
903                      CPP_OPTION (pfile, track_macro_expansion));
904
905       /* Terminate the argument.  Excess arguments loop back and
906          overwrite the final legitimate argument, before failing.  */
907       if (argc <= macro->paramc)
908         {
909           buff->cur = (unsigned char *) &arg->first[ntokens + 1];
910           if (argc != macro->paramc)
911             arg++;
912         }
913     }
914   while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
915
916   if (token->type == CPP_EOF)
917     {
918       /* We still need the CPP_EOF to end directives, and to end
919          pre-expansion of a macro argument.  Step back is not
920          unconditional, since we don't want to return a CPP_EOF to our
921          callers at the end of an -include-d file.  */
922       if (pfile->context->prev || pfile->state.in_directive)
923         _cpp_backup_tokens (pfile, 1);
924       cpp_error (pfile, CPP_DL_ERROR,
925                  "unterminated argument list invoking macro \"%s\"",
926                  NODE_NAME (node));
927     }
928   else
929     {
930       /* A single empty argument is counted as no argument.  */
931       if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
932         argc = 0;
933       if (_cpp_arguments_ok (pfile, macro, node, argc))
934         {
935           /* GCC has special semantics for , ## b where b is a varargs
936              parameter: we remove the comma if b was omitted entirely.
937              If b was merely an empty argument, the comma is retained.
938              If the macro takes just one (varargs) parameter, then we
939              retain the comma only if we are standards conforming.
940
941              If FIRST is NULL replace_args () swallows the comma.  */
942           if (macro->variadic && (argc < macro->paramc
943                                   || (argc == 1 && args[0].count == 0
944                                       && !CPP_OPTION (pfile, std))))
945             args[macro->paramc - 1].first = NULL;
946           if (num_args)
947             *num_args = num_args_alloced;
948           return base_buff;
949         }
950     }
951
952   /* An error occurred.  */
953   _cpp_release_buff (pfile, base_buff);
954   return NULL;
955 }
956
957 /* Search for an opening parenthesis to the macro of NODE, in such a
958    way that, if none is found, we don't lose the information in any
959    intervening padding tokens.  If we find the parenthesis, collect
960    the arguments and return the buffer containing them.  PRAGMA_BUFF
961    argument is the same as in collect_args.  If NUM_ARGS is non-NULL,
962    *NUM_ARGS is set to the number of arguments contained in the
963    returned buffer.  */
964 static _cpp_buff *
965 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
966                       _cpp_buff **pragma_buff, unsigned *num_args)
967 {
968   const cpp_token *token, *padding = NULL;
969
970   for (;;)
971     {
972       token = cpp_get_token (pfile);
973       if (token->type != CPP_PADDING)
974         break;
975       if (padding == NULL
976           || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
977         padding = token;
978     }
979
980   if (token->type == CPP_OPEN_PAREN)
981     {
982       pfile->state.parsing_args = 2;
983       return collect_args (pfile, node, pragma_buff, num_args);
984     }
985
986   /* CPP_EOF can be the end of macro arguments, or the end of the
987      file.  We mustn't back up over the latter.  Ugh.  */
988   if (token->type != CPP_EOF || token == &pfile->eof)
989     {
990       /* Back up.  We may have skipped padding, in which case backing
991          up more than one token when expanding macros is in general
992          too difficult.  We re-insert it in its own context.  */
993       _cpp_backup_tokens (pfile, 1);
994       if (padding)
995         _cpp_push_token_context (pfile, NULL, padding, 1);
996     }
997
998   return NULL;
999 }
1000
1001 /* Return the real number of tokens in the expansion of MACRO.  */
1002 static inline unsigned int
1003 macro_real_token_count (const cpp_macro *macro)
1004 {
1005   unsigned int i;
1006   if (__builtin_expect (!macro->extra_tokens, true))
1007     return macro->count;
1008   for (i = 0; i < macro->count; i++)
1009     if (macro->exp.tokens[i].type == CPP_PASTE)
1010       return i;
1011   abort ();
1012 }
1013
1014 /* Push the context of a macro with hash entry NODE onto the context
1015    stack.  If we can successfully expand the macro, we push a context
1016    containing its yet-to-be-rescanned replacement list and return one.
1017    If there were additionally any unexpanded deferred #pragma
1018    directives among macro arguments, push another context containing
1019    the pragma tokens before the yet-to-be-rescanned replacement list
1020    and return two.  Otherwise, we don't push a context and return
1021    zero. LOCATION is the location of the expansion point of the
1022    macro.  */
1023 static int
1024 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
1025                      const cpp_token *result, source_location location)
1026 {
1027   /* The presence of a macro invalidates a file's controlling macro.  */
1028   pfile->mi_valid = false;
1029
1030   pfile->state.angled_headers = false;
1031
1032   /* From here to when we push the context for the macro later down
1033      this function, we need to flag the fact that we are about to
1034      expand a macro.  This is useful when -ftrack-macro-expansion is
1035      turned off.  In that case, we need to record the location of the
1036      expansion point of the top-most macro we are about to to expand,
1037      into pfile->invocation_location.  But we must not record any such
1038      location once the process of expanding the macro starts; that is,
1039      we must not do that recording between now and later down this
1040      function where set this flag to FALSE.  */
1041   pfile->about_to_expand_macro_p = true;
1042
1043   if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
1044     {
1045       node->flags |= NODE_USED;
1046       if ((!pfile->cb.user_builtin_macro
1047            || !pfile->cb.user_builtin_macro (pfile, node))
1048           && pfile->cb.used_define)
1049         pfile->cb.used_define (pfile, pfile->directive_line, node);
1050     }
1051
1052   /* Handle standard macros.  */
1053   if (! (node->flags & NODE_BUILTIN))
1054     {
1055       cpp_macro *macro = node->value.macro;
1056       _cpp_buff *pragma_buff = NULL;
1057
1058       if (macro->fun_like)
1059         {
1060           _cpp_buff *buff;
1061           unsigned num_args = 0;
1062
1063           pfile->state.prevent_expansion++;
1064           pfile->keep_tokens++;
1065           pfile->state.parsing_args = 1;
1066           buff = funlike_invocation_p (pfile, node, &pragma_buff,
1067                                        &num_args);
1068           pfile->state.parsing_args = 0;
1069           pfile->keep_tokens--;
1070           pfile->state.prevent_expansion--;
1071
1072           if (buff == NULL)
1073             {
1074               if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1075                 cpp_warning (pfile, CPP_W_TRADITIONAL,
1076  "function-like macro \"%s\" must be used with arguments in traditional C",
1077                              NODE_NAME (node));
1078
1079               if (pragma_buff)
1080                 _cpp_release_buff (pfile, pragma_buff);
1081
1082               pfile->about_to_expand_macro_p = false;
1083               return 0;
1084             }
1085
1086           if (macro->paramc > 0)
1087             replace_args (pfile, node, macro,
1088                           (macro_arg *) buff->base,
1089                           location);
1090           /* Free the memory used by the arguments of this
1091              function-like macro.  This memory has been allocated by
1092              funlike_invocation_p and by replace_args.  */
1093           delete_macro_args (buff, num_args);
1094         }
1095
1096       /* Disable the macro within its expansion.  */
1097       node->flags |= NODE_DISABLED;
1098
1099       if (!(node->flags & NODE_USED))
1100         {
1101           node->flags |= NODE_USED;
1102           if (pfile->cb.used_define)
1103             pfile->cb.used_define (pfile, pfile->directive_line, node);
1104         }
1105
1106       if (pfile->cb.used)
1107         pfile->cb.used (pfile, location, node);
1108
1109       macro->used = 1;
1110
1111       if (macro->paramc == 0)
1112         {
1113           if (CPP_OPTION (pfile, track_macro_expansion))
1114             {
1115               unsigned int i, count = macro->count;
1116               const cpp_token *src = macro->exp.tokens;
1117               const struct line_map *map;
1118               source_location *virt_locs = NULL;
1119               _cpp_buff *macro_tokens =
1120                 tokens_buff_new (pfile, count, &virt_locs);
1121
1122               /* Create a macro map to record the locations of the
1123                  tokens that are involved in the expansion. LOCATION
1124                  is the location of the macro expansion point.  */
1125               map  = linemap_enter_macro (pfile->line_table,
1126                                           node, location, count);
1127               for (i = 0; i < count; ++i)
1128                 {
1129                   tokens_buff_add_token (macro_tokens, virt_locs,
1130                                          src, src->src_loc,
1131                                          src->src_loc, map, i);
1132                   ++src;
1133                 }
1134               push_extended_tokens_context (pfile, node,
1135                                             macro_tokens,
1136                                             virt_locs,
1137                                             (const cpp_token **)
1138                                             macro_tokens->base,
1139                                             count);
1140               num_macro_tokens_counter += count;
1141             }
1142           else
1143             {
1144               unsigned tokens_count = macro_real_token_count (macro);
1145               _cpp_push_token_context (pfile, node, macro->exp.tokens,
1146                                        tokens_count);
1147               num_macro_tokens_counter += tokens_count;
1148             }
1149         }
1150
1151       if (pragma_buff)
1152         {
1153           if (!pfile->state.in_directive)
1154             _cpp_push_token_context (pfile, NULL,
1155                                      padding_token (pfile, result), 1);
1156           do
1157             {
1158               unsigned tokens_count;
1159               _cpp_buff *tail = pragma_buff->next;
1160               pragma_buff->next = NULL;
1161               tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1162                               - (const cpp_token **) pragma_buff->base);
1163               push_ptoken_context (pfile, NULL, pragma_buff,
1164                                    (const cpp_token **) pragma_buff->base,
1165                                    tokens_count);
1166               pragma_buff = tail;
1167               if (!CPP_OPTION (pfile, track_macro_expansion))
1168                 num_macro_tokens_counter += tokens_count;
1169
1170             }
1171           while (pragma_buff != NULL);
1172           pfile->about_to_expand_macro_p = false;
1173           return 2;
1174         }
1175
1176       pfile->about_to_expand_macro_p = false;
1177       return 1;
1178     }
1179
1180   pfile->about_to_expand_macro_p = false;
1181   /* Handle built-in macros and the _Pragma operator.  */
1182   return builtin_macro (pfile, node);
1183 }
1184
1185 /* De-allocate the memory used by BUFF which is an array of instances
1186    of macro_arg.  NUM_ARGS is the number of instances of macro_arg
1187    present in BUFF.  */
1188 static void
1189 delete_macro_args (_cpp_buff *buff, unsigned num_args)
1190 {
1191   macro_arg *macro_args;
1192   unsigned i;
1193
1194   if (buff == NULL)
1195     return;
1196
1197   macro_args = (macro_arg *) buff->base;
1198
1199   /* Walk instances of macro_arg to free their expanded tokens as well
1200      as their macro_arg::virt_locs members.  */
1201   for (i = 0; i < num_args; ++i)
1202     {
1203       if (macro_args[i].expanded)
1204         {
1205           free (macro_args[i].expanded);
1206           macro_args[i].expanded = NULL;
1207         }
1208       if (macro_args[i].virt_locs)
1209         {
1210           free (macro_args[i].virt_locs);
1211           macro_args[i].virt_locs = NULL;
1212         }
1213       if (macro_args[i].expanded_virt_locs)
1214         {
1215           free (macro_args[i].expanded_virt_locs);
1216           macro_args[i].expanded_virt_locs = NULL;
1217         }
1218     }
1219   _cpp_free_buff (buff);
1220 }
1221
1222 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1223    to set, LOCATION is its virtual location.  "Virtual" location means
1224    the location that encodes loci accross macro expansion. Otherwise
1225    it has to be TOKEN->SRC_LOC.  KIND is the kind of tokens the
1226    argument ARG is supposed to contain.  Note that ARG must be
1227    tailored so that it has enough room to contain INDEX + 1 numbers of
1228    tokens, at least.  */
1229 static void
1230 set_arg_token (macro_arg *arg, const cpp_token *token,
1231                source_location location, size_t index,
1232                enum macro_arg_token_kind kind,
1233                bool track_macro_exp_p)
1234 {
1235   const cpp_token **token_ptr;
1236   source_location *loc = NULL;
1237
1238   token_ptr =
1239     arg_token_ptr_at (arg, index, kind,
1240                       track_macro_exp_p ? &loc : NULL);
1241   *token_ptr = token;
1242
1243   if (loc != NULL)
1244     {
1245 #ifdef ENABLE_CHECKING
1246       if (kind == MACRO_ARG_TOKEN_STRINGIFIED
1247           || !track_macro_exp_p)
1248         /* We can't set the location of a stringified argument
1249            token and we can't set any location if we aren't tracking
1250            macro expansion locations.   */
1251         abort ();
1252 #endif
1253       *loc = location;
1254     }
1255 }
1256
1257 /* Get the pointer to the location of the argument token of the
1258    function-like macro argument ARG.  This function must be called
1259    only when we -ftrack-macro-expansion is on.  */
1260 static const source_location *
1261 get_arg_token_location (const macro_arg *arg,
1262                         enum macro_arg_token_kind kind)
1263 {
1264   const source_location *loc = NULL;
1265   const cpp_token **token_ptr =
1266     arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1267
1268   if (token_ptr == NULL)
1269     return NULL;
1270
1271   return loc;
1272 }
1273
1274 /* Return the pointer to the INDEXth token of the macro argument ARG.
1275    KIND specifies the kind of token the macro argument ARG contains.
1276    If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1277    of the virtual location of the returned token if the
1278    -ftrack-macro-expansion flag is on; otherwise, it's set to the
1279    spelling location of the returned token.  */
1280 static const cpp_token **
1281 arg_token_ptr_at (const macro_arg *arg, size_t index,
1282                   enum macro_arg_token_kind kind,
1283                   source_location **virt_location)
1284 {
1285   const cpp_token **tokens_ptr = NULL;
1286
1287   switch (kind)
1288     {
1289     case MACRO_ARG_TOKEN_NORMAL:
1290       tokens_ptr = arg->first;
1291       break;
1292     case MACRO_ARG_TOKEN_STRINGIFIED:      
1293       tokens_ptr = (const cpp_token **) &arg->stringified;
1294       break;
1295     case MACRO_ARG_TOKEN_EXPANDED:
1296         tokens_ptr = arg->expanded;
1297       break;
1298     }
1299
1300   if (tokens_ptr == NULL)
1301     /* This can happen for e.g, an empty token argument to a
1302        funtion-like macro.  */
1303     return tokens_ptr;
1304
1305   if (virt_location)
1306     {
1307       if (kind == MACRO_ARG_TOKEN_NORMAL)
1308         *virt_location = &arg->virt_locs[index];
1309       else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1310         *virt_location = &arg->expanded_virt_locs[index];
1311       else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1312         *virt_location =
1313           (source_location *) &tokens_ptr[index]->src_loc;
1314     }
1315   return &tokens_ptr[index];
1316 }
1317
1318 /* Initialize an iterator so that it iterates over the tokens of a
1319    function-like macro argument.  KIND is the kind of tokens we want
1320    ITER to iterate over. TOKEN_PTR points the first token ITER will
1321    iterate over.  */
1322 static void
1323 macro_arg_token_iter_init (macro_arg_token_iter *iter,
1324                            bool track_macro_exp_p,
1325                            enum macro_arg_token_kind kind,
1326                            const macro_arg *arg,
1327                            const cpp_token **token_ptr)
1328 {
1329   iter->track_macro_exp_p = track_macro_exp_p;
1330   iter->kind = kind;
1331   iter->token_ptr = token_ptr;
1332   /* Unconditionally initialize this so that the compiler doesn't warn
1333      about iter->location_ptr being possibly uninitialized later after
1334      this code has been inlined somewhere.  */
1335   iter->location_ptr = NULL;
1336   if (track_macro_exp_p)
1337     iter->location_ptr = get_arg_token_location (arg, kind);
1338 #ifdef ENABLE_CHECKING
1339   iter->num_forwards = 0;
1340   if (track_macro_exp_p
1341       && token_ptr != NULL
1342       && iter->location_ptr == NULL)
1343     abort ();
1344 #endif
1345 }
1346
1347 /* Move the iterator one token forward. Note that if IT was
1348    initialized on an argument that has a stringified token, moving it
1349    foward doesn't make sense as a stringified token is essentially one
1350    string.  */
1351 static void
1352 macro_arg_token_iter_forward (macro_arg_token_iter *it)
1353 {
1354   switch (it->kind)
1355     {
1356     case MACRO_ARG_TOKEN_NORMAL:
1357     case MACRO_ARG_TOKEN_EXPANDED:
1358       it->token_ptr++;
1359       if (it->track_macro_exp_p)
1360         it->location_ptr++;
1361       break;
1362     case MACRO_ARG_TOKEN_STRINGIFIED:
1363 #ifdef ENABLE_CHECKING
1364       if (it->num_forwards > 0)
1365         abort ();
1366 #endif
1367       break;
1368     }
1369
1370 #ifdef ENABLE_CHECKING
1371   it->num_forwards++;
1372 #endif
1373 }
1374
1375 /* Return the token pointed to by the iterator.  */
1376 static const cpp_token *
1377 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1378 {
1379 #ifdef ENABLE_CHECKING
1380   if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1381       && it->num_forwards > 0)
1382     abort ();
1383 #endif
1384   if (it->token_ptr == NULL)
1385     return NULL;
1386   return *it->token_ptr;
1387 }
1388
1389 /* Return the location of the token pointed to by the iterator.*/
1390 static source_location
1391 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1392 {
1393 #ifdef ENABLE_CHECKING
1394   if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1395       && it->num_forwards > 0)
1396     abort ();
1397 #endif
1398   if (it->track_macro_exp_p)
1399     return *it->location_ptr;
1400   else
1401     return (*it->token_ptr)->src_loc;
1402 }
1403
1404 /* Return the index of a token [resulting from macro expansion] inside
1405    the total list of tokens resulting from a given macro
1406    expansion. The index can be different depending on whether if we
1407    want each tokens resulting from function-like macro arguments
1408    expansion to have a different location or not.
1409
1410    E.g, consider this function-like macro: 
1411
1412         #define M(x) x - 3
1413
1414    Then consider us "calling" it (and thus expanding it) like:
1415    
1416        M(1+4)
1417
1418    It will be expanded into:
1419
1420        1+4-3
1421
1422    Let's consider the case of the token '4'.
1423
1424    Its index can be 2 (it's the third token of the set of tokens
1425    resulting from the expansion) or it can be 0 if we consider that
1426    all tokens resulting from the expansion of the argument "1+2" have
1427    the same index, which is 0. In this later case, the index of token
1428    '-' would then be 1 and the index of token '3' would be 2.
1429
1430    The later case is useful to use less memory e.g, for the case of
1431    the user using the option -ftrack-macro-expansion=1.
1432
1433    ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1434    are interested in.  CUR_REPLACEMENT_TOKEN is the token of the macro
1435    parameter (inside the macro replacement list) that corresponds to
1436    the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1437    of.
1438
1439    If we refer to the example above, for the '4' argument token,
1440    ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1441    would be set to the token 'x', in the replacement list "x - 3" of
1442    macro M.
1443
1444    This is a subroutine of replace_args.  */
1445 inline static unsigned
1446 expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1447                       const cpp_token *cur_replacement_token,
1448                       unsigned absolute_token_index)
1449 {
1450   if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1451     return absolute_token_index;
1452   return cur_replacement_token - macro->exp.tokens;
1453 }
1454
1455 /* Replace the parameters in a function-like macro of NODE with the
1456    actual ARGS, and place the result in a newly pushed token context.
1457    Expand each argument before replacing, unless it is operated upon
1458    by the # or ## operators. EXPANSION_POINT_LOC is the location of
1459    the expansion point of the macro. E.g, the location of the
1460    function-like macro invocation.  */
1461 static void
1462 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1463               macro_arg *args, source_location expansion_point_loc)
1464 {
1465   unsigned int i, total;
1466   const cpp_token *src, *limit;
1467   const cpp_token **first = NULL;
1468   macro_arg *arg;
1469   _cpp_buff *buff = NULL;
1470   source_location *virt_locs = NULL;
1471   unsigned int exp_count;
1472   const struct line_map *map = NULL;
1473   int track_macro_exp;
1474
1475   /* First, fully macro-expand arguments, calculating the number of
1476      tokens in the final expansion as we go.  The ordering of the if
1477      statements below is subtle; we must handle stringification before
1478      pasting.  */
1479
1480   /* EXP_COUNT is the number of tokens in the macro replacement
1481      list.  TOTAL is the number of tokens /after/ macro parameters
1482      have been replaced by their arguments.   */
1483   exp_count = macro_real_token_count (macro);
1484   total = exp_count;
1485   limit = macro->exp.tokens + exp_count;
1486
1487   for (src = macro->exp.tokens; src < limit; src++)
1488     if (src->type == CPP_MACRO_ARG)
1489       {
1490         /* Leading and trailing padding tokens.  */
1491         total += 2;
1492         /* Account for leading and padding tokens in exp_count too.
1493            This is going to be important later down this function,
1494            when we want to handle the case of (track_macro_exp <
1495            2).  */
1496         exp_count += 2;
1497
1498         /* We have an argument.  If it is not being stringified or
1499            pasted it is macro-replaced before insertion.  */
1500         arg = &args[src->val.macro_arg.arg_no - 1];
1501
1502         if (src->flags & STRINGIFY_ARG)
1503           {
1504             if (!arg->stringified)
1505               arg->stringified = stringify_arg (pfile, arg);
1506           }
1507         else if ((src->flags & PASTE_LEFT)
1508                  || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1509           total += arg->count - 1;
1510         else
1511           {
1512             if (!arg->expanded)
1513               expand_arg (pfile, arg);
1514             total += arg->expanded_count - 1;
1515           }
1516       }
1517
1518   /* When the compiler is called with the -ftrack-macro-expansion
1519      flag, we need to keep track of the location of each token that
1520      results from macro expansion.
1521
1522      A token resulting from macro expansion is not a new token. It is
1523      simply the same token as the token coming from the macro
1524      definition.  The new things that are allocated are the buffer
1525      that holds the tokens resulting from macro expansion and a new
1526      location that records many things like the locus of the expansion
1527      point as well as the original locus inside the definition of the
1528      macro.  This location is called a virtual location.
1529      
1530      So the buffer BUFF holds a set of cpp_token*, and the buffer
1531      VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1532
1533      Both of these two buffers are going to be hung off of the macro
1534      context, when the latter is pushed.  The memory allocated to
1535      store the tokens and their locations is going to be freed once
1536      the context of macro expansion is popped.
1537      
1538      As far as tokens are concerned, the memory overhead of
1539      -ftrack-macro-expansion is proportional to the number of
1540      macros that get expanded multiplied by sizeof (source_location).
1541      The good news is that extra memory gets freed when the macro
1542      context is freed, i.e shortly after the macro got expanded.  */
1543
1544   /* Is the -ftrack-macro-expansion flag in effect?  */
1545   track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1546
1547   /* Now allocate memory space for tokens and locations resulting from
1548      the macro expansion, copy the tokens and replace the arguments.
1549      This memory must be freed when the context of the macro MACRO is
1550      popped.  */
1551   buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1552
1553   first = (const cpp_token **) buff->base;
1554
1555   /* Create a macro map to record the locations of the tokens that are
1556      involved in the expansion.  Note that the expansion point is set
1557      to the location of the closing parenthesis.  Otherwise, the
1558      subsequent map created for the first token that comes after the
1559      macro map might have a wrong line number.  That would lead to
1560      tokens with wrong line numbers after the macro expansion.  This
1561      adds up to the memory overhead of the -ftrack-macro-expansion
1562      flag; for every macro that is expanded, a "macro map" is
1563      created.  */
1564   if (track_macro_exp)
1565     {
1566       int num_macro_tokens = total;
1567       if (track_macro_exp < 2)
1568         /* Then the number of macro tokens won't take in account the
1569            fact that function-like macro arguments can expand to
1570            multiple tokens. This is to save memory at the expense of
1571            accuracy.
1572
1573            Suppose we have #define SQARE(A) A * A
1574
1575            And then we do SQARE(2+3)
1576
1577            Then the tokens 2, +, 3, will have the same location,
1578            saying they come from the expansion of the argument A.  */
1579         num_macro_tokens = exp_count;
1580       map = linemap_enter_macro (pfile->line_table, node,
1581                                  expansion_point_loc,
1582                                  num_macro_tokens);
1583     }
1584   i = 0;
1585   for (src = macro->exp.tokens; src < limit; src++)
1586     {
1587       unsigned int arg_tokens_count;
1588       macro_arg_token_iter from;
1589       const cpp_token **paste_flag = NULL;
1590       const cpp_token **tmp_token_ptr;
1591
1592       if (src->type != CPP_MACRO_ARG)
1593         {
1594           /* Allocate a virtual location for token SRC, and add that
1595              token and its virtual location into the buffers BUFF and
1596              VIRT_LOCS.  */
1597           unsigned index = expanded_token_index (pfile, macro, src, i);
1598           tokens_buff_add_token (buff, virt_locs, src,
1599                                  src->src_loc, src->src_loc,
1600                                  map, index);
1601           i += 1;
1602           continue;
1603         }
1604
1605       paste_flag = 0;
1606       arg = &args[src->val.macro_arg.arg_no - 1];
1607       /* SRC is a macro parameter that we need to replace with its
1608          corresponding argument.  So at some point we'll need to
1609          iterate over the tokens of the macro argument and copy them
1610          into the "place" now holding the correspondig macro
1611          parameter.  We are going to use the iterator type
1612          macro_argo_token_iter to handle that iterating.  The 'if'
1613          below is to initialize the iterator depending on the type of
1614          tokens the macro argument has.  It also does some adjustment
1615          related to padding tokens and some pasting corner cases.  */
1616       if (src->flags & STRINGIFY_ARG)
1617         {
1618           arg_tokens_count = 1;
1619           macro_arg_token_iter_init (&from,
1620                                      CPP_OPTION (pfile,
1621                                                  track_macro_expansion),
1622                                      MACRO_ARG_TOKEN_STRINGIFIED,
1623                                      arg, &arg->stringified);
1624         }
1625       else if (src->flags & PASTE_LEFT)
1626         {
1627           arg_tokens_count = arg->count;
1628           macro_arg_token_iter_init (&from,
1629                                      CPP_OPTION (pfile,
1630                                                  track_macro_expansion),
1631                                      MACRO_ARG_TOKEN_NORMAL,
1632                                      arg, arg->first);
1633         }
1634       else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
1635         {
1636           int num_toks;
1637           arg_tokens_count = arg->count;
1638           macro_arg_token_iter_init (&from,
1639                                      CPP_OPTION (pfile,
1640                                                  track_macro_expansion),
1641                                      MACRO_ARG_TOKEN_NORMAL,
1642                                      arg, arg->first);
1643
1644           num_toks = tokens_buff_count (buff);
1645
1646           if (num_toks != 0)
1647             {
1648               /* So the current parameter token is pasted to the previous
1649                  token in the replacement list.  Let's look at what
1650                  we have as previous and current arguments.  */
1651
1652               /* This is the previous argument's token ...  */
1653               tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1654
1655               if ((*tmp_token_ptr)->type == CPP_COMMA
1656                   && macro->variadic
1657                   && src->val.macro_arg.arg_no == macro->paramc)
1658                 {
1659                   /* ... which is a comma; and the current parameter
1660                      is the last parameter of a variadic function-like
1661                      macro.  If the argument to the current last
1662                      parameter is NULL, then swallow the comma,
1663                      otherwise drop the paste flag.  */
1664                   if (macro_arg_token_iter_get_token (&from) == NULL)
1665                     tokens_buff_remove_last_token (buff);
1666                   else
1667                     paste_flag = tmp_token_ptr;
1668                 }
1669               /* Remove the paste flag if the RHS is a placemarker.  */
1670               else if (arg_tokens_count == 0)
1671                 paste_flag = tmp_token_ptr;
1672             }
1673         }
1674       else
1675         {
1676           arg_tokens_count = arg->expanded_count;
1677           macro_arg_token_iter_init (&from,
1678                                      CPP_OPTION (pfile,
1679                                                  track_macro_expansion),
1680                                      MACRO_ARG_TOKEN_EXPANDED,
1681                                      arg, arg->expanded);
1682         }
1683
1684       /* Padding on the left of an argument (unless RHS of ##).  */
1685       if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
1686           && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1687         {
1688           const cpp_token *t = padding_token (pfile, src);
1689           unsigned index = expanded_token_index (pfile, macro, src, i);
1690           /* Allocate a virtual location for the padding token and
1691              append the token and its location to BUFF and
1692              VIRT_LOCS.   */
1693           tokens_buff_add_token (buff, virt_locs, t,
1694                                  t->src_loc, t->src_loc,
1695                                  map, index);
1696         }
1697
1698       if (arg_tokens_count)
1699         {
1700           /* So now we've got the number of tokens that make up the
1701              argument that is going to replace the current parameter
1702              in the macro's replacement list.  */
1703           unsigned int j;
1704           for (j = 0; j < arg_tokens_count; ++j)
1705             {
1706               /* So if track_macro_exp is < 2, the user wants to
1707                  save extra memory while tracking macro expansion
1708                  locations.  So in that case here is what we do:
1709
1710                  Suppose we have #define SQARE(A) A * A
1711
1712                  And then we do SQARE(2+3)
1713
1714                  Then the tokens 2, +, 3, will have the same location,
1715                  saying they come from the expansion of the argument
1716                  A.
1717
1718               So that means we are going to ignore the COUNT tokens
1719               resulting from the expansion of the current macro
1720               arugment. In other words all the ARG_TOKENS_COUNT tokens
1721               resulting from the expansion of the macro argument will
1722               have the index I. Normally, each of those token should
1723               have index I+J.  */
1724               unsigned token_index = i;
1725               unsigned index;
1726               if (track_macro_exp > 1)
1727                 token_index += j;
1728
1729               index = expanded_token_index (pfile, macro, src, token_index);
1730               tokens_buff_add_token (buff, virt_locs,
1731                                      macro_arg_token_iter_get_token (&from),
1732                                      macro_arg_token_iter_get_location (&from),
1733                                      src->src_loc, map, index);
1734               macro_arg_token_iter_forward (&from);
1735             }
1736
1737           /* With a non-empty argument on the LHS of ##, the last
1738              token should be flagged PASTE_LEFT.  */
1739           if (src->flags & PASTE_LEFT)
1740             paste_flag =
1741               (const cpp_token **) tokens_buff_last_token_ptr (buff);
1742         }
1743       else if (CPP_PEDANTIC (pfile) && ! macro->syshdr
1744                && ! CPP_OPTION (pfile, c99)
1745                && ! cpp_in_system_header (pfile))
1746         {
1747           cpp_error (pfile, CPP_DL_PEDWARN,
1748                      "invoking macro %s argument %d: "
1749                      "empty macro arguments are undefined"
1750                      " in ISO C90 and ISO C++98",
1751                      NODE_NAME (node),
1752                      src->val.macro_arg.arg_no);
1753         }
1754
1755       /* Avoid paste on RHS (even case count == 0).  */
1756       if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
1757         {
1758           const cpp_token *t = &pfile->avoid_paste;
1759           tokens_buff_add_token (buff, virt_locs,
1760                                  t, t->src_loc, t->src_loc,
1761                                  NULL, 0);
1762         }
1763
1764       /* Add a new paste flag, or remove an unwanted one.  */
1765       if (paste_flag)
1766         {
1767           cpp_token *token = _cpp_temp_token (pfile);
1768           token->type = (*paste_flag)->type;
1769           token->val = (*paste_flag)->val;
1770           if (src->flags & PASTE_LEFT)
1771             token->flags = (*paste_flag)->flags | PASTE_LEFT;
1772           else
1773             token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1774           *paste_flag = token;
1775         }
1776
1777       i += arg_tokens_count;
1778     }
1779
1780   if (track_macro_exp)
1781     push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1782                                   tokens_buff_count (buff));
1783   else
1784     push_ptoken_context (pfile, node, buff, first,
1785                          tokens_buff_count (buff));
1786
1787   num_macro_tokens_counter += tokens_buff_count (buff);
1788 }
1789
1790 /* Return a special padding token, with padding inherited from SOURCE.  */
1791 static const cpp_token *
1792 padding_token (cpp_reader *pfile, const cpp_token *source)
1793 {
1794   cpp_token *result = _cpp_temp_token (pfile);
1795
1796   result->type = CPP_PADDING;
1797
1798   /* Data in GCed data structures cannot be made const so far, so we
1799      need a cast here.  */
1800   result->val.source = (cpp_token *) source;
1801   result->flags = 0;
1802   return result;
1803 }
1804
1805 /* Get a new uninitialized context.  Create a new one if we cannot
1806    re-use an old one.  */
1807 static cpp_context *
1808 next_context (cpp_reader *pfile)
1809 {
1810   cpp_context *result = pfile->context->next;
1811
1812   if (result == 0)
1813     {
1814       result = XNEW (cpp_context);
1815       memset (result, 0, sizeof (cpp_context));
1816       result->prev = pfile->context;
1817       result->next = 0;
1818       pfile->context->next = result;
1819     }
1820
1821   pfile->context = result;
1822   return result;
1823 }
1824
1825 /* Push a list of pointers to tokens.  */
1826 static void
1827 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1828                      const cpp_token **first, unsigned int count)
1829 {
1830   cpp_context *context = next_context (pfile);
1831
1832   context->tokens_kind = TOKENS_KIND_INDIRECT;
1833   context->c.macro = macro;
1834   context->buff = buff;
1835   FIRST (context).ptoken = first;
1836   LAST (context).ptoken = first + count;
1837 }
1838
1839 /* Push a list of tokens.
1840
1841    A NULL macro means that we should continue the current macro
1842    expansion, in essence.  That means that if we are currently in a
1843    macro expansion context, we'll make the new pfile->context refer to
1844    the current macro.  */
1845 void
1846 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1847                          const cpp_token *first, unsigned int count)
1848 {
1849   cpp_context *context;
1850
1851    if (macro == NULL)
1852      macro = macro_of_context (pfile->context);
1853
1854    context = next_context (pfile);
1855    context->tokens_kind = TOKENS_KIND_DIRECT;
1856    context->c.macro = macro;
1857    context->buff = NULL;
1858    FIRST (context).token = first;
1859    LAST (context).token = first + count;
1860 }
1861
1862 /* Build a context containing a list of tokens as well as their
1863    virtual locations and push it.  TOKENS_BUFF is the buffer that
1864    contains the tokens pointed to by FIRST.  If TOKENS_BUFF is
1865    non-NULL, it means that the context owns it, meaning that
1866    _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1867    contains the virtual locations.
1868
1869    A NULL macro means that we should continue the current macro
1870    expansion, in essence.  That means that if we are currently in a
1871    macro expansion context, we'll make the new pfile->context refer to
1872    the current macro.  */
1873 static void
1874 push_extended_tokens_context (cpp_reader *pfile,
1875                               cpp_hashnode *macro,
1876                               _cpp_buff *token_buff,
1877                               source_location *virt_locs,
1878                               const cpp_token **first,
1879                               unsigned int count)
1880 {
1881   cpp_context *context;
1882   macro_context *m;
1883
1884   if (macro == NULL)
1885     macro = macro_of_context (pfile->context);
1886
1887   context = next_context (pfile);
1888   context->tokens_kind = TOKENS_KIND_EXTENDED;
1889   context->buff = token_buff;
1890
1891   m = XNEW (macro_context);
1892   m->macro_node = macro;
1893   m->virt_locs = virt_locs;
1894   m->cur_virt_loc = virt_locs;
1895   context->c.mc = m;
1896   FIRST (context).ptoken = first;
1897   LAST (context).ptoken = first + count;
1898 }
1899
1900 /* Push a traditional macro's replacement text.  */
1901 void
1902 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
1903                         const uchar *start, size_t len)
1904 {
1905   cpp_context *context = next_context (pfile);
1906
1907   context->tokens_kind = TOKENS_KIND_DIRECT;
1908   context->c.macro = macro;
1909   context->buff = NULL;
1910   CUR (context) = start;
1911   RLIMIT (context) = start + len;
1912   macro->flags |= NODE_DISABLED;
1913 }
1914
1915 /* Creates a buffer that holds tokens a.k.a "token buffer", usually
1916    for the purpose of storing them on a cpp_context. If VIRT_LOCS is
1917    non-null (which means that -ftrack-macro-expansion is on),
1918    *VIRT_LOCS is set to a newly allocated buffer that is supposed to
1919    hold the virtual locations of the tokens resulting from macro
1920    expansion.  */
1921 static _cpp_buff*
1922 tokens_buff_new (cpp_reader *pfile, size_t len,
1923                  source_location **virt_locs)
1924 {
1925   size_t tokens_size = len * sizeof (cpp_token *);
1926   size_t locs_size = len * sizeof (source_location);
1927
1928   if (virt_locs != NULL)
1929     *virt_locs = XNEWVEC (source_location, locs_size);
1930   return _cpp_get_buff (pfile, tokens_size);
1931 }
1932
1933 /* Returns the number of tokens contained in a token buffer.  The
1934    buffer holds a set of cpp_token*.  */
1935 static size_t
1936 tokens_buff_count (_cpp_buff *buff)
1937 {
1938   return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
1939 }
1940
1941 /* Return a pointer to the last token contained in the token buffer
1942    BUFF.  */
1943 static const cpp_token **
1944 tokens_buff_last_token_ptr (_cpp_buff *buff)
1945 {
1946   return &((const cpp_token **) BUFF_FRONT (buff))[-1];
1947 }
1948
1949 /* Remove the last token contained in the token buffer TOKENS_BUFF.
1950    If VIRT_LOCS_BUFF is non-NULL,  it should point at the buffer
1951    containing the virtual locations of the tokens in TOKENS_BUFF; in
1952    which case the function updates that buffer as well.   */
1953 static inline void
1954 tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
1955
1956 {
1957   if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
1958     BUFF_FRONT (tokens_buff) =
1959       (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
1960 }
1961
1962 /* Insert a token into the token buffer at the position pointed to by
1963    DEST.  Note that the buffer is not enlarged so the previous token
1964    that was at *DEST is overwritten.  VIRT_LOC_DEST, if non-null,
1965    means -ftrack-macro-expansion is effect; it then points to where to
1966    insert the virtual location of TOKEN.  TOKEN is the token to
1967    insert.  VIRT_LOC is the virtual location of the token, i.e, the
1968    location possibly encoding its locus accross macro expansion.  If
1969    TOKEN is an argument of a function-like macro (inside a macro
1970    replacement list), PARM_DEF_LOC is the spelling location of the
1971    macro parameter that TOKEN is replacing, in the replacement list of
1972    the macro.  If TOKEN is not an argument of a function-like macro or
1973    if it doesn't come from a macro expansion, then VIRT_LOC can just
1974    be set to the same value as PARM_DEF_LOC.  If MAP is non null, it
1975    means TOKEN comes from a macro expansion and MAP is the macro map
1976    associated to the macro.  MACRO_TOKEN_INDEX points to the index of
1977    the token in the macro map; it is not considered if MAP is NULL.
1978
1979    Upon successful completion this function returns the a pointer to
1980    the position of the token coming right after the insertion
1981    point.  */
1982 static inline const cpp_token **
1983 tokens_buff_put_token_to (const cpp_token **dest,
1984                           source_location *virt_loc_dest,
1985                           const cpp_token *token,
1986                           source_location virt_loc,
1987                           source_location parm_def_loc,                   
1988                           const struct line_map *map,
1989                           unsigned int macro_token_index)
1990 {
1991   source_location macro_loc = virt_loc;
1992   const cpp_token **result;
1993
1994   if (virt_loc_dest)
1995     {
1996       /* -ftrack-macro-expansion is on.  */
1997       if (map)
1998         macro_loc = linemap_add_macro_token (map, macro_token_index,
1999                                              virt_loc, parm_def_loc);
2000       *virt_loc_dest = macro_loc;
2001     }
2002   *dest = token;
2003   result = &dest[1];
2004
2005   return result;
2006 }
2007
2008 /* Adds a token at the end of the tokens contained in BUFFER.  Note
2009    that this function doesn't enlarge BUFFER when the number of tokens
2010    reaches BUFFER's size; it aborts in that situation.
2011
2012    TOKEN is the token to append. VIRT_LOC is the virtual location of
2013    the token, i.e, the location possibly encoding its locus accross
2014    macro expansion. If TOKEN is an argument of a function-like macro
2015    (inside a macro replacement list), PARM_DEF_LOC is the location of
2016    the macro parameter that TOKEN is replacing.  If TOKEN doesn't come
2017    from a macro expansion, then VIRT_LOC can just be set to the same
2018    value as PARM_DEF_LOC.  If MAP is non null, it means TOKEN comes
2019    from a macro expansion and MAP is the macro map associated to the
2020    macro.  MACRO_TOKEN_INDEX points to the index of the token in the
2021    macro map; It is not considered if MAP is NULL.  If VIRT_LOCS is
2022    non-null, it means -ftrack-macro-expansion is on; in which case
2023    this function adds the virtual location DEF_LOC to the VIRT_LOCS
2024    array, at the same index as the one of TOKEN in BUFFER.  Upon
2025    successful completion this function returns the a pointer to the
2026    position of the token coming right after the insertion point.  */
2027 static const cpp_token **
2028 tokens_buff_add_token (_cpp_buff *buffer,
2029                        source_location *virt_locs,
2030                        const cpp_token *token,
2031                        source_location virt_loc,
2032                        source_location parm_def_loc,
2033                        const struct line_map *map,
2034                        unsigned int macro_token_index)
2035 {
2036   const cpp_token **result;
2037   source_location *virt_loc_dest = NULL;
2038   unsigned token_index = 
2039     (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
2040
2041   /* Abort if we pass the end the buffer.  */
2042   if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
2043     abort ();
2044
2045   if (virt_locs != NULL)
2046     virt_loc_dest = &virt_locs[token_index];
2047
2048   result =
2049     tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
2050                               virt_loc_dest, token, virt_loc, parm_def_loc,
2051                               map, macro_token_index);
2052
2053   BUFF_FRONT (buffer) = (unsigned char *) result;
2054   return result;
2055 }
2056
2057 /* Allocate space for the function-like macro argument ARG to store
2058    the tokens resulting from the macro-expansion of the tokens that
2059    make up ARG itself. That space is allocated in ARG->expanded and
2060    needs to be freed using free.  */
2061 static void
2062 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
2063 {
2064 #ifdef ENABLE_CHECKING
2065   if (arg->expanded != NULL
2066       || arg->expanded_virt_locs != NULL)
2067     abort ();
2068 #endif
2069   arg->expanded = XNEWVEC (const cpp_token *, capacity);
2070   if (CPP_OPTION (pfile, track_macro_expansion))
2071     arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2072
2073 }
2074
2075 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2076    tokens.  */
2077 static void
2078 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2079                           size_t size, size_t *expanded_capacity)
2080 {
2081   if (size <= *expanded_capacity)
2082     return;
2083
2084   size *= 2;
2085
2086   arg->expanded =
2087     XRESIZEVEC (const cpp_token *, arg->expanded, size);
2088   *expanded_capacity = size;
2089
2090   if (CPP_OPTION (pfile, track_macro_expansion))
2091     {
2092       if (arg->expanded_virt_locs == NULL)
2093         arg->expanded_virt_locs = XNEWVEC (source_location, size);
2094       else
2095         arg->expanded_virt_locs = XRESIZEVEC (source_location,
2096                                               arg->expanded_virt_locs,
2097                                               size);
2098     }
2099 }
2100
2101 /* Expand an argument ARG before replacing parameters in a
2102    function-like macro.  This works by pushing a context with the
2103    argument's tokens, and then expanding that into a temporary buffer
2104    as if it were a normal part of the token stream.  collect_args()
2105    has terminated the argument's tokens with a CPP_EOF so that we know
2106    when we have fully expanded the argument.  */
2107 static void
2108 expand_arg (cpp_reader *pfile, macro_arg *arg)
2109 {
2110   size_t capacity;
2111   bool saved_warn_trad;
2112   bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2113
2114   if (arg->count == 0
2115       || arg->expanded != NULL)
2116     return;
2117
2118   /* Don't warn about funlike macros when pre-expanding.  */
2119   saved_warn_trad = CPP_WTRADITIONAL (pfile);
2120   CPP_WTRADITIONAL (pfile) = 0;
2121
2122   /* Loop, reading in the tokens of the argument.  */
2123   capacity = 256;
2124   alloc_expanded_arg_mem (pfile, arg, capacity);
2125
2126   if (track_macro_exp_p)
2127     push_extended_tokens_context (pfile, NULL, NULL,
2128                                   arg->virt_locs,
2129                                   arg->first,
2130                                   arg->count + 1);
2131   else
2132     push_ptoken_context (pfile, NULL, NULL,
2133                          arg->first, arg->count + 1);
2134
2135   for (;;)
2136     {
2137       const cpp_token *token;
2138       source_location location;
2139
2140       ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2141                                 &capacity);
2142
2143       token = cpp_get_token_1 (pfile, &location);
2144
2145       if (token->type == CPP_EOF)
2146         break;
2147
2148       set_arg_token (arg, token, location,
2149                      arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2150                      CPP_OPTION (pfile, track_macro_expansion));
2151       arg->expanded_count++;
2152     }
2153
2154   _cpp_pop_context (pfile);
2155
2156   CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2157 }
2158
2159 /* Returns the macro associated to the current context if we are in
2160    the context a macro expansion, NULL otherwise.  */
2161 static cpp_hashnode*
2162 macro_of_context (cpp_context *context)
2163 {
2164   if (context == NULL)
2165     return NULL;
2166
2167   return (context->tokens_kind == TOKENS_KIND_EXTENDED)
2168     ? context->c.mc->macro_node
2169     : context->c.macro;
2170 }
2171
2172 /* Return TRUE iff we are expanding a macro or are about to start
2173    expanding one.  If we are effectively expanding a macro, the
2174    function macro_of_context returns a pointer to the macro being
2175    expanded.  */
2176 static bool
2177 in_macro_expansion_p (cpp_reader *pfile)
2178 {
2179   if (pfile == NULL)
2180     return false;
2181
2182   return (pfile->about_to_expand_macro_p 
2183           || macro_of_context (pfile->context));
2184 }
2185
2186 /* Pop the current context off the stack, re-enabling the macro if the
2187    context represented a macro's replacement list.  Initially the
2188    context structure was not freed so that we can re-use it later, but
2189    now we do free it to reduce peak memory consumption.  */
2190 void
2191 _cpp_pop_context (cpp_reader *pfile)
2192 {
2193   cpp_context *context = pfile->context;
2194
2195   /* We should not be popping the base context.  */
2196   if (context == &pfile->base_context)
2197     abort ();
2198
2199   if (context->c.macro)
2200     {
2201       cpp_hashnode *macro;
2202       if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2203         {
2204           macro_context *mc = context->c.mc;
2205           macro = mc->macro_node;
2206           /* If context->buff is set, it means the life time of tokens
2207              is bound to the life time of this context; so we must
2208              free the tokens; that means we must free the virtual
2209              locations of these tokens too.  */
2210           if (context->buff && mc->virt_locs)
2211             {
2212               free (mc->virt_locs);
2213               mc->virt_locs = NULL;
2214             }
2215           free (mc);
2216           context->c.mc = NULL;
2217         }
2218       else
2219         macro = context->c.macro;
2220
2221       /* Beware that MACRO can be NULL in cases like when we are
2222          called from expand_arg.  In those cases, a dummy context with
2223          tokens is pushed just for the purpose of walking them using
2224          cpp_get_token_1.  In that case, no 'macro' field is set into
2225          the dummy context.  */
2226       if (macro != NULL
2227           /* Several contiguous macro expansion contexts can be
2228              associated to the same macro; that means it's the same
2229              macro expansion that spans accross all these (sub)
2230              contexts.  So we should re-enable an expansion-disabled
2231              macro only when we are sure we are really out of that
2232              macro expansion.  */
2233           && macro_of_context (context->prev) != macro)
2234         macro->flags &= ~NODE_DISABLED;
2235     }
2236
2237   if (context->buff)
2238     {
2239       /* Decrease memory peak consumption by freeing the memory used
2240          by the context.  */
2241       _cpp_free_buff (context->buff);
2242     }
2243
2244   pfile->context = context->prev;
2245   /* decrease peak memory consumption by feeing the context.  */
2246   pfile->context->next = NULL;
2247   free (context);
2248 }
2249
2250 /* Return TRUE if we reached the end of the set of tokens stored in
2251    CONTEXT, FALSE otherwise.  */
2252 static inline bool
2253 reached_end_of_context (cpp_context *context)
2254 {
2255   if (context->tokens_kind == TOKENS_KIND_DIRECT)
2256       return FIRST (context).token == LAST (context).token;
2257   else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2258            || context->tokens_kind == TOKENS_KIND_EXTENDED)
2259     return FIRST (context).ptoken == LAST (context).ptoken;
2260   else
2261     abort ();
2262 }
2263
2264 /* Consume the next token contained in the current context of PFILE,
2265    and return it in *TOKEN. It's "full location" is returned in
2266    *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2267    means the location encoding the locus of the token accross macro
2268    expansion; otherwise it's just is the "normal" location of the
2269    token which (*TOKEN)->src_loc.  */
2270 static inline void
2271 consume_next_token_from_context (cpp_reader *pfile,
2272                                  const cpp_token ** token,
2273                                  source_location *location)
2274 {
2275   cpp_context *c = pfile->context;
2276
2277   if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2278     {
2279       *token = FIRST (c).token;
2280       *location = (*token)->src_loc;
2281       FIRST (c).token++;
2282     }
2283   else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)            
2284     {
2285       *token = *FIRST (c).ptoken;
2286       *location = (*token)->src_loc;
2287       FIRST (c).ptoken++;
2288     }
2289   else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2290     {
2291       macro_context *m = c->c.mc;
2292       *token = *FIRST (c).ptoken;
2293       if (m->virt_locs)
2294         {
2295           *location = *m->cur_virt_loc;
2296           m->cur_virt_loc++;
2297         }
2298       else
2299         *location = (*token)->src_loc;
2300       FIRST (c).ptoken++;
2301     }
2302   else
2303     abort ();
2304 }
2305
2306 /* In the traditional mode of the preprocessor, if we are currently in
2307    a directive, the location of a token must be the location of the
2308    start of the directive line.  This function returns the proper
2309    location if we are in the traditional mode, and just returns
2310    LOCATION otherwise.  */
2311
2312 static inline source_location
2313 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2314 {
2315   if (CPP_OPTION (pfile, traditional))
2316     {
2317       if (pfile->state.in_directive)
2318         return pfile->directive_line;
2319     }
2320   return location;
2321 }
2322
2323 /* Routine to get a token as well as its location.
2324
2325    Macro expansions and directives are transparently handled,
2326    including entering included files.  Thus tokens are post-macro
2327    expansion, and after any intervening directives.  External callers
2328    see CPP_EOF only at EOF.  Internal callers also see it when meeting
2329    a directive inside a macro call, when at the end of a directive and
2330    state.in_directive is still 1, and at the end of argument
2331    pre-expansion.
2332
2333    LOC is an out parameter; *LOC is set to the location "as expected
2334    by the user".  Please read the comment of
2335    cpp_get_token_with_location to learn more about the meaning of this
2336    location.  */
2337 static const cpp_token*
2338 cpp_get_token_1 (cpp_reader *pfile, source_location *location)
2339 {
2340   const cpp_token *result;
2341   /* This token is a virtual token that either encodes a location
2342      related to macro expansion or a spelling location.  */
2343   source_location virt_loc = 0;
2344   /* pfile->about_to_expand_macro_p can be overriden by indirect calls
2345      to functions that push macro contexts.  So let's save it so that
2346      we can restore it when we are about to leave this routine.  */
2347   bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
2348
2349   for (;;)
2350     {
2351       cpp_hashnode *node;
2352       cpp_context *context = pfile->context;
2353
2354       /* Context->prev == 0 <=> base context.  */
2355       if (!context->prev)
2356         {
2357           result = _cpp_lex_token (pfile);
2358           virt_loc = result->src_loc;
2359         }
2360       else if (!reached_end_of_context (context))
2361         {
2362           consume_next_token_from_context (pfile, &result,
2363                                            &virt_loc);
2364           if (result->flags & PASTE_LEFT)
2365             {
2366               paste_all_tokens (pfile, result);
2367               if (pfile->state.in_directive)
2368                 continue;
2369               result = padding_token (pfile, result);
2370               goto out;
2371             }
2372         }
2373       else
2374         {
2375           if (pfile->context->c.macro)
2376             ++num_expanded_macros_counter;
2377           _cpp_pop_context (pfile);
2378           if (pfile->state.in_directive)
2379             continue;
2380           result = &pfile->avoid_paste;
2381           goto out;
2382         }
2383
2384       if (pfile->state.in_directive && result->type == CPP_COMMENT)
2385         continue;
2386
2387       if (result->type != CPP_NAME)
2388         break;
2389
2390       node = result->val.node.node;
2391
2392       if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2393         break;
2394
2395       if (!(node->flags & NODE_DISABLED))
2396         {
2397           int ret = 0;
2398           /* If not in a macro context, and we're going to start an
2399              expansion, record the location.  */
2400           if (!in_macro_expansion_p (pfile))
2401             pfile->invocation_location = result->src_loc;
2402           if (pfile->state.prevent_expansion)
2403             break;
2404
2405           /* Conditional macros require that a predicate be evaluated
2406              first.  */
2407           if ((node->flags & NODE_CONDITIONAL) != 0)
2408             {
2409               if (pfile->cb.macro_to_expand)
2410                 {
2411                   bool whitespace_after;
2412                   const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2413
2414                   whitespace_after = (peek_tok->type == CPP_PADDING
2415                                       || (peek_tok->flags & PREV_WHITE));
2416                   node = pfile->cb.macro_to_expand (pfile, result);
2417                   if (node)
2418                     ret = enter_macro_context (pfile, node, result,
2419                                                virt_loc);
2420                   else if (whitespace_after)
2421                     {
2422                       /* If macro_to_expand hook returned NULL and it
2423                          ate some tokens, see if we don't need to add
2424                          a padding token in between this and the
2425                          next token.  */
2426                       peek_tok = cpp_peek_token (pfile, 0);
2427                       if (peek_tok->type != CPP_PADDING
2428                           && (peek_tok->flags & PREV_WHITE) == 0)
2429                         _cpp_push_token_context (pfile, NULL,
2430                                                  padding_token (pfile,
2431                                                                 peek_tok), 1);
2432                     }
2433                 }
2434             }
2435           else
2436             ret = enter_macro_context (pfile, node, result, 
2437                                        virt_loc);
2438           if (ret)
2439             {
2440               if (pfile->state.in_directive || ret == 2)
2441                 continue;
2442               result = padding_token (pfile, result);
2443               goto out;
2444             }
2445         }
2446       else
2447         {
2448           /* Flag this token as always unexpandable.  FIXME: move this
2449              to collect_args()?.  */
2450           cpp_token *t = _cpp_temp_token (pfile);
2451           t->type = result->type;
2452           t->flags = result->flags | NO_EXPAND;
2453           t->val = result->val;
2454           result = t;
2455         }
2456
2457       break;
2458     }
2459
2460  out:
2461   if (location != NULL)
2462     {
2463       if (virt_loc == 0)
2464         virt_loc = result->src_loc;
2465       *location = virt_loc;
2466
2467       if (!CPP_OPTION (pfile, track_macro_expansion)
2468           && macro_of_context (pfile->context) != NULL)
2469         /* We are in a macro expansion context, are not tracking
2470            virtual location, but were asked to report the location
2471            of the expansion point of the macro being expanded.  */
2472         *location = pfile->invocation_location;
2473
2474       *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2475     }
2476
2477   pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
2478   return result;
2479 }
2480
2481 /* External routine to get a token.  Also used nearly everywhere
2482    internally, except for places where we know we can safely call
2483    _cpp_lex_token directly, such as lexing a directive name.
2484
2485    Macro expansions and directives are transparently handled,
2486    including entering included files.  Thus tokens are post-macro
2487    expansion, and after any intervening directives.  External callers
2488    see CPP_EOF only at EOF.  Internal callers also see it when meeting
2489    a directive inside a macro call, when at the end of a directive and
2490    state.in_directive is still 1, and at the end of argument
2491    pre-expansion.  */
2492 const cpp_token *
2493 cpp_get_token (cpp_reader *pfile)
2494 {
2495   return cpp_get_token_1 (pfile, NULL);
2496 }
2497
2498 /* Like cpp_get_token, but also returns a virtual token location
2499    separate from the spelling location carried by the returned token.
2500
2501    LOC is an out parameter; *LOC is set to the location "as expected
2502    by the user".  This matters when a token results from macro
2503    expansion; in that case the token's spelling location indicates the
2504    locus of the token in the definition of the macro but *LOC
2505    virtually encodes all the other meaningful locuses associated to
2506    the token.
2507
2508    What? virtual location? Yes, virtual location.
2509
2510    If the token results from macro expansion and if macro expansion
2511    location tracking is enabled its virtual location encodes (at the
2512    same time):
2513
2514    - the spelling location of the token
2515
2516    - the locus of the macro expansion point
2517
2518    - the locus of the point where the token got instantiated as part
2519      of the macro expansion process.
2520
2521    You have to use the linemap API to get the locus you are interested
2522    in from a given virtual location.
2523
2524    Note however that virtual locations are not necessarily ordered for
2525    relations '<' and '>'.  One must use the function
2526    linemap_location_before_p instead of using the relational operator
2527    '<'.
2528
2529    If macro expansion tracking is off and if the token results from
2530    macro expansion the virtual location is the expansion point of the
2531    macro that got expanded.
2532
2533    When the token doesn't result from macro expansion, the virtual
2534    location is just the same thing as its spelling location.  */
2535
2536 const cpp_token *
2537 cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2538 {
2539   return cpp_get_token_1 (pfile, loc);
2540 }
2541
2542 /* Returns true if we're expanding an object-like macro that was
2543    defined in a system header.  Just checks the macro at the top of
2544    the stack.  Used for diagnostic suppression.  */
2545 int
2546 cpp_sys_macro_p (cpp_reader *pfile)
2547 {
2548   cpp_hashnode *node = NULL;
2549
2550   if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2551     node = pfile->context->c.mc->macro_node;
2552   else
2553     node = pfile->context->c.macro;
2554
2555   return node && node->value.macro && node->value.macro->syshdr;
2556 }
2557
2558 /* Read each token in, until end of the current file.  Directives are
2559    transparently processed.  */
2560 void
2561 cpp_scan_nooutput (cpp_reader *pfile)
2562 {
2563   /* Request a CPP_EOF token at the end of this file, rather than
2564      transparently continuing with the including file.  */
2565   pfile->buffer->return_at_eof = true;
2566
2567   pfile->state.discarding_output++;
2568   pfile->state.prevent_expansion++;
2569
2570   if (CPP_OPTION (pfile, traditional))
2571     while (_cpp_read_logical_line_trad (pfile))
2572       ;
2573   else
2574     while (cpp_get_token (pfile)->type != CPP_EOF)
2575       ;
2576
2577   pfile->state.discarding_output--;
2578   pfile->state.prevent_expansion--;
2579 }
2580
2581 /* Step back one or more tokens obtained from the lexer.  */
2582 void
2583 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2584 {
2585   pfile->lookaheads += count;
2586   while (count--)
2587     {
2588       pfile->cur_token--;
2589       if (pfile->cur_token == pfile->cur_run->base
2590           /* Possible with -fpreprocessed and no leading #line.  */
2591           && pfile->cur_run->prev != NULL)
2592         {
2593           pfile->cur_run = pfile->cur_run->prev;
2594           pfile->cur_token = pfile->cur_run->limit;
2595         }
2596     }
2597 }
2598
2599 /* Step back one (or more) tokens.  Can only step back more than 1 if
2600    they are from the lexer, and not from macro expansion.  */
2601 void
2602 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
2603 {
2604   if (pfile->context->prev == NULL)
2605     _cpp_backup_tokens_direct (pfile, count);
2606   else
2607     {
2608       if (count != 1)
2609         abort ();
2610       if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
2611         FIRST (pfile->context).token--;
2612       else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
2613         FIRST (pfile->context).ptoken--;
2614       else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2615         {
2616           FIRST (pfile->context).ptoken--;
2617           if (pfile->context->c.macro)
2618             {
2619               macro_context *m = pfile->context->c.mc;
2620               m->cur_virt_loc--;
2621 #ifdef ENABLE_CHECKING
2622               if (m->cur_virt_loc < m->virt_locs)
2623                 abort ();
2624 #endif
2625             }
2626           else
2627             abort ();
2628         }
2629       else
2630         abort ();
2631     }
2632 }
2633
2634 /* #define directive parsing and handling.  */
2635
2636 /* Returns nonzero if a macro redefinition warning is required.  */
2637 static bool
2638 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2639                       const cpp_macro *macro2)
2640 {
2641   const cpp_macro *macro1;
2642   unsigned int i;
2643
2644   /* Some redefinitions need to be warned about regardless.  */
2645   if (node->flags & NODE_WARN)
2646     return true;
2647
2648   /* Suppress warnings for builtins that lack the NODE_WARN flag.  */
2649   if (node->flags & NODE_BUILTIN)
2650     {
2651       if (!pfile->cb.user_builtin_macro
2652           || !pfile->cb.user_builtin_macro (pfile, node))
2653         return false;
2654     }
2655
2656   /* Redefinitions of conditional (context-sensitive) macros, on
2657      the other hand, must be allowed silently.  */
2658   if (node->flags & NODE_CONDITIONAL)
2659     return false;
2660
2661   /* Redefinition of a macro is allowed if and only if the old and new
2662      definitions are the same.  (6.10.3 paragraph 2).  */
2663   macro1 = node->value.macro;
2664
2665   /* Don't check count here as it can be different in valid
2666      traditional redefinitions with just whitespace differences.  */
2667   if (macro1->paramc != macro2->paramc
2668       || macro1->fun_like != macro2->fun_like
2669       || macro1->variadic != macro2->variadic)
2670     return true;
2671
2672   /* Check parameter spellings.  */
2673   for (i = 0; i < macro1->paramc; i++)
2674     if (macro1->params[i] != macro2->params[i])
2675       return true;
2676
2677   /* Check the replacement text or tokens.  */
2678   if (CPP_OPTION (pfile, traditional))
2679     return _cpp_expansions_different_trad (macro1, macro2);
2680
2681   if (macro1->count != macro2->count)
2682     return true;
2683
2684   for (i = 0; i < macro1->count; i++)
2685     if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2686       return true;
2687
2688   return false;
2689 }
2690
2691 /* Free the definition of hashnode H.  */
2692 void
2693 _cpp_free_definition (cpp_hashnode *h)
2694 {
2695   /* Macros and assertions no longer have anything to free.  */
2696   h->type = NT_VOID;
2697   /* Clear builtin flag in case of redefinition.  */
2698   h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
2699 }
2700
2701 /* Save parameter NODE to the parameter list of macro MACRO.  Returns
2702    zero on success, nonzero if the parameter is a duplicate.  */
2703 bool
2704 _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
2705 {
2706   unsigned int len;
2707   /* Constraint 6.10.3.6 - duplicate parameter names.  */
2708   if (node->flags & NODE_MACRO_ARG)
2709     {
2710       cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
2711                  NODE_NAME (node));
2712       return true;
2713     }
2714
2715   if (BUFF_ROOM (pfile->a_buff)
2716       < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2717     _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
2718
2719   ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
2720   node->flags |= NODE_MACRO_ARG;
2721   len = macro->paramc * sizeof (union _cpp_hashnode_value);
2722   if (len > pfile->macro_buffer_len)
2723     {
2724       pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2725                                         len);
2726       pfile->macro_buffer_len = len;
2727     }
2728   ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
2729     = node->value;
2730   
2731   node->value.arg_index  = macro->paramc;
2732   return false;
2733 }
2734
2735 /* Check the syntax of the parameters in a MACRO definition.  Returns
2736    false if an error occurs.  */
2737 static bool
2738 parse_params (cpp_reader *pfile, cpp_macro *macro)
2739 {
2740   unsigned int prev_ident = 0;
2741
2742   for (;;)
2743     {
2744       const cpp_token *token = _cpp_lex_token (pfile);
2745
2746       switch (token->type)
2747         {
2748         default:
2749           /* Allow/ignore comments in parameter lists if we are
2750              preserving comments in macro expansions.  */
2751           if (token->type == CPP_COMMENT
2752               && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2753             continue;
2754
2755           cpp_error (pfile, CPP_DL_ERROR,
2756                      "\"%s\" may not appear in macro parameter list",
2757                      cpp_token_as_text (pfile, token));
2758           return false;
2759
2760         case CPP_NAME:
2761           if (prev_ident)
2762             {
2763               cpp_error (pfile, CPP_DL_ERROR,
2764                          "macro parameters must be comma-separated");
2765               return false;
2766             }
2767           prev_ident = 1;
2768
2769           if (_cpp_save_parameter (pfile, macro, token->val.node.node))
2770             return false;
2771           continue;
2772
2773         case CPP_CLOSE_PAREN:
2774           if (prev_ident || macro->paramc == 0)
2775             return true;
2776
2777           /* Fall through to pick up the error.  */
2778         case CPP_COMMA:
2779           if (!prev_ident)
2780             {
2781               cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
2782               return false;
2783             }
2784           prev_ident = 0;
2785           continue;
2786
2787         case CPP_ELLIPSIS:
2788           macro->variadic = 1;
2789           if (!prev_ident)
2790             {
2791               _cpp_save_parameter (pfile, macro,
2792                                    pfile->spec_nodes.n__VA_ARGS__);
2793               pfile->state.va_args_ok = 1;
2794               if (! CPP_OPTION (pfile, c99)
2795                   && CPP_OPTION (pfile, cpp_pedantic)
2796                   && CPP_OPTION (pfile, warn_variadic_macros))
2797                 cpp_pedwarning
2798                   (pfile, CPP_W_VARIADIC_MACROS,
2799                    "anonymous variadic macros were introduced in C99");
2800             }
2801           else if (CPP_OPTION (pfile, cpp_pedantic)
2802                    && CPP_OPTION (pfile, warn_variadic_macros))
2803             cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2804                             "ISO C does not permit named variadic macros");
2805
2806           /* We're at the end, and just expect a closing parenthesis.  */
2807           token = _cpp_lex_token (pfile);
2808           if (token->type == CPP_CLOSE_PAREN)
2809             return true;
2810           /* Fall through.  */
2811
2812         case CPP_EOF:
2813           cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
2814           return false;
2815         }
2816     }
2817 }
2818
2819 /* Allocate room for a token from a macro's replacement list.  */
2820 static cpp_token *
2821 alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2822 {
2823   if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2824     _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
2825
2826   return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
2827 }
2828
2829 /* Lex a token from the expansion of MACRO, but mark parameters as we
2830    find them and warn of traditional stringification.  */
2831 static cpp_token *
2832 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2833 {
2834   cpp_token *token, *saved_cur_token;
2835
2836   saved_cur_token = pfile->cur_token;
2837   pfile->cur_token = alloc_expansion_token (pfile, macro);
2838   token = _cpp_lex_direct (pfile);
2839   pfile->cur_token = saved_cur_token;
2840
2841   /* Is this a parameter?  */
2842   if (token->type == CPP_NAME
2843       && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
2844     {
2845       token->type = CPP_MACRO_ARG;
2846       token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
2847     }
2848   else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2849            && (token->type == CPP_STRING || token->type == CPP_CHAR))
2850     check_trad_stringification (pfile, macro, &token->val.str);
2851
2852   return token;
2853 }
2854
2855 static bool
2856 create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
2857 {
2858   cpp_token *token;
2859   const cpp_token *ctoken;
2860   bool following_paste_op = false;
2861   const char *paste_op_error_msg =
2862     N_("'##' cannot appear at either end of a macro expansion");
2863   unsigned int num_extra_tokens = 0;
2864
2865   /* Get the first token of the expansion (or the '(' of a
2866      function-like macro).  */
2867   ctoken = _cpp_lex_token (pfile);
2868
2869   if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
2870     {
2871       bool ok = parse_params (pfile, macro);
2872       macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
2873       if (!ok)
2874         return false;
2875
2876       /* Success.  Commit or allocate the parameter array.  */
2877       if (pfile->hash_table->alloc_subobject)
2878         {
2879           cpp_hashnode **params =
2880             (cpp_hashnode **) pfile->hash_table->alloc_subobject
2881             (sizeof (cpp_hashnode *) * macro->paramc);
2882           memcpy (params, macro->params,
2883                   sizeof (cpp_hashnode *) * macro->paramc);
2884           macro->params = params;
2885         }
2886       else
2887         BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
2888       macro->fun_like = 1;
2889     }
2890   else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
2891     {
2892       /* While ISO C99 requires whitespace before replacement text
2893          in a macro definition, ISO C90 with TC1 allows there characters
2894          from the basic source character set.  */
2895       if (CPP_OPTION (pfile, c99))
2896         cpp_error (pfile, CPP_DL_PEDWARN,
2897                    "ISO C99 requires whitespace after the macro name");
2898       else
2899         {
2900           int warntype = CPP_DL_WARNING;
2901           switch (ctoken->type)
2902             {
2903             case CPP_ATSIGN:
2904             case CPP_AT_NAME:
2905             case CPP_OBJC_STRING:
2906               /* '@' is not in basic character set.  */
2907               warntype = CPP_DL_PEDWARN;
2908               break;
2909             case CPP_OTHER:
2910               /* Basic character set sans letters, digits and _.  */
2911               if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
2912                           ctoken->val.str.text[0]) == NULL)
2913                 warntype = CPP_DL_PEDWARN;
2914               break;
2915             default:
2916               /* All other tokens start with a character from basic
2917                  character set.  */
2918               break;
2919             }
2920           cpp_error (pfile, warntype,
2921                      "missing whitespace after the macro name");
2922         }
2923     }
2924
2925   if (macro->fun_like)
2926     token = lex_expansion_token (pfile, macro);
2927   else
2928     {
2929       token = alloc_expansion_token (pfile, macro);
2930       *token = *ctoken;
2931     }
2932
2933   for (;;)
2934     {
2935       /* Check the stringifying # constraint 6.10.3.2.1 of
2936          function-like macros when lexing the subsequent token.  */
2937       if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
2938         {
2939           if (token->type == CPP_MACRO_ARG)
2940             {
2941               if (token->flags & PREV_WHITE)
2942                 token->flags |= SP_PREV_WHITE;
2943               if (token[-1].flags & DIGRAPH)
2944                 token->flags |= SP_DIGRAPH;
2945               token->flags &= ~PREV_WHITE;
2946               token->flags |= STRINGIFY_ARG;
2947               token->flags |= token[-1].flags & PREV_WHITE;
2948               token[-1] = token[0];
2949               macro->count--;
2950             }
2951           /* Let assembler get away with murder.  */
2952           else if (CPP_OPTION (pfile, lang) != CLK_ASM)
2953             {
2954               cpp_error (pfile, CPP_DL_ERROR,
2955                          "'#' is not followed by a macro parameter");
2956               return false;
2957             }
2958         }
2959
2960       if (token->type == CPP_EOF)
2961         {
2962           /* Paste operator constraint 6.10.3.3.1:
2963              Token-paste ##, can appear in both object-like and
2964              function-like macros, but not at the end.  */
2965           if (following_paste_op)
2966             {
2967               cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
2968               return false;
2969             }
2970           break;
2971         }
2972
2973       /* Paste operator constraint 6.10.3.3.1.  */
2974       if (token->type == CPP_PASTE)
2975         {
2976           /* Token-paste ##, can appear in both object-like and
2977              function-like macros, but not at the beginning.  */
2978           if (macro->count == 1)
2979             {
2980               cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
2981               return false;
2982             }
2983
2984           if (token[-1].flags & PASTE_LEFT)
2985             {
2986               macro->extra_tokens = 1;
2987               num_extra_tokens++;
2988               token->val.token_no = macro->count - 1;
2989             }
2990           else
2991             {
2992               --macro->count;
2993               token[-1].flags |= PASTE_LEFT;
2994               if (token->flags & DIGRAPH)
2995                 token[-1].flags |= SP_DIGRAPH;
2996               if (token->flags & PREV_WHITE)
2997                 token[-1].flags |= SP_PREV_WHITE;
2998             }
2999         }
3000
3001       following_paste_op = (token->type == CPP_PASTE);
3002       token = lex_expansion_token (pfile, macro);
3003     }
3004
3005   macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
3006   macro->traditional = 0;
3007
3008   /* Don't count the CPP_EOF.  */
3009   macro->count--;
3010
3011   /* Clear whitespace on first token for warn_of_redefinition().  */
3012   if (macro->count)
3013     macro->exp.tokens[0].flags &= ~PREV_WHITE;
3014
3015   /* Commit or allocate the memory.  */
3016   if (pfile->hash_table->alloc_subobject)
3017     {
3018       cpp_token *tokns =
3019         (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
3020                                                           * macro->count);
3021       if (num_extra_tokens)
3022         {
3023           /* Place second and subsequent ## or %:%: tokens in
3024              sequences of consecutive such tokens at the end of the
3025              list to preserve information about where they appear, how
3026              they are spelt and whether they are preceded by
3027              whitespace without otherwise interfering with macro
3028              expansion.  */
3029           cpp_token *normal_dest = tokns;
3030           cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
3031           unsigned int i;
3032           for (i = 0; i < macro->count; i++)
3033             {
3034               if (macro->exp.tokens[i].type == CPP_PASTE)
3035                 *extra_dest++ = macro->exp.tokens[i];
3036               else
3037                 *normal_dest++ = macro->exp.tokens[i];
3038             }
3039         }
3040       else
3041         memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
3042       macro->exp.tokens = tokns;
3043     }
3044   else
3045     BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
3046
3047   return true;
3048 }
3049
3050 /* Parse a macro and save its expansion.  Returns nonzero on success.  */
3051 bool
3052 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
3053 {
3054   cpp_macro *macro;
3055   unsigned int i;
3056   bool ok;
3057
3058   if (pfile->hash_table->alloc_subobject)
3059     macro = (cpp_macro *) pfile->hash_table->alloc_subobject
3060       (sizeof (cpp_macro));
3061   else
3062     macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
3063   macro->line = pfile->directive_line;
3064   macro->params = 0;
3065   macro->paramc = 0;
3066   macro->variadic = 0;
3067   macro->used = !CPP_OPTION (pfile, warn_unused_macros);
3068   macro->count = 0;
3069   macro->fun_like = 0;
3070   macro->extra_tokens = 0;
3071   /* To suppress some diagnostics.  */
3072   macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
3073
3074   if (CPP_OPTION (pfile, traditional))
3075     ok = _cpp_create_trad_definition (pfile, macro);
3076   else
3077     {
3078       ok = create_iso_definition (pfile, macro);
3079
3080       /* We set the type for SEEN_EOL() in directives.c.
3081
3082          Longer term we should lex the whole line before coming here,
3083          and just copy the expansion.  */
3084
3085       /* Stop the lexer accepting __VA_ARGS__.  */
3086       pfile->state.va_args_ok = 0;
3087     }
3088
3089   /* Clear the fast argument lookup indices.  */
3090   for (i = macro->paramc; i-- > 0; )
3091     {
3092       struct cpp_hashnode *node = macro->params[i];
3093       node->flags &= ~ NODE_MACRO_ARG;
3094       node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
3095     }
3096
3097   if (!ok)
3098     return ok;
3099
3100   if (node->type == NT_MACRO)
3101     {
3102       if (CPP_OPTION (pfile, warn_unused_macros))
3103         _cpp_warn_if_unused_macro (pfile, node, NULL);
3104
3105       if (warn_of_redefinition (pfile, node, macro))
3106         {
3107           const int reason = (node->flags & NODE_BUILTIN)
3108                              ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
3109           bool warned;
3110
3111           warned = cpp_pedwarning_with_line (pfile, reason,
3112                                              pfile->directive_line, 0,
3113                                              "\"%s\" redefined",
3114                                              NODE_NAME (node));
3115
3116           if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3117             cpp_error_with_line (pfile, CPP_DL_NOTE,
3118                                  node->value.macro->line, 0,
3119                          "this is the location of the previous definition");
3120         }
3121     }
3122
3123   if (node->type != NT_VOID)
3124     _cpp_free_definition (node);
3125
3126   /* Enter definition in hash table.  */
3127   node->type = NT_MACRO;
3128   node->value.macro = macro;
3129   if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3130       && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3131       /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3132          in the C standard, as something that one must use in C++.
3133          However DR#593 indicates that these aren't actually mentioned
3134          in the C++ standard.  We special-case them anyway.  */
3135       && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3136       && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3137     node->flags |= NODE_WARN;
3138
3139   /* If user defines one of the conditional macros, remove the
3140      conditional flag */
3141   node->flags &= ~NODE_CONDITIONAL;
3142
3143   return ok;
3144 }
3145
3146 /* Warn if a token in STRING matches one of a function-like MACRO's
3147    parameters.  */
3148 static void
3149 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3150                             const cpp_string *string)
3151 {
3152   unsigned int i, len;
3153   const uchar *p, *q, *limit;
3154
3155   /* Loop over the string.  */
3156   limit = string->text + string->len - 1;
3157   for (p = string->text + 1; p < limit; p = q)
3158     {
3159       /* Find the start of an identifier.  */
3160       while (p < limit && !is_idstart (*p))
3161         p++;
3162
3163       /* Find the end of the identifier.  */
3164       q = p;
3165       while (q < limit && is_idchar (*q))
3166         q++;
3167
3168       len = q - p;
3169
3170       /* Loop over the function macro arguments to see if the
3171          identifier inside the string matches one of them.  */
3172       for (i = 0; i < macro->paramc; i++)
3173         {
3174           const cpp_hashnode *node = macro->params[i];
3175
3176           if (NODE_LEN (node) == len
3177               && !memcmp (p, NODE_NAME (node), len))
3178             {
3179               cpp_error (pfile, CPP_DL_WARNING,
3180            "macro argument \"%s\" would be stringified in traditional C",
3181                          NODE_NAME (node));
3182               break;
3183             }
3184         }
3185     }
3186 }
3187
3188 /* Returns the name, arguments and expansion of a macro, in a format
3189    suitable to be read back in again, and therefore also for DWARF 2
3190    debugging info.  e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3191    Caller is expected to generate the "#define" bit if needed.  The
3192    returned text is temporary, and automatically freed later.  */
3193 const unsigned char *
3194 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3195 {
3196   unsigned int i, len;
3197   const cpp_macro *macro;
3198   unsigned char *buffer;
3199
3200   if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3201     {
3202       if (node->type != NT_MACRO
3203           || !pfile->cb.user_builtin_macro
3204           || !pfile->cb.user_builtin_macro (pfile, node))
3205         {
3206           cpp_error (pfile, CPP_DL_ICE,
3207                      "invalid hash type %d in cpp_macro_definition",
3208                      node->type);
3209           return 0;
3210         }
3211     }
3212
3213   macro = node->value.macro;
3214   /* Calculate length.  */
3215   len = NODE_LEN (node) + 2;                    /* ' ' and NUL.  */
3216   if (macro->fun_like)
3217     {
3218       len += 4;         /* "()" plus possible final ".." of named
3219                            varargs (we have + 1 below).  */
3220       for (i = 0; i < macro->paramc; i++)
3221         len += NODE_LEN (macro->params[i]) + 1; /* "," */
3222     }
3223
3224   /* This should match below where we fill in the buffer.  */
3225   if (CPP_OPTION (pfile, traditional))
3226     len += _cpp_replacement_text_len (macro);
3227   else
3228     {
3229       unsigned int count = macro_real_token_count (macro);
3230       for (i = 0; i < count; i++)
3231         {
3232           cpp_token *token = &macro->exp.tokens[i];
3233
3234           if (token->type == CPP_MACRO_ARG)
3235             len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
3236           else
3237             len += cpp_token_len (token);
3238
3239           if (token->flags & STRINGIFY_ARG)
3240             len++;                      /* "#" */
3241           if (token->flags & PASTE_LEFT)
3242             len += 3;           /* " ##" */
3243           if (token->flags & PREV_WHITE)
3244             len++;              /* " " */
3245         }
3246     }
3247
3248   if (len > pfile->macro_buffer_len)
3249     {
3250       pfile->macro_buffer = XRESIZEVEC (unsigned char,
3251                                         pfile->macro_buffer, len);
3252       pfile->macro_buffer_len = len;
3253     }
3254
3255   /* Fill in the buffer.  Start with the macro name.  */
3256   buffer = pfile->macro_buffer;
3257   memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
3258   buffer += NODE_LEN (node);
3259
3260   /* Parameter names.  */
3261   if (macro->fun_like)
3262     {
3263       *buffer++ = '(';
3264       for (i = 0; i < macro->paramc; i++)
3265         {
3266           cpp_hashnode *param = macro->params[i];
3267
3268           if (param != pfile->spec_nodes.n__VA_ARGS__)
3269             {
3270               memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3271               buffer += NODE_LEN (param);
3272             }
3273
3274           if (i + 1 < macro->paramc)
3275             /* Don't emit a space after the comma here; we're trying
3276                to emit a Dwarf-friendly definition, and the Dwarf spec
3277                forbids spaces in the argument list.  */
3278             *buffer++ = ',';
3279           else if (macro->variadic)
3280             *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3281         }
3282       *buffer++ = ')';
3283     }
3284
3285   /* The Dwarf spec requires a space after the macro name, even if the
3286      definition is the empty string.  */
3287   *buffer++ = ' ';
3288
3289   if (CPP_OPTION (pfile, traditional))
3290     buffer = _cpp_copy_replacement_text (macro, buffer);
3291   else if (macro->count)
3292   /* Expansion tokens.  */
3293     {
3294       unsigned int count = macro_real_token_count (macro);
3295       for (i = 0; i < count; i++)
3296         {
3297           cpp_token *token = &macro->exp.tokens[i];
3298
3299           if (token->flags & PREV_WHITE)
3300             *buffer++ = ' ';
3301           if (token->flags & STRINGIFY_ARG)
3302             *buffer++ = '#';
3303
3304           if (token->type == CPP_MACRO_ARG)
3305             {
3306               memcpy (buffer,
3307                       NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]),
3308                       NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]));
3309               buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
3310             }
3311           else
3312             buffer = cpp_spell_token (pfile, token, buffer, false);
3313
3314           if (token->flags & PASTE_LEFT)
3315             {
3316               *buffer++ = ' ';
3317               *buffer++ = '#';
3318               *buffer++ = '#';
3319               /* Next has PREV_WHITE; see _cpp_create_definition.  */
3320             }
3321         }
3322     }
3323
3324   *buffer = '\0';
3325   return pfile->macro_buffer;
3326 }