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