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