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