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