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