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