From: Nathan Sidwell Date: Mon, 16 Jul 2018 18:56:01 +0000 (+0000) Subject: internal.h (_cpp_reserve_room): New inline. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dfed6beefe497e67700a4d69b75eb69e1ae2693d;p=thirdparty%2Fgcc.git internal.h (_cpp_reserve_room): New inline. libcpp/ * internal.h (_cpp_reserve_room): New inline. (_cpp_commit_buff): Declare. * lex.c (_cpp_commit_buff): Implement. * directives.c (parse_answer): Use them. * macro.c (alloc_expansion_token): Fold ito ... (lex_expansion_token): ... here. (_cpp_save_parameter): Use _cpp_reserve_room. (create_iso_definition): Use _cpp_reserve_room, _cpp_commit_buff). From-SVN: r262745 --- diff --git a/ChangeLog.name-lookup b/ChangeLog.name-lookup index 9aa0814467f8..3127271a3ec7 100644 --- a/ChangeLog.name-lookup +++ b/ChangeLog.name-lookup @@ -1,3 +1,15 @@ +2018-07-16 Nathan Sidwell + + libcpp/ + * internal.h (_cpp_reserve_room): New inline. + (_cpp_commit_buff): Declare. + * lex.c (_cpp_commit_buff): Implement. + * directives.c (parse_answer): Use them. + * macro.c (alloc_expansion_token): Fold ito ... + (lex_expansion_token): ... here. + (_cpp_save_parameter): Use _cpp_reserve_room. + (create_iso_definition): Use _cpp_reserve_room, _cpp_commit_buff). + 2018-07-13 Nathan Sidwell libcpp/ diff --git a/libcpp/directives.c b/libcpp/directives.c index 402e325cce7b..3bc6219262b4 100644 --- a/libcpp/directives.c +++ b/libcpp/directives.c @@ -2227,11 +2227,9 @@ parse_answer (cpp_reader *pfile, int type, source_location pred_loc, return 1; } - size_t needed = (count + 1) * sizeof (cpp_token) + sizeof (answer); - if (BUFF_ROOM (pfile->a_buff) < needed) - _cpp_extend_buff (pfile, &pfile->a_buff, needed); - - cpp_token *dest = &((cpp_token *)BUFF_FRONT (pfile->a_buff))[count++]; + void *base = _cpp_reserve_room (pfile, count * sizeof (cpp_token), + sizeof (cpp_token)); + cpp_token *dest = &((cpp_token *)base)[count++]; *dest = *token; } @@ -2370,37 +2368,20 @@ do_assert (cpp_reader *pfile) next = node->value.answers; } - size_t size = count * sizeof (cpp_token); /* Commit or allocate storage for the expansion. */ - if (pfile->hash_table->alloc_subobject) - { - cpp_token *saved_exp - = (cpp_token *)pfile->hash_table->alloc_subobject (size); - memcpy (saved_exp, exp, size); - exp = saved_exp; - } - else - { - gcc_assert (BUFF_FRONT (pfile->a_buff) == (void *)exp); - BUFF_FRONT (pfile->a_buff) += size; - } + size_t size = count * sizeof (cpp_token); + exp = (cpp_token *)_cpp_commit_buff (pfile, size); /* Create the answer object. */ - answer *ans = NULL; - if (pfile->hash_table->alloc_subobject) - ans = (answer *)pfile->hash_table->alloc_subobject (sizeof (answer)); - else - { - if (BUFF_ROOM (pfile->a_buff) < sizeof (answer)) - _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (answer)); - ans = (answer *)BUFF_FRONT (pfile->a_buff); - BUFF_FRONT (pfile->a_buff) += sizeof (answer); - } + // FIXME: This'll get simpler reverting to trailing array hack + answer *ans = (answer *)_cpp_reserve_room (pfile, 0, sizeof (answer)); ans->count = count; ans->exp = exp; ans->next = next; + ans = (answer *)_cpp_commit_buff (pfile, sizeof (answer)); + node->type = NT_ASSERTION; node->value.answers = ans; diff --git a/libcpp/internal.h b/libcpp/internal.h index 7b532216c86a..655287d228a9 100644 --- a/libcpp/internal.h +++ b/libcpp/internal.h @@ -687,6 +687,14 @@ extern void _cpp_init_tokenrun (tokenrun *, unsigned int); extern cpp_hashnode *_cpp_lex_identifier (cpp_reader *, const char *); extern int _cpp_remaining_tokens_num_in_context (cpp_context *); extern void _cpp_init_lexer (void); +static inline void *_cpp_reserve_room (cpp_reader *pfile, size_t have, + size_t extra) +{ + if (BUFF_ROOM (pfile->a_buff) < (have + extra)) + _cpp_extend_buff (pfile, &pfile->a_buff, extra); + return BUFF_FRONT (pfile->a_buff); +} +extern void *_cpp_commit_buff (cpp_reader *pfile, size_t size); /* In init.c. */ extern void _cpp_maybe_push_include_file (cpp_reader *); diff --git a/libcpp/lex.c b/libcpp/lex.c index 37c365a3560b..b2a2ffdc2852 100644 --- a/libcpp/lex.c +++ b/libcpp/lex.c @@ -3724,6 +3724,25 @@ _cpp_aligned_alloc (cpp_reader *pfile, size_t len) return result; } +/* Commit or allocate storage from a buffer. */ + +void * +_cpp_commit_buff (cpp_reader *pfile, size_t size) +{ + void *ptr = BUFF_FRONT (pfile->a_buff); + + if (pfile->hash_table->alloc_subobject) + { + void *copy = pfile->hash_table->alloc_subobject (size); + memcpy (copy, ptr, size); + ptr = copy; + } + else + BUFF_FRONT (pfile->a_buff) += size; + + return ptr; +} + /* Say which field of TOK is in use. */ enum cpp_token_fld_kind diff --git a/libcpp/macro.c b/libcpp/macro.c index 8bdcfcc6e66b..52ae6ce5a9b9 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -312,7 +312,6 @@ static cpp_macro *create_iso_definition (cpp_reader *); /* #define directive parsing and handling. */ -static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *); static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *); static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *, const cpp_macro *); @@ -3086,10 +3085,9 @@ _cpp_save_parameter (cpp_reader *pfile, unsigned n, cpp_hashnode *node, saved[n].value = node->value; saved[n].canonical_node = node; - if (BUFF_ROOM (pfile->a_buff) < (n + 1) * sizeof (cpp_hashnode *)) - _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *)); - - ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[n++] = spelling; + void *base = _cpp_reserve_room (pfile, n * sizeof (cpp_hashnode *), + sizeof (cpp_hashnode *)); + ((cpp_hashnode **)base)[n++] = spelling; /* Morph into a macro arg. */ node->flags |= NODE_MACRO_ARG; @@ -3236,32 +3234,23 @@ parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *varadic_ptr) return ok; } -/* Allocate room for a token from a macro's replacement list. */ -static cpp_token * -alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro) -{ - if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token)) - _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token)); - - return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++]; -} - /* Lex a token from the expansion of MACRO, but mark parameters as we find them and warn of traditional stringification. */ static cpp_token * lex_expansion_token (cpp_reader *pfile, cpp_macro *macro) { - cpp_token *token, *saved_cur_token; - - saved_cur_token = pfile->cur_token; - pfile->cur_token = alloc_expansion_token (pfile, macro); - token = _cpp_lex_direct (pfile); + void *base = _cpp_reserve_room (pfile, macro->count * sizeof (cpp_token), + sizeof (cpp_token)); + cpp_token *saved_cur_token = pfile->cur_token; + pfile->cur_token = &((cpp_token *) base)[macro->count++]; + cpp_token *token = _cpp_lex_direct (pfile); pfile->cur_token = saved_cur_token; /* Is this a parameter? */ if (token->type == CPP_NAME && (token->val.node.node->flags & NODE_MACRO_ARG) != 0) { + /* Morph into a parameter reference. */ cpp_hashnode *spelling = token->val.node.spelling; token->type = CPP_MACRO_ARG; token->val.macro_arg.arg_no = token->val.node.node->value.arg_index; @@ -3294,30 +3283,18 @@ create_iso_definition (cpp_reader *pfile) { /* An open-paren, get a parameter list. */ bool varadic = false; + + /* Drop the '(' token. */ + macro->count = 0; if (!parse_params (pfile, &nparms, &varadic)) goto out; - macro->parm.params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff); + macro->parm.params = (cpp_hashnode **)_cpp_commit_buff + (pfile, sizeof (cpp_hashnode *) * nparms); macro->variadic = varadic; macro->paramc = nparms; - - /* Success. Commit or allocate the parameter array. */ - if (pfile->hash_table->alloc_subobject) - { - cpp_hashnode **params = - (cpp_hashnode **) pfile->hash_table->alloc_subobject - (sizeof (cpp_hashnode *) * macro->paramc); - memcpy (params, macro->parm.params, - sizeof (cpp_hashnode *) * macro->paramc); - macro->parm.params = params; - } - else - BUFF_FRONT (pfile->a_buff) - = (uchar *) ¯o->parm.params[macro->paramc]; macro->fun_like = 1; - /* Drop the '(' token. */ - macro->count = 0; token = NULL; } else if (token->type != CPP_EOF @@ -3415,14 +3392,16 @@ create_iso_definition (cpp_reader *pfile) goto out; } - if (token[-1].flags & PASTE_LEFT) + if (following_paste_op) { - macro->extra_tokens = 1; + /* Consecutive paste operators. This one will be moved + to the end. */ num_extra_tokens++; token->val.token_no = macro->count - 1; } else { + /* Drop the paste operator. */ --macro->count; token[-1].flags |= PASTE_LEFT; if (token->flags & DIGRAPH) @@ -3430,56 +3409,50 @@ create_iso_definition (cpp_reader *pfile) if (token->flags & PREV_WHITE) token[-1].flags |= SP_PREV_WHITE; } + following_paste_op = true; } + else + following_paste_op = false; if (vaopt_tracker.update (token) == vaopt_state::ERROR) goto out; - - following_paste_op = (token->type == CPP_PASTE); } ok = true; - macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff); - /* Don't count the CPP_EOF. */ macro->count--; + macro->exp.tokens = (cpp_token *)_cpp_commit_buff + (pfile, sizeof (cpp_token) * macro->count); + /* Clear whitespace on first token for warn_of_redefinition(). */ if (macro->count) macro->exp.tokens[0].flags &= ~PREV_WHITE; - /* Commit or allocate the memory. */ - if (pfile->hash_table->alloc_subobject) + if (num_extra_tokens) { - cpp_token *tokns = - (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token) - * macro->count); - if (num_extra_tokens) - { - /* Place second and subsequent ## or %:%: tokens in - sequences of consecutive such tokens at the end of the - list to preserve information about where they appear, how - they are spelt and whether they are preceded by - whitespace without otherwise interfering with macro - expansion. */ - cpp_token *normal_dest = tokns; - cpp_token *extra_dest = tokns + macro->count - num_extra_tokens; - unsigned int i; - for (i = 0; i < macro->count; i++) - { - if (macro->exp.tokens[i].type == CPP_PASTE) - *extra_dest++ = macro->exp.tokens[i]; - else - *normal_dest++ = macro->exp.tokens[i]; - } - } - else - memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count); - macro->exp.tokens = tokns; + /* Place second and subsequent ## or %:%: tokens in sequences of + consecutive such tokens at the end of the list to preserve + information about where they appear, how they are spelt and + whether they are preceded by whitespace without otherwise + interfering with macro expansion. Remember, this is + extremely rare, so efficiency is not a priority. */ + cpp_token *temp = (cpp_token *)_cpp_reserve_room + (pfile, 0, num_extra_tokens * sizeof (cpp_token)); + unsigned extra_ix = 0, norm_ix = 0; + cpp_token *exp = macro->exp.tokens; + for (unsigned ix = 0; ix != macro->count; ix++) + if (exp[ix].type == CPP_PASTE) + temp[extra_ix++] = exp[ix]; + else + exp[norm_ix++] = exp[ix]; + memcpy (&exp[norm_ix], temp, num_extra_tokens * sizeof (cpp_token)); + + /* Record there are extra tokens. */ + macro->extra_tokens = 1; } - else - BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count]; + out: pfile->state.va_args_ok = 0; _cpp_unsave_parameters (pfile, nparms);