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