From: Nathan Sidwell Date: Wed, 1 Aug 2018 18:42:13 +0000 (+0000) Subject: Macro tokens are trailing array X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99679fe034b4deb9dcc4d55cebd350b78fde7719;p=thirdparty%2Fgcc.git Macro tokens are trailing array Macro tokens are trailing array libcpp/ * include/cpp-id-data.h (cpp_macro): Move tokens to trailing array. * internal.h (_cpp_construct_macro): Declare. * macro.c (lex_expansion): Return macro pointer, adjust. (macro_real_token_count): Count backwards. (replace_args): Use pointer equality, not orderedness. (create_iso_definition): Adjust for trailing token array. (_cpp_construct_macro): New, broken out of ... (_cpp_new_macro): ... here. Call it. (cpp_macro_definition): Constify token pointer. gcc/c-family/ * c-ada-spec.c (macro_length): Constify token pointer. (dump_ada_macros): Likewise. From-SVN: r263233 --- diff --git a/ChangeLog.name-lookup b/ChangeLog.name-lookup index bb9b5a745cef..2ac60f044a7c 100644 --- a/ChangeLog.name-lookup +++ b/ChangeLog.name-lookup @@ -1,3 +1,20 @@ +2018-08-01 Nathan Sidwell + + Macro tokens are trailing array + libcpp/ + * include/cpp-id-data.h (cpp_macro): Move tokens to trailing array. + * internal.h (_cpp_construct_macro): Declare. + * macro.c (lex_expansion): Return macro pointer, adjust. + (macro_real_token_count): Count backwards. + (replace_args): Use pointer equality, not orderedness. + (create_iso_definition): Adjust for trailing token array. + (_cpp_construct_macro): New, broken out of ... + (_cpp_new_macro): ... here. Call it. + (cpp_macro_definition): Constify token pointer. + gcc/c-family/ + * c-ada-spec.c (macro_length): Constify token pointer. + (dump_ada_macros): Likewise. + 2018-07-17 Nathan Sidwell libcpp/ diff --git a/gcc/c-family/c-ada-spec.c b/gcc/c-family/c-ada-spec.c index f1b02d8f2fc1..24aad7af38eb 100644 --- a/gcc/c-family/c-ada-spec.c +++ b/gcc/c-family/c-ada-spec.c @@ -89,7 +89,7 @@ macro_length (const cpp_macro *macro, int *supported, int *buffer_len, for (j = 0; j < macro->count; j++) { - cpp_token *token = ¯o->exp.tokens[j]; + const cpp_token *token = ¯o->exp.tokens[j]; if (token->flags & PREV_WHITE) (*buffer_len)++; @@ -275,7 +275,7 @@ dump_ada_macros (pretty_printer *pp, const char* file) for (i = 0; supported && i < macro->count; i++) { - cpp_token *token = ¯o->exp.tokens[i]; + const cpp_token *token = ¯o->exp.tokens[i]; int is_one = 0; if (token->flags & PREV_WHITE) diff --git a/libcpp/include/cpp-id-data.h b/libcpp/include/cpp-id-data.h index 89774e3f918d..3ee264b4b9b5 100644 --- a/libcpp/include/cpp-id-data.h +++ b/libcpp/include/cpp-id-data.h @@ -55,17 +55,6 @@ struct GTY(()) cpp_macro { cpp_macro *GTY ((tag ("true"))) next; } GTY ((desc ("%1.kind == cmk_assert"))) parm; - union cpp_exp_u - { - /* Replacement tokens (ISO), or assertion body value. */ - cpp_token * GTY ((tag ("false"), length ("%1.count"))) tokens; - - /* Replacement text (traditional). See comment at top of - cpptrad.c for how traditional function-like macros are - encoded. */ - const unsigned char * GTY ((tag ("true"))) text; - } GTY ((desc ("%1.kind == cmk_traditional"))) exp; - /* Definition line number. */ source_location line; @@ -97,4 +86,15 @@ struct GTY(()) cpp_macro { unsigned int extra_tokens : 1; /* 9 bits spare (32-bit). 41 on 64-bit target. */ + + union cpp_exp_u + { + /* Trailing array of replacement tokens (ISO), or assertion body value. */ + cpp_token GTY ((tag ("false"), length ("%1.count"))) tokens[1]; + + /* Pointer to replacement text (traditional). See comment at top + of cpptrad.c for how traditional function-like macros are + encoded. */ + const unsigned char *GTY ((tag ("true"))) text; + } GTY ((desc ("%1.kind == cmk_traditional"))) exp; }; diff --git a/libcpp/internal.h b/libcpp/internal.h index 655287d228a9..85e3faa4df70 100644 --- a/libcpp/internal.h +++ b/libcpp/internal.h @@ -622,6 +622,7 @@ cpp_in_primary_file (cpp_reader *pfile) } /* In macro.c */ +extern cpp_macro *_cpp_construct_macro (cpp_reader *, cpp_macro_kind, void *); extern cpp_macro *_cpp_new_macro (cpp_reader *, cpp_macro_kind); extern void _cpp_free_definition (cpp_hashnode *); extern bool _cpp_create_definition (cpp_reader *, cpp_hashnode *); diff --git a/libcpp/macro.c b/libcpp/macro.c index 23edff7695e0..807ad7062d79 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -312,7 +312,7 @@ static cpp_macro *create_iso_definition (cpp_reader *); /* #define directive parsing and handling. */ -static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *); +static cpp_macro *lex_expansion_token (cpp_reader *, cpp_macro *); static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *, const cpp_macro *); static bool parse_params (cpp_reader *, unsigned *, bool *); @@ -1234,13 +1234,14 @@ funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node, static inline unsigned int macro_real_token_count (const cpp_macro *macro) { - unsigned int i; if (__builtin_expect (!macro->extra_tokens, true)) return macro->count; - for (i = 0; i < macro->count; i++) - if (macro->exp.tokens[i].type == CPP_PASTE) - return i; - abort (); + + for (unsigned i = macro->count; i--;) + if (macro->exp.tokens[i].type != CPP_PASTE) + return i + 1; + + return 0; } /* Push the context of a macro with hash entry NODE onto the context @@ -1779,7 +1780,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, arg->stringified = stringify_arg (pfile, arg); } else if ((src->flags & PASTE_LEFT) - || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT))) + || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))) total += arg->count - 1; else { @@ -3236,13 +3237,15 @@ parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *varadic_ptr) /* Lex a token from the expansion of MACRO, but mark parameters as we find them and warn of traditional stringification. */ -static cpp_token * +static cpp_macro * lex_expansion_token (cpp_reader *pfile, cpp_macro *macro) { - void *base = _cpp_reserve_room (pfile, macro->count * sizeof (cpp_token), - sizeof (cpp_token)); + macro = (cpp_macro *)_cpp_reserve_room (pfile, + sizeof (cpp_macro) - sizeof (cpp_token) + + macro->count * sizeof (cpp_token), + sizeof (cpp_token)); cpp_token *saved_cur_token = pfile->cur_token; - pfile->cur_token = &((cpp_token *) base)[macro->count++]; + pfile->cur_token = ¯o->exp.tokens[macro->count]; cpp_token *token = _cpp_lex_direct (pfile); pfile->cur_token = saved_cur_token; @@ -3260,7 +3263,7 @@ lex_expansion_token (cpp_reader *pfile, cpp_macro *macro) && (token->type == CPP_STRING || token->type == CPP_CHAR)) check_trad_stringification (pfile, macro, &token->val.str); - return token; + return macro; } static cpp_macro * @@ -3335,7 +3338,10 @@ create_iso_definition (cpp_reader *pfile) } } - macro = _cpp_new_macro (pfile, cmk_macro); + macro = _cpp_construct_macro (pfile, cmk_macro, + _cpp_reserve_room (pfile, 0, + sizeof (cpp_macro))); + if (!token) { macro->variadic = varadic; @@ -3345,17 +3351,19 @@ create_iso_definition (cpp_reader *pfile) } else { - /* Preserve the token we peeked. */ - void *base = _cpp_reserve_room (pfile, 0, sizeof (cpp_token)); - *(cpp_token *)base = *token; - token = (cpp_token *)base; + /* Preserve the token we peeked, there is already a single slot for it. */ + macro->exp.tokens[0] = *token; + token = ¯o->exp.tokens[0]; macro->count = 1; } for (vaopt_state vaopt_tracker (pfile, macro->variadic, true);; token = NULL) { if (!token) - token = lex_expansion_token (pfile, macro); + { + macro = lex_expansion_token (pfile, macro); + token = ¯o->exp.tokens[macro->count++]; + } /* Check the stringifying # constraint 6.10.3.2.1 of function-like macros when lexing the subsequent token. */ @@ -3439,8 +3447,9 @@ create_iso_definition (cpp_reader *pfile) /* Don't count the CPP_EOF. */ macro->count--; - macro->exp.tokens = (cpp_token *)_cpp_commit_buff - (pfile, sizeof (cpp_token) * macro->count); + macro = (cpp_macro *)_cpp_commit_buff + (pfile, sizeof (cpp_macro) - sizeof (cpp_token) + + sizeof (cpp_token) * macro->count); /* Clear whitespace on first token for warn_of_redefinition(). */ if (macro->count) @@ -3477,15 +3486,10 @@ create_iso_definition (cpp_reader *pfile) } cpp_macro * -_cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind) +_cpp_construct_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement) { - cpp_macro *macro; + cpp_macro *macro = (cpp_macro *) placement; - if (pfile->hash_table->alloc_subobject) - macro = (cpp_macro *) pfile->hash_table->alloc_subobject - (sizeof (cpp_macro)); - else - macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro)); macro->line = pfile->directive_line; macro->parm.params = 0; macro->paramc = 0; @@ -3502,6 +3506,18 @@ _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind) return macro; } +cpp_macro * +_cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind) +{ + void *placement; + + if (pfile->hash_table->alloc_subobject) + placement = pfile->hash_table->alloc_subobject (sizeof (cpp_macro)); + else + placement = _cpp_aligned_alloc (pfile, sizeof (cpp_macro)); + return _cpp_construct_macro (pfile, kind, placement); +} + /* Parse a macro and save its expansion. Returns nonzero on success. */ bool _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node) @@ -3655,7 +3671,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node) unsigned int count = macro_real_token_count (macro); for (i = 0; i < count; i++) { - cpp_token *token = ¯o->exp.tokens[i]; + const cpp_token *token = ¯o->exp.tokens[i]; if (token->type == CPP_MACRO_ARG) len += NODE_LEN (token->val.macro_arg.spelling); @@ -3719,7 +3735,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node) unsigned int count = macro_real_token_count (macro); for (i = 0; i < count; i++) { - cpp_token *token = ¯o->exp.tokens[i]; + const cpp_token *token = ¯o->exp.tokens[i]; if (token->flags & PREV_WHITE) *buffer++ = ' ';