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