1169695Skan/* Part of CPP library. (Macro and #define handling.) 2169695Skan Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, 3258826Spfg 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4258826Spfg 2006 Free Software Foundation, Inc. 5169695Skan Written by Per Bothner, 1994. 6169695Skan Based on CCCP program by Paul Rubin, June 1986 7169695Skan Adapted to ANSI C, Richard Stallman, Jan 1987 8169695Skan 9169695SkanThis program is free software; you can redistribute it and/or modify it 10169695Skanunder the terms of the GNU General Public License as published by the 11169695SkanFree Software Foundation; either version 2, or (at your option) any 12169695Skanlater version. 13169695Skan 14169695SkanThis program is distributed in the hope that it will be useful, 15169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 16169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17169695SkanGNU General Public License for more details. 18169695Skan 19169695SkanYou should have received a copy of the GNU General Public License 20169695Skanalong with this program; if not, write to the Free Software 21169695SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22169695Skan 23169695Skan In other words, you are welcome to use, share and improve this program. 24169695Skan You are forbidden to forbid anyone else to use, share and improve 25169695Skan what you give them. Help stamp out software-hoarding! */ 26169695Skan 27169695Skan#include "config.h" 28169695Skan#include "system.h" 29169695Skan#include "cpplib.h" 30169695Skan#include "internal.h" 31169695Skan 32169695Skantypedef struct macro_arg macro_arg; 33169695Skanstruct macro_arg 34169695Skan{ 35169695Skan const cpp_token **first; /* First token in unexpanded argument. */ 36169695Skan const cpp_token **expanded; /* Macro-expanded argument. */ 37169695Skan const cpp_token *stringified; /* Stringified argument. */ 38169695Skan unsigned int count; /* # of tokens in argument. */ 39169695Skan unsigned int expanded_count; /* # of tokens in expanded argument. */ 40169695Skan}; 41169695Skan 42169695Skan/* Macro expansion. */ 43169695Skan 44169695Skanstatic int enter_macro_context (cpp_reader *, cpp_hashnode *); 45169695Skanstatic int builtin_macro (cpp_reader *, cpp_hashnode *); 46169695Skanstatic void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *, 47169695Skan const cpp_token **, unsigned int); 48169695Skanstatic _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *); 49169695Skanstatic cpp_context *next_context (cpp_reader *); 50169695Skanstatic const cpp_token *padding_token (cpp_reader *, const cpp_token *); 51169695Skanstatic void expand_arg (cpp_reader *, macro_arg *); 52169695Skanstatic const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int); 53169695Skanstatic const cpp_token *stringify_arg (cpp_reader *, macro_arg *); 54169695Skanstatic void paste_all_tokens (cpp_reader *, const cpp_token *); 55169695Skanstatic bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *); 56169695Skanstatic void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *, 57169695Skan macro_arg *); 58169695Skanstatic _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *); 59169695Skanstatic bool create_iso_definition (cpp_reader *, cpp_macro *); 60169695Skan 61169695Skan/* #define directive parsing and handling. */ 62169695Skan 63169695Skanstatic cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *); 64169695Skanstatic cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *); 65169695Skanstatic bool warn_of_redefinition (cpp_reader *, const cpp_hashnode *, 66169695Skan const cpp_macro *); 67169695Skanstatic bool parse_params (cpp_reader *, cpp_macro *); 68169695Skanstatic void check_trad_stringification (cpp_reader *, const cpp_macro *, 69169695Skan const cpp_string *); 70169695Skan 71169695Skan/* Emits a warning if NODE is a macro defined in the main file that 72169695Skan has not been used. */ 73169695Skanint 74169695Skan_cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node, 75169695Skan void *v ATTRIBUTE_UNUSED) 76169695Skan{ 77169695Skan if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 78169695Skan { 79169695Skan cpp_macro *macro = node->value.macro; 80169695Skan 81169695Skan if (!macro->used 82169695Skan && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line))) 83169695Skan cpp_error_with_line (pfile, CPP_DL_WARNING, macro->line, 0, 84169695Skan "macro \"%s\" is not used", NODE_NAME (node)); 85169695Skan } 86169695Skan 87169695Skan return 1; 88169695Skan} 89169695Skan 90169695Skan/* Allocates and returns a CPP_STRING token, containing TEXT of length 91169695Skan LEN, after null-terminating it. TEXT must be in permanent storage. */ 92169695Skanstatic const cpp_token * 93169695Skannew_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len) 94169695Skan{ 95169695Skan cpp_token *token = _cpp_temp_token (pfile); 96169695Skan 97169695Skan text[len] = '\0'; 98169695Skan token->type = CPP_STRING; 99169695Skan token->val.str.len = len; 100169695Skan token->val.str.text = text; 101169695Skan token->flags = 0; 102169695Skan return token; 103169695Skan} 104169695Skan 105169695Skanstatic const char * const monthnames[] = 106169695Skan{ 107169695Skan "Jan", "Feb", "Mar", "Apr", "May", "Jun", 108169695Skan "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 109169695Skan}; 110169695Skan 111169695Skan/* Helper function for builtin_macro. Returns the text generated by 112169695Skan a builtin macro. */ 113169695Skanconst uchar * 114169695Skan_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node) 115169695Skan{ 116169695Skan const struct line_map *map; 117169695Skan const uchar *result = NULL; 118169695Skan unsigned int number = 1; 119169695Skan 120169695Skan switch (node->value.builtin) 121169695Skan { 122169695Skan default: 123169695Skan cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", 124169695Skan NODE_NAME (node)); 125169695Skan break; 126169695Skan 127169695Skan case BT_TIMESTAMP: 128169695Skan { 129169695Skan cpp_buffer *pbuffer = cpp_get_buffer (pfile); 130169695Skan if (pbuffer->timestamp == NULL) 131169695Skan { 132169695Skan /* Initialize timestamp value of the assotiated file. */ 133169695Skan struct _cpp_file *file = cpp_get_file (pbuffer); 134169695Skan if (file) 135169695Skan { 136169695Skan /* Generate __TIMESTAMP__ string, that represents 137169695Skan the date and time of the last modification 138169695Skan of the current source file. The string constant 139169695Skan looks like "Sun Sep 16 01:03:52 1973". */ 140169695Skan struct tm *tb = NULL; 141169695Skan struct stat *st = _cpp_get_file_stat (file); 142169695Skan if (st) 143169695Skan tb = localtime (&st->st_mtime); 144169695Skan if (tb) 145169695Skan { 146169695Skan char *str = asctime (tb); 147169695Skan size_t len = strlen (str); 148169695Skan unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2); 149169695Skan buf[0] = '"'; 150169695Skan strcpy ((char *) buf + 1, str); 151169695Skan buf[len] = '"'; 152169695Skan pbuffer->timestamp = buf; 153169695Skan } 154169695Skan else 155169695Skan { 156169695Skan cpp_errno (pfile, CPP_DL_WARNING, 157169695Skan "could not determine file timestamp"); 158169695Skan pbuffer->timestamp = U"\"??? ??? ?? ??:??:?? ????\""; 159169695Skan } 160169695Skan } 161169695Skan } 162169695Skan result = pbuffer->timestamp; 163169695Skan } 164169695Skan break; 165169695Skan case BT_FILE: 166169695Skan case BT_BASE_FILE: 167169695Skan { 168169695Skan unsigned int len; 169169695Skan const char *name; 170169695Skan uchar *buf; 171169695Skan map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line); 172169695Skan 173169695Skan if (node->value.builtin == BT_BASE_FILE) 174169695Skan while (! MAIN_FILE_P (map)) 175169695Skan map = INCLUDED_FROM (pfile->line_table, map); 176169695Skan 177169695Skan name = map->to_file; 178169695Skan len = strlen (name); 179169695Skan buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); 180169695Skan result = buf; 181169695Skan *buf = '"'; 182169695Skan buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len); 183169695Skan *buf++ = '"'; 184169695Skan *buf = '\0'; 185169695Skan } 186169695Skan break; 187169695Skan 188169695Skan case BT_INCLUDE_LEVEL: 189169695Skan /* The line map depth counts the primary source as level 1, but 190169695Skan historically __INCLUDE_DEPTH__ has called the primary source 191169695Skan level 0. */ 192169695Skan number = pfile->line_table->depth - 1; 193169695Skan break; 194169695Skan 195169695Skan case BT_SPECLINE: 196169695Skan map = &pfile->line_table->maps[pfile->line_table->used-1]; 197169695Skan /* If __LINE__ is embedded in a macro, it must expand to the 198169695Skan line of the macro's invocation, not its definition. 199169695Skan Otherwise things like assert() will not work properly. */ 200169695Skan if (CPP_OPTION (pfile, traditional)) 201169695Skan number = pfile->line_table->highest_line; 202169695Skan else 203169695Skan number = pfile->cur_token[-1].src_loc; 204169695Skan number = SOURCE_LINE (map, number); 205169695Skan break; 206169695Skan 207169695Skan /* __STDC__ has the value 1 under normal circumstances. 208169695Skan However, if (a) we are in a system header, (b) the option 209169695Skan stdc_0_in_system_headers is true (set by target config), and 210169695Skan (c) we are not in strictly conforming mode, then it has the 211169695Skan value 0. (b) and (c) are already checked in cpp_init_builtins. */ 212169695Skan case BT_STDC: 213169695Skan if (cpp_in_system_header (pfile)) 214169695Skan number = 0; 215169695Skan else 216169695Skan number = 1; 217169695Skan break; 218169695Skan 219169695Skan case BT_DATE: 220169695Skan case BT_TIME: 221169695Skan if (pfile->date == NULL) 222169695Skan { 223169695Skan /* Allocate __DATE__ and __TIME__ strings from permanent 224169695Skan storage. We only do this once, and don't generate them 225169695Skan at init time, because time() and localtime() are very 226169695Skan slow on some systems. */ 227169695Skan time_t tt; 228169695Skan struct tm *tb = NULL; 229169695Skan 230169695Skan /* (time_t) -1 is a legitimate value for "number of seconds 231169695Skan since the Epoch", so we have to do a little dance to 232169695Skan distinguish that from a genuine error. */ 233169695Skan errno = 0; 234169695Skan tt = time(NULL); 235169695Skan if (tt != (time_t)-1 || errno == 0) 236169695Skan tb = localtime (&tt); 237169695Skan 238169695Skan if (tb) 239169695Skan { 240169695Skan pfile->date = _cpp_unaligned_alloc (pfile, 241169695Skan sizeof ("\"Oct 11 1347\"")); 242169695Skan sprintf ((char *) pfile->date, "\"%s %2d %4d\"", 243169695Skan monthnames[tb->tm_mon], tb->tm_mday, 244169695Skan tb->tm_year + 1900); 245169695Skan 246169695Skan pfile->time = _cpp_unaligned_alloc (pfile, 247169695Skan sizeof ("\"12:34:56\"")); 248169695Skan sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"", 249169695Skan tb->tm_hour, tb->tm_min, tb->tm_sec); 250169695Skan } 251169695Skan else 252169695Skan { 253169695Skan cpp_errno (pfile, CPP_DL_WARNING, 254169695Skan "could not determine date and time"); 255169695Skan 256169695Skan pfile->date = U"\"??? ?? ????\""; 257169695Skan pfile->time = U"\"??:??:??\""; 258169695Skan } 259169695Skan } 260169695Skan 261169695Skan if (node->value.builtin == BT_DATE) 262169695Skan result = pfile->date; 263169695Skan else 264169695Skan result = pfile->time; 265169695Skan break; 266228474Sed 267228474Sed case BT_COUNTER: 268258501Spfg if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) 269258501Spfg cpp_error (pfile, CPP_DL_ERROR, 270258501Spfg "__COUNTER__ expanded inside directive with -fdirectives-only"); 271228474Sed number = pfile->nextcounter++; 272228474Sed break; 273169695Skan } 274169695Skan 275169695Skan if (result == NULL) 276169695Skan { 277169695Skan /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */ 278169695Skan result = _cpp_unaligned_alloc (pfile, 21); 279169695Skan sprintf ((char *) result, "%u", number); 280169695Skan } 281169695Skan 282169695Skan return result; 283169695Skan} 284169695Skan 285169695Skan/* Convert builtin macros like __FILE__ to a token and push it on the 286169695Skan context stack. Also handles _Pragma, for which a new token may not 287169695Skan be created. Returns 1 if it generates a new token context, 0 to 288169695Skan return the token to the caller. */ 289169695Skanstatic int 290169695Skanbuiltin_macro (cpp_reader *pfile, cpp_hashnode *node) 291169695Skan{ 292169695Skan const uchar *buf; 293169695Skan size_t len; 294169695Skan char *nbuf; 295169695Skan 296169695Skan if (node->value.builtin == BT_PRAGMA) 297169695Skan { 298169695Skan /* Don't interpret _Pragma within directives. The standard is 299169695Skan not clear on this, but to me this makes most sense. */ 300169695Skan if (pfile->state.in_directive) 301169695Skan return 0; 302169695Skan 303169695Skan _cpp_do__Pragma (pfile); 304169695Skan return 1; 305169695Skan } 306169695Skan 307169695Skan buf = _cpp_builtin_macro_text (pfile, node); 308169695Skan len = ustrlen (buf); 309169695Skan nbuf = (char *) alloca (len + 1); 310169695Skan memcpy (nbuf, buf, len); 311169695Skan nbuf[len]='\n'; 312169695Skan 313169695Skan cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true); 314169695Skan _cpp_clean_line (pfile); 315169695Skan 316169695Skan /* Set pfile->cur_token as required by _cpp_lex_direct. */ 317169695Skan pfile->cur_token = _cpp_temp_token (pfile); 318169695Skan _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1); 319169695Skan if (pfile->buffer->cur != pfile->buffer->rlimit) 320169695Skan cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", 321169695Skan NODE_NAME (node)); 322169695Skan _cpp_pop_buffer (pfile); 323169695Skan 324169695Skan return 1; 325169695Skan} 326169695Skan 327169695Skan/* Copies SRC, of length LEN, to DEST, adding backslashes before all 328169695Skan backslashes and double quotes. DEST must be of sufficient size. 329169695Skan Returns a pointer to the end of the string. */ 330169695Skanuchar * 331169695Skancpp_quote_string (uchar *dest, const uchar *src, unsigned int len) 332169695Skan{ 333169695Skan while (len--) 334169695Skan { 335169695Skan uchar c = *src++; 336169695Skan 337169695Skan if (c == '\\' || c == '"') 338169695Skan { 339169695Skan *dest++ = '\\'; 340169695Skan *dest++ = c; 341169695Skan } 342169695Skan else 343169695Skan *dest++ = c; 344169695Skan } 345169695Skan 346169695Skan return dest; 347169695Skan} 348169695Skan 349169695Skan/* Convert a token sequence ARG to a single string token according to 350169695Skan the rules of the ISO C #-operator. */ 351169695Skanstatic const cpp_token * 352169695Skanstringify_arg (cpp_reader *pfile, macro_arg *arg) 353169695Skan{ 354169695Skan unsigned char *dest; 355169695Skan unsigned int i, escape_it, backslash_count = 0; 356169695Skan const cpp_token *source = NULL; 357169695Skan size_t len; 358169695Skan 359169695Skan if (BUFF_ROOM (pfile->u_buff) < 3) 360169695Skan _cpp_extend_buff (pfile, &pfile->u_buff, 3); 361169695Skan dest = BUFF_FRONT (pfile->u_buff); 362169695Skan *dest++ = '"'; 363169695Skan 364169695Skan /* Loop, reading in the argument's tokens. */ 365169695Skan for (i = 0; i < arg->count; i++) 366169695Skan { 367169695Skan const cpp_token *token = arg->first[i]; 368169695Skan 369169695Skan if (token->type == CPP_PADDING) 370169695Skan { 371169695Skan if (source == NULL) 372169695Skan source = token->val.source; 373169695Skan continue; 374169695Skan } 375169695Skan 376169695Skan escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING 377169695Skan || token->type == CPP_CHAR || token->type == CPP_WCHAR); 378169695Skan 379169695Skan /* Room for each char being written in octal, initial space and 380169695Skan final quote and NUL. */ 381169695Skan len = cpp_token_len (token); 382169695Skan if (escape_it) 383169695Skan len *= 4; 384169695Skan len += 3; 385169695Skan 386169695Skan if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len) 387169695Skan { 388169695Skan size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff); 389169695Skan _cpp_extend_buff (pfile, &pfile->u_buff, len); 390169695Skan dest = BUFF_FRONT (pfile->u_buff) + len_so_far; 391169695Skan } 392169695Skan 393169695Skan /* Leading white space? */ 394169695Skan if (dest - 1 != BUFF_FRONT (pfile->u_buff)) 395169695Skan { 396169695Skan if (source == NULL) 397169695Skan source = token; 398169695Skan if (source->flags & PREV_WHITE) 399169695Skan *dest++ = ' '; 400169695Skan } 401169695Skan source = NULL; 402169695Skan 403169695Skan if (escape_it) 404169695Skan { 405169695Skan _cpp_buff *buff = _cpp_get_buff (pfile, len); 406169695Skan unsigned char *buf = BUFF_FRONT (buff); 407169695Skan len = cpp_spell_token (pfile, token, buf, true) - buf; 408169695Skan dest = cpp_quote_string (dest, buf, len); 409169695Skan _cpp_release_buff (pfile, buff); 410169695Skan } 411169695Skan else 412169695Skan dest = cpp_spell_token (pfile, token, dest, true); 413169695Skan 414169695Skan if (token->type == CPP_OTHER && token->val.str.text[0] == '\\') 415169695Skan backslash_count++; 416169695Skan else 417169695Skan backslash_count = 0; 418169695Skan } 419169695Skan 420169695Skan /* Ignore the final \ of invalid string literals. */ 421169695Skan if (backslash_count & 1) 422169695Skan { 423169695Skan cpp_error (pfile, CPP_DL_WARNING, 424169695Skan "invalid string literal, ignoring final '\\'"); 425169695Skan dest--; 426169695Skan } 427169695Skan 428169695Skan /* Commit the memory, including NUL, and return the token. */ 429169695Skan *dest++ = '"'; 430169695Skan len = dest - BUFF_FRONT (pfile->u_buff); 431169695Skan BUFF_FRONT (pfile->u_buff) = dest + 1; 432169695Skan return new_string_token (pfile, dest - len, len); 433169695Skan} 434169695Skan 435169695Skan/* Try to paste two tokens. On success, return nonzero. In any 436169695Skan case, PLHS is updated to point to the pasted token, which is 437169695Skan guaranteed to not have the PASTE_LEFT flag set. */ 438169695Skanstatic bool 439169695Skanpaste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs) 440169695Skan{ 441169695Skan unsigned char *buf, *end, *lhsend; 442258826Spfg cpp_token *lhs; 443169695Skan unsigned int len; 444169695Skan 445258826Spfg len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1; 446169695Skan buf = (unsigned char *) alloca (len); 447258826Spfg end = lhsend = cpp_spell_token (pfile, *plhs, buf, false); 448169695Skan 449169695Skan /* Avoid comment headers, since they are still processed in stage 3. 450169695Skan It is simpler to insert a space here, rather than modifying the 451169695Skan lexer to ignore comments in some circumstances. Simply returning 452169695Skan false doesn't work, since we want to clear the PASTE_LEFT flag. */ 453258826Spfg if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) 454169695Skan *end++ = ' '; 455169695Skan end = cpp_spell_token (pfile, rhs, end, false); 456169695Skan *end = '\n'; 457169695Skan 458169695Skan cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true); 459169695Skan _cpp_clean_line (pfile); 460169695Skan 461169695Skan /* Set pfile->cur_token as required by _cpp_lex_direct. */ 462169695Skan pfile->cur_token = _cpp_temp_token (pfile); 463258826Spfg lhs = _cpp_lex_direct (pfile); 464169695Skan if (pfile->buffer->cur != pfile->buffer->rlimit) 465169695Skan { 466258826Spfg source_location saved_loc = lhs->src_loc; 467258826Spfg 468169695Skan _cpp_pop_buffer (pfile); 469169695Skan _cpp_backup_tokens (pfile, 1); 470169695Skan *lhsend = '\0'; 471169695Skan 472258826Spfg /* We have to remove the PASTE_LEFT flag from the old lhs, but 473258826Spfg we want to keep the new location. */ 474258826Spfg *lhs = **plhs; 475258826Spfg *plhs = lhs; 476258826Spfg lhs->src_loc = saved_loc; 477258826Spfg lhs->flags &= ~PASTE_LEFT; 478258826Spfg 479169695Skan /* Mandatory error for all apart from assembler. */ 480169695Skan if (CPP_OPTION (pfile, lang) != CLK_ASM) 481169695Skan cpp_error (pfile, CPP_DL_ERROR, 482169695Skan "pasting \"%s\" and \"%s\" does not give a valid preprocessing token", 483169695Skan buf, cpp_token_as_text (pfile, rhs)); 484169695Skan return false; 485169695Skan } 486169695Skan 487258826Spfg *plhs = lhs; 488169695Skan _cpp_pop_buffer (pfile); 489169695Skan return true; 490169695Skan} 491169695Skan 492169695Skan/* Handles an arbitrarily long sequence of ## operators, with initial 493169695Skan operand LHS. This implementation is left-associative, 494169695Skan non-recursive, and finishes a paste before handling succeeding 495169695Skan ones. If a paste fails, we back up to the RHS of the failing ## 496169695Skan operator before pushing the context containing the result of prior 497169695Skan successful pastes, with the effect that the RHS appears in the 498169695Skan output stream after the pasted LHS normally. */ 499169695Skanstatic void 500169695Skanpaste_all_tokens (cpp_reader *pfile, const cpp_token *lhs) 501169695Skan{ 502169695Skan const cpp_token *rhs; 503169695Skan cpp_context *context = pfile->context; 504169695Skan 505169695Skan do 506169695Skan { 507169695Skan /* Take the token directly from the current context. We can do 508169695Skan this, because we are in the replacement list of either an 509169695Skan object-like macro, or a function-like macro with arguments 510169695Skan inserted. In either case, the constraints to #define 511169695Skan guarantee we have at least one more token. */ 512169695Skan if (context->direct_p) 513169695Skan rhs = FIRST (context).token++; 514169695Skan else 515169695Skan rhs = *FIRST (context).ptoken++; 516169695Skan 517169695Skan if (rhs->type == CPP_PADDING) 518169695Skan abort (); 519169695Skan 520169695Skan if (!paste_tokens (pfile, &lhs, rhs)) 521169695Skan break; 522169695Skan } 523169695Skan while (rhs->flags & PASTE_LEFT); 524169695Skan 525169695Skan /* Put the resulting token in its own context. */ 526169695Skan _cpp_push_token_context (pfile, NULL, lhs, 1); 527169695Skan} 528169695Skan 529169695Skan/* Returns TRUE if the number of arguments ARGC supplied in an 530169695Skan invocation of the MACRO referenced by NODE is valid. An empty 531169695Skan invocation to a macro with no parameters should pass ARGC as zero. 532169695Skan 533169695Skan Note that MACRO cannot necessarily be deduced from NODE, in case 534169695Skan NODE was redefined whilst collecting arguments. */ 535169695Skanbool 536169695Skan_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc) 537169695Skan{ 538169695Skan if (argc == macro->paramc) 539169695Skan return true; 540169695Skan 541169695Skan if (argc < macro->paramc) 542169695Skan { 543169695Skan /* As an extension, a rest argument is allowed to not appear in 544169695Skan the invocation at all. 545169695Skan e.g. #define debug(format, args...) something 546169695Skan debug("string"); 547169695Skan 548169695Skan This is exactly the same as if there had been an empty rest 549169695Skan argument - debug("string", ). */ 550169695Skan 551169695Skan if (argc + 1 == macro->paramc && macro->variadic) 552169695Skan { 553169695Skan if (CPP_PEDANTIC (pfile) && ! macro->syshdr) 554169695Skan cpp_error (pfile, CPP_DL_PEDWARN, 555169695Skan "ISO C99 requires rest arguments to be used"); 556169695Skan return true; 557169695Skan } 558169695Skan 559169695Skan cpp_error (pfile, CPP_DL_ERROR, 560169695Skan "macro \"%s\" requires %u arguments, but only %u given", 561169695Skan NODE_NAME (node), macro->paramc, argc); 562169695Skan } 563169695Skan else 564169695Skan cpp_error (pfile, CPP_DL_ERROR, 565169695Skan "macro \"%s\" passed %u arguments, but takes just %u", 566169695Skan NODE_NAME (node), argc, macro->paramc); 567169695Skan 568169695Skan return false; 569169695Skan} 570169695Skan 571169695Skan/* Reads and returns the arguments to a function-like macro 572169695Skan invocation. Assumes the opening parenthesis has been processed. 573169695Skan If there is an error, emits an appropriate diagnostic and returns 574169695Skan NULL. Each argument is terminated by a CPP_EOF token, for the 575169695Skan future benefit of expand_arg(). */ 576169695Skanstatic _cpp_buff * 577169695Skancollect_args (cpp_reader *pfile, const cpp_hashnode *node) 578169695Skan{ 579169695Skan _cpp_buff *buff, *base_buff; 580169695Skan cpp_macro *macro; 581169695Skan macro_arg *args, *arg; 582169695Skan const cpp_token *token; 583169695Skan unsigned int argc; 584169695Skan 585169695Skan macro = node->value.macro; 586169695Skan if (macro->paramc) 587169695Skan argc = macro->paramc; 588169695Skan else 589169695Skan argc = 1; 590169695Skan buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *) 591169695Skan + sizeof (macro_arg))); 592169695Skan base_buff = buff; 593169695Skan args = (macro_arg *) buff->base; 594169695Skan memset (args, 0, argc * sizeof (macro_arg)); 595169695Skan buff->cur = (unsigned char *) &args[argc]; 596169695Skan arg = args, argc = 0; 597169695Skan 598169695Skan /* Collect the tokens making up each argument. We don't yet know 599169695Skan how many arguments have been supplied, whether too many or too 600169695Skan few. Hence the slightly bizarre usage of "argc" and "arg". */ 601169695Skan do 602169695Skan { 603169695Skan unsigned int paren_depth = 0; 604169695Skan unsigned int ntokens = 0; 605169695Skan 606169695Skan argc++; 607169695Skan arg->first = (const cpp_token **) buff->cur; 608169695Skan 609169695Skan for (;;) 610169695Skan { 611169695Skan /* Require space for 2 new tokens (including a CPP_EOF). */ 612169695Skan if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit) 613169695Skan { 614169695Skan buff = _cpp_append_extend_buff (pfile, buff, 615169695Skan 1000 * sizeof (cpp_token *)); 616169695Skan arg->first = (const cpp_token **) buff->cur; 617169695Skan } 618169695Skan 619169695Skan token = cpp_get_token (pfile); 620169695Skan 621169695Skan if (token->type == CPP_PADDING) 622169695Skan { 623169695Skan /* Drop leading padding. */ 624169695Skan if (ntokens == 0) 625169695Skan continue; 626169695Skan } 627169695Skan else if (token->type == CPP_OPEN_PAREN) 628169695Skan paren_depth++; 629169695Skan else if (token->type == CPP_CLOSE_PAREN) 630169695Skan { 631169695Skan if (paren_depth-- == 0) 632169695Skan break; 633169695Skan } 634169695Skan else if (token->type == CPP_COMMA) 635169695Skan { 636169695Skan /* A comma does not terminate an argument within 637169695Skan parentheses or as part of a variable argument. */ 638169695Skan if (paren_depth == 0 639169695Skan && ! (macro->variadic && argc == macro->paramc)) 640169695Skan break; 641169695Skan } 642169695Skan else if (token->type == CPP_EOF 643169695Skan || (token->type == CPP_HASH && token->flags & BOL)) 644169695Skan break; 645169695Skan 646169695Skan arg->first[ntokens++] = token; 647169695Skan } 648169695Skan 649169695Skan /* Drop trailing padding. */ 650169695Skan while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING) 651169695Skan ntokens--; 652169695Skan 653169695Skan arg->count = ntokens; 654169695Skan arg->first[ntokens] = &pfile->eof; 655169695Skan 656169695Skan /* Terminate the argument. Excess arguments loop back and 657169695Skan overwrite the final legitimate argument, before failing. */ 658169695Skan if (argc <= macro->paramc) 659169695Skan { 660169695Skan buff->cur = (unsigned char *) &arg->first[ntokens + 1]; 661169695Skan if (argc != macro->paramc) 662169695Skan arg++; 663169695Skan } 664169695Skan } 665169695Skan while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF); 666169695Skan 667169695Skan if (token->type == CPP_EOF) 668169695Skan { 669169695Skan /* We still need the CPP_EOF to end directives, and to end 670169695Skan pre-expansion of a macro argument. Step back is not 671169695Skan unconditional, since we don't want to return a CPP_EOF to our 672169695Skan callers at the end of an -include-d file. */ 673169695Skan if (pfile->context->prev || pfile->state.in_directive) 674169695Skan _cpp_backup_tokens (pfile, 1); 675169695Skan cpp_error (pfile, CPP_DL_ERROR, 676169695Skan "unterminated argument list invoking macro \"%s\"", 677169695Skan NODE_NAME (node)); 678169695Skan } 679169695Skan else 680169695Skan { 681169695Skan /* A single empty argument is counted as no argument. */ 682169695Skan if (argc == 1 && macro->paramc == 0 && args[0].count == 0) 683169695Skan argc = 0; 684169695Skan if (_cpp_arguments_ok (pfile, macro, node, argc)) 685169695Skan { 686169695Skan /* GCC has special semantics for , ## b where b is a varargs 687169695Skan parameter: we remove the comma if b was omitted entirely. 688169695Skan If b was merely an empty argument, the comma is retained. 689169695Skan If the macro takes just one (varargs) parameter, then we 690169695Skan retain the comma only if we are standards conforming. 691169695Skan 692169695Skan If FIRST is NULL replace_args () swallows the comma. */ 693169695Skan if (macro->variadic && (argc < macro->paramc 694169695Skan || (argc == 1 && args[0].count == 0 695169695Skan && !CPP_OPTION (pfile, std)))) 696169695Skan args[macro->paramc - 1].first = NULL; 697169695Skan return base_buff; 698169695Skan } 699169695Skan } 700169695Skan 701169695Skan /* An error occurred. */ 702169695Skan _cpp_release_buff (pfile, base_buff); 703169695Skan return NULL; 704169695Skan} 705169695Skan 706169695Skan/* Search for an opening parenthesis to the macro of NODE, in such a 707169695Skan way that, if none is found, we don't lose the information in any 708169695Skan intervening padding tokens. If we find the parenthesis, collect 709169695Skan the arguments and return the buffer containing them. */ 710169695Skanstatic _cpp_buff * 711169695Skanfunlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node) 712169695Skan{ 713169695Skan const cpp_token *token, *padding = NULL; 714169695Skan 715169695Skan for (;;) 716169695Skan { 717169695Skan token = cpp_get_token (pfile); 718169695Skan if (token->type != CPP_PADDING) 719169695Skan break; 720169695Skan if (padding == NULL 721169695Skan || (!(padding->flags & PREV_WHITE) && token->val.source == NULL)) 722169695Skan padding = token; 723169695Skan } 724169695Skan 725169695Skan if (token->type == CPP_OPEN_PAREN) 726169695Skan { 727169695Skan pfile->state.parsing_args = 2; 728169695Skan return collect_args (pfile, node); 729169695Skan } 730169695Skan 731169695Skan /* CPP_EOF can be the end of macro arguments, or the end of the 732169695Skan file. We mustn't back up over the latter. Ugh. */ 733169695Skan if (token->type != CPP_EOF || token == &pfile->eof) 734169695Skan { 735169695Skan /* Back up. We may have skipped padding, in which case backing 736169695Skan up more than one token when expanding macros is in general 737169695Skan too difficult. We re-insert it in its own context. */ 738169695Skan _cpp_backup_tokens (pfile, 1); 739169695Skan if (padding) 740169695Skan _cpp_push_token_context (pfile, NULL, padding, 1); 741169695Skan } 742169695Skan 743169695Skan return NULL; 744169695Skan} 745169695Skan 746169695Skan/* Push the context of a macro with hash entry NODE onto the context 747169695Skan stack. If we can successfully expand the macro, we push a context 748169695Skan containing its yet-to-be-rescanned replacement list and return one. 749169695Skan Otherwise, we don't push a context and return zero. */ 750169695Skanstatic int 751169695Skanenter_macro_context (cpp_reader *pfile, cpp_hashnode *node) 752169695Skan{ 753169695Skan /* The presence of a macro invalidates a file's controlling macro. */ 754169695Skan pfile->mi_valid = false; 755169695Skan 756169695Skan pfile->state.angled_headers = false; 757169695Skan 758169695Skan /* Handle standard macros. */ 759169695Skan if (! (node->flags & NODE_BUILTIN)) 760169695Skan { 761169695Skan cpp_macro *macro = node->value.macro; 762169695Skan 763169695Skan if (macro->fun_like) 764169695Skan { 765169695Skan _cpp_buff *buff; 766169695Skan 767169695Skan pfile->state.prevent_expansion++; 768169695Skan pfile->keep_tokens++; 769169695Skan pfile->state.parsing_args = 1; 770169695Skan buff = funlike_invocation_p (pfile, node); 771169695Skan pfile->state.parsing_args = 0; 772169695Skan pfile->keep_tokens--; 773169695Skan pfile->state.prevent_expansion--; 774169695Skan 775169695Skan if (buff == NULL) 776169695Skan { 777169695Skan if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr) 778169695Skan cpp_error (pfile, CPP_DL_WARNING, 779169695Skan "function-like macro \"%s\" must be used with arguments in traditional C", 780169695Skan NODE_NAME (node)); 781169695Skan 782169695Skan return 0; 783169695Skan } 784169695Skan 785169695Skan if (macro->paramc > 0) 786169695Skan replace_args (pfile, node, macro, (macro_arg *) buff->base); 787169695Skan _cpp_release_buff (pfile, buff); 788169695Skan } 789169695Skan 790169695Skan /* Disable the macro within its expansion. */ 791169695Skan node->flags |= NODE_DISABLED; 792169695Skan 793169695Skan macro->used = 1; 794169695Skan 795169695Skan if (macro->paramc == 0) 796169695Skan _cpp_push_token_context (pfile, node, macro->exp.tokens, macro->count); 797169695Skan 798169695Skan return 1; 799169695Skan } 800169695Skan 801169695Skan /* Handle built-in macros and the _Pragma operator. */ 802169695Skan return builtin_macro (pfile, node); 803169695Skan} 804169695Skan 805169695Skan/* Replace the parameters in a function-like macro of NODE with the 806169695Skan actual ARGS, and place the result in a newly pushed token context. 807169695Skan Expand each argument before replacing, unless it is operated upon 808169695Skan by the # or ## operators. */ 809169695Skanstatic void 810169695Skanreplace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args) 811169695Skan{ 812169695Skan unsigned int i, total; 813169695Skan const cpp_token *src, *limit; 814169695Skan const cpp_token **dest, **first; 815169695Skan macro_arg *arg; 816169695Skan _cpp_buff *buff; 817169695Skan 818169695Skan /* First, fully macro-expand arguments, calculating the number of 819169695Skan tokens in the final expansion as we go. The ordering of the if 820169695Skan statements below is subtle; we must handle stringification before 821169695Skan pasting. */ 822169695Skan total = macro->count; 823169695Skan limit = macro->exp.tokens + macro->count; 824169695Skan 825169695Skan for (src = macro->exp.tokens; src < limit; src++) 826169695Skan if (src->type == CPP_MACRO_ARG) 827169695Skan { 828169695Skan /* Leading and trailing padding tokens. */ 829169695Skan total += 2; 830169695Skan 831169695Skan /* We have an argument. If it is not being stringified or 832169695Skan pasted it is macro-replaced before insertion. */ 833169695Skan arg = &args[src->val.arg_no - 1]; 834169695Skan 835169695Skan if (src->flags & STRINGIFY_ARG) 836169695Skan { 837169695Skan if (!arg->stringified) 838169695Skan arg->stringified = stringify_arg (pfile, arg); 839169695Skan } 840169695Skan else if ((src->flags & PASTE_LEFT) 841169695Skan || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT))) 842169695Skan total += arg->count - 1; 843169695Skan else 844169695Skan { 845169695Skan if (!arg->expanded) 846169695Skan expand_arg (pfile, arg); 847169695Skan total += arg->expanded_count - 1; 848169695Skan } 849169695Skan } 850169695Skan 851169695Skan /* Now allocate space for the expansion, copy the tokens and replace 852169695Skan the arguments. */ 853169695Skan buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *)); 854169695Skan first = (const cpp_token **) buff->base; 855169695Skan dest = first; 856169695Skan 857169695Skan for (src = macro->exp.tokens; src < limit; src++) 858169695Skan { 859169695Skan unsigned int count; 860169695Skan const cpp_token **from, **paste_flag; 861169695Skan 862169695Skan if (src->type != CPP_MACRO_ARG) 863169695Skan { 864169695Skan *dest++ = src; 865169695Skan continue; 866169695Skan } 867169695Skan 868169695Skan paste_flag = 0; 869169695Skan arg = &args[src->val.arg_no - 1]; 870169695Skan if (src->flags & STRINGIFY_ARG) 871169695Skan count = 1, from = &arg->stringified; 872169695Skan else if (src->flags & PASTE_LEFT) 873169695Skan count = arg->count, from = arg->first; 874169695Skan else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)) 875169695Skan { 876169695Skan count = arg->count, from = arg->first; 877169695Skan if (dest != first) 878169695Skan { 879169695Skan if (dest[-1]->type == CPP_COMMA 880169695Skan && macro->variadic 881169695Skan && src->val.arg_no == macro->paramc) 882169695Skan { 883169695Skan /* Swallow a pasted comma if from == NULL, otherwise 884169695Skan drop the paste flag. */ 885169695Skan if (from == NULL) 886169695Skan dest--; 887169695Skan else 888169695Skan paste_flag = dest - 1; 889169695Skan } 890169695Skan /* Remove the paste flag if the RHS is a placemarker. */ 891169695Skan else if (count == 0) 892169695Skan paste_flag = dest - 1; 893169695Skan } 894169695Skan } 895169695Skan else 896169695Skan count = arg->expanded_count, from = arg->expanded; 897169695Skan 898169695Skan /* Padding on the left of an argument (unless RHS of ##). */ 899169695Skan if ((!pfile->state.in_directive || pfile->state.directive_wants_padding) 900169695Skan && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)) 901169695Skan *dest++ = padding_token (pfile, src); 902169695Skan 903169695Skan if (count) 904169695Skan { 905169695Skan memcpy (dest, from, count * sizeof (cpp_token *)); 906169695Skan dest += count; 907169695Skan 908169695Skan /* With a non-empty argument on the LHS of ##, the last 909169695Skan token should be flagged PASTE_LEFT. */ 910169695Skan if (src->flags & PASTE_LEFT) 911169695Skan paste_flag = dest - 1; 912169695Skan } 913169695Skan 914169695Skan /* Avoid paste on RHS (even case count == 0). */ 915169695Skan if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) 916169695Skan *dest++ = &pfile->avoid_paste; 917169695Skan 918169695Skan /* Add a new paste flag, or remove an unwanted one. */ 919169695Skan if (paste_flag) 920169695Skan { 921169695Skan cpp_token *token = _cpp_temp_token (pfile); 922169695Skan token->type = (*paste_flag)->type; 923169695Skan token->val = (*paste_flag)->val; 924169695Skan if (src->flags & PASTE_LEFT) 925169695Skan token->flags = (*paste_flag)->flags | PASTE_LEFT; 926169695Skan else 927169695Skan token->flags = (*paste_flag)->flags & ~PASTE_LEFT; 928169695Skan *paste_flag = token; 929169695Skan } 930169695Skan } 931169695Skan 932169695Skan /* Free the expanded arguments. */ 933169695Skan for (i = 0; i < macro->paramc; i++) 934169695Skan if (args[i].expanded) 935169695Skan free (args[i].expanded); 936169695Skan 937169695Skan push_ptoken_context (pfile, node, buff, first, dest - first); 938169695Skan} 939169695Skan 940169695Skan/* Return a special padding token, with padding inherited from SOURCE. */ 941169695Skanstatic const cpp_token * 942169695Skanpadding_token (cpp_reader *pfile, const cpp_token *source) 943169695Skan{ 944169695Skan cpp_token *result = _cpp_temp_token (pfile); 945169695Skan 946169695Skan result->type = CPP_PADDING; 947169695Skan 948169695Skan /* Data in GCed data structures cannot be made const so far, so we 949169695Skan need a cast here. */ 950169695Skan result->val.source = (cpp_token *) source; 951169695Skan result->flags = 0; 952169695Skan return result; 953169695Skan} 954169695Skan 955169695Skan/* Get a new uninitialized context. Create a new one if we cannot 956169695Skan re-use an old one. */ 957169695Skanstatic cpp_context * 958169695Skannext_context (cpp_reader *pfile) 959169695Skan{ 960169695Skan cpp_context *result = pfile->context->next; 961169695Skan 962169695Skan if (result == 0) 963169695Skan { 964169695Skan result = XNEW (cpp_context); 965169695Skan result->prev = pfile->context; 966169695Skan result->next = 0; 967169695Skan pfile->context->next = result; 968169695Skan } 969169695Skan 970169695Skan pfile->context = result; 971169695Skan return result; 972169695Skan} 973169695Skan 974169695Skan/* Push a list of pointers to tokens. */ 975169695Skanstatic void 976169695Skanpush_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff, 977169695Skan const cpp_token **first, unsigned int count) 978169695Skan{ 979169695Skan cpp_context *context = next_context (pfile); 980169695Skan 981169695Skan context->direct_p = false; 982169695Skan context->macro = macro; 983169695Skan context->buff = buff; 984169695Skan FIRST (context).ptoken = first; 985169695Skan LAST (context).ptoken = first + count; 986169695Skan} 987169695Skan 988169695Skan/* Push a list of tokens. */ 989169695Skanvoid 990169695Skan_cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro, 991169695Skan const cpp_token *first, unsigned int count) 992169695Skan{ 993169695Skan cpp_context *context = next_context (pfile); 994169695Skan 995169695Skan context->direct_p = true; 996169695Skan context->macro = macro; 997169695Skan context->buff = NULL; 998169695Skan FIRST (context).token = first; 999169695Skan LAST (context).token = first + count; 1000169695Skan} 1001169695Skan 1002169695Skan/* Push a traditional macro's replacement text. */ 1003169695Skanvoid 1004169695Skan_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro, 1005169695Skan const uchar *start, size_t len) 1006169695Skan{ 1007169695Skan cpp_context *context = next_context (pfile); 1008169695Skan 1009169695Skan context->direct_p = true; 1010169695Skan context->macro = macro; 1011169695Skan context->buff = NULL; 1012169695Skan CUR (context) = start; 1013169695Skan RLIMIT (context) = start + len; 1014169695Skan macro->flags |= NODE_DISABLED; 1015169695Skan} 1016169695Skan 1017169695Skan/* Expand an argument ARG before replacing parameters in a 1018169695Skan function-like macro. This works by pushing a context with the 1019169695Skan argument's tokens, and then expanding that into a temporary buffer 1020169695Skan as if it were a normal part of the token stream. collect_args() 1021169695Skan has terminated the argument's tokens with a CPP_EOF so that we know 1022169695Skan when we have fully expanded the argument. */ 1023169695Skanstatic void 1024169695Skanexpand_arg (cpp_reader *pfile, macro_arg *arg) 1025169695Skan{ 1026169695Skan unsigned int capacity; 1027169695Skan bool saved_warn_trad; 1028169695Skan 1029169695Skan if (arg->count == 0) 1030169695Skan return; 1031169695Skan 1032169695Skan /* Don't warn about funlike macros when pre-expanding. */ 1033169695Skan saved_warn_trad = CPP_WTRADITIONAL (pfile); 1034169695Skan CPP_WTRADITIONAL (pfile) = 0; 1035169695Skan 1036169695Skan /* Loop, reading in the arguments. */ 1037169695Skan capacity = 256; 1038169695Skan arg->expanded = XNEWVEC (const cpp_token *, capacity); 1039169695Skan 1040169695Skan push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1); 1041169695Skan for (;;) 1042169695Skan { 1043169695Skan const cpp_token *token; 1044169695Skan 1045169695Skan if (arg->expanded_count + 1 >= capacity) 1046169695Skan { 1047169695Skan capacity *= 2; 1048169695Skan arg->expanded = XRESIZEVEC (const cpp_token *, arg->expanded, 1049169695Skan capacity); 1050169695Skan } 1051169695Skan 1052169695Skan token = cpp_get_token (pfile); 1053169695Skan 1054169695Skan if (token->type == CPP_EOF) 1055169695Skan break; 1056169695Skan 1057169695Skan arg->expanded[arg->expanded_count++] = token; 1058169695Skan } 1059169695Skan 1060169695Skan _cpp_pop_context (pfile); 1061169695Skan 1062169695Skan CPP_WTRADITIONAL (pfile) = saved_warn_trad; 1063169695Skan} 1064169695Skan 1065169695Skan/* Pop the current context off the stack, re-enabling the macro if the 1066169695Skan context represented a macro's replacement list. The context 1067169695Skan structure is not freed so that we can re-use it later. */ 1068169695Skanvoid 1069169695Skan_cpp_pop_context (cpp_reader *pfile) 1070169695Skan{ 1071169695Skan cpp_context *context = pfile->context; 1072169695Skan 1073169695Skan if (context->macro) 1074169695Skan context->macro->flags &= ~NODE_DISABLED; 1075169695Skan 1076169695Skan if (context->buff) 1077169695Skan _cpp_release_buff (pfile, context->buff); 1078169695Skan 1079169695Skan pfile->context = context->prev; 1080169695Skan} 1081169695Skan 1082169695Skan/* External routine to get a token. Also used nearly everywhere 1083169695Skan internally, except for places where we know we can safely call 1084169695Skan _cpp_lex_token directly, such as lexing a directive name. 1085169695Skan 1086169695Skan Macro expansions and directives are transparently handled, 1087169695Skan including entering included files. Thus tokens are post-macro 1088169695Skan expansion, and after any intervening directives. External callers 1089169695Skan see CPP_EOF only at EOF. Internal callers also see it when meeting 1090169695Skan a directive inside a macro call, when at the end of a directive and 1091169695Skan state.in_directive is still 1, and at the end of argument 1092169695Skan pre-expansion. */ 1093169695Skanconst cpp_token * 1094169695Skancpp_get_token (cpp_reader *pfile) 1095169695Skan{ 1096169695Skan const cpp_token *result; 1097169695Skan 1098169695Skan for (;;) 1099169695Skan { 1100169695Skan cpp_hashnode *node; 1101169695Skan cpp_context *context = pfile->context; 1102169695Skan 1103169695Skan /* Context->prev == 0 <=> base context. */ 1104169695Skan if (!context->prev) 1105169695Skan result = _cpp_lex_token (pfile); 1106169695Skan else if (FIRST (context).token != LAST (context).token) 1107169695Skan { 1108169695Skan if (context->direct_p) 1109169695Skan result = FIRST (context).token++; 1110169695Skan else 1111169695Skan result = *FIRST (context).ptoken++; 1112169695Skan 1113169695Skan if (result->flags & PASTE_LEFT) 1114169695Skan { 1115169695Skan paste_all_tokens (pfile, result); 1116169695Skan if (pfile->state.in_directive) 1117169695Skan continue; 1118169695Skan return padding_token (pfile, result); 1119169695Skan } 1120169695Skan } 1121169695Skan else 1122169695Skan { 1123169695Skan _cpp_pop_context (pfile); 1124169695Skan if (pfile->state.in_directive) 1125169695Skan continue; 1126169695Skan return &pfile->avoid_paste; 1127169695Skan } 1128169695Skan 1129169695Skan if (pfile->state.in_directive && result->type == CPP_COMMENT) 1130169695Skan continue; 1131169695Skan 1132169695Skan if (result->type != CPP_NAME) 1133169695Skan break; 1134169695Skan 1135169695Skan node = result->val.node; 1136169695Skan 1137169695Skan if (node->type != NT_MACRO || (result->flags & NO_EXPAND)) 1138169695Skan break; 1139169695Skan 1140169695Skan if (!(node->flags & NODE_DISABLED)) 1141169695Skan { 1142169695Skan if (!pfile->state.prevent_expansion 1143169695Skan && enter_macro_context (pfile, node)) 1144169695Skan { 1145169695Skan if (pfile->state.in_directive) 1146169695Skan continue; 1147169695Skan return padding_token (pfile, result); 1148169695Skan } 1149169695Skan } 1150169695Skan else 1151169695Skan { 1152169695Skan /* Flag this token as always unexpandable. FIXME: move this 1153169695Skan to collect_args()?. */ 1154169695Skan cpp_token *t = _cpp_temp_token (pfile); 1155169695Skan t->type = result->type; 1156169695Skan t->flags = result->flags | NO_EXPAND; 1157169695Skan t->val = result->val; 1158169695Skan result = t; 1159169695Skan } 1160169695Skan 1161169695Skan break; 1162169695Skan } 1163169695Skan 1164169695Skan return result; 1165169695Skan} 1166169695Skan 1167169695Skan/* Returns true if we're expanding an object-like macro that was 1168169695Skan defined in a system header. Just checks the macro at the top of 1169169695Skan the stack. Used for diagnostic suppression. */ 1170169695Skanint 1171169695Skancpp_sys_macro_p (cpp_reader *pfile) 1172169695Skan{ 1173169695Skan cpp_hashnode *node = pfile->context->macro; 1174169695Skan 1175169695Skan return node && node->value.macro && node->value.macro->syshdr; 1176169695Skan} 1177169695Skan 1178169695Skan/* Read each token in, until end of the current file. Directives are 1179169695Skan transparently processed. */ 1180169695Skanvoid 1181169695Skancpp_scan_nooutput (cpp_reader *pfile) 1182169695Skan{ 1183169695Skan /* Request a CPP_EOF token at the end of this file, rather than 1184169695Skan transparently continuing with the including file. */ 1185169695Skan pfile->buffer->return_at_eof = true; 1186169695Skan 1187169695Skan pfile->state.discarding_output++; 1188169695Skan pfile->state.prevent_expansion++; 1189169695Skan 1190169695Skan if (CPP_OPTION (pfile, traditional)) 1191169695Skan while (_cpp_read_logical_line_trad (pfile)) 1192169695Skan ; 1193169695Skan else 1194169695Skan while (cpp_get_token (pfile)->type != CPP_EOF) 1195169695Skan ; 1196169695Skan 1197169695Skan pfile->state.discarding_output--; 1198169695Skan pfile->state.prevent_expansion--; 1199169695Skan} 1200169695Skan 1201169695Skan/* Step back one (or more) tokens. Can only step back more than 1 if 1202169695Skan they are from the lexer, and not from macro expansion. */ 1203169695Skanvoid 1204169695Skan_cpp_backup_tokens (cpp_reader *pfile, unsigned int count) 1205169695Skan{ 1206169695Skan if (pfile->context->prev == NULL) 1207169695Skan { 1208169695Skan pfile->lookaheads += count; 1209169695Skan while (count--) 1210169695Skan { 1211169695Skan pfile->cur_token--; 1212169695Skan if (pfile->cur_token == pfile->cur_run->base 1213169695Skan /* Possible with -fpreprocessed and no leading #line. */ 1214169695Skan && pfile->cur_run->prev != NULL) 1215169695Skan { 1216169695Skan pfile->cur_run = pfile->cur_run->prev; 1217169695Skan pfile->cur_token = pfile->cur_run->limit; 1218169695Skan } 1219169695Skan } 1220169695Skan } 1221169695Skan else 1222169695Skan { 1223169695Skan if (count != 1) 1224169695Skan abort (); 1225169695Skan if (pfile->context->direct_p) 1226169695Skan FIRST (pfile->context).token--; 1227169695Skan else 1228169695Skan FIRST (pfile->context).ptoken--; 1229169695Skan } 1230169695Skan} 1231169695Skan 1232169695Skan/* #define directive parsing and handling. */ 1233169695Skan 1234169695Skan/* Returns nonzero if a macro redefinition warning is required. */ 1235169695Skanstatic bool 1236169695Skanwarn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node, 1237169695Skan const cpp_macro *macro2) 1238169695Skan{ 1239169695Skan const cpp_macro *macro1; 1240169695Skan unsigned int i; 1241169695Skan 1242169695Skan /* Some redefinitions need to be warned about regardless. */ 1243169695Skan if (node->flags & NODE_WARN) 1244169695Skan return true; 1245169695Skan 1246169695Skan /* Redefinition of a macro is allowed if and only if the old and new 1247169695Skan definitions are the same. (6.10.3 paragraph 2). */ 1248169695Skan macro1 = node->value.macro; 1249169695Skan 1250169695Skan /* Don't check count here as it can be different in valid 1251169695Skan traditional redefinitions with just whitespace differences. */ 1252169695Skan if (macro1->paramc != macro2->paramc 1253169695Skan || macro1->fun_like != macro2->fun_like 1254169695Skan || macro1->variadic != macro2->variadic) 1255169695Skan return true; 1256169695Skan 1257169695Skan /* Check parameter spellings. */ 1258169695Skan for (i = 0; i < macro1->paramc; i++) 1259169695Skan if (macro1->params[i] != macro2->params[i]) 1260169695Skan return true; 1261169695Skan 1262169695Skan /* Check the replacement text or tokens. */ 1263169695Skan if (CPP_OPTION (pfile, traditional)) 1264169695Skan return _cpp_expansions_different_trad (macro1, macro2); 1265169695Skan 1266169695Skan if (macro1->count != macro2->count) 1267169695Skan return true; 1268169695Skan 1269169695Skan for (i = 0; i < macro1->count; i++) 1270169695Skan if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i])) 1271169695Skan return true; 1272169695Skan 1273169695Skan return false; 1274169695Skan} 1275169695Skan 1276169695Skan/* Free the definition of hashnode H. */ 1277169695Skanvoid 1278169695Skan_cpp_free_definition (cpp_hashnode *h) 1279169695Skan{ 1280169695Skan /* Macros and assertions no longer have anything to free. */ 1281169695Skan h->type = NT_VOID; 1282169695Skan /* Clear builtin flag in case of redefinition. */ 1283169695Skan h->flags &= ~(NODE_BUILTIN | NODE_DISABLED); 1284169695Skan} 1285169695Skan 1286169695Skan/* Save parameter NODE to the parameter list of macro MACRO. Returns 1287169695Skan zero on success, nonzero if the parameter is a duplicate. */ 1288169695Skanbool 1289169695Skan_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node) 1290169695Skan{ 1291169695Skan unsigned int len; 1292169695Skan /* Constraint 6.10.3.6 - duplicate parameter names. */ 1293169695Skan if (node->flags & NODE_MACRO_ARG) 1294169695Skan { 1295169695Skan cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"", 1296169695Skan NODE_NAME (node)); 1297169695Skan return true; 1298169695Skan } 1299169695Skan 1300169695Skan if (BUFF_ROOM (pfile->a_buff) 1301169695Skan < (macro->paramc + 1) * sizeof (cpp_hashnode *)) 1302169695Skan _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *)); 1303169695Skan 1304169695Skan ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node; 1305169695Skan node->flags |= NODE_MACRO_ARG; 1306169695Skan len = macro->paramc * sizeof (union _cpp_hashnode_value); 1307169695Skan if (len > pfile->macro_buffer_len) 1308169695Skan { 1309169695Skan pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer, 1310169695Skan len); 1311169695Skan pfile->macro_buffer_len = len; 1312169695Skan } 1313169695Skan ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1] 1314169695Skan = node->value; 1315169695Skan 1316169695Skan node->value.arg_index = macro->paramc; 1317169695Skan return false; 1318169695Skan} 1319169695Skan 1320169695Skan/* Check the syntax of the parameters in a MACRO definition. Returns 1321169695Skan false if an error occurs. */ 1322169695Skanstatic bool 1323169695Skanparse_params (cpp_reader *pfile, cpp_macro *macro) 1324169695Skan{ 1325169695Skan unsigned int prev_ident = 0; 1326169695Skan 1327169695Skan for (;;) 1328169695Skan { 1329169695Skan const cpp_token *token = _cpp_lex_token (pfile); 1330169695Skan 1331169695Skan switch (token->type) 1332169695Skan { 1333169695Skan default: 1334169695Skan /* Allow/ignore comments in parameter lists if we are 1335169695Skan preserving comments in macro expansions. */ 1336169695Skan if (token->type == CPP_COMMENT 1337169695Skan && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)) 1338169695Skan continue; 1339169695Skan 1340169695Skan cpp_error (pfile, CPP_DL_ERROR, 1341169695Skan "\"%s\" may not appear in macro parameter list", 1342169695Skan cpp_token_as_text (pfile, token)); 1343169695Skan return false; 1344169695Skan 1345169695Skan case CPP_NAME: 1346169695Skan if (prev_ident) 1347169695Skan { 1348169695Skan cpp_error (pfile, CPP_DL_ERROR, 1349169695Skan "macro parameters must be comma-separated"); 1350169695Skan return false; 1351169695Skan } 1352169695Skan prev_ident = 1; 1353169695Skan 1354169695Skan if (_cpp_save_parameter (pfile, macro, token->val.node)) 1355169695Skan return false; 1356169695Skan continue; 1357169695Skan 1358169695Skan case CPP_CLOSE_PAREN: 1359169695Skan if (prev_ident || macro->paramc == 0) 1360169695Skan return true; 1361169695Skan 1362169695Skan /* Fall through to pick up the error. */ 1363169695Skan case CPP_COMMA: 1364169695Skan if (!prev_ident) 1365169695Skan { 1366169695Skan cpp_error (pfile, CPP_DL_ERROR, "parameter name missing"); 1367169695Skan return false; 1368169695Skan } 1369169695Skan prev_ident = 0; 1370169695Skan continue; 1371169695Skan 1372169695Skan case CPP_ELLIPSIS: 1373169695Skan macro->variadic = 1; 1374169695Skan if (!prev_ident) 1375169695Skan { 1376169695Skan _cpp_save_parameter (pfile, macro, 1377169695Skan pfile->spec_nodes.n__VA_ARGS__); 1378169695Skan pfile->state.va_args_ok = 1; 1379169695Skan if (! CPP_OPTION (pfile, c99) 1380169695Skan && CPP_OPTION (pfile, pedantic) 1381169695Skan && CPP_OPTION (pfile, warn_variadic_macros)) 1382169695Skan cpp_error (pfile, CPP_DL_PEDWARN, 1383169695Skan "anonymous variadic macros were introduced in C99"); 1384169695Skan } 1385169695Skan else if (CPP_OPTION (pfile, pedantic) 1386169695Skan && CPP_OPTION (pfile, warn_variadic_macros)) 1387169695Skan cpp_error (pfile, CPP_DL_PEDWARN, 1388169695Skan "ISO C does not permit named variadic macros"); 1389169695Skan 1390169695Skan /* We're at the end, and just expect a closing parenthesis. */ 1391169695Skan token = _cpp_lex_token (pfile); 1392169695Skan if (token->type == CPP_CLOSE_PAREN) 1393169695Skan return true; 1394169695Skan /* Fall through. */ 1395169695Skan 1396169695Skan case CPP_EOF: 1397169695Skan cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list"); 1398169695Skan return false; 1399169695Skan } 1400169695Skan } 1401169695Skan} 1402169695Skan 1403169695Skan/* Allocate room for a token from a macro's replacement list. */ 1404169695Skanstatic cpp_token * 1405169695Skanalloc_expansion_token (cpp_reader *pfile, cpp_macro *macro) 1406169695Skan{ 1407169695Skan if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token)) 1408169695Skan _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token)); 1409169695Skan 1410169695Skan return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++]; 1411169695Skan} 1412169695Skan 1413169695Skan/* Lex a token from the expansion of MACRO, but mark parameters as we 1414169695Skan find them and warn of traditional stringification. */ 1415169695Skanstatic cpp_token * 1416169695Skanlex_expansion_token (cpp_reader *pfile, cpp_macro *macro) 1417169695Skan{ 1418258826Spfg cpp_token *token, *saved_cur_token; 1419169695Skan 1420258826Spfg saved_cur_token = pfile->cur_token; 1421169695Skan pfile->cur_token = alloc_expansion_token (pfile, macro); 1422169695Skan token = _cpp_lex_direct (pfile); 1423258826Spfg pfile->cur_token = saved_cur_token; 1424169695Skan 1425169695Skan /* Is this a parameter? */ 1426169695Skan if (token->type == CPP_NAME 1427169695Skan && (token->val.node->flags & NODE_MACRO_ARG) != 0) 1428169695Skan { 1429169695Skan token->type = CPP_MACRO_ARG; 1430169695Skan token->val.arg_no = token->val.node->value.arg_index; 1431169695Skan } 1432169695Skan else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 1433169695Skan && (token->type == CPP_STRING || token->type == CPP_CHAR)) 1434169695Skan check_trad_stringification (pfile, macro, &token->val.str); 1435169695Skan 1436169695Skan return token; 1437169695Skan} 1438169695Skan 1439169695Skanstatic bool 1440169695Skancreate_iso_definition (cpp_reader *pfile, cpp_macro *macro) 1441169695Skan{ 1442169695Skan cpp_token *token; 1443169695Skan const cpp_token *ctoken; 1444169695Skan 1445169695Skan /* Get the first token of the expansion (or the '(' of a 1446169695Skan function-like macro). */ 1447169695Skan ctoken = _cpp_lex_token (pfile); 1448169695Skan 1449169695Skan if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE)) 1450169695Skan { 1451169695Skan bool ok = parse_params (pfile, macro); 1452169695Skan macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff); 1453169695Skan if (!ok) 1454169695Skan return false; 1455169695Skan 1456169695Skan /* Success. Commit or allocate the parameter array. */ 1457169695Skan if (pfile->hash_table->alloc_subobject) 1458169695Skan { 1459169695Skan cpp_hashnode **params = 1460169695Skan (cpp_hashnode **) pfile->hash_table->alloc_subobject 1461169695Skan (sizeof (cpp_hashnode *) * macro->paramc); 1462169695Skan memcpy (params, macro->params, 1463169695Skan sizeof (cpp_hashnode *) * macro->paramc); 1464169695Skan macro->params = params; 1465169695Skan } 1466169695Skan else 1467169695Skan BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc]; 1468169695Skan macro->fun_like = 1; 1469169695Skan } 1470169695Skan else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE)) 1471169695Skan { 1472169695Skan /* While ISO C99 requires whitespace before replacement text 1473169695Skan in a macro definition, ISO C90 with TC1 allows there characters 1474169695Skan from the basic source character set. */ 1475169695Skan if (CPP_OPTION (pfile, c99)) 1476169695Skan cpp_error (pfile, CPP_DL_PEDWARN, 1477169695Skan "ISO C99 requires whitespace after the macro name"); 1478169695Skan else 1479169695Skan { 1480169695Skan int warntype = CPP_DL_WARNING; 1481169695Skan switch (ctoken->type) 1482169695Skan { 1483169695Skan case CPP_ATSIGN: 1484169695Skan case CPP_AT_NAME: 1485169695Skan case CPP_OBJC_STRING: 1486169695Skan /* '@' is not in basic character set. */ 1487169695Skan warntype = CPP_DL_PEDWARN; 1488169695Skan break; 1489169695Skan case CPP_OTHER: 1490169695Skan /* Basic character set sans letters, digits and _. */ 1491169695Skan if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~", 1492169695Skan ctoken->val.str.text[0]) == NULL) 1493169695Skan warntype = CPP_DL_PEDWARN; 1494169695Skan break; 1495169695Skan default: 1496169695Skan /* All other tokens start with a character from basic 1497169695Skan character set. */ 1498169695Skan break; 1499169695Skan } 1500169695Skan cpp_error (pfile, warntype, 1501169695Skan "missing whitespace after the macro name"); 1502169695Skan } 1503169695Skan } 1504169695Skan 1505169695Skan if (macro->fun_like) 1506169695Skan token = lex_expansion_token (pfile, macro); 1507169695Skan else 1508169695Skan { 1509169695Skan token = alloc_expansion_token (pfile, macro); 1510169695Skan *token = *ctoken; 1511169695Skan } 1512169695Skan 1513169695Skan for (;;) 1514169695Skan { 1515169695Skan /* Check the stringifying # constraint 6.10.3.2.1 of 1516169695Skan function-like macros when lexing the subsequent token. */ 1517169695Skan if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) 1518169695Skan { 1519169695Skan if (token->type == CPP_MACRO_ARG) 1520169695Skan { 1521169695Skan token->flags &= ~PREV_WHITE; 1522169695Skan token->flags |= STRINGIFY_ARG; 1523169695Skan token->flags |= token[-1].flags & PREV_WHITE; 1524169695Skan token[-1] = token[0]; 1525169695Skan macro->count--; 1526169695Skan } 1527169695Skan /* Let assembler get away with murder. */ 1528169695Skan else if (CPP_OPTION (pfile, lang) != CLK_ASM) 1529169695Skan { 1530169695Skan cpp_error (pfile, CPP_DL_ERROR, 1531169695Skan "'#' is not followed by a macro parameter"); 1532169695Skan return false; 1533169695Skan } 1534169695Skan } 1535169695Skan 1536169695Skan if (token->type == CPP_EOF) 1537169695Skan break; 1538169695Skan 1539169695Skan /* Paste operator constraint 6.10.3.3.1. */ 1540169695Skan if (token->type == CPP_PASTE) 1541169695Skan { 1542169695Skan /* Token-paste ##, can appear in both object-like and 1543169695Skan function-like macros, but not at the ends. */ 1544169695Skan if (--macro->count > 0) 1545169695Skan token = lex_expansion_token (pfile, macro); 1546169695Skan 1547169695Skan if (macro->count == 0 || token->type == CPP_EOF) 1548169695Skan { 1549169695Skan cpp_error (pfile, CPP_DL_ERROR, 1550169695Skan "'##' cannot appear at either end of a macro expansion"); 1551169695Skan return false; 1552169695Skan } 1553169695Skan 1554169695Skan token[-1].flags |= PASTE_LEFT; 1555169695Skan } 1556169695Skan 1557169695Skan token = lex_expansion_token (pfile, macro); 1558169695Skan } 1559169695Skan 1560169695Skan macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff); 1561169695Skan macro->traditional = 0; 1562169695Skan 1563169695Skan /* Don't count the CPP_EOF. */ 1564169695Skan macro->count--; 1565169695Skan 1566169695Skan /* Clear whitespace on first token for warn_of_redefinition(). */ 1567169695Skan if (macro->count) 1568169695Skan macro->exp.tokens[0].flags &= ~PREV_WHITE; 1569169695Skan 1570169695Skan /* Commit or allocate the memory. */ 1571169695Skan if (pfile->hash_table->alloc_subobject) 1572169695Skan { 1573169695Skan cpp_token *tokns = 1574169695Skan (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token) 1575169695Skan * macro->count); 1576169695Skan memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count); 1577169695Skan macro->exp.tokens = tokns; 1578169695Skan } 1579169695Skan else 1580169695Skan BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count]; 1581169695Skan 1582169695Skan return true; 1583169695Skan} 1584169695Skan 1585169695Skan/* Parse a macro and save its expansion. Returns nonzero on success. */ 1586169695Skanbool 1587169695Skan_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node) 1588169695Skan{ 1589169695Skan cpp_macro *macro; 1590169695Skan unsigned int i; 1591169695Skan bool ok; 1592169695Skan 1593169695Skan if (pfile->hash_table->alloc_subobject) 1594169695Skan macro = (cpp_macro *) pfile->hash_table->alloc_subobject 1595169695Skan (sizeof (cpp_macro)); 1596169695Skan else 1597169695Skan macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro)); 1598169695Skan macro->line = pfile->directive_line; 1599169695Skan macro->params = 0; 1600169695Skan macro->paramc = 0; 1601169695Skan macro->variadic = 0; 1602169695Skan macro->used = !CPP_OPTION (pfile, warn_unused_macros); 1603169695Skan macro->count = 0; 1604169695Skan macro->fun_like = 0; 1605169695Skan /* To suppress some diagnostics. */ 1606169695Skan macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0; 1607169695Skan 1608169695Skan if (CPP_OPTION (pfile, traditional)) 1609169695Skan ok = _cpp_create_trad_definition (pfile, macro); 1610169695Skan else 1611169695Skan { 1612169695Skan ok = create_iso_definition (pfile, macro); 1613169695Skan 1614258826Spfg /* We set the type for SEEN_EOL() in directives.c. 1615169695Skan 1616169695Skan Longer term we should lex the whole line before coming here, 1617169695Skan and just copy the expansion. */ 1618169695Skan 1619169695Skan /* Stop the lexer accepting __VA_ARGS__. */ 1620169695Skan pfile->state.va_args_ok = 0; 1621169695Skan } 1622169695Skan 1623169695Skan /* Clear the fast argument lookup indices. */ 1624169695Skan for (i = macro->paramc; i-- > 0; ) 1625169695Skan { 1626169695Skan struct cpp_hashnode *node = macro->params[i]; 1627169695Skan node->flags &= ~ NODE_MACRO_ARG; 1628169695Skan node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i]; 1629169695Skan } 1630169695Skan 1631169695Skan if (!ok) 1632169695Skan return ok; 1633169695Skan 1634169695Skan if (node->type == NT_MACRO) 1635169695Skan { 1636169695Skan if (CPP_OPTION (pfile, warn_unused_macros)) 1637169695Skan _cpp_warn_if_unused_macro (pfile, node, NULL); 1638169695Skan 1639169695Skan if (warn_of_redefinition (pfile, node, macro)) 1640169695Skan { 1641169695Skan cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0, 1642169695Skan "\"%s\" redefined", NODE_NAME (node)); 1643169695Skan 1644169695Skan if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 1645169695Skan cpp_error_with_line (pfile, CPP_DL_PEDWARN, 1646169695Skan node->value.macro->line, 0, 1647169695Skan "this is the location of the previous definition"); 1648169695Skan } 1649169695Skan } 1650169695Skan 1651169695Skan if (node->type != NT_VOID) 1652169695Skan _cpp_free_definition (node); 1653169695Skan 1654169695Skan /* Enter definition in hash table. */ 1655169695Skan node->type = NT_MACRO; 1656169695Skan node->value.macro = macro; 1657169695Skan if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))) 1658169695Skan node->flags |= NODE_WARN; 1659169695Skan 1660169695Skan return ok; 1661169695Skan} 1662169695Skan 1663169695Skan/* Warn if a token in STRING matches one of a function-like MACRO's 1664169695Skan parameters. */ 1665169695Skanstatic void 1666169695Skancheck_trad_stringification (cpp_reader *pfile, const cpp_macro *macro, 1667169695Skan const cpp_string *string) 1668169695Skan{ 1669169695Skan unsigned int i, len; 1670169695Skan const uchar *p, *q, *limit; 1671169695Skan 1672169695Skan /* Loop over the string. */ 1673169695Skan limit = string->text + string->len - 1; 1674169695Skan for (p = string->text + 1; p < limit; p = q) 1675169695Skan { 1676169695Skan /* Find the start of an identifier. */ 1677169695Skan while (p < limit && !is_idstart (*p)) 1678169695Skan p++; 1679169695Skan 1680169695Skan /* Find the end of the identifier. */ 1681169695Skan q = p; 1682169695Skan while (q < limit && is_idchar (*q)) 1683169695Skan q++; 1684169695Skan 1685169695Skan len = q - p; 1686169695Skan 1687169695Skan /* Loop over the function macro arguments to see if the 1688169695Skan identifier inside the string matches one of them. */ 1689169695Skan for (i = 0; i < macro->paramc; i++) 1690169695Skan { 1691169695Skan const cpp_hashnode *node = macro->params[i]; 1692169695Skan 1693169695Skan if (NODE_LEN (node) == len 1694169695Skan && !memcmp (p, NODE_NAME (node), len)) 1695169695Skan { 1696169695Skan cpp_error (pfile, CPP_DL_WARNING, 1697169695Skan "macro argument \"%s\" would be stringified in traditional C", 1698169695Skan NODE_NAME (node)); 1699169695Skan break; 1700169695Skan } 1701169695Skan } 1702169695Skan } 1703169695Skan} 1704169695Skan 1705169695Skan/* Returns the name, arguments and expansion of a macro, in a format 1706169695Skan suitable to be read back in again, and therefore also for DWARF 2 1707169695Skan debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". 1708169695Skan Caller is expected to generate the "#define" bit if needed. The 1709169695Skan returned text is temporary, and automatically freed later. */ 1710169695Skanconst unsigned char * 1711169695Skancpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node) 1712169695Skan{ 1713169695Skan unsigned int i, len; 1714169695Skan const cpp_macro *macro = node->value.macro; 1715169695Skan unsigned char *buffer; 1716169695Skan 1717169695Skan if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN)) 1718169695Skan { 1719169695Skan cpp_error (pfile, CPP_DL_ICE, 1720169695Skan "invalid hash type %d in cpp_macro_definition", node->type); 1721169695Skan return 0; 1722169695Skan } 1723169695Skan 1724169695Skan /* Calculate length. */ 1725169695Skan len = NODE_LEN (node) + 2; /* ' ' and NUL. */ 1726169695Skan if (macro->fun_like) 1727169695Skan { 1728169695Skan len += 4; /* "()" plus possible final ".." of named 1729169695Skan varargs (we have + 1 below). */ 1730169695Skan for (i = 0; i < macro->paramc; i++) 1731169695Skan len += NODE_LEN (macro->params[i]) + 1; /* "," */ 1732169695Skan } 1733169695Skan 1734169695Skan /* This should match below where we fill in the buffer. */ 1735169695Skan if (CPP_OPTION (pfile, traditional)) 1736169695Skan len += _cpp_replacement_text_len (macro); 1737169695Skan else 1738169695Skan { 1739169695Skan for (i = 0; i < macro->count; i++) 1740169695Skan { 1741169695Skan cpp_token *token = ¯o->exp.tokens[i]; 1742169695Skan 1743169695Skan if (token->type == CPP_MACRO_ARG) 1744169695Skan len += NODE_LEN (macro->params[token->val.arg_no - 1]); 1745169695Skan else 1746169695Skan len += cpp_token_len (token); 1747169695Skan 1748169695Skan if (token->flags & STRINGIFY_ARG) 1749169695Skan len++; /* "#" */ 1750169695Skan if (token->flags & PASTE_LEFT) 1751169695Skan len += 3; /* " ##" */ 1752169695Skan if (token->flags & PREV_WHITE) 1753169695Skan len++; /* " " */ 1754169695Skan } 1755169695Skan } 1756169695Skan 1757169695Skan if (len > pfile->macro_buffer_len) 1758169695Skan { 1759169695Skan pfile->macro_buffer = XRESIZEVEC (unsigned char, 1760169695Skan pfile->macro_buffer, len); 1761169695Skan pfile->macro_buffer_len = len; 1762169695Skan } 1763169695Skan 1764169695Skan /* Fill in the buffer. Start with the macro name. */ 1765169695Skan buffer = pfile->macro_buffer; 1766169695Skan memcpy (buffer, NODE_NAME (node), NODE_LEN (node)); 1767169695Skan buffer += NODE_LEN (node); 1768169695Skan 1769169695Skan /* Parameter names. */ 1770169695Skan if (macro->fun_like) 1771169695Skan { 1772169695Skan *buffer++ = '('; 1773169695Skan for (i = 0; i < macro->paramc; i++) 1774169695Skan { 1775169695Skan cpp_hashnode *param = macro->params[i]; 1776169695Skan 1777169695Skan if (param != pfile->spec_nodes.n__VA_ARGS__) 1778169695Skan { 1779169695Skan memcpy (buffer, NODE_NAME (param), NODE_LEN (param)); 1780169695Skan buffer += NODE_LEN (param); 1781169695Skan } 1782169695Skan 1783169695Skan if (i + 1 < macro->paramc) 1784169695Skan /* Don't emit a space after the comma here; we're trying 1785169695Skan to emit a Dwarf-friendly definition, and the Dwarf spec 1786169695Skan forbids spaces in the argument list. */ 1787169695Skan *buffer++ = ','; 1788169695Skan else if (macro->variadic) 1789169695Skan *buffer++ = '.', *buffer++ = '.', *buffer++ = '.'; 1790169695Skan } 1791169695Skan *buffer++ = ')'; 1792169695Skan } 1793169695Skan 1794169695Skan /* The Dwarf spec requires a space after the macro name, even if the 1795169695Skan definition is the empty string. */ 1796169695Skan *buffer++ = ' '; 1797169695Skan 1798169695Skan if (CPP_OPTION (pfile, traditional)) 1799169695Skan buffer = _cpp_copy_replacement_text (macro, buffer); 1800169695Skan else if (macro->count) 1801169695Skan /* Expansion tokens. */ 1802169695Skan { 1803169695Skan for (i = 0; i < macro->count; i++) 1804169695Skan { 1805169695Skan cpp_token *token = ¯o->exp.tokens[i]; 1806169695Skan 1807169695Skan if (token->flags & PREV_WHITE) 1808169695Skan *buffer++ = ' '; 1809169695Skan if (token->flags & STRINGIFY_ARG) 1810169695Skan *buffer++ = '#'; 1811169695Skan 1812169695Skan if (token->type == CPP_MACRO_ARG) 1813169695Skan { 1814169695Skan memcpy (buffer, 1815169695Skan NODE_NAME (macro->params[token->val.arg_no - 1]), 1816169695Skan NODE_LEN (macro->params[token->val.arg_no - 1])); 1817169695Skan buffer += NODE_LEN (macro->params[token->val.arg_no - 1]); 1818169695Skan } 1819169695Skan else 1820169695Skan buffer = cpp_spell_token (pfile, token, buffer, false); 1821169695Skan 1822169695Skan if (token->flags & PASTE_LEFT) 1823169695Skan { 1824169695Skan *buffer++ = ' '; 1825169695Skan *buffer++ = '#'; 1826169695Skan *buffer++ = '#'; 1827169695Skan /* Next has PREV_WHITE; see _cpp_create_definition. */ 1828169695Skan } 1829169695Skan } 1830169695Skan } 1831169695Skan 1832169695Skan *buffer = '\0'; 1833169695Skan return pfile->macro_buffer; 1834169695Skan} 1835