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