1 /* Part of CPP library. (Macro and #define handling.) 2 Copyright (C) 1986-2024 Free Software Foundation, Inc. 3 Written by Per Bothner, 1994. 4 Based on CCCP program by Paul Rubin, June 1986 5 Adapted to ANSI C, Richard Stallman, Jan 1987 6 7 This program is free software; you can redistribute it and/or modify it 8 under the terms of the GNU General Public License as published by the 9 Free Software Foundation; either version 3, or (at your option) any 10 later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. 20 21 In other words, you are welcome to use, share and improve this program. 22 You are forbidden to forbid anyone else to use, share and improve 23 what you give them. Help stamp out software-hoarding! */ 24 25 #include "config.h" 26 #include "system.h" 27 #include "cpplib.h" 28 #include "internal.h" 29 30 typedef struct macro_arg macro_arg; 31 /* This structure represents the tokens of a macro argument. These 32 tokens can be macro themselves, in which case they can be either 33 expanded or unexpanded. When they are expanded, this data 34 structure keeps both the expanded and unexpanded forms. */ 35 struct macro_arg 36 { 37 const cpp_token **first; /* First token in unexpanded argument. */ 38 const cpp_token **expanded; /* Macro-expanded argument. */ 39 const cpp_token *stringified; /* Stringified argument. */ 40 unsigned int count; /* # of tokens in argument. */ 41 unsigned int expanded_count; /* # of tokens in expanded argument. */ 42 location_t *virt_locs; /* Where virtual locations for 43 unexpanded tokens are stored. */ 44 location_t *expanded_virt_locs; /* Where virtual locations for 45 expanded tokens are 46 stored. */ 47 }; 48 49 /* The kind of macro tokens which the instance of 50 macro_arg_token_iter is supposed to iterate over. */ 51 enum macro_arg_token_kind { 52 MACRO_ARG_TOKEN_NORMAL, 53 /* This is a macro argument token that got transformed into a string 54 literal, e.g. #foo. */ 55 MACRO_ARG_TOKEN_STRINGIFIED, 56 /* This is a token resulting from the expansion of a macro 57 argument that was itself a macro. */ 58 MACRO_ARG_TOKEN_EXPANDED 59 }; 60 61 /* An iterator over tokens coming from a function-like macro 62 argument. */ 63 typedef struct macro_arg_token_iter macro_arg_token_iter; 64 struct macro_arg_token_iter 65 { 66 /* Whether or not -ftrack-macro-expansion is used. */ 67 bool track_macro_exp_p; 68 /* The kind of token over which we are supposed to iterate. */ 69 enum macro_arg_token_kind kind; 70 /* A pointer to the current token pointed to by the iterator. */ 71 const cpp_token **token_ptr; 72 /* A pointer to the "full" location of the current token. If 73 -ftrack-macro-expansion is used this location tracks loci across 74 macro expansion. */ 75 const location_t *location_ptr; 76 #if CHECKING_P 77 /* The number of times the iterator went forward. This useful only 78 when checking is enabled. */ 79 size_t num_forwards; 80 #endif 81 }; 82 83 /* Saved data about an identifier being used as a macro argument 84 name. */ 85 struct macro_arg_saved_data { 86 /* The canonical (UTF-8) spelling of this identifier. */ 87 cpp_hashnode *canonical_node; 88 /* The previous value & type of this identifier. */ 89 union _cpp_hashnode_value value; 90 node_type type; 91 }; 92 93 static const char *vaopt_paste_error = 94 N_("'##' cannot appear at either end of __VA_OPT__"); 95 96 static void expand_arg (cpp_reader *, macro_arg *); 97 98 /* A class for tracking __VA_OPT__ state while iterating over a 99 sequence of tokens. This is used during both macro definition and 100 expansion. */ 101 class vaopt_state { 102 103 public: 104 105 enum update_type 106 { 107 ERROR, 108 DROP, 109 INCLUDE, 110 BEGIN, 111 END 112 }; 113 114 /* Initialize the state tracker. ANY_ARGS is true if variable 115 arguments were provided to the macro invocation. */ 116 vaopt_state (cpp_reader *pfile, bool is_variadic, macro_arg *arg) 117 : m_pfile (pfile), 118 m_arg (arg), 119 m_variadic (is_variadic), 120 m_last_was_paste (false), 121 m_stringify (false), 122 m_state (0), 123 m_paste_location (0), 124 m_location (0), 125 m_update (ERROR) 126 { 127 } 128 129 /* Given a token, update the state of this tracker and return a 130 boolean indicating whether the token should be be included in the 131 expansion. */ 132 update_type update (const cpp_token *token) 133 { 134 /* If the macro isn't variadic, just don't bother. */ 135 if (!m_variadic) 136 return INCLUDE; 137 138 if (token->type == CPP_NAME 139 && token->val.node.node == m_pfile->spec_nodes.n__VA_OPT__) 140 { 141 if (m_state > 0) 142 { 143 cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc, 144 "__VA_OPT__ may not appear in a __VA_OPT__"); 145 return ERROR; 146 } 147 ++m_state; 148 m_location = token->src_loc; 149 m_stringify = (token->flags & STRINGIFY_ARG) != 0; 150 return BEGIN; 151 } 152 else if (m_state == 1) 153 { 154 if (token->type != CPP_OPEN_PAREN) 155 { 156 cpp_error_at (m_pfile, CPP_DL_ERROR, m_location, 157 "__VA_OPT__ must be followed by an " 158 "open parenthesis"); 159 return ERROR; 160 } 161 ++m_state; 162 if (m_update == ERROR) 163 { 164 if (m_arg == NULL) 165 m_update = INCLUDE; 166 else 167 { 168 m_update = DROP; 169 if (!m_arg->expanded) 170 expand_arg (m_pfile, m_arg); 171 for (unsigned idx = 0; idx < m_arg->expanded_count; ++idx) 172 if (m_arg->expanded[idx]->type != CPP_PADDING) 173 { 174 m_update = INCLUDE; 175 break; 176 } 177 } 178 } 179 return DROP; 180 } 181 else if (m_state >= 2) 182 { 183 if (m_state == 2 && token->type == CPP_PASTE) 184 { 185 cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc, 186 vaopt_paste_error); 187 return ERROR; 188 } 189 /* Advance states before further considering this token, in 190 case we see a close paren immediately after the open 191 paren. */ 192 if (m_state == 2) 193 ++m_state; 194 195 bool was_paste = m_last_was_paste; 196 m_last_was_paste = false; 197 if (token->type == CPP_PASTE) 198 { 199 m_last_was_paste = true; 200 m_paste_location = token->src_loc; 201 } 202 else if (token->type == CPP_OPEN_PAREN) 203 ++m_state; 204 else if (token->type == CPP_CLOSE_PAREN) 205 { 206 --m_state; 207 if (m_state == 2) 208 { 209 /* Saw the final paren. */ 210 m_state = 0; 211 212 if (was_paste) 213 { 214 cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc, 215 vaopt_paste_error); 216 return ERROR; 217 } 218 219 return END; 220 } 221 } 222 return m_update; 223 } 224 225 /* Nothing to do with __VA_OPT__. */ 226 return INCLUDE; 227 } 228 229 /* Ensure that any __VA_OPT__ was completed. If ok, return true. 230 Otherwise, issue an error and return false. */ 231 bool completed () 232 { 233 if (m_variadic && m_state != 0) 234 cpp_error_at (m_pfile, CPP_DL_ERROR, m_location, 235 "unterminated __VA_OPT__"); 236 return m_state == 0; 237 } 238 239 /* Return true for # __VA_OPT__. */ 240 bool stringify () const 241 { 242 return m_stringify; 243 } 244 245 private: 246 247 /* The cpp_reader. */ 248 cpp_reader *m_pfile; 249 250 /* The __VA_ARGS__ argument. */ 251 macro_arg *m_arg; 252 253 /* True if the macro is variadic. */ 254 bool m_variadic; 255 /* If true, the previous token was ##. This is used to detect when 256 a paste occurs at the end of the sequence. */ 257 bool m_last_was_paste; 258 /* True for #__VA_OPT__. */ 259 bool m_stringify; 260 261 /* The state variable: 262 0 means not parsing 263 1 means __VA_OPT__ seen, looking for "(" 264 2 means "(" seen (so the next token can't be "##") 265 >= 3 means looking for ")", the number encodes the paren depth. */ 266 int m_state; 267 268 /* The location of the paste token. */ 269 location_t m_paste_location; 270 271 /* Location of the __VA_OPT__ token. */ 272 location_t m_location; 273 274 /* If __VA_ARGS__ substitutes to no preprocessing tokens, 275 INCLUDE, otherwise DROP. ERROR when unknown yet. */ 276 update_type m_update; 277 }; 278 279 /* Macro expansion. */ 280 281 static cpp_macro *get_deferred_or_lazy_macro (cpp_reader *, cpp_hashnode *, 282 location_t); 283 static int enter_macro_context (cpp_reader *, cpp_hashnode *, 284 const cpp_token *, location_t); 285 static int builtin_macro (cpp_reader *, cpp_hashnode *, 286 location_t, location_t); 287 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *, 288 const cpp_token **, unsigned int); 289 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *, 290 _cpp_buff *, location_t *, 291 const cpp_token **, unsigned int); 292 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *, 293 _cpp_buff **, unsigned *); 294 static cpp_context *next_context (cpp_reader *); 295 static const cpp_token *padding_token (cpp_reader *, const cpp_token *); 296 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int); 297 static const cpp_token *stringify_arg (cpp_reader *, const cpp_token **, 298 unsigned int); 299 static void paste_all_tokens (cpp_reader *, const cpp_token *); 300 static bool paste_tokens (cpp_reader *, location_t, 301 const cpp_token **, const cpp_token *); 302 static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t); 303 static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *); 304 static void delete_macro_args (_cpp_buff*, unsigned num_args); 305 static void set_arg_token (macro_arg *, const cpp_token *, 306 location_t, size_t, 307 enum macro_arg_token_kind, 308 bool); 309 static const location_t *get_arg_token_location (const macro_arg *, 310 enum macro_arg_token_kind); 311 static const cpp_token **arg_token_ptr_at (const macro_arg *, 312 size_t, 313 enum macro_arg_token_kind, 314 location_t **virt_location); 315 316 static void macro_arg_token_iter_init (macro_arg_token_iter *, bool, 317 enum macro_arg_token_kind, 318 const macro_arg *, 319 const cpp_token **); 320 static const cpp_token *macro_arg_token_iter_get_token 321 (const macro_arg_token_iter *it); 322 static location_t macro_arg_token_iter_get_location 323 (const macro_arg_token_iter *); 324 static void macro_arg_token_iter_forward (macro_arg_token_iter *); 325 static _cpp_buff *tokens_buff_new (cpp_reader *, size_t, 326 location_t **); 327 static size_t tokens_buff_count (_cpp_buff *); 328 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *); 329 static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **, 330 location_t *, 331 const cpp_token *, 332 location_t, 333 location_t, 334 const line_map_macro *, 335 unsigned int); 336 337 static const cpp_token **tokens_buff_add_token (_cpp_buff *, 338 location_t *, 339 const cpp_token *, 340 location_t, 341 location_t, 342 const line_map_macro *, 343 unsigned int); 344 static inline void tokens_buff_remove_last_token (_cpp_buff *); 345 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *, 346 macro_arg *, location_t); 347 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *, 348 _cpp_buff **, unsigned *); 349 static cpp_macro *create_iso_definition (cpp_reader *); 350 351 /* #define directive parsing and handling. */ 352 353 static cpp_macro *lex_expansion_token (cpp_reader *, cpp_macro *); 354 static bool parse_params (cpp_reader *, unsigned *, bool *); 355 static void check_trad_stringification (cpp_reader *, const cpp_macro *, 356 const cpp_string *); 357 static bool reached_end_of_context (cpp_context *); 358 static void consume_next_token_from_context (cpp_reader *pfile, 359 const cpp_token **, 360 location_t *); 361 static const cpp_token* cpp_get_token_1 (cpp_reader *, location_t *); 362 363 static cpp_hashnode* macro_of_context (cpp_context *context); 364 365 /* Statistical counter tracking the number of macros that got 366 expanded. */ 367 unsigned num_expanded_macros_counter = 0; 368 /* Statistical counter tracking the total number tokens resulting 369 from macro expansion. */ 370 unsigned num_macro_tokens_counter = 0; 371 372 /* Wrapper around cpp_get_token to skip CPP_PADDING tokens 373 and not consume CPP_EOF. */ 374 static const cpp_token * 375 cpp_get_token_no_padding (cpp_reader *pfile) 376 { 377 for (;;) 378 { 379 const cpp_token *ret = cpp_peek_token (pfile, 0); 380 if (ret->type == CPP_EOF) 381 return ret; 382 ret = cpp_get_token (pfile); 383 if (ret->type != CPP_PADDING) 384 return ret; 385 } 386 } 387 388 /* Handle meeting "__has_include" builtin macro. */ 389 390 static int 391 builtin_has_include (cpp_reader *pfile, cpp_hashnode *op, bool has_next) 392 { 393 int result = 0; 394 395 if (!pfile->state.in_directive) 396 cpp_error (pfile, CPP_DL_ERROR, 397 "\"%s\" used outside of preprocessing directive", 398 NODE_NAME (op)); 399 400 pfile->state.angled_headers = true; 401 const auto sav_padding = pfile->state.directive_wants_padding; 402 pfile->state.directive_wants_padding = true; 403 const cpp_token *token = cpp_get_token_no_padding (pfile); 404 bool paren = token->type == CPP_OPEN_PAREN; 405 if (paren) 406 token = cpp_get_token_no_padding (pfile); 407 else 408 cpp_error (pfile, CPP_DL_ERROR, 409 "missing '(' before \"%s\" operand", NODE_NAME (op)); 410 pfile->state.angled_headers = false; 411 pfile->state.directive_wants_padding = sav_padding; 412 413 bool bracket = token->type != CPP_STRING; 414 char *fname = NULL; 415 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME) 416 { 417 fname = XNEWVEC (char, token->val.str.len - 1); 418 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2); 419 fname[token->val.str.len - 2] = '\0'; 420 } 421 else if (token->type == CPP_LESS) 422 fname = _cpp_bracket_include (pfile); 423 else 424 cpp_error (pfile, CPP_DL_ERROR, 425 "operator \"%s\" requires a header-name", NODE_NAME (op)); 426 427 if (fname) 428 { 429 /* Do not do the lookup if we're skipping, that's unnecessary 430 IO. */ 431 if (!pfile->state.skip_eval 432 && _cpp_has_header (pfile, fname, bracket, 433 has_next ? IT_INCLUDE_NEXT : IT_INCLUDE)) 434 result = 1; 435 436 XDELETEVEC (fname); 437 } 438 439 if (paren 440 && cpp_get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN) 441 cpp_error (pfile, CPP_DL_ERROR, 442 "missing ')' after \"%s\" operand", NODE_NAME (op)); 443 444 return result; 445 } 446 447 /* Emits a warning if NODE is a macro defined in the main file that 448 has not been used. */ 449 int 450 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node, 451 void *v ATTRIBUTE_UNUSED) 452 { 453 if (cpp_user_macro_p (node)) 454 { 455 cpp_macro *macro = node->value.macro; 456 457 if (!macro->used 458 && MAIN_FILE_P (linemap_check_ordinary 459 (linemap_lookup (pfile->line_table, 460 macro->line)))) 461 cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0, 462 "macro \"%s\" is not used", NODE_NAME (node)); 463 } 464 465 return 1; 466 } 467 468 /* Allocates and returns a CPP_STRING token, containing TEXT of length 469 LEN, after null-terminating it. TEXT must be in permanent storage. */ 470 static const cpp_token * 471 new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len) 472 { 473 cpp_token *token = _cpp_temp_token (pfile); 474 475 text[len] = '\0'; 476 token->type = CPP_STRING; 477 token->val.str.len = len; 478 token->val.str.text = text; 479 token->flags = 0; 480 return token; 481 } 482 483 static const char * const monthnames[] = 484 { 485 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 486 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 487 }; 488 489 static size_t remap_pairs; 490 static char **remap_src; 491 static char **remap_dst; 492 493 void 494 add_cpp_remap_path (const char *arg) 495 { 496 const char *arg_dst; 497 size_t len; 498 499 arg_dst = strchr(arg, ':'); 500 if (arg_dst == NULL) { 501 fprintf(stderr, "Invalid argument for -iremap"); 502 exit(1); 503 } 504 len = arg_dst - arg; 505 ++arg_dst; 506 507 remap_src = (char **) xrealloc(remap_src, sizeof(char *) * (remap_pairs + 1)); 508 remap_dst = (char **) xrealloc(remap_dst, sizeof(char *) * (remap_pairs + 1)); 509 510 remap_src[remap_pairs] = (char *) xmalloc(len + 1); 511 memcpy(remap_src[remap_pairs], arg, len); 512 remap_src[remap_pairs][len] = '\0'; 513 remap_dst[remap_pairs] = xstrdup(arg_dst); 514 ++remap_pairs; 515 } 516 517 static const char * 518 cpp_remap_file (const char *arg, char **tmp_name) 519 { 520 char *result; 521 size_t i, len; 522 523 for (i = 0; i < remap_pairs; ++i) { 524 len = strlen (remap_src[i]); 525 if (strncmp (remap_src[i], arg, len)) 526 continue; 527 if (arg[len] == '\0') 528 return remap_dst[i]; 529 if (arg[len] != '/') 530 continue; 531 arg += len; 532 len = strlen (remap_dst[i]); 533 result = (char *) xmalloc (len + strlen (arg) + 1); 534 memcpy(result, remap_dst[i], len); 535 strcpy(result + len, arg); 536 *tmp_name = result; 537 538 return result; 539 } 540 541 return arg; 542 } 543 544 /* Helper function for builtin_macro. Returns the text generated by 545 a builtin macro. */ 546 const uchar * 547 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node, 548 location_t loc) 549 { 550 const uchar *result = NULL; 551 linenum_type number = 1; 552 553 switch (node->value.builtin) 554 { 555 default: 556 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", 557 NODE_NAME (node)); 558 break; 559 560 case BT_TIMESTAMP: 561 { 562 if (CPP_OPTION (pfile, warn_date_time)) 563 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent " 564 "reproducible builds", NODE_NAME (node)); 565 566 cpp_buffer *pbuffer = cpp_get_buffer (pfile); 567 if (pbuffer->timestamp == NULL) 568 { 569 /* Initialize timestamp value of the assotiated file. */ 570 struct _cpp_file *file = cpp_get_file (pbuffer); 571 if (file) 572 { 573 /* Generate __TIMESTAMP__ string, that represents 574 the date and time of the last modification 575 of the current source file. The string constant 576 looks like "Sun Sep 16 01:03:52 1973". */ 577 struct tm *tb = NULL; 578 struct stat *st = _cpp_get_file_stat (file); 579 if (st) 580 tb = localtime (&st->st_mtime); 581 if (tb) 582 { 583 char *str = asctime (tb); 584 size_t len = strlen (str); 585 unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2); 586 buf[0] = '"'; 587 strcpy ((char *) buf + 1, str); 588 buf[len] = '"'; 589 pbuffer->timestamp = buf; 590 } 591 else 592 { 593 cpp_errno (pfile, CPP_DL_WARNING, 594 "could not determine file timestamp"); 595 pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\""; 596 } 597 } 598 } 599 result = pbuffer->timestamp; 600 } 601 break; 602 case BT_FILE: 603 case BT_FILE_NAME: 604 case BT_BASE_FILE: 605 { 606 unsigned int len; 607 const char *name; 608 char *tmp_name; 609 uchar *buf; 610 611 if (node->value.builtin == BT_FILE 612 || node->value.builtin == BT_FILE_NAME) 613 { 614 name = linemap_get_expansion_filename (pfile->line_table, 615 pfile->line_table->highest_line); 616 if ((node->value.builtin == BT_FILE_NAME) && name) 617 name = lbasename (name); 618 } 619 else 620 { 621 name = _cpp_get_file_name (pfile->main_file); 622 if (!name) 623 abort (); 624 } 625 if (pfile->cb.remap_filename && !pfile->state.in_directive) 626 name = pfile->cb.remap_filename (name); 627 tmp_name = NULL; 628 name = cpp_remap_file (name, &tmp_name); 629 len = strlen (name); 630 buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); 631 result = buf; 632 *buf = '"'; 633 buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len); 634 free (tmp_name); 635 *buf++ = '"'; 636 *buf = '\0'; 637 } 638 break; 639 640 case BT_INCLUDE_LEVEL: 641 /* The line map depth counts the primary source as level 1, but 642 historically __INCLUDE_DEPTH__ has called the primary source 643 level 0. */ 644 number = pfile->line_table->depth - 1; 645 break; 646 647 case BT_SPECLINE: 648 /* If __LINE__ is embedded in a macro, it must expand to the 649 line of the macro's invocation, not its definition. 650 Otherwise things like assert() will not work properly. 651 See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861. */ 652 if (CPP_OPTION (pfile, traditional)) 653 loc = pfile->line_table->highest_line; 654 else 655 loc = linemap_resolve_location (pfile->line_table, loc, 656 LRK_MACRO_EXPANSION_POINT, NULL); 657 number = linemap_get_expansion_line (pfile->line_table, loc); 658 break; 659 660 /* __STDC__ has the value 1 under normal circumstances. 661 However, if (a) we are in a system header, (b) the option 662 stdc_0_in_system_headers is true (set by target config), and 663 (c) we are not in strictly conforming mode, then it has the 664 value 0. (b) and (c) are already checked in cpp_init_builtins. */ 665 case BT_STDC: 666 if (_cpp_in_system_header (pfile)) 667 number = 0; 668 else 669 number = 1; 670 break; 671 672 case BT_DATE: 673 case BT_TIME: 674 if (CPP_OPTION (pfile, warn_date_time)) 675 cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent " 676 "reproducible builds", NODE_NAME (node)); 677 if (pfile->date == NULL) 678 { 679 /* Allocate __DATE__ and __TIME__ strings from permanent 680 storage. We only do this once, and don't generate them 681 at init time, because time() and localtime() are very 682 slow on some systems. */ 683 time_t tt; 684 auto kind = cpp_get_date (pfile, &tt); 685 686 if (kind == CPP_time_kind::UNKNOWN) 687 { 688 cpp_errno (pfile, CPP_DL_WARNING, 689 "could not determine date and time"); 690 691 pfile->date = UC"\"??? ?? ????\""; 692 pfile->time = UC"\"??:??:??\""; 693 } 694 else 695 { 696 struct tm *tb = (kind == CPP_time_kind::FIXED 697 ? gmtime : localtime) (&tt); 698 699 pfile->date = _cpp_unaligned_alloc (pfile, 700 sizeof ("\"Oct 11 1347\"")); 701 sprintf ((char *) pfile->date, "\"%s %2d %4d\"", 702 monthnames[tb->tm_mon], tb->tm_mday, 703 tb->tm_year + 1900); 704 705 pfile->time = _cpp_unaligned_alloc (pfile, 706 sizeof ("\"12:34:56\"")); 707 sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"", 708 tb->tm_hour, tb->tm_min, tb->tm_sec); 709 } 710 } 711 712 if (node->value.builtin == BT_DATE) 713 result = pfile->date; 714 else 715 result = pfile->time; 716 break; 717 718 case BT_COUNTER: 719 if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) 720 cpp_error (pfile, CPP_DL_ERROR, 721 "__COUNTER__ expanded inside directive with -fdirectives-only"); 722 number = pfile->counter++; 723 break; 724 725 case BT_HAS_ATTRIBUTE: 726 number = pfile->cb.has_attribute (pfile, false); 727 break; 728 729 case BT_HAS_STD_ATTRIBUTE: 730 number = pfile->cb.has_attribute (pfile, true); 731 break; 732 733 case BT_HAS_BUILTIN: 734 number = pfile->cb.has_builtin (pfile); 735 break; 736 737 case BT_HAS_INCLUDE: 738 case BT_HAS_INCLUDE_NEXT: 739 number = builtin_has_include (pfile, node, 740 node->value.builtin == BT_HAS_INCLUDE_NEXT); 741 break; 742 743 case BT_HAS_FEATURE: 744 case BT_HAS_EXTENSION: 745 number = pfile->cb.has_feature (pfile, 746 node->value.builtin == BT_HAS_FEATURE); 747 break; 748 } 749 750 if (result == NULL) 751 { 752 /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */ 753 result = _cpp_unaligned_alloc (pfile, 21); 754 sprintf ((char *) result, "%u", number); 755 } 756 757 return result; 758 } 759 760 /* Get an idempotent date. Either the cached value, the value from 761 source epoch, or failing that, the value from time(2). Use this 762 during compilation so that every time stamp is the same. */ 763 CPP_time_kind 764 cpp_get_date (cpp_reader *pfile, time_t *result) 765 { 766 if (!pfile->time_stamp_kind) 767 { 768 int kind = 0; 769 if (pfile->cb.get_source_date_epoch) 770 { 771 /* Try reading the fixed epoch. */ 772 pfile->time_stamp = pfile->cb.get_source_date_epoch (pfile); 773 if (pfile->time_stamp != time_t (-1)) 774 kind = int (CPP_time_kind::FIXED); 775 } 776 777 if (!kind) 778 { 779 /* Pedantically time_t (-1) is a legitimate value for 780 "number of seconds since the Epoch". It is a silly 781 time. */ 782 errno = 0; 783 pfile->time_stamp = time (nullptr); 784 /* Annoyingly a library could legally set errno and return a 785 valid time! Bad library! */ 786 if (pfile->time_stamp == time_t (-1) && errno) 787 kind = errno; 788 else 789 kind = int (CPP_time_kind::DYNAMIC); 790 } 791 792 pfile->time_stamp_kind = kind; 793 } 794 795 *result = pfile->time_stamp; 796 if (pfile->time_stamp_kind >= 0) 797 { 798 errno = pfile->time_stamp_kind; 799 return CPP_time_kind::UNKNOWN; 800 } 801 802 return CPP_time_kind (pfile->time_stamp_kind); 803 } 804 805 /* Convert builtin macros like __FILE__ to a token and push it on the 806 context stack. Also handles _Pragma, for which a new token may not 807 be created. Returns 1 if it generates a new token context, 0 to 808 return the token to the caller. LOC is the location of the expansion 809 point of the macro. */ 810 static int 811 builtin_macro (cpp_reader *pfile, cpp_hashnode *node, 812 location_t loc, location_t expand_loc) 813 { 814 const uchar *buf; 815 size_t len; 816 char *nbuf; 817 818 if (node->value.builtin == BT_PRAGMA) 819 { 820 /* Don't interpret _Pragma within directives. The standard is 821 not clear on this, but to me this makes most sense. 822 Similarly, don't interpret _Pragma inside expand_args, we might 823 need to stringize it later on. */ 824 if (pfile->state.in_directive || pfile->state.ignore__Pragma) 825 return 0; 826 827 return _cpp_do__Pragma (pfile, loc); 828 } 829 830 buf = _cpp_builtin_macro_text (pfile, node, expand_loc); 831 len = ustrlen (buf); 832 nbuf = (char *) alloca (len + 1); 833 memcpy (nbuf, buf, len); 834 nbuf[len]='\n'; 835 836 cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true); 837 _cpp_clean_line (pfile); 838 839 /* Set pfile->cur_token as required by _cpp_lex_direct. */ 840 pfile->cur_token = _cpp_temp_token (pfile); 841 cpp_token *token = _cpp_lex_direct (pfile); 842 /* We should point to the expansion point of the builtin macro. */ 843 token->src_loc = loc; 844 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) 845 { 846 /* We are tracking tokens resulting from macro expansion. 847 Create a macro line map and generate a virtual location for 848 the token resulting from the expansion of the built-in 849 macro. */ 850 location_t *virt_locs = NULL; 851 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs); 852 const line_map_macro * map = 853 linemap_enter_macro (pfile->line_table, node, loc, 1); 854 tokens_buff_add_token (token_buf, virt_locs, token, 855 pfile->line_table->builtin_location, 856 pfile->line_table->builtin_location, 857 map, /*macro_token_index=*/0); 858 push_extended_tokens_context (pfile, node, token_buf, virt_locs, 859 (const cpp_token **)token_buf->base, 860 1); 861 } 862 else 863 _cpp_push_token_context (pfile, NULL, token, 1); 864 if (pfile->buffer->cur != pfile->buffer->rlimit) 865 cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", 866 NODE_NAME (node)); 867 _cpp_pop_buffer (pfile); 868 869 return 1; 870 } 871 872 /* Copies SRC, of length LEN, to DEST, adding backslashes before all 873 backslashes and double quotes. DEST must be of sufficient size. 874 Returns a pointer to the end of the string. */ 875 uchar * 876 cpp_quote_string (uchar *dest, const uchar *src, unsigned int len) 877 { 878 while (len--) 879 { 880 uchar c = *src++; 881 882 switch (c) 883 { 884 case '\n': 885 /* Naked LF can appear in raw string literals */ 886 c = 'n'; 887 /* FALLTHROUGH */ 888 889 case '\\': 890 case '"': 891 *dest++ = '\\'; 892 /* FALLTHROUGH */ 893 894 default: 895 *dest++ = c; 896 } 897 } 898 899 return dest; 900 } 901 902 /* Convert a token sequence FIRST to FIRST+COUNT-1 to a single string token 903 according to the rules of the ISO C #-operator. */ 904 static const cpp_token * 905 stringify_arg (cpp_reader *pfile, const cpp_token **first, unsigned int count) 906 { 907 unsigned char *dest; 908 unsigned int i, escape_it, backslash_count = 0; 909 const cpp_token *source = NULL; 910 size_t len; 911 912 if (BUFF_ROOM (pfile->u_buff) < 3) 913 _cpp_extend_buff (pfile, &pfile->u_buff, 3); 914 dest = BUFF_FRONT (pfile->u_buff); 915 *dest++ = '"'; 916 917 /* Loop, reading in the argument's tokens. */ 918 for (i = 0; i < count; i++) 919 { 920 const cpp_token *token = first[i]; 921 922 if (token->type == CPP_PADDING) 923 { 924 if (source == NULL 925 || (!(source->flags & PREV_WHITE) 926 && token->val.source == NULL)) 927 source = token->val.source; 928 continue; 929 } 930 931 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR 932 || token->type == CPP_WSTRING || token->type == CPP_WCHAR 933 || token->type == CPP_STRING32 || token->type == CPP_CHAR32 934 || token->type == CPP_STRING16 || token->type == CPP_CHAR16 935 || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR 936 || cpp_userdef_string_p (token->type) 937 || cpp_userdef_char_p (token->type)); 938 939 /* Room for each char being written in octal, initial space and 940 final quote and NUL. */ 941 len = cpp_token_len (token); 942 if (escape_it) 943 len *= 4; 944 len += 3; 945 946 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len) 947 { 948 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff); 949 _cpp_extend_buff (pfile, &pfile->u_buff, len); 950 dest = BUFF_FRONT (pfile->u_buff) + len_so_far; 951 } 952 953 /* Leading white space? */ 954 if (dest - 1 != BUFF_FRONT (pfile->u_buff)) 955 { 956 if (source == NULL) 957 source = token; 958 if (source->flags & PREV_WHITE) 959 *dest++ = ' '; 960 } 961 source = NULL; 962 963 if (escape_it) 964 { 965 _cpp_buff *buff = _cpp_get_buff (pfile, len); 966 unsigned char *buf = BUFF_FRONT (buff); 967 len = cpp_spell_token (pfile, token, buf, true) - buf; 968 dest = cpp_quote_string (dest, buf, len); 969 _cpp_release_buff (pfile, buff); 970 } 971 else 972 dest = cpp_spell_token (pfile, token, dest, true); 973 974 if (token->type == CPP_OTHER && token->val.str.text[0] == '\\') 975 backslash_count++; 976 else 977 backslash_count = 0; 978 } 979 980 /* Ignore the final \ of invalid string literals. */ 981 if (backslash_count & 1) 982 { 983 cpp_error (pfile, CPP_DL_WARNING, 984 "invalid string literal, ignoring final '\\'"); 985 dest--; 986 } 987 988 /* Commit the memory, including NUL, and return the token. */ 989 *dest++ = '"'; 990 len = dest - BUFF_FRONT (pfile->u_buff); 991 BUFF_FRONT (pfile->u_buff) = dest + 1; 992 return new_string_token (pfile, dest - len, len); 993 } 994 995 /* Try to paste two tokens. On success, return nonzero. In any 996 case, PLHS is updated to point to the pasted token, which is 997 guaranteed to not have the PASTE_LEFT flag set. LOCATION is 998 the virtual location used for error reporting. */ 999 static bool 1000 paste_tokens (cpp_reader *pfile, location_t location, 1001 const cpp_token **plhs, const cpp_token *rhs) 1002 { 1003 unsigned char *buf, *end, *lhsend; 1004 cpp_token *lhs; 1005 unsigned int len; 1006 1007 len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 2; 1008 buf = (unsigned char *) alloca (len); 1009 end = lhsend = cpp_spell_token (pfile, *plhs, buf, true); 1010 1011 /* Avoid comment headers, since they are still processed in stage 3. 1012 It is simpler to insert a space here, rather than modifying the 1013 lexer to ignore comments in some circumstances. Simply returning 1014 false doesn't work, since we want to clear the PASTE_LEFT flag. */ 1015 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) 1016 *end++ = ' '; 1017 /* In one obscure case we might see padding here. */ 1018 if (rhs->type != CPP_PADDING) 1019 end = cpp_spell_token (pfile, rhs, end, true); 1020 *end = '\n'; 1021 1022 cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true); 1023 _cpp_clean_line (pfile); 1024 1025 /* Set pfile->cur_token as required by _cpp_lex_direct. */ 1026 pfile->cur_token = _cpp_temp_token (pfile); 1027 lhs = _cpp_lex_direct (pfile); 1028 if (pfile->buffer->cur != pfile->buffer->rlimit) 1029 { 1030 location_t saved_loc = lhs->src_loc; 1031 1032 _cpp_pop_buffer (pfile); 1033 1034 unsigned char *rhsstart = lhsend; 1035 if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) 1036 rhsstart++; 1037 1038 /* We have to remove the PASTE_LEFT flag from the old lhs, but 1039 we want to keep the new location. */ 1040 *lhs = **plhs; 1041 *plhs = lhs; 1042 lhs->src_loc = saved_loc; 1043 lhs->flags &= ~PASTE_LEFT; 1044 1045 /* Mandatory error for all apart from assembler. */ 1046 if (CPP_OPTION (pfile, lang) != CLK_ASM) 1047 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, 1048 "pasting \"%.*s\" and \"%.*s\" does not give " 1049 "a valid preprocessing token", 1050 (int) (lhsend - buf), buf, 1051 (int) (end - rhsstart), rhsstart); 1052 return false; 1053 } 1054 1055 lhs->flags |= (*plhs)->flags & (PREV_WHITE | PREV_FALLTHROUGH); 1056 *plhs = lhs; 1057 _cpp_pop_buffer (pfile); 1058 return true; 1059 } 1060 1061 /* Handles an arbitrarily long sequence of ## operators, with initial 1062 operand LHS. This implementation is left-associative, 1063 non-recursive, and finishes a paste before handling succeeding 1064 ones. If a paste fails, we back up to the RHS of the failing ## 1065 operator before pushing the context containing the result of prior 1066 successful pastes, with the effect that the RHS appears in the 1067 output stream after the pasted LHS normally. */ 1068 static void 1069 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs) 1070 { 1071 const cpp_token *rhs = NULL; 1072 cpp_context *context = pfile->context; 1073 location_t virt_loc = 0; 1074 1075 /* We are expanding a macro and we must have been called on a token 1076 that appears at the left hand side of a ## operator. */ 1077 if (macro_of_context (pfile->context) == NULL 1078 || (!(lhs->flags & PASTE_LEFT))) 1079 abort (); 1080 1081 if (context->tokens_kind == TOKENS_KIND_EXTENDED) 1082 /* The caller must have called consume_next_token_from_context 1083 right before calling us. That has incremented the pointer to 1084 the current virtual location. So it now points to the location 1085 of the token that comes right after *LHS. We want the 1086 resulting pasted token to have the location of the current 1087 *LHS, though. */ 1088 virt_loc = context->c.mc->cur_virt_loc[-1]; 1089 else 1090 /* We are not tracking macro expansion. So the best virtual 1091 location we can get here is the expansion point of the macro we 1092 are currently expanding. */ 1093 virt_loc = pfile->invocation_location; 1094 1095 do 1096 { 1097 /* Take the token directly from the current context. We can do 1098 this, because we are in the replacement list of either an 1099 object-like macro, or a function-like macro with arguments 1100 inserted. In either case, the constraints to #define 1101 guarantee we have at least one more token. */ 1102 if (context->tokens_kind == TOKENS_KIND_DIRECT) 1103 rhs = FIRST (context).token++; 1104 else if (context->tokens_kind == TOKENS_KIND_INDIRECT) 1105 rhs = *FIRST (context).ptoken++; 1106 else if (context->tokens_kind == TOKENS_KIND_EXTENDED) 1107 { 1108 /* So we are in presence of an extended token context, which 1109 means that each token in this context has a virtual 1110 location attached to it. So let's not forget to update 1111 the pointer to the current virtual location of the 1112 current token when we update the pointer to the current 1113 token */ 1114 1115 rhs = *FIRST (context).ptoken++; 1116 /* context->c.mc must be non-null, as if we were not in a 1117 macro context, context->tokens_kind could not be equal to 1118 TOKENS_KIND_EXTENDED. */ 1119 context->c.mc->cur_virt_loc++; 1120 } 1121 1122 if (rhs->type == CPP_PADDING) 1123 { 1124 if (rhs->flags & PASTE_LEFT) 1125 abort (); 1126 } 1127 if (!paste_tokens (pfile, virt_loc, &lhs, rhs)) 1128 { 1129 _cpp_backup_tokens (pfile, 1); 1130 break; 1131 } 1132 } 1133 while (rhs->flags & PASTE_LEFT); 1134 1135 /* Put the resulting token in its own context. */ 1136 if (context->tokens_kind == TOKENS_KIND_EXTENDED) 1137 { 1138 location_t *virt_locs = NULL; 1139 _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs); 1140 tokens_buff_add_token (token_buf, virt_locs, lhs, 1141 virt_loc, 0, NULL, 0); 1142 push_extended_tokens_context (pfile, context->c.mc->macro_node, 1143 token_buf, virt_locs, 1144 (const cpp_token **)token_buf->base, 1); 1145 } 1146 else 1147 _cpp_push_token_context (pfile, NULL, lhs, 1); 1148 } 1149 1150 /* Returns TRUE if the number of arguments ARGC supplied in an 1151 invocation of the MACRO referenced by NODE is valid. An empty 1152 invocation to a macro with no parameters should pass ARGC as zero. 1153 1154 Note that MACRO cannot necessarily be deduced from NODE, in case 1155 NODE was redefined whilst collecting arguments. */ 1156 bool 1157 _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc) 1158 { 1159 if (argc == macro->paramc) 1160 return true; 1161 1162 if (argc < macro->paramc) 1163 { 1164 /* In C++20 and C23 (here the va_opt flag is used), and also as a GNU 1165 extension, variadic arguments are allowed to not appear in 1166 the invocation at all. 1167 e.g. #define debug(format, args...) something 1168 debug("string"); 1169 1170 This is exactly the same as if an empty variadic list had been 1171 supplied - debug("string", ). */ 1172 1173 if (argc + 1 == macro->paramc && macro->variadic) 1174 { 1175 if (CPP_PEDANTIC (pfile) && ! macro->syshdr 1176 && ! CPP_OPTION (pfile, va_opt)) 1177 { 1178 if (CPP_OPTION (pfile, cplusplus)) 1179 cpp_error (pfile, CPP_DL_PEDWARN, 1180 "ISO C++11 requires at least one argument " 1181 "for the \"...\" in a variadic macro"); 1182 else 1183 cpp_error (pfile, CPP_DL_PEDWARN, 1184 "ISO C99 requires at least one argument " 1185 "for the \"...\" in a variadic macro"); 1186 } 1187 return true; 1188 } 1189 1190 cpp_error (pfile, CPP_DL_ERROR, 1191 "macro \"%s\" requires %u arguments, but only %u given", 1192 NODE_NAME (node), macro->paramc, argc); 1193 } 1194 else 1195 cpp_error (pfile, CPP_DL_ERROR, 1196 "macro \"%s\" passed %u arguments, but takes just %u", 1197 NODE_NAME (node), argc, macro->paramc); 1198 1199 if (macro->line > RESERVED_LOCATION_COUNT) 1200 cpp_error_at (pfile, CPP_DL_NOTE, macro->line, "macro \"%s\" defined here", 1201 NODE_NAME (node)); 1202 1203 return false; 1204 } 1205 1206 /* Reads and returns the arguments to a function-like macro 1207 invocation. Assumes the opening parenthesis has been processed. 1208 If there is an error, emits an appropriate diagnostic and returns 1209 NULL. Each argument is terminated by a CPP_EOF token, for the 1210 future benefit of expand_arg(). If there are any deferred 1211 #pragma directives among macro arguments, store pointers to the 1212 CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer. 1213 1214 What is returned is the buffer that contains the memory allocated 1215 to hold the macro arguments. NODE is the name of the macro this 1216 function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is 1217 set to the actual number of macro arguments allocated in the 1218 returned buffer. */ 1219 static _cpp_buff * 1220 collect_args (cpp_reader *pfile, const cpp_hashnode *node, 1221 _cpp_buff **pragma_buff, unsigned *num_args) 1222 { 1223 _cpp_buff *buff, *base_buff; 1224 cpp_macro *macro; 1225 macro_arg *args, *arg; 1226 const cpp_token *token; 1227 unsigned int argc; 1228 location_t virt_loc; 1229 bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion); 1230 unsigned num_args_alloced = 0; 1231 1232 macro = node->value.macro; 1233 if (macro->paramc) 1234 argc = macro->paramc; 1235 else 1236 argc = 1; 1237 1238 #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50 1239 #define ARG_TOKENS_EXTENT 1000 1240 1241 buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG 1242 * sizeof (cpp_token *) 1243 + sizeof (macro_arg))); 1244 base_buff = buff; 1245 args = (macro_arg *) buff->base; 1246 memset (args, 0, argc * sizeof (macro_arg)); 1247 buff->cur = (unsigned char *) &args[argc]; 1248 arg = args, argc = 0; 1249 1250 /* Collect the tokens making up each argument. We don't yet know 1251 how many arguments have been supplied, whether too many or too 1252 few. Hence the slightly bizarre usage of "argc" and "arg". */ 1253 do 1254 { 1255 unsigned int paren_depth = 0; 1256 unsigned int ntokens = 0; 1257 unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; 1258 num_args_alloced++; 1259 1260 argc++; 1261 arg->first = (const cpp_token **) buff->cur; 1262 if (track_macro_expansion_p) 1263 { 1264 virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; 1265 arg->virt_locs = XNEWVEC (location_t, 1266 virt_locs_capacity); 1267 } 1268 1269 for (;;) 1270 { 1271 /* Require space for 2 new tokens (including a CPP_EOF). */ 1272 if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit) 1273 { 1274 buff = _cpp_append_extend_buff (pfile, buff, 1275 ARG_TOKENS_EXTENT 1276 * sizeof (cpp_token *)); 1277 arg->first = (const cpp_token **) buff->cur; 1278 } 1279 if (track_macro_expansion_p 1280 && (ntokens + 2 > virt_locs_capacity)) 1281 { 1282 virt_locs_capacity += ARG_TOKENS_EXTENT; 1283 arg->virt_locs = XRESIZEVEC (location_t, 1284 arg->virt_locs, 1285 virt_locs_capacity); 1286 } 1287 1288 token = cpp_get_token_1 (pfile, &virt_loc); 1289 1290 if (token->type == CPP_PADDING) 1291 { 1292 /* Drop leading padding. */ 1293 if (ntokens == 0) 1294 continue; 1295 } 1296 else if (token->type == CPP_OPEN_PAREN) 1297 paren_depth++; 1298 else if (token->type == CPP_CLOSE_PAREN) 1299 { 1300 if (paren_depth-- == 0) 1301 break; 1302 } 1303 else if (token->type == CPP_COMMA) 1304 { 1305 /* A comma does not terminate an argument within 1306 parentheses or as part of a variable argument. */ 1307 if (paren_depth == 0 1308 && ! (macro->variadic && argc == macro->paramc)) 1309 break; 1310 } 1311 else if (token->type == CPP_EOF 1312 || (token->type == CPP_HASH && token->flags & BOL)) 1313 break; 1314 else if (token->type == CPP_PRAGMA && !(token->flags & PRAGMA_OP)) 1315 { 1316 cpp_token *newtok = _cpp_temp_token (pfile); 1317 1318 /* CPP_PRAGMA token lives in directive_result, which will 1319 be overwritten on the next directive. */ 1320 *newtok = *token; 1321 token = newtok; 1322 do 1323 { 1324 if (*pragma_buff == NULL 1325 || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *)) 1326 { 1327 _cpp_buff *next; 1328 if (*pragma_buff == NULL) 1329 *pragma_buff 1330 = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *)); 1331 else 1332 { 1333 next = *pragma_buff; 1334 *pragma_buff 1335 = _cpp_get_buff (pfile, 1336 (BUFF_FRONT (*pragma_buff) 1337 - (*pragma_buff)->base) * 2); 1338 (*pragma_buff)->next = next; 1339 } 1340 } 1341 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token; 1342 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *); 1343 if (token->type == CPP_PRAGMA_EOL) 1344 break; 1345 token = cpp_get_token_1 (pfile, &virt_loc); 1346 } 1347 while (token->type != CPP_EOF); 1348 1349 /* In deferred pragmas parsing_args and prevent_expansion 1350 had been changed, reset it. */ 1351 pfile->state.parsing_args = 2; 1352 pfile->state.prevent_expansion = 1; 1353 1354 if (token->type == CPP_EOF) 1355 break; 1356 else 1357 continue; 1358 } 1359 set_arg_token (arg, token, virt_loc, 1360 ntokens, MACRO_ARG_TOKEN_NORMAL, 1361 CPP_OPTION (pfile, track_macro_expansion)); 1362 ntokens++; 1363 } 1364 1365 /* Drop trailing padding. */ 1366 while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING) 1367 ntokens--; 1368 1369 arg->count = ntokens; 1370 /* Append an EOF to mark end-of-argument. */ 1371 set_arg_token (arg, &pfile->endarg, token->src_loc, 1372 ntokens, MACRO_ARG_TOKEN_NORMAL, 1373 CPP_OPTION (pfile, track_macro_expansion)); 1374 1375 /* Terminate the argument. Excess arguments loop back and 1376 overwrite the final legitimate argument, before failing. */ 1377 if (argc <= macro->paramc) 1378 { 1379 buff->cur = (unsigned char *) &arg->first[ntokens + 1]; 1380 if (argc != macro->paramc) 1381 arg++; 1382 } 1383 } 1384 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF); 1385 1386 if (token->type == CPP_EOF) 1387 { 1388 /* Unless the EOF is marking the end of an argument, it's a fake 1389 one from the end of a file that _cpp_clean_line will not have 1390 advanced past. */ 1391 if (token == &pfile->endarg) 1392 _cpp_backup_tokens (pfile, 1); 1393 cpp_error (pfile, CPP_DL_ERROR, 1394 "unterminated argument list invoking macro \"%s\"", 1395 NODE_NAME (node)); 1396 } 1397 else 1398 { 1399 /* A single empty argument is counted as no argument. */ 1400 if (argc == 1 && macro->paramc == 0 && args[0].count == 0) 1401 argc = 0; 1402 if (_cpp_arguments_ok (pfile, macro, node, argc)) 1403 { 1404 /* GCC has special semantics for , ## b where b is a varargs 1405 parameter: we remove the comma if b was omitted entirely. 1406 If b was merely an empty argument, the comma is retained. 1407 If the macro takes just one (varargs) parameter, then we 1408 retain the comma only if we are standards conforming. 1409 1410 If FIRST is NULL replace_args () swallows the comma. */ 1411 if (macro->variadic && (argc < macro->paramc 1412 || (argc == 1 && args[0].count == 0 1413 && !CPP_OPTION (pfile, std)))) 1414 args[macro->paramc - 1].first = NULL; 1415 if (num_args) 1416 *num_args = num_args_alloced; 1417 return base_buff; 1418 } 1419 } 1420 1421 /* An error occurred. */ 1422 _cpp_release_buff (pfile, base_buff); 1423 return NULL; 1424 } 1425 1426 /* Search for an opening parenthesis to the macro of NODE, in such a 1427 way that, if none is found, we don't lose the information in any 1428 intervening padding tokens. If we find the parenthesis, collect 1429 the arguments and return the buffer containing them. PRAGMA_BUFF 1430 argument is the same as in collect_args. If NUM_ARGS is non-NULL, 1431 *NUM_ARGS is set to the number of arguments contained in the 1432 returned buffer. */ 1433 static _cpp_buff * 1434 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node, 1435 _cpp_buff **pragma_buff, unsigned *num_args) 1436 { 1437 const cpp_token *token, *padding = NULL; 1438 1439 for (;;) 1440 { 1441 token = cpp_get_token (pfile); 1442 if (token->type != CPP_PADDING) 1443 break; 1444 gcc_assert ((token->flags & PREV_WHITE) == 0); 1445 if (padding == NULL 1446 || padding->val.source == NULL 1447 || (!(padding->val.source->flags & PREV_WHITE) 1448 && token->val.source == NULL)) 1449 padding = token; 1450 } 1451 1452 if (token->type == CPP_OPEN_PAREN) 1453 { 1454 pfile->state.parsing_args = 2; 1455 return collect_args (pfile, node, pragma_buff, num_args); 1456 } 1457 1458 /* Back up. A CPP_EOF is either an EOF from an argument we're 1459 expanding, or a fake one from lex_direct. We want to backup the 1460 former, but not the latter. We may have skipped padding, in 1461 which case backing up more than one token when expanding macros 1462 is in general too difficult. We re-insert it in its own 1463 context. */ 1464 if (token->type != CPP_EOF || token == &pfile->endarg) 1465 { 1466 _cpp_backup_tokens (pfile, 1); 1467 if (padding) 1468 _cpp_push_token_context (pfile, NULL, padding, 1); 1469 } 1470 1471 return NULL; 1472 } 1473 1474 /* Return the real number of tokens in the expansion of MACRO. */ 1475 static inline unsigned int 1476 macro_real_token_count (const cpp_macro *macro) 1477 { 1478 if (__builtin_expect (!macro->extra_tokens, true)) 1479 return macro->count; 1480 1481 for (unsigned i = macro->count; i--;) 1482 if (macro->exp.tokens[i].type != CPP_PASTE) 1483 return i + 1; 1484 1485 return 0; 1486 } 1487 1488 /* Push the context of a macro with hash entry NODE onto the context 1489 stack. If we can successfully expand the macro, we push a context 1490 containing its yet-to-be-rescanned replacement list and return one. 1491 If there were additionally any unexpanded deferred #pragma 1492 directives among macro arguments, push another context containing 1493 the pragma tokens before the yet-to-be-rescanned replacement list 1494 and return two. Otherwise, we don't push a context and return 1495 zero. LOCATION is the location of the expansion point of the 1496 macro. */ 1497 static int 1498 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node, 1499 const cpp_token *result, location_t location) 1500 { 1501 /* The presence of a macro invalidates a file's controlling macro. */ 1502 pfile->mi_valid = false; 1503 1504 pfile->state.angled_headers = false; 1505 1506 /* From here to when we push the context for the macro later down 1507 this function, we need to flag the fact that we are about to 1508 expand a macro. This is useful when -ftrack-macro-expansion is 1509 turned off. In that case, we need to record the location of the 1510 expansion point of the top-most macro we are about to to expand, 1511 into pfile->invocation_location. But we must not record any such 1512 location once the process of expanding the macro starts; that is, 1513 we must not do that recording between now and later down this 1514 function where set this flag to FALSE. */ 1515 pfile->about_to_expand_macro_p = true; 1516 1517 if (cpp_user_macro_p (node)) 1518 { 1519 cpp_macro *macro = node->value.macro; 1520 _cpp_buff *pragma_buff = NULL; 1521 1522 if (macro->fun_like) 1523 { 1524 _cpp_buff *buff; 1525 unsigned num_args = 0; 1526 1527 pfile->state.prevent_expansion++; 1528 pfile->keep_tokens++; 1529 pfile->state.parsing_args = 1; 1530 buff = funlike_invocation_p (pfile, node, &pragma_buff, 1531 &num_args); 1532 pfile->state.parsing_args = 0; 1533 pfile->keep_tokens--; 1534 pfile->state.prevent_expansion--; 1535 1536 if (buff == NULL) 1537 { 1538 if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr) 1539 cpp_warning (pfile, CPP_W_TRADITIONAL, 1540 "function-like macro \"%s\" must be used with arguments in traditional C", 1541 NODE_NAME (node)); 1542 1543 if (pragma_buff) 1544 _cpp_release_buff (pfile, pragma_buff); 1545 1546 pfile->about_to_expand_macro_p = false; 1547 return 0; 1548 } 1549 1550 if (macro->paramc > 0) 1551 replace_args (pfile, node, macro, 1552 (macro_arg *) buff->base, 1553 location); 1554 /* Free the memory used by the arguments of this 1555 function-like macro. This memory has been allocated by 1556 funlike_invocation_p and by replace_args. */ 1557 delete_macro_args (buff, num_args); 1558 } 1559 1560 /* Disable the macro within its expansion. */ 1561 node->flags |= NODE_DISABLED; 1562 1563 /* Laziness can only affect the expansion tokens of the macro, 1564 not its fun-likeness or parameters. */ 1565 _cpp_maybe_notify_macro_use (pfile, node, location); 1566 if (pfile->cb.used) 1567 pfile->cb.used (pfile, location, node); 1568 1569 macro->used = 1; 1570 1571 if (macro->paramc == 0) 1572 { 1573 unsigned tokens_count = macro_real_token_count (macro); 1574 if (CPP_OPTION (pfile, track_macro_expansion)) 1575 { 1576 unsigned int i; 1577 const cpp_token *src = macro->exp.tokens; 1578 const line_map_macro *map; 1579 location_t *virt_locs = NULL; 1580 _cpp_buff *macro_tokens 1581 = tokens_buff_new (pfile, tokens_count, &virt_locs); 1582 1583 /* Create a macro map to record the locations of the 1584 tokens that are involved in the expansion. LOCATION 1585 is the location of the macro expansion point. */ 1586 map = linemap_enter_macro (pfile->line_table, 1587 node, location, tokens_count); 1588 for (i = 0; i < tokens_count; ++i) 1589 { 1590 tokens_buff_add_token (macro_tokens, virt_locs, 1591 src, src->src_loc, 1592 src->src_loc, map, i); 1593 ++src; 1594 } 1595 push_extended_tokens_context (pfile, node, 1596 macro_tokens, 1597 virt_locs, 1598 (const cpp_token **) 1599 macro_tokens->base, 1600 tokens_count); 1601 } 1602 else 1603 _cpp_push_token_context (pfile, node, macro->exp.tokens, 1604 tokens_count); 1605 num_macro_tokens_counter += tokens_count; 1606 } 1607 1608 if (pragma_buff) 1609 { 1610 if (!pfile->state.in_directive) 1611 _cpp_push_token_context (pfile, NULL, 1612 padding_token (pfile, result), 1); 1613 do 1614 { 1615 unsigned tokens_count; 1616 _cpp_buff *tail = pragma_buff->next; 1617 pragma_buff->next = NULL; 1618 tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff) 1619 - (const cpp_token **) pragma_buff->base); 1620 push_ptoken_context (pfile, NULL, pragma_buff, 1621 (const cpp_token **) pragma_buff->base, 1622 tokens_count); 1623 pragma_buff = tail; 1624 if (!CPP_OPTION (pfile, track_macro_expansion)) 1625 num_macro_tokens_counter += tokens_count; 1626 1627 } 1628 while (pragma_buff != NULL); 1629 pfile->about_to_expand_macro_p = false; 1630 return 2; 1631 } 1632 1633 pfile->about_to_expand_macro_p = false; 1634 return 1; 1635 } 1636 1637 pfile->about_to_expand_macro_p = false; 1638 /* Handle built-in macros and the _Pragma operator. */ 1639 { 1640 location_t expand_loc; 1641 1642 if (/* The top-level macro invocation that triggered the expansion 1643 we are looking at is with a function-like user macro ... */ 1644 cpp_fun_like_macro_p (pfile->top_most_macro_node) 1645 /* ... and we are tracking the macro expansion. */ 1646 && CPP_OPTION (pfile, track_macro_expansion)) 1647 /* Then the location of the end of the macro invocation is the 1648 location of the expansion point of this macro. */ 1649 expand_loc = location; 1650 else 1651 /* Otherwise, the location of the end of the macro invocation is 1652 the location of the expansion point of that top-level macro 1653 invocation. */ 1654 expand_loc = pfile->invocation_location; 1655 1656 return builtin_macro (pfile, node, location, expand_loc); 1657 } 1658 } 1659 1660 /* De-allocate the memory used by BUFF which is an array of instances 1661 of macro_arg. NUM_ARGS is the number of instances of macro_arg 1662 present in BUFF. */ 1663 static void 1664 delete_macro_args (_cpp_buff *buff, unsigned num_args) 1665 { 1666 macro_arg *macro_args; 1667 unsigned i; 1668 1669 if (buff == NULL) 1670 return; 1671 1672 macro_args = (macro_arg *) buff->base; 1673 1674 /* Walk instances of macro_arg to free their expanded tokens as well 1675 as their macro_arg::virt_locs members. */ 1676 for (i = 0; i < num_args; ++i) 1677 { 1678 if (macro_args[i].expanded) 1679 { 1680 free (macro_args[i].expanded); 1681 macro_args[i].expanded = NULL; 1682 } 1683 if (macro_args[i].virt_locs) 1684 { 1685 free (macro_args[i].virt_locs); 1686 macro_args[i].virt_locs = NULL; 1687 } 1688 if (macro_args[i].expanded_virt_locs) 1689 { 1690 free (macro_args[i].expanded_virt_locs); 1691 macro_args[i].expanded_virt_locs = NULL; 1692 } 1693 } 1694 _cpp_free_buff (buff); 1695 } 1696 1697 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token 1698 to set, LOCATION is its virtual location. "Virtual" location means 1699 the location that encodes loci across macro expansion. Otherwise 1700 it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the 1701 argument ARG is supposed to contain. Note that ARG must be 1702 tailored so that it has enough room to contain INDEX + 1 numbers of 1703 tokens, at least. */ 1704 static void 1705 set_arg_token (macro_arg *arg, const cpp_token *token, 1706 location_t location, size_t index, 1707 enum macro_arg_token_kind kind, 1708 bool track_macro_exp_p) 1709 { 1710 const cpp_token **token_ptr; 1711 location_t *loc = NULL; 1712 1713 token_ptr = 1714 arg_token_ptr_at (arg, index, kind, 1715 track_macro_exp_p ? &loc : NULL); 1716 *token_ptr = token; 1717 1718 if (loc != NULL) 1719 { 1720 /* We can't set the location of a stringified argument 1721 token and we can't set any location if we aren't tracking 1722 macro expansion locations. */ 1723 gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED 1724 && track_macro_exp_p); 1725 *loc = location; 1726 } 1727 } 1728 1729 /* Get the pointer to the location of the argument token of the 1730 function-like macro argument ARG. This function must be called 1731 only when we -ftrack-macro-expansion is on. */ 1732 static const location_t * 1733 get_arg_token_location (const macro_arg *arg, 1734 enum macro_arg_token_kind kind) 1735 { 1736 const location_t *loc = NULL; 1737 const cpp_token **token_ptr = 1738 arg_token_ptr_at (arg, 0, kind, (location_t **) &loc); 1739 1740 if (token_ptr == NULL) 1741 return NULL; 1742 1743 return loc; 1744 } 1745 1746 /* Return the pointer to the INDEXth token of the macro argument ARG. 1747 KIND specifies the kind of token the macro argument ARG contains. 1748 If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address 1749 of the virtual location of the returned token if the 1750 -ftrack-macro-expansion flag is on; otherwise, it's set to the 1751 spelling location of the returned token. */ 1752 static const cpp_token ** 1753 arg_token_ptr_at (const macro_arg *arg, size_t index, 1754 enum macro_arg_token_kind kind, 1755 location_t **virt_location) 1756 { 1757 const cpp_token **tokens_ptr = NULL; 1758 1759 switch (kind) 1760 { 1761 case MACRO_ARG_TOKEN_NORMAL: 1762 tokens_ptr = arg->first; 1763 break; 1764 case MACRO_ARG_TOKEN_STRINGIFIED: 1765 tokens_ptr = (const cpp_token **) &arg->stringified; 1766 break; 1767 case MACRO_ARG_TOKEN_EXPANDED: 1768 tokens_ptr = arg->expanded; 1769 break; 1770 } 1771 1772 if (tokens_ptr == NULL) 1773 /* This can happen for e.g, an empty token argument to a 1774 funtion-like macro. */ 1775 return tokens_ptr; 1776 1777 if (virt_location) 1778 { 1779 if (kind == MACRO_ARG_TOKEN_NORMAL) 1780 *virt_location = &arg->virt_locs[index]; 1781 else if (kind == MACRO_ARG_TOKEN_EXPANDED) 1782 *virt_location = &arg->expanded_virt_locs[index]; 1783 else if (kind == MACRO_ARG_TOKEN_STRINGIFIED) 1784 *virt_location = 1785 (location_t *) &tokens_ptr[index]->src_loc; 1786 } 1787 return &tokens_ptr[index]; 1788 } 1789 1790 /* Initialize an iterator so that it iterates over the tokens of a 1791 function-like macro argument. KIND is the kind of tokens we want 1792 ITER to iterate over. TOKEN_PTR points the first token ITER will 1793 iterate over. */ 1794 static void 1795 macro_arg_token_iter_init (macro_arg_token_iter *iter, 1796 bool track_macro_exp_p, 1797 enum macro_arg_token_kind kind, 1798 const macro_arg *arg, 1799 const cpp_token **token_ptr) 1800 { 1801 iter->track_macro_exp_p = track_macro_exp_p; 1802 iter->kind = kind; 1803 iter->token_ptr = token_ptr; 1804 /* Unconditionally initialize this so that the compiler doesn't warn 1805 about iter->location_ptr being possibly uninitialized later after 1806 this code has been inlined somewhere. */ 1807 iter->location_ptr = NULL; 1808 if (track_macro_exp_p) 1809 iter->location_ptr = get_arg_token_location (arg, kind); 1810 #if CHECKING_P 1811 iter->num_forwards = 0; 1812 if (track_macro_exp_p 1813 && token_ptr != NULL 1814 && iter->location_ptr == NULL) 1815 abort (); 1816 #endif 1817 } 1818 1819 /* Move the iterator one token forward. Note that if IT was 1820 initialized on an argument that has a stringified token, moving it 1821 forward doesn't make sense as a stringified token is essentially one 1822 string. */ 1823 static void 1824 macro_arg_token_iter_forward (macro_arg_token_iter *it) 1825 { 1826 switch (it->kind) 1827 { 1828 case MACRO_ARG_TOKEN_NORMAL: 1829 case MACRO_ARG_TOKEN_EXPANDED: 1830 it->token_ptr++; 1831 if (it->track_macro_exp_p) 1832 it->location_ptr++; 1833 break; 1834 case MACRO_ARG_TOKEN_STRINGIFIED: 1835 #if CHECKING_P 1836 if (it->num_forwards > 0) 1837 abort (); 1838 #endif 1839 break; 1840 } 1841 1842 #if CHECKING_P 1843 it->num_forwards++; 1844 #endif 1845 } 1846 1847 /* Return the token pointed to by the iterator. */ 1848 static const cpp_token * 1849 macro_arg_token_iter_get_token (const macro_arg_token_iter *it) 1850 { 1851 #if CHECKING_P 1852 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED 1853 && it->num_forwards > 0) 1854 abort (); 1855 #endif 1856 if (it->token_ptr == NULL) 1857 return NULL; 1858 return *it->token_ptr; 1859 } 1860 1861 /* Return the location of the token pointed to by the iterator.*/ 1862 static location_t 1863 macro_arg_token_iter_get_location (const macro_arg_token_iter *it) 1864 { 1865 #if CHECKING_P 1866 if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED 1867 && it->num_forwards > 0) 1868 abort (); 1869 #endif 1870 if (it->track_macro_exp_p) 1871 return *it->location_ptr; 1872 else 1873 return (*it->token_ptr)->src_loc; 1874 } 1875 1876 /* Return the index of a token [resulting from macro expansion] inside 1877 the total list of tokens resulting from a given macro 1878 expansion. The index can be different depending on whether if we 1879 want each tokens resulting from function-like macro arguments 1880 expansion to have a different location or not. 1881 1882 E.g, consider this function-like macro: 1883 1884 #define M(x) x - 3 1885 1886 Then consider us "calling" it (and thus expanding it) like: 1887 1888 M(1+4) 1889 1890 It will be expanded into: 1891 1892 1+4-3 1893 1894 Let's consider the case of the token '4'. 1895 1896 Its index can be 2 (it's the third token of the set of tokens 1897 resulting from the expansion) or it can be 0 if we consider that 1898 all tokens resulting from the expansion of the argument "1+2" have 1899 the same index, which is 0. In this later case, the index of token 1900 '-' would then be 1 and the index of token '3' would be 2. 1901 1902 The later case is useful to use less memory e.g, for the case of 1903 the user using the option -ftrack-macro-expansion=1. 1904 1905 ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we 1906 are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro 1907 parameter (inside the macro replacement list) that corresponds to 1908 the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index 1909 of. 1910 1911 If we refer to the example above, for the '4' argument token, 1912 ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN 1913 would be set to the token 'x', in the replacement list "x - 3" of 1914 macro M. 1915 1916 This is a subroutine of replace_args. */ 1917 inline static unsigned 1918 expanded_token_index (cpp_reader *pfile, cpp_macro *macro, 1919 const cpp_token *cur_replacement_token, 1920 unsigned absolute_token_index) 1921 { 1922 if (CPP_OPTION (pfile, track_macro_expansion) > 1) 1923 return absolute_token_index; 1924 return cur_replacement_token - macro->exp.tokens; 1925 } 1926 1927 /* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG. */ 1928 1929 static void 1930 copy_paste_flag (cpp_reader *pfile, const cpp_token **paste_flag, 1931 const cpp_token *src) 1932 { 1933 cpp_token *token = _cpp_temp_token (pfile); 1934 token->type = (*paste_flag)->type; 1935 token->val = (*paste_flag)->val; 1936 if (src->flags & PASTE_LEFT) 1937 token->flags = (*paste_flag)->flags | PASTE_LEFT; 1938 else 1939 token->flags = (*paste_flag)->flags & ~PASTE_LEFT; 1940 *paste_flag = token; 1941 } 1942 1943 /* True IFF the last token emitted into BUFF (if any) is PTR. */ 1944 1945 static bool 1946 last_token_is (_cpp_buff *buff, const cpp_token **ptr) 1947 { 1948 return (ptr && tokens_buff_last_token_ptr (buff) == ptr); 1949 } 1950 1951 /* Replace the parameters in a function-like macro of NODE with the 1952 actual ARGS, and place the result in a newly pushed token context. 1953 Expand each argument before replacing, unless it is operated upon 1954 by the # or ## operators. EXPANSION_POINT_LOC is the location of 1955 the expansion point of the macro. E.g, the location of the 1956 function-like macro invocation. */ 1957 static void 1958 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, 1959 macro_arg *args, location_t expansion_point_loc) 1960 { 1961 unsigned int i, total; 1962 const cpp_token *src, *limit; 1963 const cpp_token **first = NULL; 1964 macro_arg *arg; 1965 _cpp_buff *buff = NULL; 1966 location_t *virt_locs = NULL; 1967 unsigned int exp_count; 1968 const line_map_macro *map = NULL; 1969 int track_macro_exp; 1970 1971 /* First, fully macro-expand arguments, calculating the number of 1972 tokens in the final expansion as we go. The ordering of the if 1973 statements below is subtle; we must handle stringification before 1974 pasting. */ 1975 1976 /* EXP_COUNT is the number of tokens in the macro replacement 1977 list. TOTAL is the number of tokens /after/ macro parameters 1978 have been replaced by their arguments. */ 1979 exp_count = macro_real_token_count (macro); 1980 total = exp_count; 1981 limit = macro->exp.tokens + exp_count; 1982 1983 for (src = macro->exp.tokens; src < limit; src++) 1984 if (src->type == CPP_MACRO_ARG) 1985 { 1986 /* Leading and trailing padding tokens. */ 1987 total += 2; 1988 /* Account for leading and padding tokens in exp_count too. 1989 This is going to be important later down this function, 1990 when we want to handle the case of (track_macro_exp < 1991 2). */ 1992 exp_count += 2; 1993 1994 /* We have an argument. If it is not being stringified or 1995 pasted it is macro-replaced before insertion. */ 1996 arg = &args[src->val.macro_arg.arg_no - 1]; 1997 1998 if (src->flags & STRINGIFY_ARG) 1999 { 2000 if (!arg->stringified) 2001 arg->stringified = stringify_arg (pfile, arg->first, arg->count); 2002 } 2003 else if ((src->flags & PASTE_LEFT) 2004 || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))) 2005 total += arg->count - 1; 2006 else 2007 { 2008 if (!arg->expanded) 2009 expand_arg (pfile, arg); 2010 total += arg->expanded_count - 1; 2011 } 2012 } 2013 2014 /* When the compiler is called with the -ftrack-macro-expansion 2015 flag, we need to keep track of the location of each token that 2016 results from macro expansion. 2017 2018 A token resulting from macro expansion is not a new token. It is 2019 simply the same token as the token coming from the macro 2020 definition. The new things that are allocated are the buffer 2021 that holds the tokens resulting from macro expansion and a new 2022 location that records many things like the locus of the expansion 2023 point as well as the original locus inside the definition of the 2024 macro. This location is called a virtual location. 2025 2026 So the buffer BUFF holds a set of cpp_token*, and the buffer 2027 VIRT_LOCS holds the virtual locations of the tokens held by BUFF. 2028 2029 Both of these two buffers are going to be hung off of the macro 2030 context, when the latter is pushed. The memory allocated to 2031 store the tokens and their locations is going to be freed once 2032 the context of macro expansion is popped. 2033 2034 As far as tokens are concerned, the memory overhead of 2035 -ftrack-macro-expansion is proportional to the number of 2036 macros that get expanded multiplied by sizeof (location_t). 2037 The good news is that extra memory gets freed when the macro 2038 context is freed, i.e shortly after the macro got expanded. */ 2039 2040 /* Is the -ftrack-macro-expansion flag in effect? */ 2041 track_macro_exp = CPP_OPTION (pfile, track_macro_expansion); 2042 2043 /* Now allocate memory space for tokens and locations resulting from 2044 the macro expansion, copy the tokens and replace the arguments. 2045 This memory must be freed when the context of the macro MACRO is 2046 popped. */ 2047 buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL); 2048 2049 first = (const cpp_token **) buff->base; 2050 2051 /* Create a macro map to record the locations of the tokens that are 2052 involved in the expansion. Note that the expansion point is set 2053 to the location of the closing parenthesis. Otherwise, the 2054 subsequent map created for the first token that comes after the 2055 macro map might have a wrong line number. That would lead to 2056 tokens with wrong line numbers after the macro expansion. This 2057 adds up to the memory overhead of the -ftrack-macro-expansion 2058 flag; for every macro that is expanded, a "macro map" is 2059 created. */ 2060 if (track_macro_exp) 2061 { 2062 int num_macro_tokens = total; 2063 if (track_macro_exp < 2) 2064 /* Then the number of macro tokens won't take in account the 2065 fact that function-like macro arguments can expand to 2066 multiple tokens. This is to save memory at the expense of 2067 accuracy. 2068 2069 Suppose we have #define SQUARE(A) A * A 2070 2071 And then we do SQUARE(2+3) 2072 2073 Then the tokens 2, +, 3, will have the same location, 2074 saying they come from the expansion of the argument A. */ 2075 num_macro_tokens = exp_count; 2076 map = linemap_enter_macro (pfile->line_table, node, 2077 expansion_point_loc, 2078 num_macro_tokens); 2079 } 2080 i = 0; 2081 vaopt_state vaopt_tracker (pfile, macro->variadic, &args[macro->paramc - 1]); 2082 const cpp_token **vaopt_start = NULL; 2083 for (src = macro->exp.tokens; src < limit; src++) 2084 { 2085 unsigned int arg_tokens_count; 2086 macro_arg_token_iter from; 2087 const cpp_token **paste_flag = NULL; 2088 const cpp_token **tmp_token_ptr; 2089 2090 /* __VA_OPT__ handling. */ 2091 vaopt_state::update_type vostate = vaopt_tracker.update (src); 2092 if (__builtin_expect (vostate != vaopt_state::INCLUDE, false)) 2093 { 2094 if (vostate == vaopt_state::BEGIN) 2095 { 2096 /* Padding on the left of __VA_OPT__ (unless RHS of ##). */ 2097 if (src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)) 2098 { 2099 const cpp_token *t = padding_token (pfile, src); 2100 unsigned index = expanded_token_index (pfile, macro, src, i); 2101 /* Allocate a virtual location for the padding token and 2102 append the token and its location to BUFF and 2103 VIRT_LOCS. */ 2104 tokens_buff_add_token (buff, virt_locs, t, 2105 t->src_loc, t->src_loc, 2106 map, index); 2107 } 2108 vaopt_start = tokens_buff_last_token_ptr (buff); 2109 } 2110 else if (vostate == vaopt_state::END) 2111 { 2112 const cpp_token **start = vaopt_start; 2113 vaopt_start = NULL; 2114 2115 paste_flag = tokens_buff_last_token_ptr (buff); 2116 2117 if (vaopt_tracker.stringify ()) 2118 { 2119 unsigned int count 2120 = start ? paste_flag - start : tokens_buff_count (buff); 2121 const cpp_token **first 2122 = start ? start + 1 2123 : (const cpp_token **) (buff->base); 2124 unsigned int i, j; 2125 2126 /* Paste any tokens that need to be pasted before calling 2127 stringify_arg, because stringify_arg uses pfile->u_buff 2128 which paste_tokens can use as well. */ 2129 for (i = 0, j = 0; i < count; i++, j++) 2130 { 2131 const cpp_token *token = first[i]; 2132 2133 if (token->flags & PASTE_LEFT) 2134 { 2135 location_t virt_loc = pfile->invocation_location; 2136 const cpp_token *rhs; 2137 do 2138 { 2139 if (i == count) 2140 abort (); 2141 rhs = first[++i]; 2142 if (!paste_tokens (pfile, virt_loc, &token, rhs)) 2143 { 2144 --i; 2145 break; 2146 } 2147 } 2148 while (rhs->flags & PASTE_LEFT); 2149 } 2150 2151 first[j] = token; 2152 } 2153 if (j != i) 2154 { 2155 while (i-- != j) 2156 tokens_buff_remove_last_token (buff); 2157 count = j; 2158 } 2159 2160 const cpp_token *t = stringify_arg (pfile, first, count); 2161 while (count--) 2162 tokens_buff_remove_last_token (buff); 2163 if (src->flags & PASTE_LEFT) 2164 copy_paste_flag (pfile, &t, src); 2165 tokens_buff_add_token (buff, virt_locs, 2166 t, t->src_loc, t->src_loc, 2167 NULL, 0); 2168 continue; 2169 } 2170 if (start && paste_flag == start && (*start)->flags & PASTE_LEFT) 2171 /* If __VA_OPT__ expands to nothing (either because __VA_ARGS__ 2172 is empty or because it is __VA_OPT__() ), drop PASTE_LEFT 2173 flag from previous token. */ 2174 copy_paste_flag (pfile, start, &pfile->avoid_paste); 2175 if (src->flags & PASTE_LEFT) 2176 { 2177 /* Don't avoid paste after all. */ 2178 while (paste_flag && paste_flag != start 2179 && *paste_flag == &pfile->avoid_paste) 2180 { 2181 tokens_buff_remove_last_token (buff); 2182 paste_flag = tokens_buff_last_token_ptr (buff); 2183 } 2184 2185 /* With a non-empty __VA_OPT__ on the LHS of ##, the last 2186 token should be flagged PASTE_LEFT. */ 2187 if (paste_flag && (*paste_flag)->type != CPP_PADDING) 2188 copy_paste_flag (pfile, paste_flag, src); 2189 } 2190 else 2191 { 2192 /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or 2193 __VA_OPT__(c)__VA_OPT__(d). */ 2194 const cpp_token *t = &pfile->avoid_paste; 2195 tokens_buff_add_token (buff, virt_locs, 2196 t, t->src_loc, t->src_loc, 2197 NULL, 0); 2198 } 2199 } 2200 continue; 2201 } 2202 2203 if (src->type != CPP_MACRO_ARG) 2204 { 2205 /* Allocate a virtual location for token SRC, and add that 2206 token and its virtual location into the buffers BUFF and 2207 VIRT_LOCS. */ 2208 unsigned index = expanded_token_index (pfile, macro, src, i); 2209 tokens_buff_add_token (buff, virt_locs, src, 2210 src->src_loc, src->src_loc, 2211 map, index); 2212 i += 1; 2213 continue; 2214 } 2215 2216 paste_flag = 0; 2217 arg = &args[src->val.macro_arg.arg_no - 1]; 2218 /* SRC is a macro parameter that we need to replace with its 2219 corresponding argument. So at some point we'll need to 2220 iterate over the tokens of the macro argument and copy them 2221 into the "place" now holding the correspondig macro 2222 parameter. We are going to use the iterator type 2223 macro_argo_token_iter to handle that iterating. The 'if' 2224 below is to initialize the iterator depending on the type of 2225 tokens the macro argument has. It also does some adjustment 2226 related to padding tokens and some pasting corner cases. */ 2227 if (src->flags & STRINGIFY_ARG) 2228 { 2229 arg_tokens_count = 1; 2230 macro_arg_token_iter_init (&from, 2231 CPP_OPTION (pfile, 2232 track_macro_expansion), 2233 MACRO_ARG_TOKEN_STRINGIFIED, 2234 arg, &arg->stringified); 2235 } 2236 else if (src->flags & PASTE_LEFT) 2237 { 2238 arg_tokens_count = arg->count; 2239 macro_arg_token_iter_init (&from, 2240 CPP_OPTION (pfile, 2241 track_macro_expansion), 2242 MACRO_ARG_TOKEN_NORMAL, 2243 arg, arg->first); 2244 } 2245 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)) 2246 { 2247 int num_toks; 2248 arg_tokens_count = arg->count; 2249 macro_arg_token_iter_init (&from, 2250 CPP_OPTION (pfile, 2251 track_macro_expansion), 2252 MACRO_ARG_TOKEN_NORMAL, 2253 arg, arg->first); 2254 2255 num_toks = tokens_buff_count (buff); 2256 2257 if (num_toks != 0) 2258 { 2259 /* So the current parameter token is pasted to the previous 2260 token in the replacement list. Let's look at what 2261 we have as previous and current arguments. */ 2262 2263 /* This is the previous argument's token ... */ 2264 tmp_token_ptr = tokens_buff_last_token_ptr (buff); 2265 2266 if ((*tmp_token_ptr)->type == CPP_COMMA 2267 && macro->variadic 2268 && src->val.macro_arg.arg_no == macro->paramc) 2269 { 2270 /* ... which is a comma; and the current parameter 2271 is the last parameter of a variadic function-like 2272 macro. If the argument to the current last 2273 parameter is NULL, then swallow the comma, 2274 otherwise drop the paste flag. */ 2275 if (macro_arg_token_iter_get_token (&from) == NULL) 2276 tokens_buff_remove_last_token (buff); 2277 else 2278 paste_flag = tmp_token_ptr; 2279 } 2280 /* Remove the paste flag if the RHS is a placemarker. */ 2281 else if (arg_tokens_count == 0) 2282 paste_flag = tmp_token_ptr; 2283 } 2284 } 2285 else 2286 { 2287 arg_tokens_count = arg->expanded_count; 2288 macro_arg_token_iter_init (&from, 2289 CPP_OPTION (pfile, 2290 track_macro_expansion), 2291 MACRO_ARG_TOKEN_EXPANDED, 2292 arg, arg->expanded); 2293 2294 if (last_token_is (buff, vaopt_start)) 2295 { 2296 /* We're expanding an arg at the beginning of __VA_OPT__. 2297 Skip padding. */ 2298 while (arg_tokens_count) 2299 { 2300 const cpp_token *t = macro_arg_token_iter_get_token (&from); 2301 if (t->type != CPP_PADDING) 2302 break; 2303 macro_arg_token_iter_forward (&from); 2304 --arg_tokens_count; 2305 } 2306 } 2307 } 2308 2309 /* Padding on the left of an argument (unless RHS of ##). */ 2310 if ((!pfile->state.in_directive || pfile->state.directive_wants_padding) 2311 && src != macro->exp.tokens 2312 && !(src[-1].flags & PASTE_LEFT) 2313 && !last_token_is (buff, vaopt_start)) 2314 { 2315 const cpp_token *t = padding_token (pfile, src); 2316 unsigned index = expanded_token_index (pfile, macro, src, i); 2317 /* Allocate a virtual location for the padding token and 2318 append the token and its location to BUFF and 2319 VIRT_LOCS. */ 2320 tokens_buff_add_token (buff, virt_locs, t, 2321 t->src_loc, t->src_loc, 2322 map, index); 2323 } 2324 2325 if (arg_tokens_count) 2326 { 2327 /* So now we've got the number of tokens that make up the 2328 argument that is going to replace the current parameter 2329 in the macro's replacement list. */ 2330 unsigned int j; 2331 for (j = 0; j < arg_tokens_count; ++j) 2332 { 2333 /* So if track_macro_exp is < 2, the user wants to 2334 save extra memory while tracking macro expansion 2335 locations. So in that case here is what we do: 2336 2337 Suppose we have #define SQUARE(A) A * A 2338 2339 And then we do SQUARE(2+3) 2340 2341 Then the tokens 2, +, 3, will have the same location, 2342 saying they come from the expansion of the argument 2343 A. 2344 2345 So that means we are going to ignore the COUNT tokens 2346 resulting from the expansion of the current macro 2347 argument. In other words all the ARG_TOKENS_COUNT tokens 2348 resulting from the expansion of the macro argument will 2349 have the index I. Normally, each of those tokens should 2350 have index I+J. */ 2351 unsigned token_index = i; 2352 unsigned index; 2353 if (track_macro_exp > 1) 2354 token_index += j; 2355 2356 index = expanded_token_index (pfile, macro, src, token_index); 2357 const cpp_token *tok = macro_arg_token_iter_get_token (&from); 2358 tokens_buff_add_token (buff, virt_locs, tok, 2359 macro_arg_token_iter_get_location (&from), 2360 src->src_loc, map, index); 2361 macro_arg_token_iter_forward (&from); 2362 } 2363 2364 /* With a non-empty argument on the LHS of ##, the last 2365 token should be flagged PASTE_LEFT. */ 2366 if (src->flags & PASTE_LEFT) 2367 paste_flag 2368 = (const cpp_token **) tokens_buff_last_token_ptr (buff); 2369 } 2370 else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99) 2371 && ! macro->syshdr && ! _cpp_in_system_header (pfile)) 2372 { 2373 if (CPP_OPTION (pfile, cplusplus)) 2374 cpp_pedwarning (pfile, CPP_W_PEDANTIC, 2375 "invoking macro %s argument %d: " 2376 "empty macro arguments are undefined" 2377 " in ISO C++98", 2378 NODE_NAME (node), src->val.macro_arg.arg_no); 2379 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat)) 2380 cpp_pedwarning (pfile, 2381 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 2382 ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC, 2383 "invoking macro %s argument %d: " 2384 "empty macro arguments are undefined" 2385 " in ISO C90", 2386 NODE_NAME (node), src->val.macro_arg.arg_no); 2387 } 2388 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 2389 && ! CPP_OPTION (pfile, cplusplus) 2390 && ! macro->syshdr && ! _cpp_in_system_header (pfile)) 2391 cpp_warning (pfile, CPP_W_C90_C99_COMPAT, 2392 "invoking macro %s argument %d: " 2393 "empty macro arguments are undefined" 2394 " in ISO C90", 2395 NODE_NAME (node), src->val.macro_arg.arg_no); 2396 2397 /* Avoid paste on RHS (even case count == 0). */ 2398 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) 2399 { 2400 const cpp_token *t = &pfile->avoid_paste; 2401 tokens_buff_add_token (buff, virt_locs, 2402 t, t->src_loc, t->src_loc, 2403 NULL, 0); 2404 } 2405 2406 /* Add a new paste flag, or remove an unwanted one. */ 2407 if (paste_flag) 2408 copy_paste_flag (pfile, paste_flag, src); 2409 2410 i += arg_tokens_count; 2411 } 2412 2413 if (track_macro_exp) 2414 push_extended_tokens_context (pfile, node, buff, virt_locs, first, 2415 tokens_buff_count (buff)); 2416 else 2417 push_ptoken_context (pfile, node, buff, first, 2418 tokens_buff_count (buff)); 2419 2420 num_macro_tokens_counter += tokens_buff_count (buff); 2421 } 2422 2423 /* Return a special padding token, with padding inherited from SOURCE. */ 2424 static const cpp_token * 2425 padding_token (cpp_reader *pfile, const cpp_token *source) 2426 { 2427 cpp_token *result = _cpp_temp_token (pfile); 2428 2429 result->type = CPP_PADDING; 2430 2431 /* Data in GCed data structures cannot be made const so far, so we 2432 need a cast here. */ 2433 result->val.source = (cpp_token *) source; 2434 result->flags = 0; 2435 return result; 2436 } 2437 2438 /* Get a new uninitialized context. Create a new one if we cannot 2439 re-use an old one. */ 2440 static cpp_context * 2441 next_context (cpp_reader *pfile) 2442 { 2443 cpp_context *result = pfile->context->next; 2444 2445 if (result == 0) 2446 { 2447 result = XNEW (cpp_context); 2448 memset (result, 0, sizeof (cpp_context)); 2449 result->prev = pfile->context; 2450 result->next = 0; 2451 pfile->context->next = result; 2452 } 2453 2454 pfile->context = result; 2455 return result; 2456 } 2457 2458 /* Push a list of pointers to tokens. */ 2459 static void 2460 push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff, 2461 const cpp_token **first, unsigned int count) 2462 { 2463 cpp_context *context = next_context (pfile); 2464 2465 context->tokens_kind = TOKENS_KIND_INDIRECT; 2466 context->c.macro = macro; 2467 context->buff = buff; 2468 FIRST (context).ptoken = first; 2469 LAST (context).ptoken = first + count; 2470 } 2471 2472 /* Push a list of tokens. 2473 2474 A NULL macro means that we should continue the current macro 2475 expansion, in essence. That means that if we are currently in a 2476 macro expansion context, we'll make the new pfile->context refer to 2477 the current macro. */ 2478 void 2479 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro, 2480 const cpp_token *first, unsigned int count) 2481 { 2482 cpp_context *context; 2483 2484 if (macro == NULL) 2485 macro = macro_of_context (pfile->context); 2486 2487 context = next_context (pfile); 2488 context->tokens_kind = TOKENS_KIND_DIRECT; 2489 context->c.macro = macro; 2490 context->buff = NULL; 2491 FIRST (context).token = first; 2492 LAST (context).token = first + count; 2493 } 2494 2495 /* Build a context containing a list of tokens as well as their 2496 virtual locations and push it. TOKENS_BUFF is the buffer that 2497 contains the tokens pointed to by FIRST. If TOKENS_BUFF is 2498 non-NULL, it means that the context owns it, meaning that 2499 _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that 2500 contains the virtual locations. 2501 2502 A NULL macro means that we should continue the current macro 2503 expansion, in essence. That means that if we are currently in a 2504 macro expansion context, we'll make the new pfile->context refer to 2505 the current macro. */ 2506 static void 2507 push_extended_tokens_context (cpp_reader *pfile, 2508 cpp_hashnode *macro, 2509 _cpp_buff *token_buff, 2510 location_t *virt_locs, 2511 const cpp_token **first, 2512 unsigned int count) 2513 { 2514 cpp_context *context; 2515 macro_context *m; 2516 2517 if (macro == NULL) 2518 macro = macro_of_context (pfile->context); 2519 2520 context = next_context (pfile); 2521 context->tokens_kind = TOKENS_KIND_EXTENDED; 2522 context->buff = token_buff; 2523 2524 m = XNEW (macro_context); 2525 m->macro_node = macro; 2526 m->virt_locs = virt_locs; 2527 m->cur_virt_loc = virt_locs; 2528 context->c.mc = m; 2529 FIRST (context).ptoken = first; 2530 LAST (context).ptoken = first + count; 2531 } 2532 2533 /* Push a traditional macro's replacement text. */ 2534 void 2535 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro, 2536 const uchar *start, size_t len) 2537 { 2538 cpp_context *context = next_context (pfile); 2539 2540 context->tokens_kind = TOKENS_KIND_DIRECT; 2541 context->c.macro = macro; 2542 context->buff = NULL; 2543 CUR (context) = start; 2544 RLIMIT (context) = start + len; 2545 macro->flags |= NODE_DISABLED; 2546 } 2547 2548 /* Creates a buffer that holds tokens a.k.a "token buffer", usually 2549 for the purpose of storing them on a cpp_context. If VIRT_LOCS is 2550 non-null (which means that -ftrack-macro-expansion is on), 2551 *VIRT_LOCS is set to a newly allocated buffer that is supposed to 2552 hold the virtual locations of the tokens resulting from macro 2553 expansion. */ 2554 static _cpp_buff* 2555 tokens_buff_new (cpp_reader *pfile, size_t len, 2556 location_t **virt_locs) 2557 { 2558 size_t tokens_size = len * sizeof (cpp_token *); 2559 size_t locs_size = len * sizeof (location_t); 2560 2561 if (virt_locs != NULL) 2562 *virt_locs = XNEWVEC (location_t, locs_size); 2563 return _cpp_get_buff (pfile, tokens_size); 2564 } 2565 2566 /* Returns the number of tokens contained in a token buffer. The 2567 buffer holds a set of cpp_token*. */ 2568 static size_t 2569 tokens_buff_count (_cpp_buff *buff) 2570 { 2571 return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *); 2572 } 2573 2574 /* Return a pointer to the last token contained in the token buffer 2575 BUFF. */ 2576 static const cpp_token ** 2577 tokens_buff_last_token_ptr (_cpp_buff *buff) 2578 { 2579 if (BUFF_FRONT (buff) == buff->base) 2580 return NULL; 2581 return &((const cpp_token **) BUFF_FRONT (buff))[-1]; 2582 } 2583 2584 /* Remove the last token contained in the token buffer TOKENS_BUFF. 2585 If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer 2586 containing the virtual locations of the tokens in TOKENS_BUFF; in 2587 which case the function updates that buffer as well. */ 2588 static inline void 2589 tokens_buff_remove_last_token (_cpp_buff *tokens_buff) 2590 2591 { 2592 if (BUFF_FRONT (tokens_buff) > tokens_buff->base) 2593 BUFF_FRONT (tokens_buff) = 2594 (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1]; 2595 } 2596 2597 /* Insert a token into the token buffer at the position pointed to by 2598 DEST. Note that the buffer is not enlarged so the previous token 2599 that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null, 2600 means -ftrack-macro-expansion is effect; it then points to where to 2601 insert the virtual location of TOKEN. TOKEN is the token to 2602 insert. VIRT_LOC is the virtual location of the token, i.e, the 2603 location possibly encoding its locus across macro expansion. If 2604 TOKEN is an argument of a function-like macro (inside a macro 2605 replacement list), PARM_DEF_LOC is the spelling location of the 2606 macro parameter that TOKEN is replacing, in the replacement list of 2607 the macro. If TOKEN is not an argument of a function-like macro or 2608 if it doesn't come from a macro expansion, then VIRT_LOC can just 2609 be set to the same value as PARM_DEF_LOC. If MAP is non null, it 2610 means TOKEN comes from a macro expansion and MAP is the macro map 2611 associated to the macro. MACRO_TOKEN_INDEX points to the index of 2612 the token in the macro map; it is not considered if MAP is NULL. 2613 2614 Upon successful completion this function returns the a pointer to 2615 the position of the token coming right after the insertion 2616 point. */ 2617 static inline const cpp_token ** 2618 tokens_buff_put_token_to (const cpp_token **dest, 2619 location_t *virt_loc_dest, 2620 const cpp_token *token, 2621 location_t virt_loc, 2622 location_t parm_def_loc, 2623 const line_map_macro *map, 2624 unsigned int macro_token_index) 2625 { 2626 location_t macro_loc = virt_loc; 2627 const cpp_token **result; 2628 2629 if (virt_loc_dest) 2630 { 2631 /* -ftrack-macro-expansion is on. */ 2632 if (map) 2633 macro_loc = linemap_add_macro_token (map, macro_token_index, 2634 virt_loc, parm_def_loc); 2635 *virt_loc_dest = macro_loc; 2636 } 2637 *dest = token; 2638 result = &dest[1]; 2639 2640 return result; 2641 } 2642 2643 /* Adds a token at the end of the tokens contained in BUFFER. Note 2644 that this function doesn't enlarge BUFFER when the number of tokens 2645 reaches BUFFER's size; it aborts in that situation. 2646 2647 TOKEN is the token to append. VIRT_LOC is the virtual location of 2648 the token, i.e, the location possibly encoding its locus across 2649 macro expansion. If TOKEN is an argument of a function-like macro 2650 (inside a macro replacement list), PARM_DEF_LOC is the location of 2651 the macro parameter that TOKEN is replacing. If TOKEN doesn't come 2652 from a macro expansion, then VIRT_LOC can just be set to the same 2653 value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes 2654 from a macro expansion and MAP is the macro map associated to the 2655 macro. MACRO_TOKEN_INDEX points to the index of the token in the 2656 macro map; It is not considered if MAP is NULL. If VIRT_LOCS is 2657 non-null, it means -ftrack-macro-expansion is on; in which case 2658 this function adds the virtual location DEF_LOC to the VIRT_LOCS 2659 array, at the same index as the one of TOKEN in BUFFER. Upon 2660 successful completion this function returns the a pointer to the 2661 position of the token coming right after the insertion point. */ 2662 static const cpp_token ** 2663 tokens_buff_add_token (_cpp_buff *buffer, 2664 location_t *virt_locs, 2665 const cpp_token *token, 2666 location_t virt_loc, 2667 location_t parm_def_loc, 2668 const line_map_macro *map, 2669 unsigned int macro_token_index) 2670 { 2671 const cpp_token **result; 2672 location_t *virt_loc_dest = NULL; 2673 unsigned token_index = 2674 (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *); 2675 2676 /* Abort if we pass the end the buffer. */ 2677 if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer)) 2678 abort (); 2679 2680 if (virt_locs != NULL) 2681 virt_loc_dest = &virt_locs[token_index]; 2682 2683 result = 2684 tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer), 2685 virt_loc_dest, token, virt_loc, parm_def_loc, 2686 map, macro_token_index); 2687 2688 BUFF_FRONT (buffer) = (unsigned char *) result; 2689 return result; 2690 } 2691 2692 /* Allocate space for the function-like macro argument ARG to store 2693 the tokens resulting from the macro-expansion of the tokens that 2694 make up ARG itself. That space is allocated in ARG->expanded and 2695 needs to be freed using free. */ 2696 static void 2697 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity) 2698 { 2699 gcc_checking_assert (arg->expanded == NULL 2700 && arg->expanded_virt_locs == NULL); 2701 2702 arg->expanded = XNEWVEC (const cpp_token *, capacity); 2703 if (CPP_OPTION (pfile, track_macro_expansion)) 2704 arg->expanded_virt_locs = XNEWVEC (location_t, capacity); 2705 2706 } 2707 2708 /* If necessary, enlarge ARG->expanded to so that it can contain SIZE 2709 tokens. */ 2710 static void 2711 ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg, 2712 size_t size, size_t *expanded_capacity) 2713 { 2714 if (size <= *expanded_capacity) 2715 return; 2716 2717 size *= 2; 2718 2719 arg->expanded = 2720 XRESIZEVEC (const cpp_token *, arg->expanded, size); 2721 *expanded_capacity = size; 2722 2723 if (CPP_OPTION (pfile, track_macro_expansion)) 2724 { 2725 if (arg->expanded_virt_locs == NULL) 2726 arg->expanded_virt_locs = XNEWVEC (location_t, size); 2727 else 2728 arg->expanded_virt_locs = XRESIZEVEC (location_t, 2729 arg->expanded_virt_locs, 2730 size); 2731 } 2732 } 2733 2734 /* Expand an argument ARG before replacing parameters in a 2735 function-like macro. This works by pushing a context with the 2736 argument's tokens, and then expanding that into a temporary buffer 2737 as if it were a normal part of the token stream. collect_args() 2738 has terminated the argument's tokens with a CPP_EOF so that we know 2739 when we have fully expanded the argument. */ 2740 static void 2741 expand_arg (cpp_reader *pfile, macro_arg *arg) 2742 { 2743 size_t capacity; 2744 bool saved_warn_trad; 2745 bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion); 2746 bool saved_ignore__Pragma; 2747 2748 if (arg->count == 0 2749 || arg->expanded != NULL) 2750 return; 2751 2752 /* Don't warn about funlike macros when pre-expanding. */ 2753 saved_warn_trad = CPP_WTRADITIONAL (pfile); 2754 CPP_WTRADITIONAL (pfile) = 0; 2755 2756 /* Loop, reading in the tokens of the argument. */ 2757 capacity = 256; 2758 alloc_expanded_arg_mem (pfile, arg, capacity); 2759 2760 if (track_macro_exp_p) 2761 push_extended_tokens_context (pfile, NULL, NULL, 2762 arg->virt_locs, 2763 arg->first, 2764 arg->count + 1); 2765 else 2766 push_ptoken_context (pfile, NULL, NULL, 2767 arg->first, arg->count + 1); 2768 2769 saved_ignore__Pragma = pfile->state.ignore__Pragma; 2770 pfile->state.ignore__Pragma = 1; 2771 2772 for (;;) 2773 { 2774 const cpp_token *token; 2775 location_t location; 2776 2777 ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1, 2778 &capacity); 2779 2780 token = cpp_get_token_1 (pfile, &location); 2781 2782 if (token->type == CPP_EOF) 2783 break; 2784 2785 set_arg_token (arg, token, location, 2786 arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED, 2787 CPP_OPTION (pfile, track_macro_expansion)); 2788 arg->expanded_count++; 2789 } 2790 2791 _cpp_pop_context (pfile); 2792 2793 CPP_WTRADITIONAL (pfile) = saved_warn_trad; 2794 pfile->state.ignore__Pragma = saved_ignore__Pragma; 2795 } 2796 2797 /* Returns the macro associated to the current context if we are in 2798 the context a macro expansion, NULL otherwise. */ 2799 static cpp_hashnode* 2800 macro_of_context (cpp_context *context) 2801 { 2802 if (context == NULL) 2803 return NULL; 2804 2805 return (context->tokens_kind == TOKENS_KIND_EXTENDED) 2806 ? context->c.mc->macro_node 2807 : context->c.macro; 2808 } 2809 2810 /* Return TRUE iff we are expanding a macro or are about to start 2811 expanding one. If we are effectively expanding a macro, the 2812 function macro_of_context returns a pointer to the macro being 2813 expanded. */ 2814 static bool 2815 in_macro_expansion_p (cpp_reader *pfile) 2816 { 2817 if (pfile == NULL) 2818 return false; 2819 2820 return (pfile->about_to_expand_macro_p 2821 || macro_of_context (pfile->context)); 2822 } 2823 2824 /* Pop the current context off the stack, re-enabling the macro if the 2825 context represented a macro's replacement list. Initially the 2826 context structure was not freed so that we can re-use it later, but 2827 now we do free it to reduce peak memory consumption. */ 2828 void 2829 _cpp_pop_context (cpp_reader *pfile) 2830 { 2831 cpp_context *context = pfile->context; 2832 2833 /* We should not be popping the base context. */ 2834 gcc_assert (context != &pfile->base_context); 2835 2836 if (context->c.macro) 2837 { 2838 cpp_hashnode *macro; 2839 if (context->tokens_kind == TOKENS_KIND_EXTENDED) 2840 { 2841 macro_context *mc = context->c.mc; 2842 macro = mc->macro_node; 2843 /* If context->buff is set, it means the life time of tokens 2844 is bound to the life time of this context; so we must 2845 free the tokens; that means we must free the virtual 2846 locations of these tokens too. */ 2847 if (context->buff && mc->virt_locs) 2848 { 2849 free (mc->virt_locs); 2850 mc->virt_locs = NULL; 2851 } 2852 free (mc); 2853 context->c.mc = NULL; 2854 } 2855 else 2856 macro = context->c.macro; 2857 2858 /* Beware that MACRO can be NULL in cases like when we are 2859 called from expand_arg. In those cases, a dummy context with 2860 tokens is pushed just for the purpose of walking them using 2861 cpp_get_token_1. In that case, no 'macro' field is set into 2862 the dummy context. */ 2863 if (macro != NULL 2864 /* Several contiguous macro expansion contexts can be 2865 associated to the same macro; that means it's the same 2866 macro expansion that spans across all these (sub) 2867 contexts. So we should re-enable an expansion-disabled 2868 macro only when we are sure we are really out of that 2869 macro expansion. */ 2870 && macro_of_context (context->prev) != macro) 2871 macro->flags &= ~NODE_DISABLED; 2872 2873 if (macro == pfile->top_most_macro_node && context->prev == NULL) 2874 /* We are popping the context of the top-most macro node. */ 2875 pfile->top_most_macro_node = NULL; 2876 } 2877 2878 if (context->buff) 2879 { 2880 /* Decrease memory peak consumption by freeing the memory used 2881 by the context. */ 2882 _cpp_free_buff (context->buff); 2883 } 2884 2885 pfile->context = context->prev; 2886 /* decrease peak memory consumption by feeing the context. */ 2887 pfile->context->next = NULL; 2888 free (context); 2889 } 2890 2891 /* Return TRUE if we reached the end of the set of tokens stored in 2892 CONTEXT, FALSE otherwise. */ 2893 static inline bool 2894 reached_end_of_context (cpp_context *context) 2895 { 2896 if (context->tokens_kind == TOKENS_KIND_DIRECT) 2897 return FIRST (context).token == LAST (context).token; 2898 else if (context->tokens_kind == TOKENS_KIND_INDIRECT 2899 || context->tokens_kind == TOKENS_KIND_EXTENDED) 2900 return FIRST (context).ptoken == LAST (context).ptoken; 2901 else 2902 abort (); 2903 } 2904 2905 /* Consume the next token contained in the current context of PFILE, 2906 and return it in *TOKEN. It's "full location" is returned in 2907 *LOCATION. If -ftrack-macro-location is in effeect, fFull location" 2908 means the location encoding the locus of the token across macro 2909 expansion; otherwise it's just is the "normal" location of the 2910 token which (*TOKEN)->src_loc. */ 2911 static inline void 2912 consume_next_token_from_context (cpp_reader *pfile, 2913 const cpp_token ** token, 2914 location_t *location) 2915 { 2916 cpp_context *c = pfile->context; 2917 2918 if ((c)->tokens_kind == TOKENS_KIND_DIRECT) 2919 { 2920 *token = FIRST (c).token; 2921 *location = (*token)->src_loc; 2922 FIRST (c).token++; 2923 } 2924 else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT) 2925 { 2926 *token = *FIRST (c).ptoken; 2927 *location = (*token)->src_loc; 2928 FIRST (c).ptoken++; 2929 } 2930 else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED) 2931 { 2932 macro_context *m = c->c.mc; 2933 *token = *FIRST (c).ptoken; 2934 if (m->virt_locs) 2935 { 2936 *location = *m->cur_virt_loc; 2937 m->cur_virt_loc++; 2938 } 2939 else 2940 *location = (*token)->src_loc; 2941 FIRST (c).ptoken++; 2942 } 2943 else 2944 abort (); 2945 } 2946 2947 /* In the traditional mode of the preprocessor, if we are currently in 2948 a directive, the location of a token must be the location of the 2949 start of the directive line. This function returns the proper 2950 location if we are in the traditional mode, and just returns 2951 LOCATION otherwise. */ 2952 2953 static inline location_t 2954 maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, location_t location) 2955 { 2956 if (CPP_OPTION (pfile, traditional)) 2957 { 2958 if (pfile->state.in_directive) 2959 return pfile->directive_line; 2960 } 2961 return location; 2962 } 2963 2964 /* Routine to get a token as well as its location. 2965 2966 Macro expansions and directives are transparently handled, 2967 including entering included files. Thus tokens are post-macro 2968 expansion, and after any intervening directives. External callers 2969 see CPP_EOF only at EOF. Internal callers also see it when meeting 2970 a directive inside a macro call, when at the end of a directive and 2971 state.in_directive is still 1, and at the end of argument 2972 pre-expansion. 2973 2974 LOC is an out parameter; *LOC is set to the location "as expected 2975 by the user". Please read the comment of 2976 cpp_get_token_with_location to learn more about the meaning of this 2977 location. */ 2978 static const cpp_token* 2979 cpp_get_token_1 (cpp_reader *pfile, location_t *location) 2980 { 2981 const cpp_token *result; 2982 /* This token is a virtual token that either encodes a location 2983 related to macro expansion or a spelling location. */ 2984 location_t virt_loc = 0; 2985 /* pfile->about_to_expand_macro_p can be overriden by indirect calls 2986 to functions that push macro contexts. So let's save it so that 2987 we can restore it when we are about to leave this routine. */ 2988 bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p; 2989 2990 for (;;) 2991 { 2992 cpp_hashnode *node; 2993 cpp_context *context = pfile->context; 2994 2995 /* Context->prev == 0 <=> base context. */ 2996 if (!context->prev) 2997 { 2998 result = _cpp_lex_token (pfile); 2999 virt_loc = result->src_loc; 3000 } 3001 else if (!reached_end_of_context (context)) 3002 { 3003 consume_next_token_from_context (pfile, &result, 3004 &virt_loc); 3005 if (result->flags & PASTE_LEFT) 3006 { 3007 paste_all_tokens (pfile, result); 3008 if (pfile->state.in_directive) 3009 continue; 3010 result = padding_token (pfile, result); 3011 goto out; 3012 } 3013 } 3014 else 3015 { 3016 if (pfile->context->c.macro) 3017 ++num_expanded_macros_counter; 3018 _cpp_pop_context (pfile); 3019 if (pfile->state.in_directive) 3020 continue; 3021 result = &pfile->avoid_paste; 3022 goto out; 3023 } 3024 3025 if (pfile->state.in_directive && result->type == CPP_COMMENT) 3026 continue; 3027 3028 if (result->type != CPP_NAME) 3029 break; 3030 3031 node = result->val.node.node; 3032 3033 if (node->type == NT_VOID || (result->flags & NO_EXPAND)) 3034 break; 3035 3036 if (!(node->flags & NODE_USED) 3037 && node->type == NT_USER_MACRO 3038 && !node->value.macro 3039 && !cpp_get_deferred_macro (pfile, node, result->src_loc)) 3040 break; 3041 3042 if (!(node->flags & NODE_DISABLED)) 3043 { 3044 int ret = 0; 3045 /* If not in a macro context, and we're going to start an 3046 expansion, record the location and the top level macro 3047 about to be expanded. */ 3048 if (!in_macro_expansion_p (pfile)) 3049 { 3050 pfile->invocation_location = result->src_loc; 3051 pfile->top_most_macro_node = node; 3052 } 3053 if (pfile->state.prevent_expansion) 3054 break; 3055 3056 /* Conditional macros require that a predicate be evaluated 3057 first. */ 3058 if ((node->flags & NODE_CONDITIONAL) != 0) 3059 { 3060 if (pfile->cb.macro_to_expand) 3061 { 3062 bool whitespace_after; 3063 const cpp_token *peek_tok = cpp_peek_token (pfile, 0); 3064 3065 whitespace_after = (peek_tok->type == CPP_PADDING 3066 || (peek_tok->flags & PREV_WHITE)); 3067 node = pfile->cb.macro_to_expand (pfile, result); 3068 if (node) 3069 ret = enter_macro_context (pfile, node, result, virt_loc); 3070 else if (whitespace_after) 3071 { 3072 /* If macro_to_expand hook returned NULL and it 3073 ate some tokens, see if we don't need to add 3074 a padding token in between this and the 3075 next token. */ 3076 peek_tok = cpp_peek_token (pfile, 0); 3077 if (peek_tok->type != CPP_PADDING 3078 && (peek_tok->flags & PREV_WHITE) == 0) 3079 _cpp_push_token_context (pfile, NULL, 3080 padding_token (pfile, 3081 peek_tok), 1); 3082 } 3083 } 3084 } 3085 else 3086 ret = enter_macro_context (pfile, node, result, virt_loc); 3087 if (ret) 3088 { 3089 if (pfile->state.in_directive || ret == 2) 3090 continue; 3091 result = padding_token (pfile, result); 3092 goto out; 3093 } 3094 } 3095 else 3096 { 3097 /* Flag this token as always unexpandable. FIXME: move this 3098 to collect_args()?. */ 3099 cpp_token *t = _cpp_temp_token (pfile); 3100 t->type = result->type; 3101 t->flags = result->flags | NO_EXPAND; 3102 t->val = result->val; 3103 result = t; 3104 } 3105 3106 break; 3107 } 3108 3109 out: 3110 if (location != NULL) 3111 { 3112 if (virt_loc == 0) 3113 virt_loc = result->src_loc; 3114 *location = virt_loc; 3115 3116 if (!CPP_OPTION (pfile, track_macro_expansion) 3117 && macro_of_context (pfile->context) != NULL) 3118 /* We are in a macro expansion context, are not tracking 3119 virtual location, but were asked to report the location 3120 of the expansion point of the macro being expanded. */ 3121 *location = pfile->invocation_location; 3122 3123 *location = maybe_adjust_loc_for_trad_cpp (pfile, *location); 3124 } 3125 3126 pfile->about_to_expand_macro_p = saved_about_to_expand_macro; 3127 3128 if (pfile->state.directive_file_token 3129 && !pfile->state.parsing_args 3130 && !(result->type == CPP_PADDING || result->type == CPP_COMMENT) 3131 && !(15 & --pfile->state.directive_file_token)) 3132 { 3133 /* Do header-name frobbery. Concatenate < ... > as approprate. 3134 Do header search if needed, and finally drop the outer <> or 3135 "". */ 3136 pfile->state.angled_headers = false; 3137 3138 /* Do angle-header reconstitution. Then do include searching. 3139 We'll always end up with a ""-quoted header-name in that 3140 case. If searching finds nothing, we emit a diagnostic and 3141 an empty string. */ 3142 size_t len = 0; 3143 char *fname = NULL; 3144 3145 cpp_token *tmp = _cpp_temp_token (pfile); 3146 *tmp = *result; 3147 3148 tmp->type = CPP_HEADER_NAME; 3149 bool need_search = !pfile->state.directive_file_token; 3150 pfile->state.directive_file_token = 0; 3151 3152 bool angle = result->type != CPP_STRING; 3153 if (result->type == CPP_HEADER_NAME 3154 || (result->type == CPP_STRING && result->val.str.text[0] != 'R')) 3155 { 3156 len = result->val.str.len - 2; 3157 fname = XNEWVEC (char, len + 1); 3158 memcpy (fname, result->val.str.text + 1, len); 3159 fname[len] = 0; 3160 } 3161 else if (result->type == CPP_LESS) 3162 fname = _cpp_bracket_include (pfile); 3163 3164 if (fname) 3165 { 3166 /* We have a header-name. Look it up. This will emit an 3167 unfound diagnostic. Canonicalize the found name. */ 3168 const char *found = fname; 3169 3170 if (need_search) 3171 { 3172 found = _cpp_find_header_unit (pfile, fname, angle, tmp->src_loc); 3173 if (!found) 3174 found = ""; 3175 len = strlen (found); 3176 } 3177 /* Force a leading './' if it's not absolute. */ 3178 bool dotme = (found[0] == '.' ? !IS_DIR_SEPARATOR (found[1]) 3179 : found[0] && !IS_ABSOLUTE_PATH (found)); 3180 3181 if (BUFF_ROOM (pfile->u_buff) < len + 1 + dotme * 2) 3182 _cpp_extend_buff (pfile, &pfile->u_buff, len + 1 + dotme * 2); 3183 unsigned char *buf = BUFF_FRONT (pfile->u_buff); 3184 size_t pos = 0; 3185 3186 if (dotme) 3187 { 3188 buf[pos++] = '.'; 3189 /* Apparently '/' is unconditional. */ 3190 buf[pos++] = '/'; 3191 } 3192 memcpy (&buf[pos], found, len); 3193 pos += len; 3194 buf[pos] = 0; 3195 3196 tmp->val.str.len = pos; 3197 tmp->val.str.text = buf; 3198 3199 tmp->type = CPP_HEADER_NAME; 3200 XDELETEVEC (fname); 3201 3202 result = tmp; 3203 } 3204 } 3205 3206 return result; 3207 } 3208 3209 /* External routine to get a token. Also used nearly everywhere 3210 internally, except for places where we know we can safely call 3211 _cpp_lex_token directly, such as lexing a directive name. 3212 3213 Macro expansions and directives are transparently handled, 3214 including entering included files. Thus tokens are post-macro 3215 expansion, and after any intervening directives. External callers 3216 see CPP_EOF only at EOF. Internal callers also see it when meeting 3217 a directive inside a macro call, when at the end of a directive and 3218 state.in_directive is still 1, and at the end of argument 3219 pre-expansion. */ 3220 const cpp_token * 3221 cpp_get_token (cpp_reader *pfile) 3222 { 3223 return cpp_get_token_1 (pfile, NULL); 3224 } 3225 3226 /* Like cpp_get_token, but also returns a virtual token location 3227 separate from the spelling location carried by the returned token. 3228 3229 LOC is an out parameter; *LOC is set to the location "as expected 3230 by the user". This matters when a token results from macro 3231 expansion; in that case the token's spelling location indicates the 3232 locus of the token in the definition of the macro but *LOC 3233 virtually encodes all the other meaningful locuses associated to 3234 the token. 3235 3236 What? virtual location? Yes, virtual location. 3237 3238 If the token results from macro expansion and if macro expansion 3239 location tracking is enabled its virtual location encodes (at the 3240 same time): 3241 3242 - the spelling location of the token 3243 3244 - the locus of the macro expansion point 3245 3246 - the locus of the point where the token got instantiated as part 3247 of the macro expansion process. 3248 3249 You have to use the linemap API to get the locus you are interested 3250 in from a given virtual location. 3251 3252 Note however that virtual locations are not necessarily ordered for 3253 relations '<' and '>'. One must use the function 3254 linemap_location_before_p instead of using the relational operator 3255 '<'. 3256 3257 If macro expansion tracking is off and if the token results from 3258 macro expansion the virtual location is the expansion point of the 3259 macro that got expanded. 3260 3261 When the token doesn't result from macro expansion, the virtual 3262 location is just the same thing as its spelling location. */ 3263 3264 const cpp_token * 3265 cpp_get_token_with_location (cpp_reader *pfile, location_t *loc) 3266 { 3267 return cpp_get_token_1 (pfile, loc); 3268 } 3269 3270 /* Returns true if we're expanding an object-like macro that was 3271 defined in a system header. Just checks the macro at the top of 3272 the stack. Used for diagnostic suppression. 3273 Also return true for builtin macros. */ 3274 int 3275 cpp_sys_macro_p (cpp_reader *pfile) 3276 { 3277 cpp_hashnode *node = NULL; 3278 3279 if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) 3280 node = pfile->context->c.mc->macro_node; 3281 else 3282 node = pfile->context->c.macro; 3283 3284 if (!node) 3285 return false; 3286 if (cpp_builtin_macro_p (node)) 3287 return true; 3288 return node->value.macro && node->value.macro->syshdr; 3289 } 3290 3291 /* Read each token in, until end of the current file. Directives are 3292 transparently processed. */ 3293 void 3294 cpp_scan_nooutput (cpp_reader *pfile) 3295 { 3296 /* Request a CPP_EOF token at the end of this file, rather than 3297 transparently continuing with the including file. */ 3298 pfile->buffer->return_at_eof = true; 3299 3300 pfile->state.discarding_output++; 3301 pfile->state.prevent_expansion++; 3302 3303 if (CPP_OPTION (pfile, traditional)) 3304 while (_cpp_read_logical_line_trad (pfile)) 3305 ; 3306 else 3307 while (cpp_get_token (pfile)->type != CPP_EOF) 3308 ; 3309 3310 pfile->state.discarding_output--; 3311 pfile->state.prevent_expansion--; 3312 } 3313 3314 /* Step back one or more tokens obtained from the lexer. */ 3315 void 3316 _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count) 3317 { 3318 pfile->lookaheads += count; 3319 while (count--) 3320 { 3321 pfile->cur_token--; 3322 if (pfile->cur_token == pfile->cur_run->base 3323 /* Possible with -fpreprocessed and no leading #line. */ 3324 && pfile->cur_run->prev != NULL) 3325 { 3326 pfile->cur_run = pfile->cur_run->prev; 3327 pfile->cur_token = pfile->cur_run->limit; 3328 } 3329 } 3330 } 3331 3332 /* Step back one (or more) tokens. Can only step back more than 1 if 3333 they are from the lexer, and not from macro expansion. */ 3334 void 3335 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count) 3336 { 3337 if (pfile->context->prev == NULL) 3338 _cpp_backup_tokens_direct (pfile, count); 3339 else 3340 { 3341 if (count != 1) 3342 abort (); 3343 if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT) 3344 FIRST (pfile->context).token--; 3345 else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT) 3346 FIRST (pfile->context).ptoken--; 3347 else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) 3348 { 3349 FIRST (pfile->context).ptoken--; 3350 if (pfile->context->c.macro) 3351 { 3352 macro_context *m = pfile->context->c.mc; 3353 m->cur_virt_loc--; 3354 gcc_checking_assert (m->cur_virt_loc >= m->virt_locs); 3355 } 3356 else 3357 abort (); 3358 } 3359 else 3360 abort (); 3361 } 3362 } 3363 3364 /* #define directive parsing and handling. */ 3365 3366 /* Returns true if a macro redefinition warning is required. */ 3367 static bool 3368 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node, 3369 const cpp_macro *macro2) 3370 { 3371 /* Some redefinitions need to be warned about regardless. */ 3372 if (node->flags & NODE_WARN) 3373 return true; 3374 3375 /* Suppress warnings for builtins that lack the NODE_WARN flag, 3376 unless Wbuiltin-macro-redefined. */ 3377 if (cpp_builtin_macro_p (node)) 3378 return CPP_OPTION (pfile, warn_builtin_macro_redefined); 3379 3380 /* Redefinitions of conditional (context-sensitive) macros, on 3381 the other hand, must be allowed silently. */ 3382 if (node->flags & NODE_CONDITIONAL) 3383 return false; 3384 3385 if (cpp_macro *macro1 = get_deferred_or_lazy_macro (pfile, node, macro2->line)) 3386 return cpp_compare_macros (macro1, macro2); 3387 return false; 3388 } 3389 3390 /* Return TRUE if MACRO1 and MACRO2 differ. */ 3391 3392 bool 3393 cpp_compare_macros (const cpp_macro *macro1, const cpp_macro *macro2) 3394 { 3395 /* Redefinition of a macro is allowed if and only if the old and new 3396 definitions are the same. (6.10.3 paragraph 2). */ 3397 3398 /* Don't check count here as it can be different in valid 3399 traditional redefinitions with just whitespace differences. */ 3400 if (macro1->paramc != macro2->paramc 3401 || macro1->fun_like != macro2->fun_like 3402 || macro1->variadic != macro2->variadic) 3403 return true; 3404 3405 /* Check parameter spellings. */ 3406 for (unsigned i = macro1->paramc; i--; ) 3407 if (macro1->parm.params[i] != macro2->parm.params[i]) 3408 return true; 3409 3410 /* Check the replacement text or tokens. */ 3411 if (macro1->kind == cmk_traditional) 3412 return _cpp_expansions_different_trad (macro1, macro2); 3413 3414 if (macro1->count != macro2->count) 3415 return true; 3416 3417 for (unsigned i= macro1->count; i--; ) 3418 if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i])) 3419 return true; 3420 3421 return false; 3422 } 3423 3424 /* Free the definition of hashnode H. */ 3425 void 3426 _cpp_free_definition (cpp_hashnode *h) 3427 { 3428 /* Macros and assertions no longer have anything to free. */ 3429 h->type = NT_VOID; 3430 h->value.answers = NULL; 3431 h->flags &= ~(NODE_DISABLED | NODE_USED); 3432 } 3433 3434 /* Save parameter NODE (spelling SPELLING) to the parameter list of 3435 macro MACRO. Returns true on success, false on failure. */ 3436 bool 3437 _cpp_save_parameter (cpp_reader *pfile, unsigned n, cpp_hashnode *node, 3438 cpp_hashnode *spelling) 3439 { 3440 /* Constraint 6.10.3.6 - duplicate parameter names. */ 3441 if (node->type == NT_MACRO_ARG) 3442 { 3443 cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"", 3444 NODE_NAME (node)); 3445 return false; 3446 } 3447 3448 unsigned len = (n + 1) * sizeof (struct macro_arg_saved_data); 3449 if (len > pfile->macro_buffer_len) 3450 { 3451 pfile->macro_buffer 3452 = XRESIZEVEC (unsigned char, pfile->macro_buffer, len); 3453 pfile->macro_buffer_len = len; 3454 } 3455 3456 macro_arg_saved_data *saved = (macro_arg_saved_data *)pfile->macro_buffer; 3457 saved[n].canonical_node = node; 3458 saved[n].value = node->value; 3459 saved[n].type = node->type; 3460 3461 void *base = _cpp_reserve_room (pfile, n * sizeof (cpp_hashnode *), 3462 sizeof (cpp_hashnode *)); 3463 ((cpp_hashnode **)base)[n] = spelling; 3464 3465 /* Morph into a macro arg. */ 3466 node->type = NT_MACRO_ARG; 3467 /* Index is 1 based. */ 3468 node->value.arg_index = n + 1; 3469 3470 return true; 3471 } 3472 3473 /* Restore the parameters to their previous state. */ 3474 void 3475 _cpp_unsave_parameters (cpp_reader *pfile, unsigned n) 3476 { 3477 /* Clear the fast argument lookup indices. */ 3478 while (n--) 3479 { 3480 struct macro_arg_saved_data *save = 3481 &((struct macro_arg_saved_data *) pfile->macro_buffer)[n]; 3482 3483 struct cpp_hashnode *node = save->canonical_node; 3484 node->type = save->type; 3485 node->value = save->value; 3486 } 3487 } 3488 3489 /* Check the syntax of the parameters in a MACRO definition. Return 3490 false on failure. Set *N_PTR and *VARADIC_PTR as appropriate. 3491 '(' ')' 3492 '(' parm-list ',' last-parm ')' 3493 '(' last-parm ')' 3494 parm-list: name 3495 | parm-list, name 3496 last-parm: name 3497 | name '...' 3498 | '...' 3499 */ 3500 3501 static bool 3502 parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *variadic_ptr) 3503 { 3504 unsigned nparms = 0; 3505 bool ok = false; 3506 3507 for (bool prev_ident = false;;) 3508 { 3509 const cpp_token *token = _cpp_lex_token (pfile); 3510 3511 switch (token->type) 3512 { 3513 case CPP_COMMENT: 3514 /* Allow/ignore comments in parameter lists if we are 3515 preserving comments in macro expansions. */ 3516 if (!CPP_OPTION (pfile, discard_comments_in_macro_exp)) 3517 break; 3518 3519 /* FALLTHRU */ 3520 default: 3521 bad: 3522 { 3523 const char *const msgs[5] = 3524 { 3525 N_("expected parameter name, found \"%s\""), 3526 N_("expected ',' or ')', found \"%s\""), 3527 N_("expected parameter name before end of line"), 3528 N_("expected ')' before end of line"), 3529 N_("expected ')' after \"...\"") 3530 }; 3531 unsigned ix = prev_ident; 3532 const unsigned char *as_text = NULL; 3533 if (*variadic_ptr) 3534 ix = 4; 3535 else if (token->type == CPP_EOF) 3536 ix += 2; 3537 else 3538 as_text = cpp_token_as_text (pfile, token); 3539 cpp_error (pfile, CPP_DL_ERROR, msgs[ix], as_text); 3540 } 3541 goto out; 3542 3543 case CPP_NAME: 3544 if (prev_ident || *variadic_ptr) 3545 goto bad; 3546 prev_ident = true; 3547 3548 if (!_cpp_save_parameter (pfile, nparms, token->val.node.node, 3549 token->val.node.spelling)) 3550 goto out; 3551 nparms++; 3552 break; 3553 3554 case CPP_CLOSE_PAREN: 3555 if (prev_ident || !nparms || *variadic_ptr) 3556 { 3557 ok = true; 3558 goto out; 3559 } 3560 3561 /* FALLTHRU */ 3562 case CPP_COMMA: 3563 if (!prev_ident || *variadic_ptr) 3564 goto bad; 3565 prev_ident = false; 3566 break; 3567 3568 case CPP_ELLIPSIS: 3569 if (*variadic_ptr) 3570 goto bad; 3571 *variadic_ptr = true; 3572 if (!prev_ident) 3573 { 3574 /* An ISO bare ellipsis. */ 3575 _cpp_save_parameter (pfile, nparms, 3576 pfile->spec_nodes.n__VA_ARGS__, 3577 pfile->spec_nodes.n__VA_ARGS__); 3578 nparms++; 3579 pfile->state.va_args_ok = 1; 3580 if (! CPP_OPTION (pfile, c99) 3581 && CPP_OPTION (pfile, cpp_pedantic) 3582 && CPP_OPTION (pfile, warn_variadic_macros)) 3583 cpp_pedwarning 3584 (pfile, CPP_W_VARIADIC_MACROS, 3585 CPP_OPTION (pfile, cplusplus) 3586 ? N_("anonymous variadic macros were introduced in C++11") 3587 : N_("anonymous variadic macros were introduced in C99")); 3588 else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0 3589 && ! CPP_OPTION (pfile, cplusplus)) 3590 cpp_error (pfile, CPP_DL_WARNING, 3591 "anonymous variadic macros were introduced in C99"); 3592 } 3593 else if (CPP_OPTION (pfile, cpp_pedantic) 3594 && CPP_OPTION (pfile, warn_variadic_macros)) 3595 cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, 3596 CPP_OPTION (pfile, cplusplus) 3597 ? N_("ISO C++ does not permit named variadic macros") 3598 : N_("ISO C does not permit named variadic macros")); 3599 break; 3600 } 3601 } 3602 3603 out: 3604 *n_ptr = nparms; 3605 3606 return ok; 3607 } 3608 3609 /* Lex a token from the expansion of MACRO, but mark parameters as we 3610 find them and warn of traditional stringification. */ 3611 static cpp_macro * 3612 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro) 3613 { 3614 macro = (cpp_macro *)_cpp_reserve_room (pfile, 3615 sizeof (cpp_macro) - sizeof (cpp_token) 3616 + macro->count * sizeof (cpp_token), 3617 sizeof (cpp_token)); 3618 cpp_token *saved_cur_token = pfile->cur_token; 3619 pfile->cur_token = ¯o->exp.tokens[macro->count]; 3620 cpp_token *token = _cpp_lex_direct (pfile); 3621 pfile->cur_token = saved_cur_token; 3622 3623 /* Is this a parameter? */ 3624 if (token->type == CPP_NAME && token->val.node.node->type == NT_MACRO_ARG) 3625 { 3626 /* Morph into a parameter reference. */ 3627 cpp_hashnode *spelling = token->val.node.spelling; 3628 token->type = CPP_MACRO_ARG; 3629 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index; 3630 token->val.macro_arg.spelling = spelling; 3631 } 3632 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 3633 && (token->type == CPP_STRING || token->type == CPP_CHAR)) 3634 check_trad_stringification (pfile, macro, &token->val.str); 3635 3636 return macro; 3637 } 3638 3639 static cpp_macro * 3640 create_iso_definition (cpp_reader *pfile) 3641 { 3642 bool following_paste_op = false; 3643 const char *paste_op_error_msg = 3644 N_("'##' cannot appear at either end of a macro expansion"); 3645 unsigned int num_extra_tokens = 0; 3646 unsigned nparms = 0; 3647 cpp_hashnode **params = NULL; 3648 bool variadic = false; 3649 bool ok = false; 3650 cpp_macro *macro = NULL; 3651 3652 /* Look at the first token, to see if this is a function-like 3653 macro. */ 3654 cpp_token first; 3655 cpp_token *saved_cur_token = pfile->cur_token; 3656 pfile->cur_token = &first; 3657 cpp_token *token = _cpp_lex_direct (pfile); 3658 pfile->cur_token = saved_cur_token; 3659 3660 if (token->flags & PREV_WHITE) 3661 /* Preceeded by space, must be part of expansion. */; 3662 else if (token->type == CPP_OPEN_PAREN) 3663 { 3664 /* An open-paren, get a parameter list. */ 3665 if (!parse_params (pfile, &nparms, &variadic)) 3666 goto out; 3667 3668 params = (cpp_hashnode **)_cpp_commit_buff 3669 (pfile, sizeof (cpp_hashnode *) * nparms); 3670 token = NULL; 3671 } 3672 else if (token->type != CPP_EOF 3673 && !(token->type == CPP_COMMENT 3674 && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))) 3675 { 3676 /* While ISO C99 requires whitespace before replacement text 3677 in a macro definition, ISO C90 with TC1 allows characters 3678 from the basic source character set there. */ 3679 if (CPP_OPTION (pfile, c99)) 3680 cpp_error (pfile, CPP_DL_PEDWARN, 3681 CPP_OPTION (pfile, cplusplus) 3682 ? N_("ISO C++11 requires whitespace after the macro name") 3683 : N_("ISO C99 requires whitespace after the macro name")); 3684 else 3685 { 3686 enum cpp_diagnostic_level warntype = CPP_DL_WARNING; 3687 switch (token->type) 3688 { 3689 case CPP_ATSIGN: 3690 case CPP_AT_NAME: 3691 case CPP_OBJC_STRING: 3692 /* '@' is not in basic character set. */ 3693 warntype = CPP_DL_PEDWARN; 3694 break; 3695 case CPP_OTHER: 3696 /* Basic character set sans letters, digits and _. */ 3697 if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~", 3698 token->val.str.text[0]) == NULL) 3699 warntype = CPP_DL_PEDWARN; 3700 break; 3701 default: 3702 /* All other tokens start with a character from basic 3703 character set. */ 3704 break; 3705 } 3706 cpp_error (pfile, warntype, 3707 "missing whitespace after the macro name"); 3708 } 3709 } 3710 3711 macro = _cpp_new_macro (pfile, cmk_macro, 3712 _cpp_reserve_room (pfile, 0, sizeof (cpp_macro))); 3713 3714 if (!token) 3715 { 3716 macro->variadic = variadic; 3717 macro->paramc = nparms; 3718 macro->parm.params = params; 3719 macro->fun_like = true; 3720 } 3721 else 3722 { 3723 /* Preserve the token we peeked, there is already a single slot for it. */ 3724 macro->exp.tokens[0] = *token; 3725 token = ¯o->exp.tokens[0]; 3726 macro->count = 1; 3727 } 3728 3729 for (vaopt_state vaopt_tracker (pfile, macro->variadic, NULL);; token = NULL) 3730 { 3731 if (!token) 3732 { 3733 macro = lex_expansion_token (pfile, macro); 3734 token = ¯o->exp.tokens[macro->count++]; 3735 } 3736 3737 /* Check the stringifying # constraint 6.10.3.2.1 of 3738 function-like macros when lexing the subsequent token. */ 3739 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) 3740 { 3741 if (token->type == CPP_MACRO_ARG 3742 || (macro->variadic 3743 && token->type == CPP_NAME 3744 && token->val.node.node == pfile->spec_nodes.n__VA_OPT__)) 3745 { 3746 if (token->flags & PREV_WHITE) 3747 token->flags |= SP_PREV_WHITE; 3748 if (token[-1].flags & DIGRAPH) 3749 token->flags |= SP_DIGRAPH; 3750 token->flags &= ~PREV_WHITE; 3751 token->flags |= STRINGIFY_ARG; 3752 token->flags |= token[-1].flags & PREV_WHITE; 3753 token[-1] = token[0]; 3754 macro->count--; 3755 } 3756 /* Let assembler get away with murder. */ 3757 else if (CPP_OPTION (pfile, lang) != CLK_ASM) 3758 { 3759 cpp_error (pfile, CPP_DL_ERROR, 3760 "'#' is not followed by a macro parameter"); 3761 goto out; 3762 } 3763 } 3764 3765 if (token->type == CPP_EOF) 3766 { 3767 /* Paste operator constraint 6.10.3.3.1: 3768 Token-paste ##, can appear in both object-like and 3769 function-like macros, but not at the end. */ 3770 if (following_paste_op) 3771 { 3772 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); 3773 goto out; 3774 } 3775 if (!vaopt_tracker.completed ()) 3776 goto out; 3777 break; 3778 } 3779 3780 /* Paste operator constraint 6.10.3.3.1. */ 3781 if (token->type == CPP_PASTE) 3782 { 3783 /* Token-paste ##, can appear in both object-like and 3784 function-like macros, but not at the beginning. */ 3785 if (macro->count == 1) 3786 { 3787 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); 3788 goto out; 3789 } 3790 3791 if (following_paste_op) 3792 { 3793 /* Consecutive paste operators. This one will be moved 3794 to the end. */ 3795 num_extra_tokens++; 3796 token->val.token_no = macro->count - 1; 3797 } 3798 else 3799 { 3800 /* Drop the paste operator. */ 3801 --macro->count; 3802 token[-1].flags |= PASTE_LEFT; 3803 if (token->flags & DIGRAPH) 3804 token[-1].flags |= SP_DIGRAPH; 3805 if (token->flags & PREV_WHITE) 3806 token[-1].flags |= SP_PREV_WHITE; 3807 } 3808 following_paste_op = true; 3809 } 3810 else 3811 following_paste_op = false; 3812 3813 if (vaopt_tracker.update (token) == vaopt_state::ERROR) 3814 goto out; 3815 } 3816 3817 /* We're committed to winning now. */ 3818 ok = true; 3819 3820 /* Don't count the CPP_EOF. */ 3821 macro->count--; 3822 3823 macro = (cpp_macro *)_cpp_commit_buff 3824 (pfile, sizeof (cpp_macro) - sizeof (cpp_token) 3825 + sizeof (cpp_token) * macro->count); 3826 3827 /* Clear whitespace on first token. */ 3828 if (macro->count) 3829 macro->exp.tokens[0].flags &= ~PREV_WHITE; 3830 3831 if (num_extra_tokens) 3832 { 3833 /* Place second and subsequent ## or %:%: tokens in sequences of 3834 consecutive such tokens at the end of the list to preserve 3835 information about where they appear, how they are spelt and 3836 whether they are preceded by whitespace without otherwise 3837 interfering with macro expansion. Remember, this is 3838 extremely rare, so efficiency is not a priority. */ 3839 cpp_token *temp = (cpp_token *)_cpp_reserve_room 3840 (pfile, 0, num_extra_tokens * sizeof (cpp_token)); 3841 unsigned extra_ix = 0, norm_ix = 0; 3842 cpp_token *exp = macro->exp.tokens; 3843 for (unsigned ix = 0; ix != macro->count; ix++) 3844 if (exp[ix].type == CPP_PASTE) 3845 temp[extra_ix++] = exp[ix]; 3846 else 3847 exp[norm_ix++] = exp[ix]; 3848 memcpy (&exp[norm_ix], temp, num_extra_tokens * sizeof (cpp_token)); 3849 3850 /* Record there are extra tokens. */ 3851 macro->extra_tokens = 1; 3852 } 3853 3854 out: 3855 pfile->state.va_args_ok = 0; 3856 _cpp_unsave_parameters (pfile, nparms); 3857 3858 return ok ? macro : NULL; 3859 } 3860 3861 cpp_macro * 3862 _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement) 3863 { 3864 cpp_macro *macro = (cpp_macro *) placement; 3865 3866 /* Zero init all the fields. This'll tell the compiler know all the 3867 following inits are writing a virgin object. */ 3868 memset (macro, 0, offsetof (cpp_macro, exp)); 3869 3870 macro->line = pfile->directive_line; 3871 macro->parm.params = 0; 3872 macro->lazy = 0; 3873 macro->paramc = 0; 3874 macro->variadic = 0; 3875 macro->used = !CPP_OPTION (pfile, warn_unused_macros); 3876 macro->count = 0; 3877 macro->fun_like = 0; 3878 macro->imported_p = false; 3879 macro->extra_tokens = 0; 3880 /* To suppress some diagnostics. */ 3881 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0; 3882 3883 macro->kind = kind; 3884 3885 return macro; 3886 } 3887 3888 /* Parse a macro and save its expansion. Returns nonzero on success. */ 3889 bool 3890 _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node, 3891 location_t name_loc) 3892 { 3893 cpp_macro *macro; 3894 3895 if (CPP_OPTION (pfile, traditional)) 3896 macro = _cpp_create_trad_definition (pfile); 3897 else 3898 macro = create_iso_definition (pfile); 3899 3900 if (!macro) 3901 return false; 3902 3903 /* _cpp_new_macro () has set macro->line to pfile->directive_line, which 3904 denotes the line containing the #define with no column information. If 3905 provided, change to name_loc, which will be the token src_loc for the 3906 macro name, including the location and range information. */ 3907 if (name_loc) 3908 macro->line = name_loc; 3909 3910 if (cpp_macro_p (node)) 3911 { 3912 if (CPP_OPTION (pfile, warn_unused_macros)) 3913 _cpp_warn_if_unused_macro (pfile, node, NULL); 3914 3915 if (warn_of_redefinition (pfile, node, macro)) 3916 { 3917 const enum cpp_warning_reason reason 3918 = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN)) 3919 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; 3920 3921 bool warned = 3922 cpp_pedwarning_with_line (pfile, reason, 3923 macro->line, 0, 3924 "\"%s\" redefined", NODE_NAME (node)); 3925 3926 if (warned && cpp_user_macro_p (node)) 3927 cpp_error_with_line (pfile, CPP_DL_NOTE, 3928 node->value.macro->line, 0, 3929 "this is the location of the previous definition"); 3930 } 3931 _cpp_free_definition (node); 3932 } 3933 3934 /* Enter definition in hash table. */ 3935 node->type = NT_USER_MACRO; 3936 node->value.macro = macro; 3937 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")) 3938 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS") 3939 /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned 3940 in the C standard, as something that one must use in C++. 3941 However DR#593 and C++11 indicate that they play no role in C++. 3942 We special-case them anyway. */ 3943 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS") 3944 && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS")) 3945 node->flags |= NODE_WARN; 3946 3947 /* If user defines one of the conditional macros, remove the 3948 conditional flag */ 3949 node->flags &= ~NODE_CONDITIONAL; 3950 3951 return true; 3952 } 3953 3954 extern void 3955 cpp_define_lazily (cpp_reader *pfile, cpp_hashnode *node, unsigned num) 3956 { 3957 cpp_macro *macro = node->value.macro; 3958 3959 gcc_checking_assert (pfile->cb.user_lazy_macro && macro && num < UCHAR_MAX); 3960 3961 macro->lazy = num + 1; 3962 } 3963 3964 /* NODE is a deferred macro, resolve it, returning the definition 3965 (which may be NULL). */ 3966 cpp_macro * 3967 cpp_get_deferred_macro (cpp_reader *pfile, cpp_hashnode *node, 3968 location_t loc) 3969 { 3970 gcc_checking_assert (node->type == NT_USER_MACRO); 3971 3972 node->value.macro = pfile->cb.user_deferred_macro (pfile, loc, node); 3973 3974 if (!node->value.macro) 3975 node->type = NT_VOID; 3976 3977 return node->value.macro; 3978 } 3979 3980 static cpp_macro * 3981 get_deferred_or_lazy_macro (cpp_reader *pfile, cpp_hashnode *node, 3982 location_t loc) 3983 { 3984 cpp_macro *macro = node->value.macro; 3985 if (!macro) 3986 { 3987 macro = cpp_get_deferred_macro (pfile, node, loc); 3988 gcc_checking_assert (!macro || !macro->lazy); 3989 } 3990 else if (macro->lazy) 3991 { 3992 pfile->cb.user_lazy_macro (pfile, macro, macro->lazy - 1); 3993 macro->lazy = 0; 3994 } 3995 3996 return macro; 3997 } 3998 3999 /* Notify the use of NODE in a macro-aware context (i.e. expanding it, 4000 or testing its existance). Also applies any lazy definition. 4001 Return FALSE if the macro isn't really there. */ 4002 4003 extern bool 4004 _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node, 4005 location_t loc) 4006 { 4007 node->flags |= NODE_USED; 4008 switch (node->type) 4009 { 4010 case NT_USER_MACRO: 4011 if (!get_deferred_or_lazy_macro (pfile, node, loc)) 4012 return false; 4013 /* FALLTHROUGH. */ 4014 4015 case NT_BUILTIN_MACRO: 4016 if (pfile->cb.used_define) 4017 pfile->cb.used_define (pfile, loc, node); 4018 break; 4019 4020 case NT_VOID: 4021 if (pfile->cb.used_undef) 4022 pfile->cb.used_undef (pfile, loc, node); 4023 break; 4024 4025 default: 4026 abort (); 4027 } 4028 4029 return true; 4030 } 4031 4032 /* Warn if a token in STRING matches one of a function-like MACRO's 4033 parameters. */ 4034 static void 4035 check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro, 4036 const cpp_string *string) 4037 { 4038 unsigned int i, len; 4039 const uchar *p, *q, *limit; 4040 4041 /* Loop over the string. */ 4042 limit = string->text + string->len - 1; 4043 for (p = string->text + 1; p < limit; p = q) 4044 { 4045 /* Find the start of an identifier. */ 4046 while (p < limit && !is_idstart (*p)) 4047 p++; 4048 4049 /* Find the end of the identifier. */ 4050 q = p; 4051 while (q < limit && is_idchar (*q)) 4052 q++; 4053 4054 len = q - p; 4055 4056 /* Loop over the function macro arguments to see if the 4057 identifier inside the string matches one of them. */ 4058 for (i = 0; i < macro->paramc; i++) 4059 { 4060 const cpp_hashnode *node = macro->parm.params[i]; 4061 4062 if (NODE_LEN (node) == len 4063 && !memcmp (p, NODE_NAME (node), len)) 4064 { 4065 cpp_warning (pfile, CPP_W_TRADITIONAL, 4066 "macro argument \"%s\" would be stringified in traditional C", 4067 NODE_NAME (node)); 4068 break; 4069 } 4070 } 4071 } 4072 } 4073 4074 /* Returns the name, arguments and expansion of a macro, in a format 4075 suitable to be read back in again, and therefore also for DWARF 2 4076 debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". 4077 Caller is expected to generate the "#define" bit if needed. The 4078 returned text is temporary, and automatically freed later. */ 4079 const unsigned char * 4080 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node) 4081 { 4082 gcc_checking_assert (cpp_user_macro_p (node)); 4083 4084 if (const cpp_macro *macro = get_deferred_or_lazy_macro (pfile, node, 0)) 4085 return cpp_macro_definition (pfile, node, macro); 4086 return NULL; 4087 } 4088 4089 const unsigned char * 4090 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node, 4091 const cpp_macro *macro) 4092 { 4093 unsigned int i, len; 4094 unsigned char *buffer; 4095 4096 /* Calculate length. */ 4097 len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */ 4098 if (macro->fun_like) 4099 { 4100 len += 4; /* "()" plus possible final ".." of named 4101 varargs (we have + 1 below). */ 4102 for (i = 0; i < macro->paramc; i++) 4103 len += NODE_LEN (macro->parm.params[i]) + 1; /* "," */ 4104 } 4105 4106 /* This should match below where we fill in the buffer. */ 4107 if (CPP_OPTION (pfile, traditional)) 4108 len += _cpp_replacement_text_len (macro); 4109 else 4110 { 4111 unsigned int count = macro_real_token_count (macro); 4112 for (i = 0; i < count; i++) 4113 { 4114 const cpp_token *token = ¯o->exp.tokens[i]; 4115 4116 if (token->type == CPP_MACRO_ARG) 4117 len += NODE_LEN (token->val.macro_arg.spelling); 4118 else 4119 len += cpp_token_len (token); 4120 4121 if (token->flags & STRINGIFY_ARG) 4122 len++; /* "#" */ 4123 if (token->flags & PASTE_LEFT) 4124 len += 3; /* " ##" */ 4125 if (token->flags & PREV_WHITE) 4126 len++; /* " " */ 4127 } 4128 } 4129 4130 if (len > pfile->macro_buffer_len) 4131 { 4132 pfile->macro_buffer = XRESIZEVEC (unsigned char, 4133 pfile->macro_buffer, len); 4134 pfile->macro_buffer_len = len; 4135 } 4136 4137 /* Fill in the buffer. Start with the macro name. */ 4138 buffer = pfile->macro_buffer; 4139 buffer = _cpp_spell_ident_ucns (buffer, node); 4140 4141 /* Parameter names. */ 4142 if (macro->fun_like) 4143 { 4144 *buffer++ = '('; 4145 for (i = 0; i < macro->paramc; i++) 4146 { 4147 cpp_hashnode *param = macro->parm.params[i]; 4148 4149 if (param != pfile->spec_nodes.n__VA_ARGS__) 4150 { 4151 memcpy (buffer, NODE_NAME (param), NODE_LEN (param)); 4152 buffer += NODE_LEN (param); 4153 } 4154 4155 if (i + 1 < macro->paramc) 4156 /* Don't emit a space after the comma here; we're trying 4157 to emit a Dwarf-friendly definition, and the Dwarf spec 4158 forbids spaces in the argument list. */ 4159 *buffer++ = ','; 4160 else if (macro->variadic) 4161 *buffer++ = '.', *buffer++ = '.', *buffer++ = '.'; 4162 } 4163 *buffer++ = ')'; 4164 } 4165 4166 /* The Dwarf spec requires a space after the macro name, even if the 4167 definition is the empty string. */ 4168 *buffer++ = ' '; 4169 4170 if (CPP_OPTION (pfile, traditional)) 4171 buffer = _cpp_copy_replacement_text (macro, buffer); 4172 else if (macro->count) 4173 /* Expansion tokens. */ 4174 { 4175 unsigned int count = macro_real_token_count (macro); 4176 for (i = 0; i < count; i++) 4177 { 4178 const cpp_token *token = ¯o->exp.tokens[i]; 4179 4180 if (token->flags & PREV_WHITE) 4181 *buffer++ = ' '; 4182 if (token->flags & STRINGIFY_ARG) 4183 *buffer++ = '#'; 4184 4185 if (token->type == CPP_MACRO_ARG) 4186 { 4187 memcpy (buffer, 4188 NODE_NAME (token->val.macro_arg.spelling), 4189 NODE_LEN (token->val.macro_arg.spelling)); 4190 buffer += NODE_LEN (token->val.macro_arg.spelling); 4191 } 4192 else 4193 buffer = cpp_spell_token (pfile, token, buffer, true); 4194 4195 if (token->flags & PASTE_LEFT) 4196 { 4197 *buffer++ = ' '; 4198 *buffer++ = '#'; 4199 *buffer++ = '#'; 4200 /* Next has PREV_WHITE; see _cpp_create_definition. */ 4201 } 4202 } 4203 } 4204 4205 *buffer = '\0'; 4206 return pfile->macro_buffer; 4207 } 4208