cppmacro.c 48.7 KB
Newer Older
Neil Booth committed
1
/* Part of CPP library.  (Macro and #define handling.)
Zack Weinberg committed
2
   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3
   1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
Zack Weinberg committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   Written by Per Bothner, 1994.
   Based on CCCP program by Paul Rubin, June 1986
   Adapted to ANSI C, Richard Stallman, Jan 1987

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

 In other words, you are welcome to use, share and improve this program.
 You are forbidden to forbid anyone else to use, share and improve
 what you give them.   Help stamp out software-hoarding!  */

#include "config.h"
#include "system.h"
#include "cpplib.h"
#include "cpphash.h"

Neil Booth committed
31 32 33
typedef struct macro_arg macro_arg;
struct macro_arg
{
34
  const cpp_token **first;	/* First token in unexpanded argument.  */
Kazu Hirata committed
35
  const cpp_token **expanded;	/* Macro-expanded argument.  */
36
  const cpp_token *stringified;	/* Stringified argument.  */
Neil Booth committed
37 38
  unsigned int count;		/* # of tokens in argument.  */
  unsigned int expanded_count;	/* # of tokens in expanded argument.  */
Zack Weinberg committed
39 40
};

Neil Booth committed
41 42
/* Macro expansion.  */

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static int enter_macro_context (cpp_reader *, cpp_hashnode *);
static int builtin_macro (cpp_reader *, cpp_hashnode *);
static void push_token_context (cpp_reader *, cpp_hashnode *,
				const cpp_token *, unsigned int);
static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
				 const cpp_token **, unsigned int);
static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *);
static cpp_context *next_context (cpp_reader *);
static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
static void expand_arg (cpp_reader *, macro_arg *);
static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
static void paste_all_tokens (cpp_reader *, const cpp_token *);
static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
			  macro_arg *);
static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *);
static bool create_iso_definition (cpp_reader *, cpp_macro *);
Neil Booth committed
61 62 63

/* #define directive parsing and handling.  */

64 65 66 67 68 69 70
static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
static bool warn_of_redefinition (cpp_reader *, const cpp_hashnode *,
				  const cpp_macro *);
static bool parse_params (cpp_reader *, cpp_macro *);
static void check_trad_stringification (cpp_reader *, const cpp_macro *,
					const cpp_string *);
Zack Weinberg committed
71

72 73 74
/* Emits a warning if NODE is a macro defined in the main file that
   has not been used.  */
int
75 76
_cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
			   void *v ATTRIBUTE_UNUSED)
77 78 79 80 81 82
{
  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
    {
      cpp_macro *macro = node->value.macro;

      if (!macro->used
83
	  && MAIN_FILE_P (linemap_lookup (&pfile->line_maps, macro->line)))
84
	cpp_error_with_line (pfile, CPP_DL_WARNING, macro->line, 0,
85 86 87 88 89 90
			     "macro \"%s\" is not used", NODE_NAME (node));
    }

  return 1;
}

91 92 93
/* Allocates and returns a CPP_STRING token, containing TEXT of length
   LEN, after null-terminating it.  TEXT must be in permanent storage.  */
static const cpp_token *
94
new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
Neil Booth committed
95
{
96
  cpp_token *token = _cpp_temp_token (pfile);
Neil Booth committed
97

98
  text[len] = '\0';
Neil Booth committed
99
  token->type = CPP_STRING;
100 101
  token->val.str.len = len;
  token->val.str.text = text;
Neil Booth committed
102
  token->flags = 0;
103
  return token;
Neil Booth committed
104 105 106 107 108 109 110 111
}

static const char * const monthnames[] =
{
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

Neil Booth committed
112 113
/* Handle builtin macros like __FILE__, and push the resulting token
   on the context stack.  Also handles _Pragma, for which no new token
114 115
   is created.  Returns 1 if it generates a new token context, 0 to
   return the token to the caller.  */
116
const uchar *
117
_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth committed
118
{
119 120
  const uchar *result = NULL;
  unsigned int number = 1;
Neil Booth committed
121

Neil Booth committed
122 123
  switch (node->value.builtin)
    {
124
    default:
125
      cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
126
		 NODE_NAME (node));
127
      break;
128

Neil Booth committed
129 130
    case BT_FILE:
    case BT_BASE_FILE:
Zack Weinberg committed
131
      {
132
	unsigned int len;
133
	const char *name;
134
	uchar *buf;
135
	const struct line_map *map = pfile->map;
136 137

	if (node->value.builtin == BT_BASE_FILE)
138 139
	  while (! MAIN_FILE_P (map))
	    map = INCLUDED_FROM (&pfile->line_maps, map);
140

141
	name = map->to_file;
142
	len = strlen (name);
143 144 145 146 147 148
	buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
	result = buf;
	*buf = '"';
	buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
	*buf++ = '"';
	*buf = '\0';
Zack Weinberg committed
149
      }
Neil Booth committed
150 151
      break;

Neil Booth committed
152
    case BT_INCLUDE_LEVEL:
153 154 155
      /* The line map depth counts the primary source as level 1, but
	 historically __INCLUDE_DEPTH__ has called the primary source
	 level 0.  */
156
      number = pfile->line_maps.depth - 1;
Neil Booth committed
157
      break;
Neil Booth committed
158 159 160 161 162

    case BT_SPECLINE:
      /* If __LINE__ is embedded in a macro, it must expand to the
	 line of the macro's invocation, not its definition.
	 Otherwise things like assert() will not work properly.  */
163 164 165 166 167
      if (CPP_OPTION (pfile, traditional))
	number = pfile->line;
      else
	number = pfile->cur_token[-1].line;
      number = SOURCE_LINE (pfile->map, number);
Neil Booth committed
168
      break;
Neil Booth committed
169

170 171
      /* __STDC__ has the value 1 under normal circumstances.
	 However, if (a) we are in a system header, (b) the option
172 173 174
	 stdc_0_in_system_headers is true (set by target config), and
	 (c) we are not in strictly conforming mode, then it has the
	 value 0.  */
Neil Booth committed
175 176
    case BT_STDC:
      {
177 178
	if (CPP_IN_SYSTEM_HEADER (pfile)
	    && CPP_OPTION (pfile, stdc_0_in_system_headers)
179
	    && !CPP_OPTION (pfile,std))
180
	  number = 0;
181
	else
182
	  number = 1;
Neil Booth committed
183
      }
Neil Booth committed
184
      break;
Zack Weinberg committed
185

Neil Booth committed
186 187
    case BT_DATE:
    case BT_TIME:
188
      if (pfile->date == NULL)
Neil Booth committed
189
	{
190 191 192 193
	  /* Allocate __DATE__ and __TIME__ strings from permanent
	     storage.  We only do this once, and don't generate them
	     at init time, because time() and localtime() are very
	     slow on some systems.  */
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	  time_t tt;
	  struct tm *tb = NULL;

	  /* (time_t) -1 is a legitimate value for "number of seconds
	     since the Epoch", so we have to do a little dance to
	     distinguish that from a genuine error.  */
	  errno = 0;
	  tt = time(NULL);
	  if (tt != (time_t)-1 || errno == 0)
	    tb = localtime (&tt);

	  if (tb)
	    {
	      pfile->date = _cpp_unaligned_alloc (pfile,
						  sizeof ("\"Oct 11 1347\""));
	      sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
210 211
		       monthnames[tb->tm_mon], tb->tm_mday,
		       tb->tm_year + 1900);
212 213 214 215 216 217 218 219

	      pfile->time = _cpp_unaligned_alloc (pfile,
						  sizeof ("\"12:34:56\""));
	      sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
		       tb->tm_hour, tb->tm_min, tb->tm_sec);
	    }
	  else
	    {
220
	      cpp_errno (pfile, CPP_DL_WARNING,
221 222 223 224 225
			 "could not determine date and time");
		
	      pfile->date = U"\"??? ?? ????\"";
	      pfile->time = U"\"??:??:??\"";
	    }
Neil Booth committed
226 227
	}

Neil Booth committed
228
      if (node->value.builtin == BT_DATE)
229
	result = pfile->date;
Neil Booth committed
230
      else
231
	result = pfile->time;
Neil Booth committed
232
      break;
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    }

  if (result == NULL)
    {
      /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers.  */
      result = _cpp_unaligned_alloc (pfile, 21);
      sprintf ((char *) result, "%u", number);
    }

  return result;      
}

/* Convert builtin macros like __FILE__ to a token and push it on the
   context stack.  Also handles _Pragma, for which no new token is
   created.  Returns 1 if it generates a new token context, 0 to
   return the token to the caller.  */
static int
250
builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
251 252
{
  const uchar *buf;
253 254
  size_t len;
  char *nbuf;
Neil Booth committed
255

256 257
  if (node->value.builtin == BT_PRAGMA)
    {
Neil Booth committed
258 259 260 261 262 263 264
      /* Don't interpret _Pragma within directives.  The standard is
         not clear on this, but to me this makes most sense.  */
      if (pfile->state.in_directive)
	return 0;

      _cpp_do__Pragma (pfile);
      return 1;
Neil Booth committed
265
    }
Neil Booth committed
266

267
  buf = _cpp_builtin_macro_text (pfile, node);
268 269 270 271
  len = ustrlen (buf);
  nbuf = alloca (len + 1);
  memcpy (nbuf, buf, len);
  nbuf[len]='\n';
272

273
  cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
274
  _cpp_clean_line (pfile);
275 276 277 278 279

  /* Set pfile->cur_token as required by _cpp_lex_direct.  */
  pfile->cur_token = _cpp_temp_token (pfile);
  push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
  if (pfile->buffer->cur != pfile->buffer->rlimit)
280
    cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
281 282 283
	       NODE_NAME (node));
  _cpp_pop_buffer (pfile);

Neil Booth committed
284
  return 1;
Zack Weinberg committed
285 286
}

287 288
/* Copies SRC, of length LEN, to DEST, adding backslashes before all
   backslashes and double quotes.  Non-printable characters are
289 290
   converted to octal.  DEST must be of sufficient size.  Returns
   a pointer to the end of the string.  */
291
uchar *
292
cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
Neil Booth committed
293 294 295
{
  while (len--)
    {
296
      uchar c = *src++;
Zack Weinberg committed
297

Neil Booth committed
298 299 300 301 302 303 304 305 306 307
      if (c == '\\' || c == '"')
	{
	  *dest++ = '\\';
	  *dest++ = c;
	}
      else
	{
	  if (ISPRINT (c))
	    *dest++ = c;
	  else
Zack Weinberg committed
308
	    {
Neil Booth committed
309 310
	      sprintf ((char *) dest, "\\%03o", c);
	      dest += 4;
Zack Weinberg committed
311
	    }
Neil Booth committed
312 313
	}
    }
Zack Weinberg committed
314

Neil Booth committed
315 316
  return dest;
}
Zack Weinberg committed
317

318 319
/* Convert a token sequence ARG to a single string token according to
   the rules of the ISO C #-operator.  */
320
static const cpp_token *
321
stringify_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth committed
322
{
323
  unsigned char *dest;
324
  unsigned int i, escape_it, backslash_count = 0;
325
  const cpp_token *source = NULL;
326
  size_t len;
Zack Weinberg committed
327

328 329 330 331 332
  if (BUFF_ROOM (pfile->u_buff) < 3)
    _cpp_extend_buff (pfile, &pfile->u_buff, 3);
  dest = BUFF_FRONT (pfile->u_buff);
  *dest++ = '"';

Neil Booth committed
333 334 335
  /* Loop, reading in the argument's tokens.  */
  for (i = 0; i < arg->count; i++)
    {
336 337 338 339 340 341 342 343
      const cpp_token *token = arg->first[i];

      if (token->type == CPP_PADDING)
	{
	  if (source == NULL)
	    source = token->val.source;
	  continue;
	}
Zack Weinberg committed
344

Neil Booth committed
345
      escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
346
		   || token->type == CPP_CHAR || token->type == CPP_WCHAR);
Zack Weinberg committed
347

348
      /* Room for each char being written in octal, initial space and
349
	 final quote and NUL.  */
350
      len = cpp_token_len (token);
Neil Booth committed
351 352
      if (escape_it)
	len *= 4;
353
      len += 3;
Zack Weinberg committed
354

355
      if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth committed
356
	{
357
	  size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
358
	  _cpp_extend_buff (pfile, &pfile->u_buff, len);
359
	  dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth committed
360
	}
Zack Weinberg committed
361

362
      /* Leading white space?  */
363
      if (dest - 1 != BUFF_FRONT (pfile->u_buff))
364 365 366 367 368 369 370
	{
	  if (source == NULL)
	    source = token;
	  if (source->flags & PREV_WHITE)
	    *dest++ = ' ';
	}
      source = NULL;
Zack Weinberg committed
371

Neil Booth committed
372 373
      if (escape_it)
	{
374 375
	  _cpp_buff *buff = _cpp_get_buff (pfile, len);
	  unsigned char *buf = BUFF_FRONT (buff);
Neil Booth committed
376
	  len = cpp_spell_token (pfile, token, buf) - buf;
377
	  dest = cpp_quote_string (dest, buf, len);
378
	  _cpp_release_buff (pfile, buff);
Neil Booth committed
379 380 381 382
	}
      else
	dest = cpp_spell_token (pfile, token, dest);

383
      if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
Neil Booth committed
384 385 386 387 388 389 390 391
	backslash_count++;
      else
	backslash_count = 0;
    }

  /* Ignore the final \ of invalid string literals.  */
  if (backslash_count & 1)
    {
392
      cpp_error (pfile, CPP_DL_WARNING,
393
		 "invalid string literal, ignoring final '\\'");
394
      dest--;
Neil Booth committed
395 396
    }

397
  /* Commit the memory, including NUL, and return the token.  */
398
  *dest++ = '"';
399 400 401
  len = dest - BUFF_FRONT (pfile->u_buff);
  BUFF_FRONT (pfile->u_buff) = dest + 1;
  return new_string_token (pfile, dest - len, len);
Neil Booth committed
402 403
}

404
/* Try to paste two tokens.  On success, return nonzero.  In any
Neil Booth committed
405 406 407
   case, PLHS is updated to point to the pasted token, which is
   guaranteed to not have the PASTE_LEFT flag set.  */
static bool
408
paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
409
{
Neil Booth committed
410 411 412 413 414 415 416
  unsigned char *buf, *end;
  const cpp_token *lhs;
  unsigned int len;
  bool valid;

  lhs = *plhs;
  len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
417
  buf = alloca (len);
Neil Booth committed
418 419 420 421 422 423
  end = cpp_spell_token (pfile, lhs, buf);

  /* Avoid comment headers, since they are still processed in stage 3.
     It is simpler to insert a space here, rather than modifying the
     lexer to ignore comments in some circumstances.  Simply returning
     false doesn't work, since we want to clear the PASTE_LEFT flag.  */
424
  if (lhs->type == CPP_DIV && rhs->type != CPP_EQ)
Neil Booth committed
425 426
    *end++ = ' ';
  end = cpp_spell_token (pfile, rhs, end);
427
  *end = '\n';
Neil Booth committed
428

429
  cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
430
  _cpp_clean_line (pfile);
Neil Booth committed
431 432 433 434

  /* Set pfile->cur_token as required by _cpp_lex_direct.  */
  pfile->cur_token = _cpp_temp_token (pfile);
  *plhs = _cpp_lex_direct (pfile);
435
  valid = pfile->buffer->cur == pfile->buffer->rlimit;
Neil Booth committed
436 437 438
  _cpp_pop_buffer (pfile);

  return valid;
439 440
}

441 442 443 444 445 446 447
/* Handles an arbitrarily long sequence of ## operators, with initial
   operand LHS.  This implementation is left-associative,
   non-recursive, and finishes a paste before handling succeeding
   ones.  If a paste fails, we back up to the RHS of the failing ##
   operator before pushing the context containing the result of prior
   successful pastes, with the effect that the RHS appears in the
   output stream after the pasted LHS normally.  */
Neil Booth committed
448
static void
449
paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
Neil Booth committed
450
{
451 452 453
  const cpp_token *rhs;
  cpp_context *context = pfile->context;

Neil Booth committed
454 455 456 457 458 459
  do
    {
      /* Take the token directly from the current context.  We can do
	 this, because we are in the replacement list of either an
	 object-like macro, or a function-like macro with arguments
	 inserted.  In either case, the constraints to #define
460
	 guarantee we have at least one more token.  */
461
      if (context->direct_p)
462
	rhs = FIRST (context).token++;
463
      else
464
	rhs = *FIRST (context).ptoken++;
465 466 467 468

      if (rhs->type == CPP_PADDING)
	abort ();

Neil Booth committed
469
      if (!paste_tokens (pfile, &lhs, rhs))
Neil Booth committed
470
	{
471
	  _cpp_backup_tokens (pfile, 1);
Neil Booth committed
472

473
	  /* Mandatory error for all apart from assembler.  */
Neil Booth committed
474
	  if (CPP_OPTION (pfile, lang) != CLK_ASM)
475
	    cpp_error (pfile, CPP_DL_ERROR,
Neil Booth committed
476
	 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
477 478
		       cpp_token_as_text (pfile, lhs),
		       cpp_token_as_text (pfile, rhs));
Neil Booth committed
479
	  break;
Neil Booth committed
480 481 482 483
	}
    }
  while (rhs->flags & PASTE_LEFT);

Neil Booth committed
484 485
  /* Put the resulting token in its own context.  */
  push_token_context (pfile, NULL, lhs, 1);
Neil Booth committed
486 487
}

488 489 490 491 492 493 494
/* Returns TRUE if the number of arguments ARGC supplied in an
   invocation of the MACRO referenced by NODE is valid.  An empty
   invocation to a macro with no parameters should pass ARGC as zero.

   Note that MACRO cannot necessarily be deduced from NODE, in case
   NODE was redefined whilst collecting arguments.  */
bool
495
_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
{
  if (argc == macro->paramc)
    return true;

  if (argc < macro->paramc)
    {
      /* As an extension, a rest argument is allowed to not appear in
	 the invocation at all.
	 e.g. #define debug(format, args...) something
	 debug("string");

	 This is exactly the same as if there had been an empty rest
	 argument - debug("string", ).  */

      if (argc + 1 == macro->paramc && macro->variadic)
	{
	  if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
513
	    cpp_error (pfile, CPP_DL_PEDWARN,
514 515 516 517
		       "ISO C99 requires rest arguments to be used");
	  return true;
	}

518
      cpp_error (pfile, CPP_DL_ERROR,
519 520 521 522
		 "macro \"%s\" requires %u arguments, but only %u given",
		 NODE_NAME (node), macro->paramc, argc);
    }
  else
523
    cpp_error (pfile, CPP_DL_ERROR,
524 525 526 527 528 529
	       "macro \"%s\" passed %u arguments, but takes just %u",
	       NODE_NAME (node), argc, macro->paramc);

  return false;
}

530 531 532 533 534
/* Reads and returns the arguments to a function-like macro
   invocation.  Assumes the opening parenthesis has been processed.
   If there is an error, emits an appropriate diagnostic and returns
   NULL.  Each argument is terminated by a CPP_EOF token, for the
   future benefit of expand_arg().  */
535
static _cpp_buff *
536
collect_args (cpp_reader *pfile, const cpp_hashnode *node)
Neil Booth committed
537
{
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
  _cpp_buff *buff, *base_buff;
  cpp_macro *macro;
  macro_arg *args, *arg;
  const cpp_token *token;
  unsigned int argc;

  macro = node->value.macro;
  if (macro->paramc)
    argc = macro->paramc;
  else
    argc = 1;
  buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
				       + sizeof (macro_arg)));
  base_buff = buff;
  args = (macro_arg *) buff->base;
  memset (args, 0, argc * sizeof (macro_arg));
554
  buff->cur = (unsigned char *) &args[argc];
555 556 557 558 559 560
  arg = args, argc = 0;

  /* Collect the tokens making up each argument.  We don't yet know
     how many arguments have been supplied, whether too many or too
     few.  Hence the slightly bizarre usage of "argc" and "arg".  */
  do
Neil Booth committed
561
    {
562 563
      unsigned int paren_depth = 0;
      unsigned int ntokens = 0;
Neil Booth committed
564

565 566
      argc++;
      arg->first = (const cpp_token **) buff->cur;
Zack Weinberg committed
567

568 569 570
      for (;;)
	{
	  /* Require space for 2 new tokens (including a CPP_EOF).  */
571
	  if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
572
	    {
573 574
	      buff = _cpp_append_extend_buff (pfile, buff,
					      1000 * sizeof (cpp_token *));
575 576
	      arg->first = (const cpp_token **) buff->cur;
	    }
577

578
	  token = cpp_get_token (pfile);
Zack Weinberg committed
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	  if (token->type == CPP_PADDING)
	    {
	      /* Drop leading padding.  */
	      if (ntokens == 0)
		continue;
	    }
	  else if (token->type == CPP_OPEN_PAREN)
	    paren_depth++;
	  else if (token->type == CPP_CLOSE_PAREN)
	    {
	      if (paren_depth-- == 0)
		break;
	    }
	  else if (token->type == CPP_COMMA)
	    {
	      /* A comma does not terminate an argument within
		 parentheses or as part of a variable argument.  */
	      if (paren_depth == 0
		  && ! (macro->variadic && argc == macro->paramc))
		break;
	    }
	  else if (token->type == CPP_EOF
		   || (token->type == CPP_HASH && token->flags & BOL))
	    break;
Neil Booth committed
604

605 606
	  arg->first[ntokens++] = token;
	}
Neil Booth committed
607

608 609 610
      /* Drop trailing padding.  */
      while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
	ntokens--;
Neil Booth committed
611

612 613
      arg->count = ntokens;
      arg->first[ntokens] = &pfile->eof;
Neil Booth committed
614

615 616 617 618
      /* Terminate the argument.  Excess arguments loop back and
	 overwrite the final legitimate argument, before failing.  */
      if (argc <= macro->paramc)
	{
619
	  buff->cur = (unsigned char *) &arg->first[ntokens + 1];
620 621 622
	  if (argc != macro->paramc)
	    arg++;
	}
Neil Booth committed
623
    }
624
  while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth committed
625

626
  if (token->type == CPP_EOF)
Neil Booth committed
627
    {
628 629 630 631
      /* We still need the CPP_EOF to end directives, and to end
	 pre-expansion of a macro argument.  Step back is not
	 unconditional, since we don't want to return a CPP_EOF to our
	 callers at the end of an -include-d file.  */
632
      if (pfile->context->prev || pfile->state.in_directive)
633
	_cpp_backup_tokens (pfile, 1);
634
      cpp_error (pfile, CPP_DL_ERROR,
635
		 "unterminated argument list invoking macro \"%s\"",
636
		 NODE_NAME (node));
Neil Booth committed
637
    }
638
  else
Neil Booth committed
639
    {
640 641 642 643
      /* A single empty argument is counted as no argument.  */
      if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
	argc = 0;
      if (_cpp_arguments_ok (pfile, macro, node, argc))
644 645 646 647 648 649 650 651 652 653 654 655 656 657
	{
	  /* GCC has special semantics for , ## b where b is a varargs
	     parameter: we remove the comma if b was omitted entirely.
	     If b was merely an empty argument, the comma is retained.
	     If the macro takes just one (varargs) parameter, then we
	     retain the comma only if we are standards conforming.

	     If FIRST is NULL replace_args () swallows the comma.  */
	  if (macro->variadic && (argc < macro->paramc
				  || (argc == 1 && args[0].count == 0
				      && !CPP_OPTION (pfile, std))))
	    args[macro->paramc - 1].first = NULL;
	  return base_buff;
	}
Neil Booth committed
658 659
    }

660
  /* An error occurred.  */
661 662
  _cpp_release_buff (pfile, base_buff);
  return NULL;
Neil Booth committed
663 664
}

665 666 667 668 669
/* Search for an opening parenthesis to the macro of NODE, in such a
   way that, if none is found, we don't lose the information in any
   intervening padding tokens.  If we find the parenthesis, collect
   the arguments and return the buffer containing them.  */
static _cpp_buff *
670
funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
Neil Booth committed
671
{
672
  const cpp_token *token, *padding = NULL;
673

674
  for (;;)
675
    {
676 677 678 679 680 681
      token = cpp_get_token (pfile);
      if (token->type != CPP_PADDING)
	break;
      if (padding == NULL
	  || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
	padding = token;
682
    }
Neil Booth committed
683

684
  if (token->type == CPP_OPEN_PAREN)
Neil Booth committed
685
    {
686 687
      pfile->state.parsing_args = 2;
      return collect_args (pfile, node);
Neil Booth committed
688 689
    }

690 691 692 693 694 695 696 697 698 699 700
  /* CPP_EOF can be the end of macro arguments, or the end of the
     file.  We mustn't back up over the latter.  Ugh.  */
  if (token->type != CPP_EOF || token == &pfile->eof)
    {
      /* Back up.  We may have skipped padding, in which case backing
	 up more than one token when expanding macros is in general
	 too difficult.  We re-insert it in its own context.  */
      _cpp_backup_tokens (pfile, 1);
      if (padding)
	push_token_context (pfile, NULL, padding, 1);
    }
701 702

  return NULL;
Neil Booth committed
703 704
}

705 706 707 708
/* Push the context of a macro with hash entry NODE onto the context
   stack.  If we can successfully expand the macro, we push a context
   containing its yet-to-be-rescanned replacement list and return one.
   Otherwise, we don't push a context and return zero.  */
Zack Weinberg committed
709
static int
710
enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
Zack Weinberg committed
711
{
712
  /* The presence of a macro invalidates a file's controlling macro.  */
Neil Booth committed
713 714
  pfile->mi_valid = false;

715 716
  pfile->state.angled_headers = false;

717
  /* Handle standard macros.  */
Neil Booth committed
718
  if (! (node->flags & NODE_BUILTIN))
Zack Weinberg committed
719
    {
720
      cpp_macro *macro = node->value.macro;
Neil Booth committed
721

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
      if (macro->fun_like)
	{
	  _cpp_buff *buff;

	  pfile->state.prevent_expansion++;
	  pfile->keep_tokens++;
	  pfile->state.parsing_args = 1;
	  buff = funlike_invocation_p (pfile, node);
	  pfile->state.parsing_args = 0;
	  pfile->keep_tokens--;
	  pfile->state.prevent_expansion--;

	  if (buff == NULL)
	    {
	      if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
737
		cpp_error (pfile, CPP_DL_WARNING,
738
 "function-like macro \"%s\" must be used with arguments in traditional C",
739
			   NODE_NAME (node));
740 741 742 743

	      return 0;
	    }

744 745
	  if (macro->paramc > 0)
	    replace_args (pfile, node, macro, (macro_arg *) buff->base);
746 747
	  _cpp_release_buff (pfile, buff);
	}
Neil Booth committed
748

749
      /* Disable the macro within its expansion.  */
Neil Booth committed
750
      node->flags |= NODE_DISABLED;
Neil Booth committed
751

752 753
      macro->used = 1;

754
      if (macro->paramc == 0)
755
	push_token_context (pfile, node, macro->exp.tokens, macro->count);
Neil Booth committed
756 757

      return 1;
Zack Weinberg committed
758
    }
Neil Booth committed
759

760
  /* Handle built-in macros and the _Pragma operator.  */
Neil Booth committed
761
  return builtin_macro (pfile, node);
Neil Booth committed
762 763
}

764 765 766 767
/* Replace the parameters in a function-like macro of NODE with the
   actual ARGS, and place the result in a newly pushed token context.
   Expand each argument before replacing, unless it is operated upon
   by the # or ## operators.  */
Neil Booth committed
768
static void
769
replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
Neil Booth committed
770 771 772
{
  unsigned int i, total;
  const cpp_token *src, *limit;
773
  const cpp_token **dest, **first;
Neil Booth committed
774
  macro_arg *arg;
775
  _cpp_buff *buff;
Neil Booth committed
776 777

  /* First, fully macro-expand arguments, calculating the number of
778 779 780
     tokens in the final expansion as we go.  The ordering of the if
     statements below is subtle; we must handle stringification before
     pasting.  */
781
  total = macro->count;
782
  limit = macro->exp.tokens + macro->count;
783

784
  for (src = macro->exp.tokens; src < limit; src++)
Neil Booth committed
785 786
    if (src->type == CPP_MACRO_ARG)
      {
787 788 789
	/* Leading and trailing padding tokens.  */
	total += 2;

Neil Booth committed
790 791
	/* We have an argument.  If it is not being stringified or
	   pasted it is macro-replaced before insertion.  */
792
	arg = &args[src->val.arg_no - 1];
Neil Booth committed
793

Neil Booth committed
794 795 796
	if (src->flags & STRINGIFY_ARG)
	  {
	    if (!arg->stringified)
797
	      arg->stringified = stringify_arg (pfile, arg);
Neil Booth committed
798 799
	  }
	else if ((src->flags & PASTE_LEFT)
800
		 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
Neil Booth committed
801 802 803 804
	  total += arg->count - 1;
	else
	  {
	    if (!arg->expanded)
805
	      expand_arg (pfile, arg);
Neil Booth committed
806 807 808 809
	    total += arg->expanded_count - 1;
	  }
      }

810 811
  /* Now allocate space for the expansion, copy the tokens and replace
     the arguments.  */
812 813
  buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
  first = (const cpp_token **) buff->base;
814
  dest = first;
Neil Booth committed
815

816
  for (src = macro->exp.tokens; src < limit; src++)
817 818 819
    {
      unsigned int count;
      const cpp_token **from, **paste_flag;
Neil Booth committed
820

821 822 823 824 825
      if (src->type != CPP_MACRO_ARG)
	{
	  *dest++ = src;
	  continue;
	}
Neil Booth committed
826

827 828 829 830 831 832
      paste_flag = 0;
      arg = &args[src->val.arg_no - 1];
      if (src->flags & STRINGIFY_ARG)
	count = 1, from = &arg->stringified;
      else if (src->flags & PASTE_LEFT)
	count = arg->count, from = arg->first;
833
      else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
834 835 836 837 838 839 840 841
	{
	  count = arg->count, from = arg->first;
	  if (dest != first)
	    {
	      if (dest[-1]->type == CPP_COMMA
		  && macro->variadic
		  && src->val.arg_no == macro->paramc)
		{
842 843 844
		  /* Swallow a pasted comma if from == NULL, otherwise
		     drop the paste flag.  */
		  if (from == NULL)
845 846 847 848 849 850 851 852 853 854 855
		    dest--;
		  else
		    paste_flag = dest - 1;
		}
	      /* Remove the paste flag if the RHS is a placemarker.  */
	      else if (count == 0)
		paste_flag = dest - 1;
	    }
	}
      else
	count = arg->expanded_count, from = arg->expanded;
Neil Booth committed
856

857
      /* Padding on the left of an argument (unless RHS of ##).  */
858
      if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
859
	  && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
860
	*dest++ = padding_token (pfile, src);
Neil Booth committed
861

862 863 864 865
      if (count)
	{
	  memcpy (dest, from, count * sizeof (cpp_token *));
	  dest += count;
Neil Booth committed
866

867 868 869 870 871
	  /* With a non-empty argument on the LHS of ##, the last
	     token should be flagged PASTE_LEFT.  */
	  if (src->flags & PASTE_LEFT)
	    paste_flag = dest - 1;
	}
872

873 874 875
      /* Avoid paste on RHS (even case count == 0).  */
      if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
	*dest++ = &pfile->avoid_paste;
Neil Booth committed
876

877 878 879 880 881 882 883 884 885 886 887 888 889
      /* Add a new paste flag, or remove an unwanted one.  */
      if (paste_flag)
	{
	  cpp_token *token = _cpp_temp_token (pfile);
	  token->type = (*paste_flag)->type;
	  token->val.str = (*paste_flag)->val.str;
	  if (src->flags & PASTE_LEFT)
	    token->flags = (*paste_flag)->flags | PASTE_LEFT;
	  else
	    token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
	  *paste_flag = token;
	}
    }
Neil Booth committed
890

Neil Booth committed
891 892
  /* Free the expanded arguments.  */
  for (i = 0; i < macro->paramc; i++)
893 894 895
    if (args[i].expanded)
      free (args[i].expanded);

Neil Booth committed
896
  push_ptoken_context (pfile, node, buff, first, dest - first);
897 898 899 900
}

/* Return a special padding token, with padding inherited from SOURCE.  */
static const cpp_token *
901
padding_token (cpp_reader *pfile, const cpp_token *source)
902 903 904 905 906 907 908 909 910
{
  cpp_token *result = _cpp_temp_token (pfile);

  result->type = CPP_PADDING;
  result->val.source = source;
  result->flags = 0;
  return result;
}

911 912
/* Get a new uninitialized context.  Create a new one if we cannot
   re-use an old one.  */
913
static cpp_context *
914
next_context (cpp_reader *pfile)
915 916 917 918
{
  cpp_context *result = pfile->context->next;

  if (result == 0)
Zack Weinberg committed
919
    {
920 921 922 923
      result = xnew (cpp_context);
      result->prev = pfile->context;
      result->next = 0;
      pfile->context->next = result;
Neil Booth committed
924
    }
925 926 927

  pfile->context = result;
  return result;
Neil Booth committed
928 929
}

930 931
/* Push a list of pointers to tokens.  */
static void
932 933
push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
		     const cpp_token **first, unsigned int count)
Neil Booth committed
934 935 936
{
  cpp_context *context = next_context (pfile);

937 938
  context->direct_p = false;
  context->macro = macro;
939
  context->buff = buff;
940 941
  FIRST (context).ptoken = first;
  LAST (context).ptoken = first + count;
942 943 944 945
}

/* Push a list of tokens.  */
static void
946 947
push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
		    const cpp_token *first, unsigned int count)
948 949 950 951 952
{
  cpp_context *context = next_context (pfile);

  context->direct_p = true;
  context->macro = macro;
953
  context->buff = NULL;
954 955
  FIRST (context).token = first;
  LAST (context).token = first + count;
Neil Booth committed
956 957
}

958 959
/* Push a traditional macro's replacement text.  */
void
960 961
_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
			const uchar *start, size_t len)
962 963 964 965 966 967 968
{
  cpp_context *context = next_context (pfile);

  context->direct_p = true;
  context->macro = macro;
  context->buff = NULL;
  CUR (context) = start;
969
  RLIMIT (context) = start + len;
970
  macro->flags |= NODE_DISABLED;
971 972
}

973 974 975 976 977 978
/* Expand an argument ARG before replacing parameters in a
   function-like macro.  This works by pushing a context with the
   argument's tokens, and then expanding that into a temporary buffer
   as if it were a normal part of the token stream.  collect_args()
   has terminated the argument's tokens with a CPP_EOF so that we know
   when we have fully expanded the argument.  */
Neil Booth committed
979
static void
980
expand_arg (cpp_reader *pfile, macro_arg *arg)
Neil Booth committed
981
{
982
  unsigned int capacity;
983
  bool saved_warn_trad;
984 985 986

  if (arg->count == 0)
    return;
Neil Booth committed
987

988 989 990 991
  /* Don't warn about funlike macros when pre-expanding.  */
  saved_warn_trad = CPP_WTRADITIONAL (pfile);
  CPP_WTRADITIONAL (pfile) = 0;

Neil Booth committed
992
  /* Loop, reading in the arguments.  */
993
  capacity = 256;
994
  arg->expanded = xmalloc (capacity * sizeof (cpp_token *));
Neil Booth committed
995

996
  push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
997
  for (;;)
Neil Booth committed
998
    {
999 1000 1001
      const cpp_token *token;

      if (arg->expanded_count + 1 >= capacity)
Zack Weinberg committed
1002
	{
Neil Booth committed
1003
	  capacity *= 2;
1004 1005
	  arg->expanded = xrealloc (arg->expanded,
				    capacity * sizeof (cpp_token *));
Neil Booth committed
1006 1007
	}

1008
      token = cpp_get_token (pfile);
Neil Booth committed
1009

1010 1011 1012 1013 1014 1015
      if (token->type == CPP_EOF)
	break;

      arg->expanded[arg->expanded_count++] = token;
    }

1016
  _cpp_pop_context (pfile);
1017 1018

  CPP_WTRADITIONAL (pfile) = saved_warn_trad;
Neil Booth committed
1019 1020
}

1021 1022 1023
/* Pop the current context off the stack, re-enabling the macro if the
   context represented a macro's replacement list.  The context
   structure is not freed so that we can re-use it later.  */
Neil Booth committed
1024
void
1025
_cpp_pop_context (cpp_reader *pfile)
Neil Booth committed
1026
{
1027 1028 1029
  cpp_context *context = pfile->context;

  if (context->macro)
Neil Booth committed
1030
    context->macro->flags &= ~NODE_DISABLED;
1031 1032 1033

  if (context->buff)
    _cpp_release_buff (pfile, context->buff);
Neil Booth committed
1034

1035
  pfile->context = context->prev;
Neil Booth committed
1036 1037
}

1038
/* External routine to get a token.  Also used nearly everywhere
1039
   internally, except for places where we know we can safely call
1040
   _cpp_lex_token directly, such as lexing a directive name.
1041 1042 1043 1044 1045 1046 1047 1048

   Macro expansions and directives are transparently handled,
   including entering included files.  Thus tokens are post-macro
   expansion, and after any intervening directives.  External callers
   see CPP_EOF only at EOF.  Internal callers also see it when meeting
   a directive inside a macro call, when at the end of a directive and
   state.in_directive is still 1, and at the end of argument
   pre-expansion.  */
1049
const cpp_token *
1050
cpp_get_token (cpp_reader *pfile)
Neil Booth committed
1051
{
1052 1053
  const cpp_token *result;

1054
  for (;;)
Neil Booth committed
1055
    {
1056
      cpp_hashnode *node;
Neil Booth committed
1057 1058 1059
      cpp_context *context = pfile->context;

      /* Context->prev == 0 <=> base context.  */
1060
      if (!context->prev)
1061
	result = _cpp_lex_token (pfile);
1062
      else if (FIRST (context).token != LAST (context).token)
1063
	{
1064
	  if (context->direct_p)
1065
	    result = FIRST (context).token++;
1066
	  else
1067
	    result = *FIRST (context).ptoken++;
1068 1069

	  if (result->flags & PASTE_LEFT)
1070
	    {
1071 1072 1073 1074
	      paste_all_tokens (pfile, result);
	      if (pfile->state.in_directive)
		continue;
	      return padding_token (pfile, result);
1075
	    }
1076
	}
Neil Booth committed
1077 1078
      else
	{
1079
	  _cpp_pop_context (pfile);
1080 1081 1082
	  if (pfile->state.in_directive)
	    continue;
	  return &pfile->avoid_paste;
Neil Booth committed
1083 1084
	}

1085 1086 1087
      if (pfile->state.in_directive && result->type == CPP_COMMENT)
	continue;

1088
      if (result->type != CPP_NAME)
Neil Booth committed
1089 1090
	break;

1091 1092
      node = result->val.node;

Neil Booth committed
1093 1094
      if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
	break;
1095

Neil Booth committed
1096
      if (!(node->flags & NODE_DISABLED))
Neil Booth committed
1097
	{
Neil Booth committed
1098 1099
	  if (!pfile->state.prevent_expansion
	      && enter_macro_context (pfile, node))
1100
	    {
1101 1102 1103
	      if (pfile->state.in_directive)
		continue;
	      return padding_token (pfile, result);
1104
	    }
Neil Booth committed
1105
	}
Neil Booth committed
1106 1107
      else
	{
1108 1109
	  /* Flag this token as always unexpandable.  FIXME: move this
	     to collect_args()?.  */
Neil Booth committed
1110 1111 1112 1113 1114 1115
	  cpp_token *t = _cpp_temp_token (pfile);
	  t->type = result->type;
	  t->flags = result->flags | NO_EXPAND;
	  t->val.str = result->val.str;
	  result = t;
	}
1116

Neil Booth committed
1117
      break;
Neil Booth committed
1118
    }
1119 1120

  return result;
Neil Booth committed
1121 1122
}

1123 1124 1125 1126
/* Returns true if we're expanding an object-like macro that was
   defined in a system header.  Just checks the macro at the top of
   the stack.  Used for diagnostic suppression.  */
int
1127
cpp_sys_macro_p (cpp_reader *pfile)
1128
{
Neil Booth committed
1129
  cpp_hashnode *node = pfile->context->macro;
1130

Neil Booth committed
1131
  return node && node->value.macro && node->value.macro->syshdr;
1132 1133
}

1134 1135
/* Read each token in, until end of the current file.  Directives are
   transparently processed.  */
Neil Booth committed
1136
void
1137
cpp_scan_nooutput (cpp_reader *pfile)
Neil Booth committed
1138
{
1139 1140 1141 1142 1143 1144
  if (CPP_OPTION (pfile, traditional))
    while (_cpp_read_logical_line_trad (pfile))
      ;
  else
    while (cpp_get_token (pfile)->type != CPP_EOF)
      ;
Neil Booth committed
1145 1146
}

1147 1148
/* Step back one (or more) tokens.  Can only step mack more than 1 if
   they are from the lexer, and not from macro expansion.  */
1149
void
1150
_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
Neil Booth committed
1151
{
1152
  if (pfile->context->prev == NULL)
Neil Booth committed
1153
    {
1154 1155 1156
      pfile->lookaheads += count;
      while (count--)
	{
1157 1158 1159 1160
	  pfile->cur_token--;
	  if (pfile->cur_token == pfile->cur_run->base
	      /* Possible with -fpreprocessed and no leading #line.  */
	      && pfile->cur_run->prev != NULL)
1161 1162 1163 1164 1165
	    {
	      pfile->cur_run = pfile->cur_run->prev;
	      pfile->cur_token = pfile->cur_run->limit;
	    }
	}
Neil Booth committed
1166
    }
1167
  else
Neil Booth committed
1168
    {
1169 1170
      if (count != 1)
	abort ();
1171
      if (pfile->context->direct_p)
1172
	FIRST (pfile->context).token--;
1173
      else
1174
	FIRST (pfile->context).ptoken--;
Neil Booth committed
1175 1176
    }
}
Zack Weinberg committed
1177

Neil Booth committed
1178 1179
/* #define directive parsing and handling.  */

1180
/* Returns nonzero if a macro redefinition warning is required.  */
1181
static bool
1182 1183
warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
		      const cpp_macro *macro2)
Neil Booth committed
1184 1185 1186 1187
{
  const cpp_macro *macro1;
  unsigned int i;

1188 1189
  /* Some redefinitions need to be warned about regardless.  */
  if (node->flags & NODE_WARN)
1190
    return true;
Neil Booth committed
1191

1192
  /* Redefinition of a macro is allowed if and only if the old and new
1193
     definitions are the same.  (6.10.3 paragraph 2).  */
Neil Booth committed
1194 1195
  macro1 = node->value.macro;

1196 1197 1198
  /* Don't check count here as it can be different in valid
     traditional redefinitions with just whitespace differences.  */
  if (macro1->paramc != macro2->paramc
Neil Booth committed
1199
      || macro1->fun_like != macro2->fun_like
1200
      || macro1->variadic != macro2->variadic)
1201
    return true;
Neil Booth committed
1202 1203 1204 1205

  /* Check parameter spellings.  */
  for (i = 0; i < macro1->paramc; i++)
    if (macro1->params[i] != macro2->params[i])
1206
      return true;
Neil Booth committed
1207

1208 1209
  /* Check the replacement text or tokens.  */
  if (CPP_OPTION (pfile, traditional))
1210
    return _cpp_expansions_different_trad (macro1, macro2);
1211

1212 1213 1214 1215 1216 1217
  if (macro1->count != macro2->count)
    return true;

  for (i = 0; i < macro1->count; i++)
    if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
      return true;
1218 1219

  return false;
Neil Booth committed
1220 1221 1222
}

/* Free the definition of hashnode H.  */
Zack Weinberg committed
1223
void
1224
_cpp_free_definition (cpp_hashnode *h)
Zack Weinberg committed
1225
{
Neil Booth committed
1226 1227 1228
  /* Macros and assertions no longer have anything to free.  */
  h->type = NT_VOID;
  /* Clear builtin flag in case of redefinition.  */
Neil Booth committed
1229
  h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
Neil Booth committed
1230 1231
}

1232
/* Save parameter NODE to the parameter list of macro MACRO.  Returns
1233
   zero on success, nonzero if the parameter is a duplicate.  */
1234
bool
1235
_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
Neil Booth committed
1236
{
1237
  unsigned int len;
Neil Booth committed
1238
  /* Constraint 6.10.3.6 - duplicate parameter names.  */
1239
  if (node->flags & NODE_MACRO_ARG)
1240
    {
1241
      cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
1242
		 NODE_NAME (node));
1243
      return true;
Neil Booth committed
1244
    }
1245

1246 1247 1248
  if (BUFF_ROOM (pfile->a_buff)
      < (macro->paramc + 1) * sizeof (cpp_hashnode *))
    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1249

1250
  ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
1251 1252 1253 1254
  node->flags |= NODE_MACRO_ARG;
  len = macro->paramc * sizeof (union _cpp_hashnode_value);
  if (len > pfile->macro_buffer_len)
    {
1255
      pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
1256 1257 1258 1259 1260 1261
      pfile->macro_buffer_len = len;
    }
  ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
    = node->value;
  
  node->value.arg_index  = macro->paramc;
1262
  return false;
Zack Weinberg committed
1263 1264
}

1265 1266 1267
/* Check the syntax of the parameters in a MACRO definition.  Returns
   false if an error occurs.  */
static bool
1268
parse_params (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg committed
1269
{
Neil Booth committed
1270
  unsigned int prev_ident = 0;
Zack Weinberg committed
1271

Neil Booth committed
1272
  for (;;)
Zack Weinberg committed
1273
    {
1274
      const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg committed
1275

1276
      switch (token->type)
Zack Weinberg committed
1277
	{
Neil Booth committed
1278
	default:
1279 1280 1281 1282 1283 1284
	  /* Allow/ignore comments in parameter lists if we are
	     preserving comments in macro expansions.  */
	  if (token->type == CPP_COMMENT
	      && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
	    continue;

1285
	  cpp_error (pfile, CPP_DL_ERROR,
1286
		     "\"%s\" may not appear in macro parameter list",
1287
		     cpp_token_as_text (pfile, token));
1288
	  return false;
Neil Booth committed
1289

Zack Weinberg committed
1290
	case CPP_NAME:
Neil Booth committed
1291 1292
	  if (prev_ident)
	    {
1293
	      cpp_error (pfile, CPP_DL_ERROR,
1294
			 "macro parameters must be comma-separated");
1295
	      return false;
Neil Booth committed
1296 1297 1298
	    }
	  prev_ident = 1;

1299
	  if (_cpp_save_parameter (pfile, macro, token->val.node))
1300
	    return false;
Neil Booth committed
1301
	  continue;
Zack Weinberg committed
1302

Neil Booth committed
1303 1304
	case CPP_CLOSE_PAREN:
	  if (prev_ident || macro->paramc == 0)
1305
	    return true;
Zack Weinberg committed
1306

Neil Booth committed
1307 1308 1309 1310
	  /* Fall through to pick up the error.  */
	case CPP_COMMA:
	  if (!prev_ident)
	    {
1311
	      cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
1312
	      return false;
Neil Booth committed
1313 1314
	    }
	  prev_ident = 0;
Zack Weinberg committed
1315 1316
	  continue;

Neil Booth committed
1317
	case CPP_ELLIPSIS:
1318
	  macro->variadic = 1;
Neil Booth committed
1319 1320
	  if (!prev_ident)
	    {
1321 1322
	      _cpp_save_parameter (pfile, macro,
				   pfile->spec_nodes.n__VA_ARGS__);
Neil Booth committed
1323 1324
	      pfile->state.va_args_ok = 1;
	      if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1325
		cpp_error (pfile, CPP_DL_PEDWARN,
1326
			   "anonymous variadic macros were introduced in C99");
Neil Booth committed
1327 1328
	    }
	  else if (CPP_OPTION (pfile, pedantic))
1329
	    cpp_error (pfile, CPP_DL_PEDWARN,
1330
		       "ISO C does not permit named variadic macros");
Zack Weinberg committed
1331

Neil Booth committed
1332
	  /* We're at the end, and just expect a closing parenthesis.  */
1333 1334
	  token = _cpp_lex_token (pfile);
	  if (token->type == CPP_CLOSE_PAREN)
1335
	    return true;
Neil Booth committed
1336
	  /* Fall through.  */
Zack Weinberg committed
1337

Neil Booth committed
1338
	case CPP_EOF:
1339
	  cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
1340
	  return false;
Zack Weinberg committed
1341 1342
	}
    }
Neil Booth committed
1343 1344
}

1345
/* Allocate room for a token from a macro's replacement list.  */
Neil Booth committed
1346
static cpp_token *
1347
alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
Neil Booth committed
1348
{
1349 1350
  if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
Zack Weinberg committed
1351

1352
  return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1353 1354
}

1355 1356
/* Lex a token from the expansion of MACRO, but mark parameters as we
   find them and warn of traditional stringification.  */
1357
static cpp_token *
1358
lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
1359 1360 1361 1362 1363
{
  cpp_token *token;

  pfile->cur_token = alloc_expansion_token (pfile, macro);
  token = _cpp_lex_direct (pfile);
Neil Booth committed
1364

1365
  /* Is this a parameter?  */
1366 1367
  if (token->type == CPP_NAME
      && (token->val.node->flags & NODE_MACRO_ARG) != 0)
Neil Booth committed
1368 1369
    {
      token->type = CPP_MACRO_ARG;
1370
      token->val.arg_no = token->val.node->value.arg_index;
Neil Booth committed
1371 1372 1373 1374 1375 1376
    }
  else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
	   && (token->type == CPP_STRING || token->type == CPP_CHAR))
    check_trad_stringification (pfile, macro, &token->val.str);

  return token;
Zack Weinberg committed
1377 1378
}

1379
static bool
1380
create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
Zack Weinberg committed
1381
{
1382
  cpp_token *token;
1383
  const cpp_token *ctoken;
Neil Booth committed
1384 1385 1386

  /* Get the first token of the expansion (or the '(' of a
     function-like macro).  */
1387 1388 1389
  ctoken = _cpp_lex_token (pfile);

  if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Neil Booth committed
1390
    {
1391
      bool ok = parse_params (pfile, macro);
1392 1393
      macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
      if (!ok)
1394
	return false;
1395 1396

      /* Success.  Commit the parameter array.  */
1397
      BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
Neil Booth committed
1398 1399
      macro->fun_like = 1;
    }
1400
  else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1401
    cpp_error (pfile, CPP_DL_PEDWARN,
1402
	       "ISO C requires whitespace after the macro name");
Zack Weinberg committed
1403

1404 1405 1406 1407 1408 1409 1410
  if (macro->fun_like)
    token = lex_expansion_token (pfile, macro);
  else
    {
      token = alloc_expansion_token (pfile, macro);
      *token = *ctoken;
    }
Zack Weinberg committed
1411

Neil Booth committed
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  for (;;)
    {
      /* Check the stringifying # constraint 6.10.3.2.1 of
	 function-like macros when lexing the subsequent token.  */
      if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
	{
	  if (token->type == CPP_MACRO_ARG)
	    {
	      token->flags &= ~PREV_WHITE;
	      token->flags |= STRINGIFY_ARG;
	      token->flags |= token[-1].flags & PREV_WHITE;
	      token[-1] = token[0];
	      macro->count--;
	    }
	  /* Let assembler get away with murder.  */
1427
	  else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth committed
1428
	    {
1429
	      cpp_error (pfile, CPP_DL_ERROR,
1430
			 "'#' is not followed by a macro parameter");
1431
	      return false;
Neil Booth committed
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
	    }
	}

      if (token->type == CPP_EOF)
	break;

      /* Paste operator constraint 6.10.3.3.1.  */
      if (token->type == CPP_PASTE)
	{
	  /* Token-paste ##, can appear in both object-like and
	     function-like macros, but not at the ends.  */
	  if (--macro->count > 0)
	    token = lex_expansion_token (pfile, macro);

	  if (macro->count == 0 || token->type == CPP_EOF)
	    {
1448
	      cpp_error (pfile, CPP_DL_ERROR,
1449
		 "'##' cannot appear at either end of a macro expansion");
1450
	      return false;
Neil Booth committed
1451
	    }
Zack Weinberg committed
1452

Neil Booth committed
1453 1454 1455 1456 1457 1458
	  token[-1].flags |= PASTE_LEFT;
	}

      token = lex_expansion_token (pfile, macro);
    }

1459
  macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1460

Neil Booth committed
1461 1462
  /* Don't count the CPP_EOF.  */
  macro->count--;
Neil Booth committed
1463

1464
  /* Clear whitespace on first token for warn_of_redefinition().  */
1465
  if (macro->count)
1466
    macro->exp.tokens[0].flags &= ~PREV_WHITE;
1467 1468

  /* Commit the memory.  */
1469
  BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
1470

1471 1472 1473
  return true;
}

1474
/* Parse a macro and save its expansion.  Returns nonzero on success.  */
1475
bool
1476
_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
1477 1478 1479 1480
{
  cpp_macro *macro;
  unsigned int i;
  bool ok;
1481

1482 1483 1484 1485 1486
  macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
  macro->line = pfile->directive_line;
  macro->params = 0;
  macro->paramc = 0;
  macro->variadic = 0;
1487
  macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1488 1489
  macro->count = 0;
  macro->fun_like = 0;
1490
  /* To suppress some diagnostics.  */
Neil Booth committed
1491
  macro->syshdr = pfile->map->sysp != 0;
1492

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
  if (CPP_OPTION (pfile, traditional))
    ok = _cpp_create_trad_definition (pfile, macro);
  else
    {
      cpp_token *saved_cur_token = pfile->cur_token;

      ok = create_iso_definition (pfile, macro);

      /* Restore lexer position because of games lex_expansion_token()
	 plays lexing the macro.  We set the type for SEEN_EOL() in
	 cpplib.c.

	 Longer term we should lex the whole line before coming here,
	 and just copy the expansion.  */
      saved_cur_token[-1].type = pfile->cur_token[-1].type;
      pfile->cur_token = saved_cur_token;

      /* Stop the lexer accepting __VA_ARGS__.  */
      pfile->state.va_args_ok = 0;
    }

  /* Clear the fast argument lookup indices.  */
  for (i = macro->paramc; i-- > 0; )
1516 1517 1518 1519 1520
    {
      struct cpp_hashnode *node = macro->params[i];
      node->flags &= ~ NODE_MACRO_ARG;
      node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
    }
1521 1522 1523 1524

  if (!ok)
    return ok;

1525
  if (node->type == NT_MACRO)
Zack Weinberg committed
1526
    {
1527 1528 1529
      if (CPP_OPTION (pfile, warn_unused_macros))
	_cpp_warn_if_unused_macro (pfile, node, NULL);

1530
      if (warn_of_redefinition (pfile, node, macro))
Zack Weinberg committed
1531
	{
1532
	  cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
1533
			       "\"%s\" redefined", NODE_NAME (node));
Neil Booth committed
1534

1535
	  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1536
	    cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1537 1538
				 node->value.macro->line, 0,
			 "this is the location of the previous definition");
Zack Weinberg committed
1539 1540 1541
	}
    }

1542 1543 1544
  if (node->type != NT_VOID)
    _cpp_free_definition (node);

Zack Weinberg committed
1545
  /* Enter definition in hash table.  */
Neil Booth committed
1546 1547
  node->type = NT_MACRO;
  node->value.macro = macro;
1548
  if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1549
    node->flags |= NODE_WARN;
Zack Weinberg committed
1550

Neil Booth committed
1551
  return ok;
Zack Weinberg committed
1552 1553
}

1554 1555
/* Warn if a token in STRING matches one of a function-like MACRO's
   parameters.  */
1556
static void
1557 1558
check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
			    const cpp_string *string)
1559
{
Neil Booth committed
1560
  unsigned int i, len;
1561
  const uchar *p, *q, *limit;
1562

1563
  /* Loop over the string.  */
1564 1565
  limit = string->text + string->len - 1;
  for (p = string->text + 1; p < limit; p = q)
1566 1567
    {
      /* Find the start of an identifier.  */
1568 1569
      while (p < limit && !is_idstart (*p))
	p++;
1570 1571 1572

      /* Find the end of the identifier.  */
      q = p;
1573 1574
      while (q < limit && is_idchar (*q))
	q++;
Neil Booth committed
1575 1576 1577

      len = q - p;

1578 1579
      /* Loop over the function macro arguments to see if the
	 identifier inside the string matches one of them.  */
Neil Booth committed
1580 1581 1582
      for (i = 0; i < macro->paramc; i++)
	{
	  const cpp_hashnode *node = macro->params[i];
1583

1584 1585
	  if (NODE_LEN (node) == len
	      && !memcmp (p, NODE_NAME (node), len))
1586
	    {
1587
	      cpp_error (pfile, CPP_DL_WARNING,
1588
	   "macro argument \"%s\" would be stringified in traditional C",
1589
			 NODE_NAME (node));
1590 1591 1592 1593 1594
	      break;
	    }
	}
    }
}
Neil Booth committed
1595

1596 1597 1598 1599
/* Returns the name, arguments and expansion of a macro, in a format
   suitable to be read back in again, and therefore also for DWARF 2
   debugging info.  e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
   Caller is expected to generate the "#define" bit if needed.  The
Neil Booth committed
1600 1601
   returned text is temporary, and automatically freed later.  */
const unsigned char *
1602
cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
Neil Booth committed
1603 1604 1605 1606 1607 1608 1609
{
  unsigned int i, len;
  const cpp_macro *macro = node->value.macro;
  unsigned char *buffer;

  if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
    {
1610
      cpp_error (pfile, CPP_DL_ICE,
1611
		 "invalid hash type %d in cpp_macro_definition", node->type);
Neil Booth committed
1612 1613 1614 1615
      return 0;
    }

  /* Calculate length.  */
1616
  len = NODE_LEN (node) + 2;			/* ' ' and NUL.  */
Neil Booth committed
1617 1618
  if (macro->fun_like)
    {
1619 1620
      len += 4;		/* "()" plus possible final ".." of named
			   varargs (we have + 1 below).  */
Neil Booth committed
1621
      for (i = 0; i < macro->paramc; i++)
1622
	len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth committed
1623 1624
    }

1625 1626 1627
  if (CPP_OPTION (pfile, traditional))
    len += _cpp_replacement_text_len (macro);
  else
Neil Booth committed
1628
    {
1629 1630 1631
      for (i = 0; i < macro->count; i++)
	{
	  cpp_token *token = &macro->exp.tokens[i];
Neil Booth committed
1632

1633 1634 1635
	  if (token->type == CPP_MACRO_ARG)
	    len += NODE_LEN (macro->params[token->val.arg_no - 1]);
	  else
1636
	    len += cpp_token_len (token) + 1; /* Includes room for ' '.  */
1637 1638 1639 1640 1641
	  if (token->flags & STRINGIFY_ARG)
	    len++;			/* "#" */
	  if (token->flags & PASTE_LEFT)
	    len += 3;		/* " ##" */
	}
Neil Booth committed
1642 1643 1644
    }

  if (len > pfile->macro_buffer_len)
1645
    {
1646
      pfile->macro_buffer = xrealloc (pfile->macro_buffer, len);
1647 1648
      pfile->macro_buffer_len = len;
    }
1649 1650

  /* Fill in the buffer.  Start with the macro name.  */
Neil Booth committed
1651
  buffer = pfile->macro_buffer;
1652 1653
  memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
  buffer += NODE_LEN (node);
Neil Booth committed
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664

  /* Parameter names.  */
  if (macro->fun_like)
    {
      *buffer++ = '(';
      for (i = 0; i < macro->paramc; i++)
	{
	  cpp_hashnode *param = macro->params[i];

	  if (param != pfile->spec_nodes.n__VA_ARGS__)
	    {
1665 1666
	      memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
	      buffer += NODE_LEN (param);
Neil Booth committed
1667 1668 1669
	    }

	  if (i + 1 < macro->paramc)
1670 1671 1672
	    /* Don't emit a space after the comma here; we're trying
	       to emit a Dwarf-friendly definition, and the Dwarf spec
	       forbids spaces in the argument list.  */
1673
	    *buffer++ = ',';
1674
	  else if (macro->variadic)
Neil Booth committed
1675 1676 1677 1678 1679
	    *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
	}
      *buffer++ = ')';
    }

1680 1681 1682 1683
  /* The Dwarf spec requires a space after the macro name, even if the
     definition is the empty string.  */
  *buffer++ = ' ';

1684 1685 1686
  if (CPP_OPTION (pfile, traditional))
    buffer = _cpp_copy_replacement_text (macro, buffer);
  else if (macro->count)
Neil Booth committed
1687 1688 1689 1690
  /* Expansion tokens.  */
    {
      for (i = 0; i < macro->count; i++)
	{
1691
	  cpp_token *token = &macro->exp.tokens[i];
Neil Booth committed
1692 1693 1694 1695 1696 1697 1698 1699

	  if (token->flags & PREV_WHITE)
	    *buffer++ = ' ';
	  if (token->flags & STRINGIFY_ARG)
	    *buffer++ = '#';

	  if (token->type == CPP_MACRO_ARG)
	    {
1700 1701 1702
	      len = NODE_LEN (macro->params[token->val.arg_no - 1]);
	      memcpy (buffer,
		      NODE_NAME (macro->params[token->val.arg_no - 1]), len);
Neil Booth committed
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
	      buffer += len;
	    }
	  else
	    buffer = cpp_spell_token (pfile, token, buffer);

	  if (token->flags & PASTE_LEFT)
	    {
	      *buffer++ = ' ';
	      *buffer++ = '#';
	      *buffer++ = '#';
	      /* Next has PREV_WHITE; see _cpp_create_definition.  */
	    }
	}
    }

  *buffer = '\0';
  return pfile->macro_buffer;
}