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