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