cppmacro.c 46.2 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 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
static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
Neil Booth committed
44
static int builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
45
static void push_token_context
Neil Booth committed
46
  PARAMS ((cpp_reader *, cpp_hashnode *, const cpp_token *, unsigned int));
47
static void push_ptoken_context
Neil Booth committed
48
  PARAMS ((cpp_reader *, cpp_hashnode *, _cpp_buff *,
49
	   const cpp_token **, unsigned int));
50
static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
Neil Booth committed
51
static cpp_context *next_context PARAMS ((cpp_reader *));
52 53
static const cpp_token *padding_token
  PARAMS ((cpp_reader *, const cpp_token *));
Neil Booth committed
54
static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
55
static const cpp_token *new_string_token PARAMS ((cpp_reader *, uchar *,
56
						  unsigned int));
57
static const cpp_token *new_number_token PARAMS ((cpp_reader *, unsigned int));
58 59
static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
Neil Booth committed
60 61
static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
				  const cpp_token *));
62 63
static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, cpp_macro *,
				  macro_arg *));
64
static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *));
65
static bool create_iso_definition PARAMS ((cpp_reader *, cpp_macro *));
Neil Booth committed
66 67 68

/* #define directive parsing and handling.  */

69
static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
Neil Booth committed
70
static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
71 72
static bool warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
					  const cpp_macro *));
Neil Booth committed
73
static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
74
static void check_trad_stringification PARAMS ((cpp_reader *,
Neil Booth committed
75
						const cpp_macro *,
76
						const cpp_string *));
Zack Weinberg committed
77

78 79 80 81 82 83
/* 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 *
new_string_token (pfile, text, len)
     cpp_reader *pfile;
     unsigned char *text;
Neil Booth committed
84 85
     unsigned int len;
{
86
  cpp_token *token = _cpp_temp_token (pfile);
Neil Booth committed
87

88
  text[len] = '\0';
Neil Booth committed
89
  token->type = CPP_STRING;
90 91
  token->val.str.len = len;
  token->val.str.text = text;
Neil Booth committed
92
  token->flags = 0;
93
  return token;
Neil Booth committed
94 95
}

96 97 98
/* Allocates and returns a CPP_NUMBER token evaluating to NUMBER.  */
static const cpp_token *
new_number_token (pfile, number)
Neil Booth committed
99
     cpp_reader *pfile;
100
     unsigned int number;
Neil Booth committed
101
{
102
  cpp_token *token = _cpp_temp_token (pfile);
103 104
  /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers.  */
  unsigned char *buf = _cpp_unaligned_alloc (pfile, 21);
Neil Booth committed
105

106
  sprintf ((char *) buf, "%u", number);
Neil Booth committed
107 108 109 110
  token->type = CPP_NUMBER;
  token->val.str.text = buf;
  token->val.str.len = ustrlen (buf);
  token->flags = 0;
111
  return token;
Neil Booth committed
112 113 114 115 116 117 118 119
}

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

Neil Booth committed
120 121
/* Handle builtin macros like __FILE__, and push the resulting token
   on the context stack.  Also handles _Pragma, for which no new token
122 123
   is created.  Returns 1 if it generates a new token context, 0 to
   return the token to the caller.  */
Neil Booth committed
124
static int
125
builtin_macro (pfile, node)
Neil Booth committed
126
     cpp_reader *pfile;
127
     cpp_hashnode *node;
Neil Booth committed
128
{
Neil Booth committed
129 130
  const cpp_token *result;

Neil Booth committed
131 132
  switch (node->value.builtin)
    {
133
    default:
134 135
      cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
		 NODE_NAME (node));
Neil Booth committed
136
      return 0;
137

Neil Booth committed
138 139
    case BT_FILE:
    case BT_BASE_FILE:
Zack Weinberg committed
140
      {
141
	unsigned int len;
142
	const char *name;
143
	uchar *buf;
144
	const struct line_map *map = pfile->map;
145 146

	if (node->value.builtin == BT_BASE_FILE)
147 148
	  while (! MAIN_FILE_P (map))
	    map = INCLUDED_FROM (&pfile->line_maps, map);
149

150
	name = map->to_file;
151
	len = strlen (name);
152
	buf = _cpp_unaligned_alloc (pfile, len * 4 + 1);
153
	len = cpp_quote_string (buf, (const unsigned char *) name, len) - buf;
154

Neil Booth committed
155
	result = new_string_token (pfile, buf, len);
Zack Weinberg committed
156
      }
Neil Booth committed
157 158
      break;

Neil Booth committed
159
    case BT_INCLUDE_LEVEL:
160 161 162
      /* The line map depth counts the primary source as level 1, but
	 historically __INCLUDE_DEPTH__ has called the primary source
	 level 0.  */
Neil Booth committed
163 164
      result = new_number_token (pfile, pfile->line_maps.depth - 1);
      break;
Neil Booth committed
165 166 167 168 169

    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.  */
Neil Booth committed
170 171 172 173
      result = new_number_token (pfile,
				 SOURCE_LINE (pfile->map,
					      pfile->cur_token[-1].line));
      break;
Neil Booth committed
174

175 176
      /* __STDC__ has the value 1 under normal circumstances.
	 However, if (a) we are in a system header, (b) the option
177 178 179
	 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
180 181
    case BT_STDC:
      {
182
	int stdc;
183
	enum c_lang lang = CPP_OPTION (pfile, lang);
184 185
	if (CPP_IN_SYSTEM_HEADER (pfile)
	    && CPP_OPTION (pfile, stdc_0_in_system_headers)
186 187
	    && !(lang == CLK_STDC89 || lang == CLK_STDC94
		 || lang == CLK_STDC99))  /* || lang == CLK_CXX98 ? */
188 189 190 191
	  stdc = 0;
	else
	  stdc = 1;

Neil Booth committed
192
	result = new_number_token (pfile, stdc);
Neil Booth committed
193
      }
Neil Booth committed
194
      break;
Zack Weinberg committed
195

Neil Booth committed
196 197 198 199
    case BT_DATE:
    case BT_TIME:
      if (pfile->date.type == CPP_EOF)
	{
200 201 202 203
	  /* 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.  */
Neil Booth committed
204 205 206
	  time_t tt = time (NULL);
	  struct tm *tb = localtime (&tt);

207
	  pfile->date.val.str.text =
208
	    _cpp_unaligned_alloc (pfile, sizeof ("Oct 11 1347"));
209 210 211
	  pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
	  pfile->date.type = CPP_STRING;
	  pfile->date.flags = 0;
Neil Booth committed
212 213
	  sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
		   monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
214 215

	  pfile->time.val.str.text =
216
	    _cpp_unaligned_alloc (pfile, sizeof ("12:34:56"));
217 218 219
	  pfile->time.val.str.len = sizeof ("12:34:56") - 1;
	  pfile->time.type = CPP_STRING;
	  pfile->time.flags = 0;
Neil Booth committed
220 221 222 223
	  sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
		   tb->tm_hour, tb->tm_min, tb->tm_sec);
	}

Neil Booth committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237
      if (node->value.builtin == BT_DATE)
	result = &pfile->date;
      else
	result = &pfile->time;
      break;

    case BT_PRAGMA:
      /* 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
238
    }
Neil Booth committed
239 240 241

  push_token_context (pfile, NULL, result, 1);
  return 1;
Zack Weinberg committed
242 243
}

244 245
/* Copies SRC, of length LEN, to DEST, adding backslashes before all
   backslashes and double quotes.  Non-printable characters are
246 247
   converted to octal.  DEST must be of sufficient size.  Returns
   a pointer to the end of the string.  */
248
uchar *
249
cpp_quote_string (dest, src, len)
250 251
     uchar *dest;
     const uchar *src;
Neil Booth committed
252 253 254 255
     unsigned int len;
{
  while (len--)
    {
256
      uchar c = *src++;
Zack Weinberg committed
257

Neil Booth committed
258 259 260 261 262 263 264 265 266 267
      if (c == '\\' || c == '"')
	{
	  *dest++ = '\\';
	  *dest++ = c;
	}
      else
	{
	  if (ISPRINT (c))
	    *dest++ = c;
	  else
Zack Weinberg committed
268
	    {
Neil Booth committed
269 270
	      sprintf ((char *) dest, "\\%03o", c);
	      dest += 4;
Zack Weinberg committed
271
	    }
Neil Booth committed
272 273
	}
    }
Zack Weinberg committed
274

Neil Booth committed
275 276
  return dest;
}
Zack Weinberg committed
277

278 279
/* Convert a token sequence ARG to a single string token according to
   the rules of the ISO C #-operator.  */
280
static const cpp_token *
Neil Booth committed
281 282 283 284
stringify_arg (pfile, arg)
     cpp_reader *pfile;
     macro_arg *arg;
{
285 286
  unsigned char *dest = BUFF_FRONT (pfile->u_buff);
  unsigned int i, escape_it, backslash_count = 0;
287
  const cpp_token *source = NULL;
288
  size_t len;
Zack Weinberg committed
289

Neil Booth committed
290 291 292
  /* Loop, reading in the argument's tokens.  */
  for (i = 0; i < arg->count; i++)
    {
293 294 295 296 297 298 299 300
      const cpp_token *token = arg->first[i];

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

Neil Booth committed
302
      escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
303
		   || token->type == CPP_CHAR || token->type == CPP_WCHAR);
Zack Weinberg committed
304

305 306
      /* Room for each char being written in octal, initial space and
	 final NUL.  */
307
      len = cpp_token_len (token);
Neil Booth committed
308 309
      if (escape_it)
	len *= 4;
310
      len += 2;
Zack Weinberg committed
311

312
      if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
Neil Booth committed
313
	{
314
	  size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
315
	  _cpp_extend_buff (pfile, &pfile->u_buff, len);
316
	  dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
Neil Booth committed
317
	}
Zack Weinberg committed
318

319
      /* Leading white space?  */
320
      if (dest != BUFF_FRONT (pfile->u_buff))
321 322 323 324 325 326 327
	{
	  if (source == NULL)
	    source = token;
	  if (source->flags & PREV_WHITE)
	    *dest++ = ' ';
	}
      source = NULL;
Zack Weinberg committed
328

Neil Booth committed
329 330
      if (escape_it)
	{
331 332
	  _cpp_buff *buff = _cpp_get_buff (pfile, len);
	  unsigned char *buf = BUFF_FRONT (buff);
Neil Booth committed
333
	  len = cpp_spell_token (pfile, token, buf) - buf;
334
	  dest = cpp_quote_string (dest, buf, len);
335
	  _cpp_release_buff (pfile, buff);
Neil Booth committed
336 337 338 339
	}
      else
	dest = cpp_spell_token (pfile, token, dest);

340
      if (token->type == CPP_OTHER && token->val.c == '\\')
Neil Booth committed
341 342 343 344 345 346 347 348
	backslash_count++;
      else
	backslash_count = 0;
    }

  /* Ignore the final \ of invalid string literals.  */
  if (backslash_count & 1)
    {
349 350
      cpp_error (pfile, DL_WARNING,
		 "invalid string literal, ignoring final '\\'");
351
      dest--;
Neil Booth committed
352 353
    }

354
  /* Commit the memory, including NUL, and return the token.  */
355 356 357
  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
358 359
}

Neil Booth committed
360 361 362 363 364
/* Try to paste two tokens.  On success, return non-zero.  In any
   case, PLHS is updated to point to the pasted token, which is
   guaranteed to not have the PASTE_LEFT flag set.  */
static bool
paste_tokens (pfile, plhs, rhs)
365
     cpp_reader *pfile;
Neil Booth committed
366
     const cpp_token **plhs, *rhs;
367
{
Neil Booth committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  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;
  buf = (unsigned char *) alloca (len);
  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.  */
  if (lhs->type == CPP_DIV
      && (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
    *end++ = ' ';
  end = cpp_spell_token (pfile, rhs, end);
386
  *end = '\0';
Neil Booth committed
387 388 389 390 391 392 393 394 395 396 397 398

  cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);

  /* Tweak the column number the lexer will report.  */
  pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;

  /* We don't want a leading # to be interpreted as a directive.  */
  pfile->buffer->saved_flags = 0;

  /* Set pfile->cur_token as required by _cpp_lex_direct.  */
  pfile->cur_token = _cpp_temp_token (pfile);
  *plhs = _cpp_lex_direct (pfile);
399
  valid = pfile->buffer->cur == pfile->buffer->rlimit;
Neil Booth committed
400 401 402
  _cpp_pop_buffer (pfile);

  return valid;
403 404
}

405 406 407 408 409 410 411
/* 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
412 413 414
static void
paste_all_tokens (pfile, lhs)
     cpp_reader *pfile;
415
     const cpp_token *lhs;
Neil Booth committed
416
{
417 418 419
  const cpp_token *rhs;
  cpp_context *context = pfile->context;

Neil Booth committed
420 421 422 423 424 425
  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
426
	 guarantee we have at least one more token.  */
427
      if (context->direct_p)
428
	rhs = FIRST (context).token++;
429
      else
430
	rhs = *FIRST (context).ptoken++;
431 432 433 434

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

Neil Booth committed
435
      if (!paste_tokens (pfile, &lhs, rhs))
Neil Booth committed
436
	{
437
	  _cpp_backup_tokens (pfile, 1);
Neil Booth committed
438

439
	  /* Mandatory error for all apart from assembler.  */
Neil Booth committed
440
	  if (CPP_OPTION (pfile, lang) != CLK_ASM)
441
	    cpp_error (pfile, DL_ERROR,
Neil Booth committed
442
	 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
443 444
		       cpp_token_as_text (pfile, lhs),
		       cpp_token_as_text (pfile, rhs));
Neil Booth committed
445
	  break;
Neil Booth committed
446 447 448 449
	}
    }
  while (rhs->flags & PASTE_LEFT);

Neil Booth committed
450 451
  /* Put the resulting token in its own context.  */
  push_token_context (pfile, NULL, lhs, 1);
Neil Booth committed
452 453
}

454 455 456 457 458
/* 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().  */
459 460
static _cpp_buff *
collect_args (pfile, node)
Neil Booth committed
461
     cpp_reader *pfile;
462
     const cpp_hashnode *node;
Neil Booth committed
463
{
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
  _cpp_buff *buff, *base_buff;
  cpp_macro *macro;
  macro_arg *args, *arg;
  const cpp_token *token;
  unsigned int argc;
  bool error = false;

  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));
481
  buff->cur = (unsigned char *) &args[argc];
482 483 484 485 486 487
  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
488
    {
489 490
      unsigned int paren_depth = 0;
      unsigned int ntokens = 0;
Neil Booth committed
491

492 493
      argc++;
      arg->first = (const cpp_token **) buff->cur;
Zack Weinberg committed
494

495 496 497
      for (;;)
	{
	  /* Require space for 2 new tokens (including a CPP_EOF).  */
498
	  if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
499
	    {
500 501
	      buff = _cpp_append_extend_buff (pfile, buff,
					      1000 * sizeof (cpp_token *));
502 503
	      arg->first = (const cpp_token **) buff->cur;
	    }
504

505
	  token = cpp_get_token (pfile);
Zack Weinberg committed
506

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
	  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
531

532 533
	  arg->first[ntokens++] = token;
	}
Neil Booth committed
534

535 536 537
      /* Drop trailing padding.  */
      while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
	ntokens--;
Neil Booth committed
538

539 540
      arg->count = ntokens;
      arg->first[ntokens] = &pfile->eof;
Neil Booth committed
541

542 543 544 545
      /* Terminate the argument.  Excess arguments loop back and
	 overwrite the final legitimate argument, before failing.  */
      if (argc <= macro->paramc)
	{
546
	  buff->cur = (unsigned char *) &arg->first[ntokens + 1];
547 548 549
	  if (argc != macro->paramc)
	    arg++;
	}
Neil Booth committed
550
    }
551
  while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
Neil Booth committed
552

553
  if (token->type == CPP_EOF)
Neil Booth committed
554
    {
555 556 557 558
      /* 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.  */
559
      if (pfile->context->prev || pfile->state.in_directive)
560
	_cpp_backup_tokens (pfile, 1);
561 562
      cpp_error (pfile, DL_ERROR,
		 "unterminated argument list invoking macro \"%s\"",
563
		 NODE_NAME (node));
564
      error = true;
Neil Booth committed
565 566 567 568 569 570 571
    }
  else 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");
572

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

576
      if (argc + 1 == macro->paramc && macro->variadic)
Neil Booth committed
577
	{
578
	  if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
579 580
	    cpp_error (pfile, DL_PEDWARN,
		       "ISO C99 requires rest arguments to be used");
Neil Booth committed
581 582 583
	}
      else
	{
584
	  cpp_error (pfile, DL_ERROR,
Neil Booth committed
585
		     "macro \"%s\" requires %u arguments, but only %u given",
586
		     NODE_NAME (node), macro->paramc, argc);
587
	  error = true;
Neil Booth committed
588 589 590 591
	}
    }
  else if (argc > macro->paramc)
    {
Neil Booth committed
592
      /* Empty argument to a macro taking no arguments is OK.  */
593
      if (argc != 1 || arg->count)
Neil Booth committed
594
	{
595
	  cpp_error (pfile, DL_ERROR,
Neil Booth committed
596
		     "macro \"%s\" passed %u arguments, but takes just %u",
597
		     NODE_NAME (node), argc, macro->paramc);
598
	  error = true;
Neil Booth committed
599 600 601
	}
    }

602 603
  if (!error)
    return base_buff;
Neil Booth committed
604

605 606
  _cpp_release_buff (pfile, base_buff);
  return NULL;
Neil Booth committed
607 608
}

609 610 611 612 613
/* 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 *
614
funlike_invocation_p (pfile, node)
Neil Booth committed
615
     cpp_reader *pfile;
Neil Booth committed
616
     cpp_hashnode *node;
Neil Booth committed
617
{
618
  const cpp_token *token, *padding = NULL;
619

620
  for (;;)
621
    {
622 623 624 625 626 627
      token = cpp_get_token (pfile);
      if (token->type != CPP_PADDING)
	break;
      if (padding == NULL
	  || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
	padding = token;
628
    }
Neil Booth committed
629

630
  if (token->type == CPP_OPEN_PAREN)
Neil Booth committed
631
    {
632 633
      pfile->state.parsing_args = 2;
      return collect_args (pfile, node);
Neil Booth committed
634 635
    }

636 637 638 639 640 641 642 643 644 645 646
  /* 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);
    }
647 648

  return NULL;
Neil Booth committed
649 650
}

651 652 653 654
/* 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
655
static int
656
enter_macro_context (pfile, node)
Zack Weinberg committed
657
     cpp_reader *pfile;
658
     cpp_hashnode *node;
Zack Weinberg committed
659
{
660
  /* The presence of a macro invalidates a file's controlling macro.  */
Neil Booth committed
661 662
  pfile->mi_valid = false;

663 664
  pfile->state.angled_headers = false;

665
  /* Handle standard macros.  */
Neil Booth committed
666
  if (! (node->flags & NODE_BUILTIN))
Zack Weinberg committed
667
    {
668
      cpp_macro *macro = node->value.macro;
Neil Booth committed
669

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
      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)
685
		cpp_error (pfile, DL_WARNING,
686
 "function-like macro \"%s\" must be used with arguments in traditional C",
687
			   NODE_NAME (node));
688 689 690 691

	      return 0;
	    }

692 693
	  if (macro->paramc > 0)
	    replace_args (pfile, node, macro, (macro_arg *) buff->base);
694 695
	  _cpp_release_buff (pfile, buff);
	}
Neil Booth committed
696

697
      /* Disable the macro within its expansion.  */
Neil Booth committed
698
      node->flags |= NODE_DISABLED;
Neil Booth committed
699

700
      if (macro->paramc == 0)
701
	push_token_context (pfile, node, macro->exp.tokens, macro->count);
Neil Booth committed
702 703

      return 1;
Zack Weinberg committed
704
    }
Neil Booth committed
705

706
  /* Handle built-in macros and the _Pragma operator.  */
Neil Booth committed
707
  return builtin_macro (pfile, node);
Neil Booth committed
708 709
}

710 711 712 713
/* 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
714
static void
715
replace_args (pfile, node, macro, args)
Neil Booth committed
716
     cpp_reader *pfile;
Neil Booth committed
717
     cpp_hashnode *node;
718
     cpp_macro *macro;
Neil Booth committed
719 720 721 722
     macro_arg *args;
{
  unsigned int i, total;
  const cpp_token *src, *limit;
723
  const cpp_token **dest, **first;
Neil Booth committed
724
  macro_arg *arg;
725
  _cpp_buff *buff;
Neil Booth committed
726 727

  /* First, fully macro-expand arguments, calculating the number of
728 729 730
     tokens in the final expansion as we go.  The ordering of the if
     statements below is subtle; we must handle stringification before
     pasting.  */
731
  total = macro->count;
732
  limit = macro->exp.tokens + macro->count;
733

734
  for (src = macro->exp.tokens; src < limit; src++)
Neil Booth committed
735 736
    if (src->type == CPP_MACRO_ARG)
      {
737 738 739
	/* Leading and trailing padding tokens.  */
	total += 2;

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

Neil Booth committed
744 745 746
	if (src->flags & STRINGIFY_ARG)
	  {
	    if (!arg->stringified)
747
	      arg->stringified = stringify_arg (pfile, arg);
Neil Booth committed
748 749
	  }
	else if ((src->flags & PASTE_LEFT)
750
		 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
Neil Booth committed
751 752 753 754
	  total += arg->count - 1;
	else
	  {
	    if (!arg->expanded)
755
	      expand_arg (pfile, arg);
Neil Booth committed
756 757 758 759
	    total += arg->expanded_count - 1;
	  }
      }

760 761
  /* Now allocate space for the expansion, copy the tokens and replace
     the arguments.  */
762 763
  buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
  first = (const cpp_token **) buff->base;
764
  dest = first;
Neil Booth committed
765

766
  for (src = macro->exp.tokens; src < limit; src++)
767 768 769
    {
      unsigned int count;
      const cpp_token **from, **paste_flag;
Neil Booth committed
770

771 772 773 774 775
      if (src->type != CPP_MACRO_ARG)
	{
	  *dest++ = src;
	  continue;
	}
Neil Booth committed
776

777 778 779 780 781 782
      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;
783
      else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	{
	  count = arg->count, from = arg->first;
	  if (dest != first)
	    {
	      /* GCC has special semantics for , ## b where b is a
		 varargs parameter: the comma disappears if b was
		 given no actual arguments (not merely if b is an
		 empty argument); otherwise the paste flag is removed.  */
	      if (dest[-1]->type == CPP_COMMA
		  && macro->variadic
		  && src->val.arg_no == macro->paramc)
		{
		  if (count == 0)
		    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
808

809 810
      /* Padding on the left of an argument (unless RHS of ##).  */
      if (!pfile->state.in_directive
811
	  && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
812
	*dest++ = padding_token (pfile, src);
Neil Booth committed
813

814 815 816 817
      if (count)
	{
	  memcpy (dest, from, count * sizeof (cpp_token *));
	  dest += count;
Neil Booth committed
818

819 820 821 822 823
	  /* 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;
	}
824

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

829 830 831 832 833 834 835 836 837 838 839 840 841
      /* 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
842

Neil Booth committed
843 844
  /* Free the expanded arguments.  */
  for (i = 0; i < macro->paramc; i++)
845 846 847
    if (args[i].expanded)
      free (args[i].expanded);

Neil Booth committed
848
  push_ptoken_context (pfile, node, buff, first, dest - first);
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
}

/* Return a special padding token, with padding inherited from SOURCE.  */
static const cpp_token *
padding_token (pfile, source)
     cpp_reader *pfile;
     const cpp_token *source;
{
  cpp_token *result = _cpp_temp_token (pfile);

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

865 866
/* Get a new uninitialized context.  Create a new one if we cannot
   re-use an old one.  */
867 868 869 870 871 872 873
static cpp_context *
next_context (pfile)
     cpp_reader *pfile;
{
  cpp_context *result = pfile->context->next;

  if (result == 0)
Zack Weinberg committed
874
    {
875 876 877 878
      result = xnew (cpp_context);
      result->prev = pfile->context;
      result->next = 0;
      pfile->context->next = result;
Neil Booth committed
879
    }
880 881 882

  pfile->context = result;
  return result;
Neil Booth committed
883 884
}

885 886
/* Push a list of pointers to tokens.  */
static void
887
push_ptoken_context (pfile, macro, buff, first, count)
Neil Booth committed
888
     cpp_reader *pfile;
Neil Booth committed
889
     cpp_hashnode *macro;
890
     _cpp_buff *buff;
891 892
     const cpp_token **first;
     unsigned int count;
Neil Booth committed
893 894 895
{
  cpp_context *context = next_context (pfile);

896 897
  context->direct_p = false;
  context->macro = macro;
898
  context->buff = buff;
899 900
  FIRST (context).ptoken = first;
  LAST (context).ptoken = first + count;
901 902 903 904 905 906
}

/* Push a list of tokens.  */
static void
push_token_context (pfile, macro, first, count)
     cpp_reader *pfile;
Neil Booth committed
907
     cpp_hashnode *macro;
908 909 910 911 912 913 914
     const cpp_token *first;
     unsigned int count;
{
  cpp_context *context = next_context (pfile);

  context->direct_p = true;
  context->macro = macro;
915
  context->buff = NULL;
916 917
  FIRST (context).token = first;
  LAST (context).token = first + count;
Neil Booth committed
918 919
}

920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
/* Push a traditional macro's replacement text.  */
void
_cpp_push_text_context (pfile, macro, start, end)
     cpp_reader *pfile;
     cpp_hashnode *macro;
     const uchar *start, *end;
{
  cpp_context *context = next_context (pfile);

  context->direct_p = true;
  context->macro = macro;
  context->buff = NULL;
  CUR (context) = start;
  RLIMIT (context) = end;
}

936 937 938 939 940 941
/* 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
942 943 944 945 946
static void
expand_arg (pfile, arg)
     cpp_reader *pfile;
     macro_arg *arg;
{
947 948 949 950
  unsigned int capacity;

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

  /* Loop, reading in the arguments.  */
953 954 955
  capacity = 256;
  arg->expanded = (const cpp_token **)
    xmalloc (capacity * sizeof (cpp_token *));
Neil Booth committed
956

957
  push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
958
  for (;;)
Neil Booth committed
959
    {
960 961 962
      const cpp_token *token;

      if (arg->expanded_count + 1 >= capacity)
Zack Weinberg committed
963
	{
Neil Booth committed
964
	  capacity *= 2;
965 966
	  arg->expanded = (const cpp_token **)
	    xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
Neil Booth committed
967 968
	}

969
      token = cpp_get_token (pfile);
Neil Booth committed
970

971 972 973 974 975 976
      if (token->type == CPP_EOF)
	break;

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

977
  _cpp_pop_context (pfile);
Neil Booth committed
978 979
}

980 981 982
/* 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
983 984 985 986
void
_cpp_pop_context (pfile)
     cpp_reader *pfile;
{
987 988 989
  cpp_context *context = pfile->context;

  if (context->macro)
Neil Booth committed
990
    context->macro->flags &= ~NODE_DISABLED;
991 992 993

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

995
  pfile->context = context->prev;
Neil Booth committed
996 997
}

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
/* Eternal routine to get a token.  Also used nearly everywhere
   internally, except for places where we know we can safely call
   the lexer directly, such as lexing a directive name.

   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.  */
1009 1010
const cpp_token *
cpp_get_token (pfile)
Neil Booth committed
1011 1012
     cpp_reader *pfile;
{
1013 1014
  const cpp_token *result;

1015
  for (;;)
Neil Booth committed
1016
    {
1017
      cpp_hashnode *node;
Neil Booth committed
1018 1019 1020
      cpp_context *context = pfile->context;

      /* Context->prev == 0 <=> base context.  */
1021
      if (!context->prev)
1022
	result = _cpp_lex_token (pfile);
1023
      else if (FIRST (context).token != LAST (context).token)
1024
	{
1025
	  if (context->direct_p)
1026
	    result = FIRST (context).token++;
1027
	  else
1028
	    result = *FIRST (context).ptoken++;
1029 1030

	  if (result->flags & PASTE_LEFT)
1031
	    {
1032 1033 1034 1035
	      paste_all_tokens (pfile, result);
	      if (pfile->state.in_directive)
		continue;
	      return padding_token (pfile, result);
1036
	    }
1037
	}
Neil Booth committed
1038 1039
      else
	{
1040
	  _cpp_pop_context (pfile);
1041 1042 1043
	  if (pfile->state.in_directive)
	    continue;
	  return &pfile->avoid_paste;
Neil Booth committed
1044 1045
	}

1046 1047 1048
      if (pfile->state.in_directive && result->type == CPP_COMMENT)
	continue;

1049
      if (result->type != CPP_NAME)
Neil Booth committed
1050 1051
	break;

1052 1053
      node = result->val.node;

Neil Booth committed
1054 1055
      if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
	break;
1056

Neil Booth committed
1057
      if (!(node->flags & NODE_DISABLED))
Neil Booth committed
1058
	{
Neil Booth committed
1059 1060
	  if (!pfile->state.prevent_expansion
	      && enter_macro_context (pfile, node))
1061
	    {
1062 1063 1064
	      if (pfile->state.in_directive)
		continue;
	      return padding_token (pfile, result);
1065
	    }
Neil Booth committed
1066
	}
Neil Booth committed
1067 1068
      else
	{
1069 1070
	  /* Flag this token as always unexpandable.  FIXME: move this
	     to collect_args()?.  */
Neil Booth committed
1071 1072 1073 1074 1075 1076
	  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;
	}
1077

Neil Booth committed
1078
      break;
Neil Booth committed
1079
    }
1080 1081

  return result;
Neil Booth committed
1082 1083
}

1084 1085 1086 1087
/* 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
Neil Booth committed
1088
cpp_sys_macro_p (pfile)
1089 1090
     cpp_reader *pfile;
{
Neil Booth committed
1091
  cpp_hashnode *node = pfile->context->macro;
1092

Neil Booth committed
1093
  return node && node->value.macro && node->value.macro->syshdr;
1094 1095
}

1096 1097
/* Read each token in, until end of the current file.  Directives are
   transparently processed.  */
Neil Booth committed
1098
void
1099
cpp_scan_nooutput (pfile)
Neil Booth committed
1100 1101
     cpp_reader *pfile;
{
1102 1103 1104 1105
  /* Request a CPP_EOF token at the end of this file, rather than
     transparently continuing with the including file.  */
  pfile->buffer->return_at_eof = true;

1106 1107
  while (cpp_get_token (pfile)->type != CPP_EOF)
    ;
Neil Booth committed
1108 1109
}

1110 1111
/* Step back one (or more) tokens.  Can only step mack more than 1 if
   they are from the lexer, and not from macro expansion.  */
1112
void
1113
_cpp_backup_tokens (pfile, count)
Neil Booth committed
1114
     cpp_reader *pfile;
1115
     unsigned int count;
Neil Booth committed
1116
{
1117
  if (pfile->context->prev == NULL)
Neil Booth committed
1118
    {
1119 1120 1121
      pfile->lookaheads += count;
      while (count--)
	{
1122 1123 1124 1125
	  pfile->cur_token--;
	  if (pfile->cur_token == pfile->cur_run->base
	      /* Possible with -fpreprocessed and no leading #line.  */
	      && pfile->cur_run->prev != NULL)
1126 1127 1128 1129 1130
	    {
	      pfile->cur_run = pfile->cur_run->prev;
	      pfile->cur_token = pfile->cur_run->limit;
	    }
	}
Neil Booth committed
1131
    }
1132
  else
Neil Booth committed
1133
    {
1134 1135
      if (count != 1)
	abort ();
1136
      if (pfile->context->direct_p)
1137
	FIRST (pfile->context).token--;
1138
      else
1139
	FIRST (pfile->context).ptoken--;
Neil Booth committed
1140 1141
    }
}
Zack Weinberg committed
1142

Neil Booth committed
1143 1144
/* #define directive parsing and handling.  */

1145
/* Returns non-zero if a macro redefinition warning is required.  */
1146 1147 1148
static bool
warn_of_redefinition (pfile, node, macro2)
     cpp_reader *pfile;
Neil Booth committed
1149 1150 1151 1152 1153 1154
     const cpp_hashnode *node;
     const cpp_macro *macro2;
{
  const cpp_macro *macro1;
  unsigned int i;

1155 1156
  /* Some redefinitions need to be warned about regardless.  */
  if (node->flags & NODE_WARN)
1157
    return true;
Neil Booth committed
1158

1159
  /* Redefinition of a macro is allowed if and only if the old and new
1160
     definitions are the same.  (6.10.3 paragraph 2).  */
Neil Booth committed
1161 1162 1163 1164 1165 1166
  macro1 = node->value.macro;

  /* The quick failures.  */
  if (macro1->count != macro2->count
      || macro1->paramc != macro2->paramc
      || macro1->fun_like != macro2->fun_like
1167
      || macro1->variadic != macro2->variadic)
1168
    return true;
Neil Booth committed
1169 1170 1171 1172

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

1175 1176 1177 1178 1179 1180 1181 1182 1183
  /* Check the replacement text or tokens.  */
  if (CPP_OPTION (pfile, traditional))
    return memcmp (macro1->exp.text, macro2->exp.text, macro1->count);

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

  return false;
Neil Booth committed
1184 1185 1186
}

/* Free the definition of hashnode H.  */
Zack Weinberg committed
1187 1188 1189 1190
void
_cpp_free_definition (h)
     cpp_hashnode *h;
{
Neil Booth committed
1191 1192 1193
  /* Macros and assertions no longer have anything to free.  */
  h->type = NT_VOID;
  /* Clear builtin flag in case of redefinition.  */
Neil Booth committed
1194
  h->flags &= ~(NODE_BUILTIN | NODE_DISABLED);
Neil Booth committed
1195 1196
}

1197
/* Save parameter NODE to the parameter list of macro MACRO.  Returns
1198
   zero on success, non-zero if the parameter is a duplicate.  */
1199 1200
bool
_cpp_save_parameter (pfile, macro, node)
Neil Booth committed
1201 1202 1203 1204 1205 1206
     cpp_reader *pfile;
     cpp_macro *macro;
     cpp_hashnode *node;
{
  /* Constraint 6.10.3.6 - duplicate parameter names.  */
  if (node->arg_index)
1207
    {
1208 1209
      cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"",
		 NODE_NAME (node));
Neil Booth committed
1210 1211
      return 1;
    }
1212

1213 1214 1215
  if (BUFF_ROOM (pfile->a_buff)
      < (macro->paramc + 1) * sizeof (cpp_hashnode *))
    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
1216

1217 1218
  ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
  node->arg_index = macro->paramc;
Neil Booth committed
1219
  return 0;
Zack Weinberg committed
1220 1221
}

1222
/* Check the syntax of the parameters in a MACRO definition.  */
Neil Booth committed
1223 1224
static int
parse_params (pfile, macro)
Zack Weinberg committed
1225
     cpp_reader *pfile;
Neil Booth committed
1226
     cpp_macro *macro;
Zack Weinberg committed
1227
{
Neil Booth committed
1228
  unsigned int prev_ident = 0;
Zack Weinberg committed
1229

Neil Booth committed
1230
  for (;;)
Zack Weinberg committed
1231
    {
1232
      const cpp_token *token = _cpp_lex_token (pfile);
Zack Weinberg committed
1233

1234
      switch (token->type)
Zack Weinberg committed
1235
	{
Neil Booth committed
1236
	default:
1237 1238 1239 1240 1241 1242
	  /* 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;

1243 1244
	  cpp_error (pfile, DL_ERROR,
		     "\"%s\" may not appear in macro parameter list",
1245
		     cpp_token_as_text (pfile, token));
Neil Booth committed
1246 1247
	  return 0;

Zack Weinberg committed
1248
	case CPP_NAME:
Neil Booth committed
1249 1250
	  if (prev_ident)
	    {
1251 1252
	      cpp_error (pfile, DL_ERROR,
			 "macro parameters must be comma-separated");
Neil Booth committed
1253 1254 1255 1256
	      return 0;
	    }
	  prev_ident = 1;

1257
	  if (_cpp_save_parameter (pfile, macro, token->val.node))
Neil Booth committed
1258 1259
	    return 0;
	  continue;
Zack Weinberg committed
1260

Neil Booth committed
1261 1262
	case CPP_CLOSE_PAREN:
	  if (prev_ident || macro->paramc == 0)
1263
	    return 1;
Zack Weinberg committed
1264

Neil Booth committed
1265 1266 1267 1268
	  /* Fall through to pick up the error.  */
	case CPP_COMMA:
	  if (!prev_ident)
	    {
1269
	      cpp_error (pfile, DL_ERROR, "parameter name missing");
Neil Booth committed
1270 1271 1272
	      return 0;
	    }
	  prev_ident = 0;
Zack Weinberg committed
1273 1274
	  continue;

Neil Booth committed
1275
	case CPP_ELLIPSIS:
1276
	  macro->variadic = 1;
Neil Booth committed
1277 1278
	  if (!prev_ident)
	    {
1279 1280
	      _cpp_save_parameter (pfile, macro,
				   pfile->spec_nodes.n__VA_ARGS__);
Neil Booth committed
1281 1282
	      pfile->state.va_args_ok = 1;
	      if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic))
1283 1284
		cpp_error (pfile, DL_PEDWARN,
			   "anonymous variadic macros were introduced in C99");
Neil Booth committed
1285 1286
	    }
	  else if (CPP_OPTION (pfile, pedantic))
1287 1288
	    cpp_error (pfile, DL_PEDWARN,
		       "ISO C does not permit named variadic macros");
Zack Weinberg committed
1289

Neil Booth committed
1290
	  /* We're at the end, and just expect a closing parenthesis.  */
1291 1292
	  token = _cpp_lex_token (pfile);
	  if (token->type == CPP_CLOSE_PAREN)
1293
	    return 1;
Neil Booth committed
1294
	  /* Fall through.  */
Zack Weinberg committed
1295

Neil Booth committed
1296
	case CPP_EOF:
1297
	  cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list");
Neil Booth committed
1298
	  return 0;
Zack Weinberg committed
1299 1300
	}
    }
Neil Booth committed
1301 1302
}

1303
/* Allocate room for a token from a macro's replacement list.  */
Neil Booth committed
1304
static cpp_token *
1305
alloc_expansion_token (pfile, macro)
Neil Booth committed
1306 1307 1308
     cpp_reader *pfile;
     cpp_macro *macro;
{
1309 1310
  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
1311

1312
  return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
1313 1314
}

1315 1316
/* Lex a token from the expansion of MACRO, but mark parameters as we
   find them and warn of traditional stringification.  */
1317 1318 1319 1320 1321 1322 1323 1324 1325
static cpp_token *
lex_expansion_token (pfile, macro)
     cpp_reader *pfile;
     cpp_macro *macro;
{
  cpp_token *token;

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

1327
  /* Is this a parameter?  */
Neil Booth committed
1328 1329 1330
  if (token->type == CPP_NAME && token->val.node->arg_index)
    {
      token->type = CPP_MACRO_ARG;
1331
      token->val.arg_no = token->val.node->arg_index;
Neil Booth committed
1332 1333 1334 1335 1336 1337
    }
  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
1338 1339
}

1340 1341
static bool
create_iso_definition (pfile, macro)
Zack Weinberg committed
1342
     cpp_reader *pfile;
1343
     cpp_macro *macro;
Zack Weinberg committed
1344
{
1345
  cpp_token *token;
1346
  const cpp_token *ctoken;
Neil Booth committed
1347 1348 1349

  /* Get the first token of the expansion (or the '(' of a
     function-like macro).  */
1350 1351 1352
  ctoken = _cpp_lex_token (pfile);

  if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
Neil Booth committed
1353
    {
1354
      bool ok = parse_params (pfile, macro);
1355 1356
      macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
      if (!ok)
1357
	return false;
1358 1359

      /* Success.  Commit the parameter array.  */
1360
      BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
Neil Booth committed
1361 1362
      macro->fun_like = 1;
    }
1363
  else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
1364 1365
    cpp_error (pfile, DL_PEDWARN,
	       "ISO C requires whitespace after the macro name");
Zack Weinberg committed
1366

1367 1368 1369 1370 1371 1372 1373
  if (macro->fun_like)
    token = lex_expansion_token (pfile, macro);
  else
    {
      token = alloc_expansion_token (pfile, macro);
      *token = *ctoken;
    }
Zack Weinberg committed
1374

Neil Booth committed
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
  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.  */
1390
	  else if (CPP_OPTION (pfile, lang) != CLK_ASM)
Neil Booth committed
1391
	    {
1392 1393
	      cpp_error (pfile, DL_ERROR,
			 "'#' is not followed by a macro parameter");
1394
	      return false;
Neil Booth committed
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
	    }
	}

      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)
	    {
1411
	      cpp_error (pfile, DL_ERROR,
Neil Booth committed
1412
			 "'##' cannot appear at either end of a macro expansion");
1413
	      return false;
Neil Booth committed
1414
	    }
Zack Weinberg committed
1415

Neil Booth committed
1416 1417 1418 1419 1420 1421
	  token[-1].flags |= PASTE_LEFT;
	}

      token = lex_expansion_token (pfile, macro);
    }

1422
  macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
1423

Neil Booth committed
1424 1425
  /* Don't count the CPP_EOF.  */
  macro->count--;
Neil Booth committed
1426

1427
  /* Clear whitespace on first token for warn_of_redefinition().  */
1428
  if (macro->count)
1429
    macro->exp.tokens[0].flags &= ~PREV_WHITE;
1430 1431

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

1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
  return true;
}

/* Parse a macro and save its expansion.  Returns non-zero on success.  */
bool
_cpp_create_definition (pfile, node)
     cpp_reader *pfile;
     cpp_hashnode *node;
{
  cpp_macro *macro;
  unsigned int i;
  bool ok;
1446

1447 1448 1449 1450 1451 1452 1453
  macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
  macro->line = pfile->directive_line;
  macro->params = 0;
  macro->paramc = 0;
  macro->variadic = 0;
  macro->count = 0;
  macro->fun_like = 0;
1454
  /* To suppress some diagnostics.  */
Neil Booth committed
1455
  macro->syshdr = pfile->map->sysp != 0;
1456

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
  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; )
    macro->params[i]->arg_index = 0;

  if (!ok)
    return ok;

Neil Booth committed
1485
  if (node->type != NT_VOID)
Zack Weinberg committed
1486
    {
1487
      if (warn_of_redefinition (pfile, node, macro))
Zack Weinberg committed
1488
	{
1489 1490
	  cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0,
			       "\"%s\" redefined", NODE_NAME (node));
Neil Booth committed
1491

1492
	  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1493 1494 1495
	    cpp_error_with_line (pfile, DL_PEDWARN,
				 node->value.macro->line, 0,
			 "this is the location of the previous definition");
Zack Weinberg committed
1496
	}
Neil Booth committed
1497
      _cpp_free_definition (node);
Zack Weinberg committed
1498 1499 1500
    }

  /* Enter definition in hash table.  */
Neil Booth committed
1501 1502
  node->type = NT_MACRO;
  node->value.macro = macro;
1503
  if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
1504
    node->flags |= NODE_WARN;
Zack Weinberg committed
1505

Neil Booth committed
1506
  return ok;
Zack Weinberg committed
1507 1508
}

1509 1510
/* Warn if a token in STRING matches one of a function-like MACRO's
   parameters.  */
1511
static void
Neil Booth committed
1512
check_trad_stringification (pfile, macro, string)
1513
     cpp_reader *pfile;
Neil Booth committed
1514
     const cpp_macro *macro;
1515 1516
     const cpp_string *string;
{
Neil Booth committed
1517
  unsigned int i, len;
1518
  const uchar *p, *q, *limit = string->text + string->len;
1519

1520 1521 1522 1523
  /* Loop over the string.  */
  for (p = string->text; p < limit; p = q)
    {
      /* Find the start of an identifier.  */
1524 1525
      while (p < limit && !is_idstart (*p))
	p++;
1526 1527 1528

      /* Find the end of the identifier.  */
      q = p;
1529 1530
      while (q < limit && is_idchar (*q))
	q++;
Neil Booth committed
1531 1532 1533

      len = q - p;

1534 1535
      /* Loop over the function macro arguments to see if the
	 identifier inside the string matches one of them.  */
Neil Booth committed
1536 1537 1538
      for (i = 0; i < macro->paramc; i++)
	{
	  const cpp_hashnode *node = macro->params[i];
1539

1540 1541
	  if (NODE_LEN (node) == len
	      && !memcmp (p, NODE_NAME (node), len))
1542
	    {
1543
	      cpp_error (pfile, DL_WARNING,
1544
	   "macro argument \"%s\" would be stringified in traditional C",
1545
			 NODE_NAME (node));
1546 1547 1548 1549 1550
	      break;
	    }
	}
    }
}
Neil Booth committed
1551

1552 1553 1554 1555
/* 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
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
   returned text is temporary, and automatically freed later.  */
const unsigned char *
cpp_macro_definition (pfile, node)
     cpp_reader *pfile;
     const cpp_hashnode *node;
{
  unsigned int i, len;
  const cpp_macro *macro = node->value.macro;
  unsigned char *buffer;

  if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
    {
1568 1569
      cpp_error (pfile, DL_ICE,
		 "invalid hash type %d in cpp_macro_definition", node->type);
Neil Booth committed
1570 1571 1572 1573
      return 0;
    }

  /* Calculate length.  */
1574
  len = NODE_LEN (node) + 2;			/* ' ' and NUL.  */
Neil Booth committed
1575 1576
  if (macro->fun_like)
    {
1577 1578
      len += 4;		/* "()" plus possible final ".." of named
			   varargs (we have + 1 below).  */
Neil Booth committed
1579
      for (i = 0; i < macro->paramc; i++)
1580
	len += NODE_LEN (macro->params[i]) + 1; /* "," */
Neil Booth committed
1581 1582
    }

Neil Booth committed
1583
  for (i = 0; i < macro->count; i++)
Neil Booth committed
1584
    {
1585
      cpp_token *token = &macro->exp.tokens[i];
Neil Booth committed
1586

Neil Booth committed
1587
      if (token->type == CPP_MACRO_ARG)
1588
	len += NODE_LEN (macro->params[token->val.arg_no - 1]);
Neil Booth committed
1589 1590 1591 1592 1593 1594
      else
	len += cpp_token_len (token); /* Includes room for ' '.  */
      if (token->flags & STRINGIFY_ARG)
	len++;			/* "#" */
      if (token->flags & PASTE_LEFT)
	len += 3;		/* " ##" */
Neil Booth committed
1595 1596 1597
    }

  if (len > pfile->macro_buffer_len)
1598
    {
1599
      pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len);
1600 1601
      pfile->macro_buffer_len = len;
    }
1602 1603

  /* Fill in the buffer.  Start with the macro name.  */
Neil Booth committed
1604
  buffer = pfile->macro_buffer;
1605 1606
  memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
  buffer += NODE_LEN (node);
Neil Booth committed
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617

  /* 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__)
	    {
1618 1619
	      memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
	      buffer += NODE_LEN (param);
Neil Booth committed
1620 1621 1622
	    }

	  if (i + 1 < macro->paramc)
1623 1624 1625
	    /* 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.  */
1626
	    *buffer++ = ',';
1627
	  else if (macro->variadic)
Neil Booth committed
1628 1629 1630 1631 1632
	    *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
	}
      *buffer++ = ')';
    }

1633 1634 1635 1636
  /* The Dwarf spec requires a space after the macro name, even if the
     definition is the empty string.  */
  *buffer++ = ' ';

Neil Booth committed
1637
  /* Expansion tokens.  */
Neil Booth committed
1638
  if (macro->count)
Neil Booth committed
1639 1640 1641
    {
      for (i = 0; i < macro->count; i++)
	{
1642
	  cpp_token *token = &macro->exp.tokens[i];
Neil Booth committed
1643 1644 1645 1646 1647 1648 1649 1650

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

	  if (token->type == CPP_MACRO_ARG)
	    {
1651 1652 1653
	      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
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
	      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;
}