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