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