macro.c revision 258501
197403Sobrien/* Part of CPP library.  (Macro and #define handling.)
251920Sobrien   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
397403Sobrien   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
497403Sobrien   Written by Per Bothner, 1994.
597403Sobrien   Based on CCCP program by Paul Rubin, June 1986
697403Sobrien   Adapted to ANSI C, Richard Stallman, Jan 1987
751920Sobrien
897403SobrienThis program is free software; you can redistribute it and/or modify it
997403Sobrienunder the terms of the GNU General Public License as published by the
1097403SobrienFree Software Foundation; either version 2, or (at your option) any
1197403Sobrienlater version.
1251920Sobrien
1351920SobrienThis program is distributed in the hope that it will be useful,
1497403Sobrienbut WITHOUT ANY WARRANTY; without even the implied warranty of
1551920SobrienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1697403SobrienGNU General Public License for more details.
1797403Sobrien
1897403SobrienYou should have received a copy of the GNU General Public License
1997403Sobrienalong with this program; if not, write to the Free Software
2097403SobrienFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2152277Sobrien
2297403Sobrien In other words, you are welcome to use, share and improve this program.
2397403Sobrien You are forbidden to forbid anyone else to use, share and improve
2497403Sobrien what you give them.   Help stamp out software-hoarding!  */
2597403Sobrien
2697403Sobrien#include "config.h"
2797403Sobrien#include "system.h"
2897403Sobrien#include "cpplib.h"
2997403Sobrien#include "internal.h"
3097403Sobrien
3197403Sobrientypedef struct macro_arg macro_arg;
3297403Sobrienstruct macro_arg
3397403Sobrien{
3451920Sobrien  const cpp_token **first;	/* First token in unexpanded argument.  */
3597403Sobrien  const cpp_token **expanded;	/* Macro-expanded argument.  */
3651920Sobrien  const cpp_token *stringified;	/* Stringified argument.  */
3797403Sobrien  unsigned int count;		/* # of tokens in argument.  */
3897403Sobrien  unsigned int expanded_count;	/* # of tokens in expanded argument.  */
3997403Sobrien};
4051920Sobrien
4197403Sobrien/* Macro expansion.  */
4251920Sobrien
4397403Sobrienstatic int enter_macro_context (cpp_reader *, cpp_hashnode *);
4497403Sobrienstatic int builtin_macro (cpp_reader *, cpp_hashnode *);
4597403Sobrienstatic void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
4697403Sobrien				 const cpp_token **, unsigned int);
4751920Sobrienstatic _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *);
4897403Sobrienstatic cpp_context *next_context (cpp_reader *);
4997403Sobrienstatic const cpp_token *padding_token (cpp_reader *, const cpp_token *);
5097403Sobrienstatic void expand_arg (cpp_reader *, macro_arg *);
5197403Sobrienstatic const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
5297403Sobrienstatic const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
5351920Sobrienstatic void paste_all_tokens (cpp_reader *, const cpp_token *);
5497403Sobrienstatic bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
5597403Sobrienstatic void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
5697403Sobrien			  macro_arg *);
5797403Sobrienstatic _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *);
5897403Sobrienstatic bool create_iso_definition (cpp_reader *, cpp_macro *);
5997403Sobrien
6097403Sobrien/* #define directive parsing and handling.  */
6197403Sobrien
6297403Sobrienstatic cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
6397403Sobrienstatic cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
6497403Sobrienstatic bool warn_of_redefinition (cpp_reader *, const cpp_hashnode *,
6597403Sobrien				  const cpp_macro *);
6697403Sobrienstatic bool parse_params (cpp_reader *, cpp_macro *);
6797403Sobrienstatic void check_trad_stringification (cpp_reader *, const cpp_macro *,
6897403Sobrien					const cpp_string *);
6997403Sobrien
7097403Sobrien/* Emits a warning if NODE is a macro defined in the main file that
7197403Sobrien   has not been used.  */
7297403Sobrienint
7397403Sobrien_cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
7497403Sobrien			   void *v ATTRIBUTE_UNUSED)
7597403Sobrien{
7697403Sobrien  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
7797403Sobrien    {
7897403Sobrien      cpp_macro *macro = node->value.macro;
7997403Sobrien
8097403Sobrien      if (!macro->used
8197403Sobrien	  && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
8297403Sobrien	cpp_error_with_line (pfile, CPP_DL_WARNING, macro->line, 0,
8397403Sobrien			     "macro \"%s\" is not used", NODE_NAME (node));
8497403Sobrien    }
8597403Sobrien
8697403Sobrien  return 1;
8797403Sobrien}
8897403Sobrien
8997403Sobrien/* Allocates and returns a CPP_STRING token, containing TEXT of length
9097403Sobrien   LEN, after null-terminating it.  TEXT must be in permanent storage.  */
9197403Sobrienstatic const cpp_token *
9297403Sobriennew_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
9397403Sobrien{
9497403Sobrien  cpp_token *token = _cpp_temp_token (pfile);
9597403Sobrien
9697403Sobrien  text[len] = '\0';
9797403Sobrien  token->type = CPP_STRING;
9897403Sobrien  token->val.str.len = len;
9997403Sobrien  token->val.str.text = text;
10097403Sobrien  token->flags = 0;
10197403Sobrien  return token;
10297403Sobrien}
10397403Sobrien
10497403Sobrienstatic const char * const monthnames[] =
10597403Sobrien{
10697403Sobrien  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
10797403Sobrien  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
10897403Sobrien};
10997403Sobrien
11097403Sobrien/* Helper function for builtin_macro.  Returns the text generated by
11197403Sobrien   a builtin macro. */
11297403Sobrienconst uchar *
11397403Sobrien_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
11497403Sobrien{
11597403Sobrien  const struct line_map *map;
11697403Sobrien  const uchar *result = NULL;
11797403Sobrien  unsigned int number = 1;
11897403Sobrien
11997403Sobrien  switch (node->value.builtin)
12097403Sobrien    {
12197403Sobrien    default:
12297403Sobrien      cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
12397403Sobrien		 NODE_NAME (node));
12497403Sobrien      break;
12597403Sobrien
12697403Sobrien    case BT_TIMESTAMP:
12797403Sobrien      {
12897403Sobrien	cpp_buffer *pbuffer = cpp_get_buffer (pfile);
12997403Sobrien	if (pbuffer->timestamp == NULL)
13097403Sobrien	  {
13197403Sobrien	    /* Initialize timestamp value of the assotiated file. */
13297403Sobrien            struct _cpp_file *file = cpp_get_file (pbuffer);
13397403Sobrien	    if (file)
13497403Sobrien	      {
13597403Sobrien    		/* Generate __TIMESTAMP__ string, that represents
13697403Sobrien		   the date and time of the last modification
13797403Sobrien		   of the current source file. The string constant
13897403Sobrien		   looks like "Sun Sep 16 01:03:52 1973".  */
13997403Sobrien		struct tm *tb = NULL;
14097403Sobrien		struct stat *st = _cpp_get_file_stat (file);
14197403Sobrien		if (st)
14297403Sobrien		  tb = localtime (&st->st_mtime);
14397403Sobrien		if (tb)
14497403Sobrien		  {
14597403Sobrien		    char *str = asctime (tb);
14697403Sobrien		    size_t len = strlen (str);
14797403Sobrien		    unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
14897403Sobrien		    buf[0] = '"';
14997403Sobrien		    strcpy ((char *) buf + 1, str);
15097403Sobrien		    buf[len] = '"';
15197403Sobrien		    pbuffer->timestamp = buf;
15297403Sobrien		  }
15397403Sobrien		else
15497403Sobrien		  {
15551920Sobrien		    cpp_errno (pfile, CPP_DL_WARNING,
15697403Sobrien			"could not determine file timestamp");
15797403Sobrien		    pbuffer->timestamp = U"\"??? ??? ?? ??:??:?? ????\"";
15851920Sobrien		  }
15997403Sobrien	      }
16051920Sobrien	  }
16197403Sobrien	result = pbuffer->timestamp;
16251920Sobrien      }
16397403Sobrien      break;
16497403Sobrien    case BT_FILE:
16551920Sobrien    case BT_BASE_FILE:
16697403Sobrien      {
16797403Sobrien	unsigned int len;
16897403Sobrien	const char *name;
16997403Sobrien	uchar *buf;
17097403Sobrien	map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
17197403Sobrien
17297403Sobrien	if (node->value.builtin == BT_BASE_FILE)
17351920Sobrien	  while (! MAIN_FILE_P (map))
17497403Sobrien	    map = INCLUDED_FROM (pfile->line_table, map);
17597403Sobrien
17697403Sobrien	name = map->to_file;
17797403Sobrien	len = strlen (name);
17897403Sobrien	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
17997403Sobrien	result = buf;
18097403Sobrien	*buf = '"';
18197403Sobrien	buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
18297403Sobrien	*buf++ = '"';
18397403Sobrien	*buf = '\0';
18497403Sobrien      }
18597403Sobrien      break;
18697403Sobrien
18797403Sobrien    case BT_INCLUDE_LEVEL:
18897403Sobrien      /* The line map depth counts the primary source as level 1, but
18997403Sobrien	 historically __INCLUDE_DEPTH__ has called the primary source
19097403Sobrien	 level 0.  */
19197403Sobrien      number = pfile->line_table->depth - 1;
19297403Sobrien      break;
19397403Sobrien
19497403Sobrien    case BT_SPECLINE:
19597403Sobrien      map = &pfile->line_table->maps[pfile->line_table->used-1];
19697403Sobrien      /* If __LINE__ is embedded in a macro, it must expand to the
19797403Sobrien	 line of the macro's invocation, not its definition.
19897403Sobrien	 Otherwise things like assert() will not work properly.  */
19997403Sobrien      if (CPP_OPTION (pfile, traditional))
20097403Sobrien	number = pfile->line_table->highest_line;
20197403Sobrien      else
20297403Sobrien	number = pfile->cur_token[-1].src_loc;
20397403Sobrien      number = SOURCE_LINE (map, number);
20497403Sobrien      break;
20597403Sobrien
20697403Sobrien      /* __STDC__ has the value 1 under normal circumstances.
20797403Sobrien	 However, if (a) we are in a system header, (b) the option
20897403Sobrien	 stdc_0_in_system_headers is true (set by target config), and
20997403Sobrien	 (c) we are not in strictly conforming mode, then it has the
21097403Sobrien	 value 0.  (b) and (c) are already checked in cpp_init_builtins.  */
21197403Sobrien    case BT_STDC:
21297403Sobrien      if (cpp_in_system_header (pfile))
21397403Sobrien	number = 0;
21497403Sobrien      else
21551920Sobrien	number = 1;
21697403Sobrien      break;
21797403Sobrien
21897403Sobrien    case BT_DATE:
21997403Sobrien    case BT_TIME:
22097403Sobrien      if (pfile->date == NULL)
22197403Sobrien	{
22251920Sobrien	  /* Allocate __DATE__ and __TIME__ strings from permanent
22352277Sobrien	     storage.  We only do this once, and don't generate them
22497403Sobrien	     at init time, because time() and localtime() are very
22552277Sobrien	     slow on some systems.  */
22697403Sobrien	  time_t tt;
22797403Sobrien	  struct tm *tb = NULL;
22897403Sobrien
22997403Sobrien	  /* (time_t) -1 is a legitimate value for "number of seconds
23097403Sobrien	     since the Epoch", so we have to do a little dance to
23197403Sobrien	     distinguish that from a genuine error.  */
23251920Sobrien	  errno = 0;
23397403Sobrien	  tt = time(NULL);
23497403Sobrien	  if (tt != (time_t)-1 || errno == 0)
23597403Sobrien	    tb = localtime (&tt);
23651920Sobrien
23797403Sobrien	  if (tb)
23897403Sobrien	    {
23951920Sobrien	      pfile->date = _cpp_unaligned_alloc (pfile,
24097403Sobrien						  sizeof ("\"Oct 11 1347\""));
24197403Sobrien	      sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
24297403Sobrien		       monthnames[tb->tm_mon], tb->tm_mday,
24397403Sobrien		       tb->tm_year + 1900);
24451920Sobrien
24597403Sobrien	      pfile->time = _cpp_unaligned_alloc (pfile,
24697403Sobrien						  sizeof ("\"12:34:56\""));
24797403Sobrien	      sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
24897403Sobrien		       tb->tm_hour, tb->tm_min, tb->tm_sec);
24997403Sobrien	    }
25097403Sobrien	  else
25197403Sobrien	    {
25297403Sobrien	      cpp_errno (pfile, CPP_DL_WARNING,
25397403Sobrien			 "could not determine date and time");
25497403Sobrien
25597403Sobrien	      pfile->date = U"\"??? ?? ????\"";
25697403Sobrien	      pfile->time = U"\"??:??:??\"";
25797403Sobrien	    }
25897403Sobrien	}
25997403Sobrien
26097403Sobrien      if (node->value.builtin == BT_DATE)
26197403Sobrien	result = pfile->date;
26297403Sobrien      else
26351920Sobrien	result = pfile->time;
26497403Sobrien      break;
26551920Sobrien
26697403Sobrien    case BT_COUNTER:
26751920Sobrien      if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
26897403Sobrien	cpp_error (pfile, CPP_DL_ERROR,
26997403Sobrien	    "__COUNTER__ expanded inside directive with -fdirectives-only");
27051920Sobrien      number = pfile->nextcounter++;
27197403Sobrien      break;
27297403Sobrien    }
27397403Sobrien
27497403Sobrien  if (result == NULL)
27597403Sobrien    {
27651920Sobrien      /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers.  */
27797403Sobrien      result = _cpp_unaligned_alloc (pfile, 21);
27897403Sobrien      sprintf ((char *) result, "%u", number);
27997403Sobrien    }
28097403Sobrien
28197403Sobrien  return result;
28297403Sobrien}
28351920Sobrien
28497403Sobrien/* Convert builtin macros like __FILE__ to a token and push it on the
28551920Sobrien   context stack.  Also handles _Pragma, for which a new token may not
28697403Sobrien   be created.  Returns 1 if it generates a new token context, 0 to
28797403Sobrien   return the token to the caller.  */
28897403Sobrienstatic int
28997403Sobrienbuiltin_macro (cpp_reader *pfile, cpp_hashnode *node)
29097403Sobrien{
29197403Sobrien  const uchar *buf;
29297403Sobrien  size_t len;
29397403Sobrien  char *nbuf;
29497403Sobrien
29597403Sobrien  if (node->value.builtin == BT_PRAGMA)
29697403Sobrien    {
29797403Sobrien      /* Don't interpret _Pragma within directives.  The standard is
29897403Sobrien         not clear on this, but to me this makes most sense.  */
29997403Sobrien      if (pfile->state.in_directive)
30097403Sobrien	return 0;
30197403Sobrien
30297403Sobrien      _cpp_do__Pragma (pfile);
30397403Sobrien      return 1;
30497403Sobrien    }
30597403Sobrien
30697403Sobrien  buf = _cpp_builtin_macro_text (pfile, node);
30797403Sobrien  len = ustrlen (buf);
30897403Sobrien  nbuf = (char *) alloca (len + 1);
30997403Sobrien  memcpy (nbuf, buf, len);
31097403Sobrien  nbuf[len]='\n';
31197403Sobrien
31297403Sobrien  cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
31397403Sobrien  _cpp_clean_line (pfile);
31497403Sobrien
31597403Sobrien  /* Set pfile->cur_token as required by _cpp_lex_direct.  */
31697403Sobrien  pfile->cur_token = _cpp_temp_token (pfile);
31797403Sobrien  _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
31897403Sobrien  if (pfile->buffer->cur != pfile->buffer->rlimit)
31997403Sobrien    cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
32097403Sobrien	       NODE_NAME (node));
32197403Sobrien  _cpp_pop_buffer (pfile);
32297403Sobrien
32397403Sobrien  return 1;
32497403Sobrien}
32597403Sobrien
32697403Sobrien/* Copies SRC, of length LEN, to DEST, adding backslashes before all
32797403Sobrien   backslashes and double quotes. DEST must be of sufficient size.
32897403Sobrien   Returns a pointer to the end of the string.  */
32997403Sobrienuchar *
33051920Sobriencpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
33151920Sobrien{
33297403Sobrien  while (len--)
33351920Sobrien    {
33497403Sobrien      uchar c = *src++;
33597403Sobrien
33697403Sobrien      if (c == '\\' || c == '"')
33797403Sobrien	{
33897403Sobrien	  *dest++ = '\\';
33997403Sobrien	  *dest++ = c;
34097403Sobrien	}
34197403Sobrien      else
34297403Sobrien	  *dest++ = c;
34397403Sobrien    }
34497403Sobrien
34597403Sobrien  return dest;
34697403Sobrien}
34797403Sobrien
34897403Sobrien/* Convert a token sequence ARG to a single string token according to
34997403Sobrien   the rules of the ISO C #-operator.  */
35097403Sobrienstatic const cpp_token *
35197403Sobrienstringify_arg (cpp_reader *pfile, macro_arg *arg)
35297403Sobrien{
35397403Sobrien  unsigned char *dest;
35497403Sobrien  unsigned int i, escape_it, backslash_count = 0;
35597403Sobrien  const cpp_token *source = NULL;
35697403Sobrien  size_t len;
35797403Sobrien
35897403Sobrien  if (BUFF_ROOM (pfile->u_buff) < 3)
35997403Sobrien    _cpp_extend_buff (pfile, &pfile->u_buff, 3);
36097403Sobrien  dest = BUFF_FRONT (pfile->u_buff);
36197403Sobrien  *dest++ = '"';
36297403Sobrien
36397403Sobrien  /* Loop, reading in the argument's tokens.  */
36497403Sobrien  for (i = 0; i < arg->count; i++)
36597403Sobrien    {
36697403Sobrien      const cpp_token *token = arg->first[i];
36797403Sobrien
36897403Sobrien      if (token->type == CPP_PADDING)
36997403Sobrien	{
37097403Sobrien	  if (source == NULL)
37197403Sobrien	    source = token->val.source;
37297403Sobrien	  continue;
37397403Sobrien	}
37497403Sobrien
37597403Sobrien      escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
37697403Sobrien		   || token->type == CPP_CHAR || token->type == CPP_WCHAR);
37797403Sobrien
37897403Sobrien      /* Room for each char being written in octal, initial space and
37997403Sobrien	 final quote and NUL.  */
38097403Sobrien      len = cpp_token_len (token);
38197403Sobrien      if (escape_it)
38297403Sobrien	len *= 4;
38397403Sobrien      len += 3;
38497403Sobrien
38597403Sobrien      if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
38697403Sobrien	{
38797403Sobrien	  size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
38897403Sobrien	  _cpp_extend_buff (pfile, &pfile->u_buff, len);
38997403Sobrien	  dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
39097403Sobrien	}
39197403Sobrien
39297403Sobrien      /* Leading white space?  */
39352277Sobrien      if (dest - 1 != BUFF_FRONT (pfile->u_buff))
39497403Sobrien	{
39597403Sobrien	  if (source == NULL)
39697403Sobrien	    source = token;
39752277Sobrien	  if (source->flags & PREV_WHITE)
39897403Sobrien	    *dest++ = ' ';
39997403Sobrien	}
40097403Sobrien      source = NULL;
40197403Sobrien
40297403Sobrien      if (escape_it)
40397403Sobrien	{
40497403Sobrien	  _cpp_buff *buff = _cpp_get_buff (pfile, len);
40597403Sobrien	  unsigned char *buf = BUFF_FRONT (buff);
40697403Sobrien	  len = cpp_spell_token (pfile, token, buf, true) - buf;
40752277Sobrien	  dest = cpp_quote_string (dest, buf, len);
40851920Sobrien	  _cpp_release_buff (pfile, buff);
40997403Sobrien	}
41097403Sobrien      else
41197403Sobrien	dest = cpp_spell_token (pfile, token, dest, true);
41297403Sobrien
41397403Sobrien      if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
41497403Sobrien	backslash_count++;
41597403Sobrien      else
41697403Sobrien	backslash_count = 0;
41797403Sobrien    }
41897403Sobrien
41997403Sobrien  /* Ignore the final \ of invalid string literals.  */
42097403Sobrien  if (backslash_count & 1)
42151920Sobrien    {
42297403Sobrien      cpp_error (pfile, CPP_DL_WARNING,
42397403Sobrien		 "invalid string literal, ignoring final '\\'");
42451920Sobrien      dest--;
42597403Sobrien    }
42697403Sobrien
42751920Sobrien  /* Commit the memory, including NUL, and return the token.  */
42897403Sobrien  *dest++ = '"';
42997403Sobrien  len = dest - BUFF_FRONT (pfile->u_buff);
43097403Sobrien  BUFF_FRONT (pfile->u_buff) = dest + 1;
43197403Sobrien  return new_string_token (pfile, dest - len, len);
43297403Sobrien}
43397403Sobrien
43497403Sobrien/* Try to paste two tokens.  On success, return nonzero.  In any
43597403Sobrien   case, PLHS is updated to point to the pasted token, which is
43697403Sobrien   guaranteed to not have the PASTE_LEFT flag set.  */
43797403Sobrienstatic bool
43897403Sobrienpaste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
43951920Sobrien{
44051920Sobrien  unsigned char *buf, *end, *lhsend;
44197403Sobrien  const cpp_token *lhs;
44297403Sobrien  unsigned int len;
44397403Sobrien
44497403Sobrien  lhs = *plhs;
44597403Sobrien  len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
44697403Sobrien  buf = (unsigned char *) alloca (len);
44797403Sobrien  end = lhsend = cpp_spell_token (pfile, lhs, buf, false);
44897403Sobrien
44997403Sobrien  /* Avoid comment headers, since they are still processed in stage 3.
45097403Sobrien     It is simpler to insert a space here, rather than modifying the
45197403Sobrien     lexer to ignore comments in some circumstances.  Simply returning
45297403Sobrien     false doesn't work, since we want to clear the PASTE_LEFT flag.  */
45397403Sobrien  if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
45497403Sobrien    *end++ = ' ';
45597403Sobrien  end = cpp_spell_token (pfile, rhs, end, false);
45697403Sobrien  *end = '\n';
45797403Sobrien
45897403Sobrien  cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
45997403Sobrien  _cpp_clean_line (pfile);
46097403Sobrien
46197403Sobrien  /* Set pfile->cur_token as required by _cpp_lex_direct.  */
46297403Sobrien  pfile->cur_token = _cpp_temp_token (pfile);
46397403Sobrien  *plhs = _cpp_lex_direct (pfile);
46497403Sobrien  if (pfile->buffer->cur != pfile->buffer->rlimit)
46597403Sobrien    {
46697403Sobrien      _cpp_pop_buffer (pfile);
46797403Sobrien      _cpp_backup_tokens (pfile, 1);
46897403Sobrien      *lhsend = '\0';
46997403Sobrien
47097403Sobrien      /* Mandatory error for all apart from assembler.  */
47197403Sobrien      if (CPP_OPTION (pfile, lang) != CLK_ASM)
47297403Sobrien	cpp_error (pfile, CPP_DL_ERROR,
47397403Sobrien	 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
47497403Sobrien		   buf, cpp_token_as_text (pfile, rhs));
47597403Sobrien      return false;
47697403Sobrien    }
47797403Sobrien
47897403Sobrien  _cpp_pop_buffer (pfile);
47997403Sobrien  return true;
48097403Sobrien}
48197403Sobrien
48297403Sobrien/* Handles an arbitrarily long sequence of ## operators, with initial
48397403Sobrien   operand LHS.  This implementation is left-associative,
48497403Sobrien   non-recursive, and finishes a paste before handling succeeding
48597403Sobrien   ones.  If a paste fails, we back up to the RHS of the failing ##
48697403Sobrien   operator before pushing the context containing the result of prior
48797403Sobrien   successful pastes, with the effect that the RHS appears in the
48897403Sobrien   output stream after the pasted LHS normally.  */
48997403Sobrienstatic void
49097403Sobrienpaste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
49197403Sobrien{
49297403Sobrien  const cpp_token *rhs;
49397403Sobrien  cpp_context *context = pfile->context;
49497403Sobrien
49597403Sobrien  do
49697403Sobrien    {
49797403Sobrien      /* Take the token directly from the current context.  We can do
49897403Sobrien	 this, because we are in the replacement list of either an
49997403Sobrien	 object-like macro, or a function-like macro with arguments
50097403Sobrien	 inserted.  In either case, the constraints to #define
50197403Sobrien	 guarantee we have at least one more token.  */
50297403Sobrien      if (context->direct_p)
50397403Sobrien	rhs = FIRST (context).token++;
50497403Sobrien      else
50597403Sobrien	rhs = *FIRST (context).ptoken++;
50697403Sobrien
50797403Sobrien      if (rhs->type == CPP_PADDING)
50897403Sobrien	abort ();
50997403Sobrien
51097403Sobrien      if (!paste_tokens (pfile, &lhs, rhs))
51197403Sobrien	break;
51297403Sobrien    }
51397403Sobrien  while (rhs->flags & PASTE_LEFT);
51497403Sobrien
51597403Sobrien  /* Put the resulting token in its own context.  */
51697403Sobrien  _cpp_push_token_context (pfile, NULL, lhs, 1);
51797403Sobrien}
51897403Sobrien
51997403Sobrien/* Returns TRUE if the number of arguments ARGC supplied in an
52097403Sobrien   invocation of the MACRO referenced by NODE is valid.  An empty
52197403Sobrien   invocation to a macro with no parameters should pass ARGC as zero.
52297403Sobrien
52397403Sobrien   Note that MACRO cannot necessarily be deduced from NODE, in case
52497403Sobrien   NODE was redefined whilst collecting arguments.  */
52597403Sobrienbool
52697403Sobrien_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
52797403Sobrien{
52897403Sobrien  if (argc == macro->paramc)
52997403Sobrien    return true;
53097403Sobrien
53197403Sobrien  if (argc < macro->paramc)
53297403Sobrien    {
53397403Sobrien      /* As an extension, a rest argument is allowed to not appear in
53497403Sobrien	 the invocation at all.
53597403Sobrien	 e.g. #define debug(format, args...) something
53697403Sobrien	 debug("string");
53797403Sobrien
53897403Sobrien	 This is exactly the same as if there had been an empty rest
53997403Sobrien	 argument - debug("string", ).  */
54097403Sobrien
54197403Sobrien      if (argc + 1 == macro->paramc && macro->variadic)
54297403Sobrien	{
54397403Sobrien	  if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
54497403Sobrien	    cpp_error (pfile, CPP_DL_PEDWARN,
54597403Sobrien		       "ISO C99 requires rest arguments to be used");
54697403Sobrien	  return true;
54797403Sobrien	}
54897403Sobrien
54997403Sobrien      cpp_error (pfile, CPP_DL_ERROR,
55097403Sobrien		 "macro \"%s\" requires %u arguments, but only %u given",
55197403Sobrien		 NODE_NAME (node), macro->paramc, argc);
55297403Sobrien    }
55397403Sobrien  else
554    cpp_error (pfile, CPP_DL_ERROR,
555	       "macro \"%s\" passed %u arguments, but takes just %u",
556	       NODE_NAME (node), argc, macro->paramc);
557
558  return false;
559}
560
561/* Reads and returns the arguments to a function-like macro
562   invocation.  Assumes the opening parenthesis has been processed.
563   If there is an error, emits an appropriate diagnostic and returns
564   NULL.  Each argument is terminated by a CPP_EOF token, for the
565   future benefit of expand_arg().  */
566static _cpp_buff *
567collect_args (cpp_reader *pfile, const cpp_hashnode *node)
568{
569  _cpp_buff *buff, *base_buff;
570  cpp_macro *macro;
571  macro_arg *args, *arg;
572  const cpp_token *token;
573  unsigned int argc;
574
575  macro = node->value.macro;
576  if (macro->paramc)
577    argc = macro->paramc;
578  else
579    argc = 1;
580  buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
581				       + sizeof (macro_arg)));
582  base_buff = buff;
583  args = (macro_arg *) buff->base;
584  memset (args, 0, argc * sizeof (macro_arg));
585  buff->cur = (unsigned char *) &args[argc];
586  arg = args, argc = 0;
587
588  /* Collect the tokens making up each argument.  We don't yet know
589     how many arguments have been supplied, whether too many or too
590     few.  Hence the slightly bizarre usage of "argc" and "arg".  */
591  do
592    {
593      unsigned int paren_depth = 0;
594      unsigned int ntokens = 0;
595
596      argc++;
597      arg->first = (const cpp_token **) buff->cur;
598
599      for (;;)
600	{
601	  /* Require space for 2 new tokens (including a CPP_EOF).  */
602	  if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
603	    {
604	      buff = _cpp_append_extend_buff (pfile, buff,
605					      1000 * sizeof (cpp_token *));
606	      arg->first = (const cpp_token **) buff->cur;
607	    }
608
609	  token = cpp_get_token (pfile);
610
611	  if (token->type == CPP_PADDING)
612	    {
613	      /* Drop leading padding.  */
614	      if (ntokens == 0)
615		continue;
616	    }
617	  else if (token->type == CPP_OPEN_PAREN)
618	    paren_depth++;
619	  else if (token->type == CPP_CLOSE_PAREN)
620	    {
621	      if (paren_depth-- == 0)
622		break;
623	    }
624	  else if (token->type == CPP_COMMA)
625	    {
626	      /* A comma does not terminate an argument within
627		 parentheses or as part of a variable argument.  */
628	      if (paren_depth == 0
629		  && ! (macro->variadic && argc == macro->paramc))
630		break;
631	    }
632	  else if (token->type == CPP_EOF
633		   || (token->type == CPP_HASH && token->flags & BOL))
634	    break;
635
636	  arg->first[ntokens++] = token;
637	}
638
639      /* Drop trailing padding.  */
640      while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
641	ntokens--;
642
643      arg->count = ntokens;
644      arg->first[ntokens] = &pfile->eof;
645
646      /* Terminate the argument.  Excess arguments loop back and
647	 overwrite the final legitimate argument, before failing.  */
648      if (argc <= macro->paramc)
649	{
650	  buff->cur = (unsigned char *) &arg->first[ntokens + 1];
651	  if (argc != macro->paramc)
652	    arg++;
653	}
654    }
655  while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
656
657  if (token->type == CPP_EOF)
658    {
659      /* We still need the CPP_EOF to end directives, and to end
660	 pre-expansion of a macro argument.  Step back is not
661	 unconditional, since we don't want to return a CPP_EOF to our
662	 callers at the end of an -include-d file.  */
663      if (pfile->context->prev || pfile->state.in_directive)
664	_cpp_backup_tokens (pfile, 1);
665      cpp_error (pfile, CPP_DL_ERROR,
666		 "unterminated argument list invoking macro \"%s\"",
667		 NODE_NAME (node));
668    }
669  else
670    {
671      /* A single empty argument is counted as no argument.  */
672      if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
673	argc = 0;
674      if (_cpp_arguments_ok (pfile, macro, node, argc))
675	{
676	  /* GCC has special semantics for , ## b where b is a varargs
677	     parameter: we remove the comma if b was omitted entirely.
678	     If b was merely an empty argument, the comma is retained.
679	     If the macro takes just one (varargs) parameter, then we
680	     retain the comma only if we are standards conforming.
681
682	     If FIRST is NULL replace_args () swallows the comma.  */
683	  if (macro->variadic && (argc < macro->paramc
684				  || (argc == 1 && args[0].count == 0
685				      && !CPP_OPTION (pfile, std))))
686	    args[macro->paramc - 1].first = NULL;
687	  return base_buff;
688	}
689    }
690
691  /* An error occurred.  */
692  _cpp_release_buff (pfile, base_buff);
693  return NULL;
694}
695
696/* Search for an opening parenthesis to the macro of NODE, in such a
697   way that, if none is found, we don't lose the information in any
698   intervening padding tokens.  If we find the parenthesis, collect
699   the arguments and return the buffer containing them.  */
700static _cpp_buff *
701funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
702{
703  const cpp_token *token, *padding = NULL;
704
705  for (;;)
706    {
707      token = cpp_get_token (pfile);
708      if (token->type != CPP_PADDING)
709	break;
710      if (padding == NULL
711	  || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
712	padding = token;
713    }
714
715  if (token->type == CPP_OPEN_PAREN)
716    {
717      pfile->state.parsing_args = 2;
718      return collect_args (pfile, node);
719    }
720
721  /* CPP_EOF can be the end of macro arguments, or the end of the
722     file.  We mustn't back up over the latter.  Ugh.  */
723  if (token->type != CPP_EOF || token == &pfile->eof)
724    {
725      /* Back up.  We may have skipped padding, in which case backing
726	 up more than one token when expanding macros is in general
727	 too difficult.  We re-insert it in its own context.  */
728      _cpp_backup_tokens (pfile, 1);
729      if (padding)
730	_cpp_push_token_context (pfile, NULL, padding, 1);
731    }
732
733  return NULL;
734}
735
736/* Push the context of a macro with hash entry NODE onto the context
737   stack.  If we can successfully expand the macro, we push a context
738   containing its yet-to-be-rescanned replacement list and return one.
739   Otherwise, we don't push a context and return zero.  */
740static int
741enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
742{
743  /* The presence of a macro invalidates a file's controlling macro.  */
744  pfile->mi_valid = false;
745
746  pfile->state.angled_headers = false;
747
748  /* Handle standard macros.  */
749  if (! (node->flags & NODE_BUILTIN))
750    {
751      cpp_macro *macro = node->value.macro;
752
753      if (macro->fun_like)
754	{
755	  _cpp_buff *buff;
756
757	  pfile->state.prevent_expansion++;
758	  pfile->keep_tokens++;
759	  pfile->state.parsing_args = 1;
760	  buff = funlike_invocation_p (pfile, node);
761	  pfile->state.parsing_args = 0;
762	  pfile->keep_tokens--;
763	  pfile->state.prevent_expansion--;
764
765	  if (buff == NULL)
766	    {
767	      if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
768		cpp_error (pfile, CPP_DL_WARNING,
769 "function-like macro \"%s\" must be used with arguments in traditional C",
770			   NODE_NAME (node));
771
772	      return 0;
773	    }
774
775	  if (macro->paramc > 0)
776	    replace_args (pfile, node, macro, (macro_arg *) buff->base);
777	  _cpp_release_buff (pfile, buff);
778	}
779
780      /* Disable the macro within its expansion.  */
781      node->flags |= NODE_DISABLED;
782
783      macro->used = 1;
784
785      if (macro->paramc == 0)
786	_cpp_push_token_context (pfile, node, macro->exp.tokens, macro->count);
787
788      return 1;
789    }
790
791  /* Handle built-in macros and the _Pragma operator.  */
792  return builtin_macro (pfile, node);
793}
794
795/* Replace the parameters in a function-like macro of NODE with the
796   actual ARGS, and place the result in a newly pushed token context.
797   Expand each argument before replacing, unless it is operated upon
798   by the # or ## operators.  */
799static void
800replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
801{
802  unsigned int i, total;
803  const cpp_token *src, *limit;
804  const cpp_token **dest, **first;
805  macro_arg *arg;
806  _cpp_buff *buff;
807
808  /* First, fully macro-expand arguments, calculating the number of
809     tokens in the final expansion as we go.  The ordering of the if
810     statements below is subtle; we must handle stringification before
811     pasting.  */
812  total = macro->count;
813  limit = macro->exp.tokens + macro->count;
814
815  for (src = macro->exp.tokens; src < limit; src++)
816    if (src->type == CPP_MACRO_ARG)
817      {
818	/* Leading and trailing padding tokens.  */
819	total += 2;
820
821	/* We have an argument.  If it is not being stringified or
822	   pasted it is macro-replaced before insertion.  */
823	arg = &args[src->val.arg_no - 1];
824
825	if (src->flags & STRINGIFY_ARG)
826	  {
827	    if (!arg->stringified)
828	      arg->stringified = stringify_arg (pfile, arg);
829	  }
830	else if ((src->flags & PASTE_LEFT)
831		 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
832	  total += arg->count - 1;
833	else
834	  {
835	    if (!arg->expanded)
836	      expand_arg (pfile, arg);
837	    total += arg->expanded_count - 1;
838	  }
839      }
840
841  /* Now allocate space for the expansion, copy the tokens and replace
842     the arguments.  */
843  buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
844  first = (const cpp_token **) buff->base;
845  dest = first;
846
847  for (src = macro->exp.tokens; src < limit; src++)
848    {
849      unsigned int count;
850      const cpp_token **from, **paste_flag;
851
852      if (src->type != CPP_MACRO_ARG)
853	{
854	  *dest++ = src;
855	  continue;
856	}
857
858      paste_flag = 0;
859      arg = &args[src->val.arg_no - 1];
860      if (src->flags & STRINGIFY_ARG)
861	count = 1, from = &arg->stringified;
862      else if (src->flags & PASTE_LEFT)
863	count = arg->count, from = arg->first;
864      else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
865	{
866	  count = arg->count, from = arg->first;
867	  if (dest != first)
868	    {
869	      if (dest[-1]->type == CPP_COMMA
870		  && macro->variadic
871		  && src->val.arg_no == macro->paramc)
872		{
873		  /* Swallow a pasted comma if from == NULL, otherwise
874		     drop the paste flag.  */
875		  if (from == NULL)
876		    dest--;
877		  else
878		    paste_flag = dest - 1;
879		}
880	      /* Remove the paste flag if the RHS is a placemarker.  */
881	      else if (count == 0)
882		paste_flag = dest - 1;
883	    }
884	}
885      else
886	count = arg->expanded_count, from = arg->expanded;
887
888      /* Padding on the left of an argument (unless RHS of ##).  */
889      if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
890	  && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
891	*dest++ = padding_token (pfile, src);
892
893      if (count)
894	{
895	  memcpy (dest, from, count * sizeof (cpp_token *));
896	  dest += count;
897
898	  /* With a non-empty argument on the LHS of ##, the last
899	     token should be flagged PASTE_LEFT.  */
900	  if (src->flags & PASTE_LEFT)
901	    paste_flag = dest - 1;
902	}
903
904      /* Avoid paste on RHS (even case count == 0).  */
905      if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
906	*dest++ = &pfile->avoid_paste;
907
908      /* Add a new paste flag, or remove an unwanted one.  */
909      if (paste_flag)
910	{
911	  cpp_token *token = _cpp_temp_token (pfile);
912	  token->type = (*paste_flag)->type;
913	  token->val = (*paste_flag)->val;
914	  if (src->flags & PASTE_LEFT)
915	    token->flags = (*paste_flag)->flags | PASTE_LEFT;
916	  else
917	    token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
918	  *paste_flag = token;
919	}
920    }
921
922  /* Free the expanded arguments.  */
923  for (i = 0; i < macro->paramc; i++)
924    if (args[i].expanded)
925      free (args[i].expanded);
926
927  push_ptoken_context (pfile, node, buff, first, dest - first);
928}
929
930/* Return a special padding token, with padding inherited from SOURCE.  */
931static const cpp_token *
932padding_token (cpp_reader *pfile, const cpp_token *source)
933{
934  cpp_token *result = _cpp_temp_token (pfile);
935
936  result->type = CPP_PADDING;
937
938  /* Data in GCed data structures cannot be made const so far, so we
939     need a cast here.  */
940  result->val.source = (cpp_token *) source;
941  result->flags = 0;
942  return result;
943}
944
945/* Get a new uninitialized context.  Create a new one if we cannot
946   re-use an old one.  */
947static cpp_context *
948next_context (cpp_reader *pfile)
949{
950  cpp_context *result = pfile->context->next;
951
952  if (result == 0)
953    {
954      result = XNEW (cpp_context);
955      result->prev = pfile->context;
956      result->next = 0;
957      pfile->context->next = result;
958    }
959
960  pfile->context = result;
961  return result;
962}
963
964/* Push a list of pointers to tokens.  */
965static void
966push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
967		     const cpp_token **first, unsigned int count)
968{
969  cpp_context *context = next_context (pfile);
970
971  context->direct_p = false;
972  context->macro = macro;
973  context->buff = buff;
974  FIRST (context).ptoken = first;
975  LAST (context).ptoken = first + count;
976}
977
978/* Push a list of tokens.  */
979void
980_cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
981			 const cpp_token *first, unsigned int count)
982{
983  cpp_context *context = next_context (pfile);
984
985  context->direct_p = true;
986  context->macro = macro;
987  context->buff = NULL;
988  FIRST (context).token = first;
989  LAST (context).token = first + count;
990}
991
992/* Push a traditional macro's replacement text.  */
993void
994_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
995			const uchar *start, size_t len)
996{
997  cpp_context *context = next_context (pfile);
998
999  context->direct_p = true;
1000  context->macro = macro;
1001  context->buff = NULL;
1002  CUR (context) = start;
1003  RLIMIT (context) = start + len;
1004  macro->flags |= NODE_DISABLED;
1005}
1006
1007/* Expand an argument ARG before replacing parameters in a
1008   function-like macro.  This works by pushing a context with the
1009   argument's tokens, and then expanding that into a temporary buffer
1010   as if it were a normal part of the token stream.  collect_args()
1011   has terminated the argument's tokens with a CPP_EOF so that we know
1012   when we have fully expanded the argument.  */
1013static void
1014expand_arg (cpp_reader *pfile, macro_arg *arg)
1015{
1016  unsigned int capacity;
1017  bool saved_warn_trad;
1018
1019  if (arg->count == 0)
1020    return;
1021
1022  /* Don't warn about funlike macros when pre-expanding.  */
1023  saved_warn_trad = CPP_WTRADITIONAL (pfile);
1024  CPP_WTRADITIONAL (pfile) = 0;
1025
1026  /* Loop, reading in the arguments.  */
1027  capacity = 256;
1028  arg->expanded = XNEWVEC (const cpp_token *, capacity);
1029
1030  push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
1031  for (;;)
1032    {
1033      const cpp_token *token;
1034
1035      if (arg->expanded_count + 1 >= capacity)
1036	{
1037	  capacity *= 2;
1038	  arg->expanded = XRESIZEVEC (const cpp_token *, arg->expanded,
1039                                      capacity);
1040	}
1041
1042      token = cpp_get_token (pfile);
1043
1044      if (token->type == CPP_EOF)
1045	break;
1046
1047      arg->expanded[arg->expanded_count++] = token;
1048    }
1049
1050  _cpp_pop_context (pfile);
1051
1052  CPP_WTRADITIONAL (pfile) = saved_warn_trad;
1053}
1054
1055/* Pop the current context off the stack, re-enabling the macro if the
1056   context represented a macro's replacement list.  The context
1057   structure is not freed so that we can re-use it later.  */
1058void
1059_cpp_pop_context (cpp_reader *pfile)
1060{
1061  cpp_context *context = pfile->context;
1062
1063  if (context->macro)
1064    context->macro->flags &= ~NODE_DISABLED;
1065
1066  if (context->buff)
1067    _cpp_release_buff (pfile, context->buff);
1068
1069  pfile->context = context->prev;
1070}
1071
1072/* External routine to get a token.  Also used nearly everywhere
1073   internally, except for places where we know we can safely call
1074   _cpp_lex_token directly, such as lexing a directive name.
1075
1076   Macro expansions and directives are transparently handled,
1077   including entering included files.  Thus tokens are post-macro
1078   expansion, and after any intervening directives.  External callers
1079   see CPP_EOF only at EOF.  Internal callers also see it when meeting
1080   a directive inside a macro call, when at the end of a directive and
1081   state.in_directive is still 1, and at the end of argument
1082   pre-expansion.  */
1083const cpp_token *
1084cpp_get_token (cpp_reader *pfile)
1085{
1086  const cpp_token *result;
1087
1088  for (;;)
1089    {
1090      cpp_hashnode *node;
1091      cpp_context *context = pfile->context;
1092
1093      /* Context->prev == 0 <=> base context.  */
1094      if (!context->prev)
1095	result = _cpp_lex_token (pfile);
1096      else if (FIRST (context).token != LAST (context).token)
1097	{
1098	  if (context->direct_p)
1099	    result = FIRST (context).token++;
1100	  else
1101	    result = *FIRST (context).ptoken++;
1102
1103	  if (result->flags & PASTE_LEFT)
1104	    {
1105	      paste_all_tokens (pfile, result);
1106	      if (pfile->state.in_directive)
1107		continue;
1108	      return padding_token (pfile, result);
1109	    }
1110	}
1111      else
1112	{
1113	  _cpp_pop_context (pfile);
1114	  if (pfile->state.in_directive)
1115	    continue;
1116	  return &pfile->avoid_paste;
1117	}
1118
1119      if (pfile->state.in_directive && result->type == CPP_COMMENT)
1120	continue;
1121
1122      if (result->type != CPP_NAME)
1123	break;
1124
1125      node = result->val.node;
1126
1127      if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1128	break;
1129
1130      if (!(node->flags & NODE_DISABLED))
1131	{
1132	  if (!pfile->state.prevent_expansion
1133	      && enter_macro_context (pfile, node))
1134	    {
1135	      if (pfile->state.in_directive)
1136		continue;
1137	      return padding_token (pfile, result);
1138	    }
1139	}
1140      else
1141	{
1142	  /* Flag this token as always unexpandable.  FIXME: move this
1143	     to collect_args()?.  */
1144	  cpp_token *t = _cpp_temp_token (pfile);
1145	  t->type = result->type;
1146	  t->flags = result->flags | NO_EXPAND;
1147	  t->val = result->val;
1148	  result = t;
1149	}
1150
1151      break;
1152    }
1153
1154  return result;
1155}
1156
1157/* Returns true if we're expanding an object-like macro that was
1158   defined in a system header.  Just checks the macro at the top of
1159   the stack.  Used for diagnostic suppression.  */
1160int
1161cpp_sys_macro_p (cpp_reader *pfile)
1162{
1163  cpp_hashnode *node = pfile->context->macro;
1164
1165  return node && node->value.macro && node->value.macro->syshdr;
1166}
1167
1168/* Read each token in, until end of the current file.  Directives are
1169   transparently processed.  */
1170void
1171cpp_scan_nooutput (cpp_reader *pfile)
1172{
1173  /* Request a CPP_EOF token at the end of this file, rather than
1174     transparently continuing with the including file.  */
1175  pfile->buffer->return_at_eof = true;
1176
1177  pfile->state.discarding_output++;
1178  pfile->state.prevent_expansion++;
1179
1180  if (CPP_OPTION (pfile, traditional))
1181    while (_cpp_read_logical_line_trad (pfile))
1182      ;
1183  else
1184    while (cpp_get_token (pfile)->type != CPP_EOF)
1185      ;
1186
1187  pfile->state.discarding_output--;
1188  pfile->state.prevent_expansion--;
1189}
1190
1191/* Step back one (or more) tokens.  Can only step back more than 1 if
1192   they are from the lexer, and not from macro expansion.  */
1193void
1194_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
1195{
1196  if (pfile->context->prev == NULL)
1197    {
1198      pfile->lookaheads += count;
1199      while (count--)
1200	{
1201	  pfile->cur_token--;
1202	  if (pfile->cur_token == pfile->cur_run->base
1203	      /* Possible with -fpreprocessed and no leading #line.  */
1204	      && pfile->cur_run->prev != NULL)
1205	    {
1206	      pfile->cur_run = pfile->cur_run->prev;
1207	      pfile->cur_token = pfile->cur_run->limit;
1208	    }
1209	}
1210    }
1211  else
1212    {
1213      if (count != 1)
1214	abort ();
1215      if (pfile->context->direct_p)
1216	FIRST (pfile->context).token--;
1217      else
1218	FIRST (pfile->context).ptoken--;
1219    }
1220}
1221
1222/* #define directive parsing and handling.  */
1223
1224/* Returns nonzero if a macro redefinition warning is required.  */
1225static bool
1226warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
1227		      const cpp_macro *macro2)
1228{
1229  const cpp_macro *macro1;
1230  unsigned int i;
1231
1232  /* Some redefinitions need to be warned about regardless.  */
1233  if (node->flags & NODE_WARN)
1234    return true;
1235
1236  /* Redefinition of a macro is allowed if and only if the old and new
1237     definitions are the same.  (6.10.3 paragraph 2).  */
1238  macro1 = node->value.macro;
1239
1240  /* Don't check count here as it can be different in valid
1241     traditional redefinitions with just whitespace differences.  */
1242  if (macro1->paramc != macro2->paramc
1243      || macro1->fun_like != macro2->fun_like
1244      || macro1->variadic != macro2->variadic)
1245    return true;
1246
1247  /* Check parameter spellings.  */
1248  for (i = 0; i < macro1->paramc; i++)
1249    if (macro1->params[i] != macro2->params[i])
1250      return true;
1251
1252  /* Check the replacement text or tokens.  */
1253  if (CPP_OPTION (pfile, traditional))
1254    return _cpp_expansions_different_trad (macro1, macro2);
1255
1256  if (macro1->count != macro2->count)
1257    return true;
1258
1259  for (i = 0; i < macro1->count; i++)
1260    if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
1261      return true;
1262
1263  return false;
1264}
1265
1266/* Free the definition of hashnode H.  */
1267void
1268_cpp_free_definition (cpp_hashnode *h)
1269{
1270  /* Macros and assertions no longer have anything to free.  */
1271  h->type = NT_VOID;
1272  /* Clear builtin flag in case of redefinition.  */
1273  h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
1274}
1275
1276/* Save parameter NODE to the parameter list of macro MACRO.  Returns
1277   zero on success, nonzero if the parameter is a duplicate.  */
1278bool
1279_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
1280{
1281  unsigned int len;
1282  /* Constraint 6.10.3.6 - duplicate parameter names.  */
1283  if (node->flags & NODE_MACRO_ARG)
1284    {
1285      cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
1286		 NODE_NAME (node));
1287      return true;
1288    }
1289
1290  if (BUFF_ROOM (pfile->a_buff)
1291      < (macro->paramc + 1) * sizeof (cpp_hashnode *))
1292    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1293
1294  ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1295  node->flags |= NODE_MACRO_ARG;
1296  len = macro->paramc * sizeof (union _cpp_hashnode_value);
1297  if (len > pfile->macro_buffer_len)
1298    {
1299      pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
1300                                        len);
1301      pfile->macro_buffer_len = len;
1302    }
1303  ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
1304    = node->value;
1305
1306  node->value.arg_index  = macro->paramc;
1307  return false;
1308}
1309
1310/* Check the syntax of the parameters in a MACRO definition.  Returns
1311   false if an error occurs.  */
1312static bool
1313parse_params (cpp_reader *pfile, cpp_macro *macro)
1314{
1315  unsigned int prev_ident = 0;
1316
1317  for (;;)
1318    {
1319      const cpp_token *token = _cpp_lex_token (pfile);
1320
1321      switch (token->type)
1322	{
1323	default:
1324	  /* Allow/ignore comments in parameter lists if we are
1325	     preserving comments in macro expansions.  */
1326	  if (token->type == CPP_COMMENT
1327	      && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
1328	    continue;
1329
1330	  cpp_error (pfile, CPP_DL_ERROR,
1331		     "\"%s\" may not appear in macro parameter list",
1332		     cpp_token_as_text (pfile, token));
1333	  return false;
1334
1335	case CPP_NAME:
1336	  if (prev_ident)
1337	    {
1338	      cpp_error (pfile, CPP_DL_ERROR,
1339			 "macro parameters must be comma-separated");
1340	      return false;
1341	    }
1342	  prev_ident = 1;
1343
1344	  if (_cpp_save_parameter (pfile, macro, token->val.node))
1345	    return false;
1346	  continue;
1347
1348	case CPP_CLOSE_PAREN:
1349	  if (prev_ident || macro->paramc == 0)
1350	    return true;
1351
1352	  /* Fall through to pick up the error.  */
1353	case CPP_COMMA:
1354	  if (!prev_ident)
1355	    {
1356	      cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
1357	      return false;
1358	    }
1359	  prev_ident = 0;
1360	  continue;
1361
1362	case CPP_ELLIPSIS:
1363	  macro->variadic = 1;
1364	  if (!prev_ident)
1365	    {
1366	      _cpp_save_parameter (pfile, macro,
1367				   pfile->spec_nodes.n__VA_ARGS__);
1368	      pfile->state.va_args_ok = 1;
1369	      if (! CPP_OPTION (pfile, c99)
1370		  && CPP_OPTION (pfile, pedantic)
1371		  && CPP_OPTION (pfile, warn_variadic_macros))
1372		cpp_error (pfile, CPP_DL_PEDWARN,
1373			   "anonymous variadic macros were introduced in C99");
1374	    }
1375	  else if (CPP_OPTION (pfile, pedantic)
1376		   && CPP_OPTION (pfile, warn_variadic_macros))
1377	    cpp_error (pfile, CPP_DL_PEDWARN,
1378		       "ISO C does not permit named variadic macros");
1379
1380	  /* We're at the end, and just expect a closing parenthesis.  */
1381	  token = _cpp_lex_token (pfile);
1382	  if (token->type == CPP_CLOSE_PAREN)
1383	    return true;
1384	  /* Fall through.  */
1385
1386	case CPP_EOF:
1387	  cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
1388	  return false;
1389	}
1390    }
1391}
1392
1393/* Allocate room for a token from a macro's replacement list.  */
1394static cpp_token *
1395alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1396{
1397  if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
1398    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
1399
1400  return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1401}
1402
1403/* Lex a token from the expansion of MACRO, but mark parameters as we
1404   find them and warn of traditional stringification.  */
1405static cpp_token *
1406lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1407{
1408  cpp_token *token;
1409
1410  pfile->cur_token = alloc_expansion_token (pfile, macro);
1411  token = _cpp_lex_direct (pfile);
1412
1413  /* Is this a parameter?  */
1414  if (token->type == CPP_NAME
1415      && (token->val.node->flags & NODE_MACRO_ARG) != 0)
1416    {
1417      token->type = CPP_MACRO_ARG;
1418      token->val.arg_no = token->val.node->value.arg_index;
1419    }
1420  else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1421	   && (token->type == CPP_STRING || token->type == CPP_CHAR))
1422    check_trad_stringification (pfile, macro, &token->val.str);
1423
1424  return token;
1425}
1426
1427static bool
1428create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
1429{
1430  cpp_token *token;
1431  const cpp_token *ctoken;
1432
1433  /* Get the first token of the expansion (or the '(' of a
1434     function-like macro).  */
1435  ctoken = _cpp_lex_token (pfile);
1436
1437  if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
1438    {
1439      bool ok = parse_params (pfile, macro);
1440      macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1441      if (!ok)
1442	return false;
1443
1444      /* Success.  Commit or allocate the parameter array.  */
1445      if (pfile->hash_table->alloc_subobject)
1446	{
1447	  cpp_hashnode **params =
1448            (cpp_hashnode **) pfile->hash_table->alloc_subobject
1449            (sizeof (cpp_hashnode *) * macro->paramc);
1450	  memcpy (params, macro->params,
1451		  sizeof (cpp_hashnode *) * macro->paramc);
1452	  macro->params = params;
1453	}
1454      else
1455	BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
1456      macro->fun_like = 1;
1457    }
1458  else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1459    {
1460      /* While ISO C99 requires whitespace before replacement text
1461	 in a macro definition, ISO C90 with TC1 allows there characters
1462	 from the basic source character set.  */
1463      if (CPP_OPTION (pfile, c99))
1464	cpp_error (pfile, CPP_DL_PEDWARN,
1465		   "ISO C99 requires whitespace after the macro name");
1466      else
1467	{
1468	  int warntype = CPP_DL_WARNING;
1469	  switch (ctoken->type)
1470	    {
1471	    case CPP_ATSIGN:
1472	    case CPP_AT_NAME:
1473	    case CPP_OBJC_STRING:
1474	      /* '@' is not in basic character set.  */
1475	      warntype = CPP_DL_PEDWARN;
1476	      break;
1477	    case CPP_OTHER:
1478	      /* Basic character set sans letters, digits and _.  */
1479	      if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
1480			  ctoken->val.str.text[0]) == NULL)
1481		warntype = CPP_DL_PEDWARN;
1482	      break;
1483	    default:
1484	      /* All other tokens start with a character from basic
1485		 character set.  */
1486	      break;
1487	    }
1488	  cpp_error (pfile, warntype,
1489		     "missing whitespace after the macro name");
1490	}
1491    }
1492
1493  if (macro->fun_like)
1494    token = lex_expansion_token (pfile, macro);
1495  else
1496    {
1497      token = alloc_expansion_token (pfile, macro);
1498      *token = *ctoken;
1499    }
1500
1501  for (;;)
1502    {
1503      /* Check the stringifying # constraint 6.10.3.2.1 of
1504	 function-like macros when lexing the subsequent token.  */
1505      if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1506	{
1507	  if (token->type == CPP_MACRO_ARG)
1508	    {
1509	      token->flags &= ~PREV_WHITE;
1510	      token->flags |= STRINGIFY_ARG;
1511	      token->flags |= token[-1].flags & PREV_WHITE;
1512	      token[-1] = token[0];
1513	      macro->count--;
1514	    }
1515	  /* Let assembler get away with murder.  */
1516	  else if (CPP_OPTION (pfile, lang) != CLK_ASM)
1517	    {
1518	      cpp_error (pfile, CPP_DL_ERROR,
1519			 "'#' is not followed by a macro parameter");
1520	      return false;
1521	    }
1522	}
1523
1524      if (token->type == CPP_EOF)
1525	break;
1526
1527      /* Paste operator constraint 6.10.3.3.1.  */
1528      if (token->type == CPP_PASTE)
1529	{
1530	  /* Token-paste ##, can appear in both object-like and
1531	     function-like macros, but not at the ends.  */
1532	  if (--macro->count > 0)
1533	    token = lex_expansion_token (pfile, macro);
1534
1535	  if (macro->count == 0 || token->type == CPP_EOF)
1536	    {
1537	      cpp_error (pfile, CPP_DL_ERROR,
1538		 "'##' cannot appear at either end of a macro expansion");
1539	      return false;
1540	    }
1541
1542	  token[-1].flags |= PASTE_LEFT;
1543	}
1544
1545      token = lex_expansion_token (pfile, macro);
1546    }
1547
1548  macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1549  macro->traditional = 0;
1550
1551  /* Don't count the CPP_EOF.  */
1552  macro->count--;
1553
1554  /* Clear whitespace on first token for warn_of_redefinition().  */
1555  if (macro->count)
1556    macro->exp.tokens[0].flags &= ~PREV_WHITE;
1557
1558  /* Commit or allocate the memory.  */
1559  if (pfile->hash_table->alloc_subobject)
1560    {
1561      cpp_token *tokns =
1562        (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
1563                                                          * macro->count);
1564      memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
1565      macro->exp.tokens = tokns;
1566    }
1567  else
1568    BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
1569
1570  return true;
1571}
1572
1573/* Parse a macro and save its expansion.  Returns nonzero on success.  */
1574bool
1575_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
1576{
1577  cpp_macro *macro;
1578  unsigned int i;
1579  bool ok;
1580
1581  if (pfile->hash_table->alloc_subobject)
1582    macro = (cpp_macro *) pfile->hash_table->alloc_subobject
1583      (sizeof (cpp_macro));
1584  else
1585    macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
1586  macro->line = pfile->directive_line;
1587  macro->params = 0;
1588  macro->paramc = 0;
1589  macro->variadic = 0;
1590  macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1591  macro->count = 0;
1592  macro->fun_like = 0;
1593  /* To suppress some diagnostics.  */
1594  macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
1595
1596  if (CPP_OPTION (pfile, traditional))
1597    ok = _cpp_create_trad_definition (pfile, macro);
1598  else
1599    {
1600      cpp_token *saved_cur_token = pfile->cur_token;
1601
1602      ok = create_iso_definition (pfile, macro);
1603
1604      /* Restore lexer position because of games lex_expansion_token()
1605	 plays lexing the macro.  We set the type for SEEN_EOL() in
1606	 directives.c.
1607
1608	 Longer term we should lex the whole line before coming here,
1609	 and just copy the expansion.  */
1610      saved_cur_token[-1].type = pfile->cur_token[-1].type;
1611      pfile->cur_token = saved_cur_token;
1612
1613      /* Stop the lexer accepting __VA_ARGS__.  */
1614      pfile->state.va_args_ok = 0;
1615    }
1616
1617  /* Clear the fast argument lookup indices.  */
1618  for (i = macro->paramc; i-- > 0; )
1619    {
1620      struct cpp_hashnode *node = macro->params[i];
1621      node->flags &= ~ NODE_MACRO_ARG;
1622      node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
1623    }
1624
1625  if (!ok)
1626    return ok;
1627
1628  if (node->type == NT_MACRO)
1629    {
1630      if (CPP_OPTION (pfile, warn_unused_macros))
1631	_cpp_warn_if_unused_macro (pfile, node, NULL);
1632
1633      if (warn_of_redefinition (pfile, node, macro))
1634	{
1635	  cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
1636			       "\"%s\" redefined", NODE_NAME (node));
1637
1638	  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1639	    cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1640				 node->value.macro->line, 0,
1641			 "this is the location of the previous definition");
1642	}
1643    }
1644
1645  if (node->type != NT_VOID)
1646    _cpp_free_definition (node);
1647
1648  /* Enter definition in hash table.  */
1649  node->type = NT_MACRO;
1650  node->value.macro = macro;
1651  if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1652    node->flags |= NODE_WARN;
1653
1654  return ok;
1655}
1656
1657/* Warn if a token in STRING matches one of a function-like MACRO's
1658   parameters.  */
1659static void
1660check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
1661			    const cpp_string *string)
1662{
1663  unsigned int i, len;
1664  const uchar *p, *q, *limit;
1665
1666  /* Loop over the string.  */
1667  limit = string->text + string->len - 1;
1668  for (p = string->text + 1; p < limit; p = q)
1669    {
1670      /* Find the start of an identifier.  */
1671      while (p < limit && !is_idstart (*p))
1672	p++;
1673
1674      /* Find the end of the identifier.  */
1675      q = p;
1676      while (q < limit && is_idchar (*q))
1677	q++;
1678
1679      len = q - p;
1680
1681      /* Loop over the function macro arguments to see if the
1682	 identifier inside the string matches one of them.  */
1683      for (i = 0; i < macro->paramc; i++)
1684	{
1685	  const cpp_hashnode *node = macro->params[i];
1686
1687	  if (NODE_LEN (node) == len
1688	      && !memcmp (p, NODE_NAME (node), len))
1689	    {
1690	      cpp_error (pfile, CPP_DL_WARNING,
1691	   "macro argument \"%s\" would be stringified in traditional C",
1692			 NODE_NAME (node));
1693	      break;
1694	    }
1695	}
1696    }
1697}
1698
1699/* Returns the name, arguments and expansion of a macro, in a format
1700   suitable to be read back in again, and therefore also for DWARF 2
1701   debugging info.  e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
1702   Caller is expected to generate the "#define" bit if needed.  The
1703   returned text is temporary, and automatically freed later.  */
1704const unsigned char *
1705cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
1706{
1707  unsigned int i, len;
1708  const cpp_macro *macro = node->value.macro;
1709  unsigned char *buffer;
1710
1711  if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
1712    {
1713      cpp_error (pfile, CPP_DL_ICE,
1714		 "invalid hash type %d in cpp_macro_definition", node->type);
1715      return 0;
1716    }
1717
1718  /* Calculate length.  */
1719  len = NODE_LEN (node) + 2;			/* ' ' and NUL.  */
1720  if (macro->fun_like)
1721    {
1722      len += 4;		/* "()" plus possible final ".." of named
1723			   varargs (we have + 1 below).  */
1724      for (i = 0; i < macro->paramc; i++)
1725	len += NODE_LEN (macro->params[i]) + 1; /* "," */
1726    }
1727
1728  /* This should match below where we fill in the buffer.  */
1729  if (CPP_OPTION (pfile, traditional))
1730    len += _cpp_replacement_text_len (macro);
1731  else
1732    {
1733      for (i = 0; i < macro->count; i++)
1734	{
1735	  cpp_token *token = &macro->exp.tokens[i];
1736
1737	  if (token->type == CPP_MACRO_ARG)
1738	    len += NODE_LEN (macro->params[token->val.arg_no - 1]);
1739	  else
1740	    len += cpp_token_len (token);
1741
1742	  if (token->flags & STRINGIFY_ARG)
1743	    len++;			/* "#" */
1744	  if (token->flags & PASTE_LEFT)
1745	    len += 3;		/* " ##" */
1746	  if (token->flags & PREV_WHITE)
1747	    len++;              /* " " */
1748	}
1749    }
1750
1751  if (len > pfile->macro_buffer_len)
1752    {
1753      pfile->macro_buffer = XRESIZEVEC (unsigned char,
1754                                        pfile->macro_buffer, len);
1755      pfile->macro_buffer_len = len;
1756    }
1757
1758  /* Fill in the buffer.  Start with the macro name.  */
1759  buffer = pfile->macro_buffer;
1760  memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
1761  buffer += NODE_LEN (node);
1762
1763  /* Parameter names.  */
1764  if (macro->fun_like)
1765    {
1766      *buffer++ = '(';
1767      for (i = 0; i < macro->paramc; i++)
1768	{
1769	  cpp_hashnode *param = macro->params[i];
1770
1771	  if (param != pfile->spec_nodes.n__VA_ARGS__)
1772	    {
1773	      memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
1774	      buffer += NODE_LEN (param);
1775	    }
1776
1777	  if (i + 1 < macro->paramc)
1778	    /* Don't emit a space after the comma here; we're trying
1779	       to emit a Dwarf-friendly definition, and the Dwarf spec
1780	       forbids spaces in the argument list.  */
1781	    *buffer++ = ',';
1782	  else if (macro->variadic)
1783	    *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
1784	}
1785      *buffer++ = ')';
1786    }
1787
1788  /* The Dwarf spec requires a space after the macro name, even if the
1789     definition is the empty string.  */
1790  *buffer++ = ' ';
1791
1792  if (CPP_OPTION (pfile, traditional))
1793    buffer = _cpp_copy_replacement_text (macro, buffer);
1794  else if (macro->count)
1795  /* Expansion tokens.  */
1796    {
1797      for (i = 0; i < macro->count; i++)
1798	{
1799	  cpp_token *token = &macro->exp.tokens[i];
1800
1801	  if (token->flags & PREV_WHITE)
1802	    *buffer++ = ' ';
1803	  if (token->flags & STRINGIFY_ARG)
1804	    *buffer++ = '#';
1805
1806	  if (token->type == CPP_MACRO_ARG)
1807	    {
1808	      memcpy (buffer,
1809		      NODE_NAME (macro->params[token->val.arg_no - 1]),
1810		      NODE_LEN (macro->params[token->val.arg_no - 1]));
1811	      buffer += NODE_LEN (macro->params[token->val.arg_no - 1]);
1812	    }
1813	  else
1814	    buffer = cpp_spell_token (pfile, token, buffer, false);
1815
1816	  if (token->flags & PASTE_LEFT)
1817	    {
1818	      *buffer++ = ' ';
1819	      *buffer++ = '#';
1820	      *buffer++ = '#';
1821	      /* Next has PREV_WHITE; see _cpp_create_definition.  */
1822	    }
1823	}
1824    }
1825
1826  *buffer = '\0';
1827  return pfile->macro_buffer;
1828}
1829