From: Tom Tromey Date: Fri, 19 Dec 2025 14:33:30 +0000 (-0700) Subject: Don't use stoken in rust-parse.c X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f4c562f7f44ef8a073360a53ff64a9237051f7d2;p=thirdparty%2Fbinutils-gdb.git Don't use stoken in rust-parse.c I wanted to change rust-parse.c to use string_view and not stoken. This was simple enough, but it seemed good to also change parser_state::push_dollar to use string_view; and as it turned out, this simplified the code a little. Approved-By: Simon Marchi --- diff --git a/gdb/parse.c b/gdb/parse.c index f59d3e0f2d0..7385cf6adcb 100644 --- a/gdb/parse.c +++ b/gdb/parse.c @@ -160,10 +160,17 @@ parser_state::push_symbol (const char *name, block_symbol sym) void parser_state::push_dollar (struct stoken str) +{ + push_dollar (std::string_view (str.ptr, str.length)); +} + +/* See parser-defs.h. */ + +void +parser_state::push_dollar (std::string_view str) { struct block_symbol sym; struct internalvar *isym = NULL; - std::string copy; /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1) and $$digits (equivalent to $<-digits> if you could type that). */ @@ -172,12 +179,12 @@ parser_state::push_dollar (struct stoken str) int i = 1; /* Double dollar means negate the number and add -1 as well. Thus $$ alone means -1. */ - if (str.length >= 2 && str.ptr[1] == '$') + if (str.length () >= 2 && str[1] == '$') { negate = 1; i = 2; } - if (i == str.length) + if (i == str.length ()) { /* Just dollars (one or two). */ i = -negate; @@ -185,12 +192,12 @@ parser_state::push_dollar (struct stoken str) return; } /* Is the rest of the token digits? */ - for (; i < str.length; i++) - if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9')) + for (; i < str.length (); i++) + if (!(str[i] >= '0' && str[i] <= '9')) break; - if (i == str.length) + if (i == str.length ()) { - i = atoi (str.ptr + 1 + negate); + i = atoi (&str[1 + negate]); if (negate) i = -i; push_new (i); @@ -199,13 +206,10 @@ parser_state::push_dollar (struct stoken str) /* Handle tokens that refer to machine registers: $ followed by a register name. */ - i = user_reg_map_name_to_regnum (gdbarch (), - { str.ptr + 1, size_t (str.length - 1) }); + i = user_reg_map_name_to_regnum (gdbarch (), str.substr (1)); if (i >= 0) { - str.length--; - str.ptr++; - push_new (copy_name (str)); + push_new (std::string (str.substr (1))); block_tracker->update (expression_context_block, INNERMOST_BLOCK_FOR_REGISTERS); return; @@ -213,7 +217,7 @@ parser_state::push_dollar (struct stoken str) /* Any names starting with $ are probably debugger internal variables. */ - copy = copy_name (str); + std::string copy (str); isym = lookup_only_internalvar (copy.c_str () + 1); if (isym) { diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h index f5618f3a9ce..d44baa11a19 100644 --- a/gdb/parser-defs.h +++ b/gdb/parser-defs.h @@ -222,6 +222,10 @@ struct parser_state : public expr_builder symbol. */ void push_symbol (const char *name, block_symbol sym); + /* Push a reference to $mumble. This may result in a convenience + variable, a history reference, or a register. */ + void push_dollar (std::string_view str); + /* Push a reference to $mumble. This may result in a convenience variable, a history reference, or a register. */ void push_dollar (struct stoken str); diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c index 2256cf89aa4..98dc75167e1 100644 --- a/gdb/rust-parse.c +++ b/gdb/rust-parse.c @@ -332,7 +332,7 @@ struct rust_parser /* Return the token's string value as a string. */ std::string get_string () const { - return std::string (current_string_val.ptr, current_string_val.length); + return std::string (current_string_val); } /* Storage for use while parsing. */ @@ -349,7 +349,7 @@ struct rust_parser /* The current token's payload, if any. */ typed_val_int current_int_val {}; typed_val_float current_float_val {}; - struct stoken current_string_val {}; + std::string_view current_string_val; enum exp_opcode current_opcode = OP_NULL; /* When completing, this may be set to the field operation to @@ -751,8 +751,8 @@ rust_parser::lex_string () } } - current_string_val.length = obstack_object_size (&obstack); - current_string_val.ptr = (const char *) obstack_finish (&obstack); + size_t size = obstack_object_size (&obstack); + current_string_val = { (const char *) obstack_finish (&obstack), size }; return is_byte ? BYTESTRING : STRING; } @@ -854,10 +854,7 @@ rust_parser::lex_identifier () } if (token == NULL || (pstate->parse_completion && pstate->lexptr[0] == '\0')) - { - current_string_val.length = length; - current_string_val.ptr = start; - } + current_string_val = { start, length }; if (pstate->parse_completion && pstate->lexptr[0] == '\0') { @@ -1082,8 +1079,7 @@ rust_parser::lex_one_token (bool decimal_only) { if (pstate->parse_completion) { - current_string_val.length =0; - current_string_val.ptr = ""; + current_string_val = ""; return COMPLETE; } return 0; @@ -1960,7 +1956,7 @@ rust_parser::parse_string () std::vector> field_v; - size_t len = current_string_val.length; + size_t len = current_string_val.length (); operation_up str = make_operation (get_string ()); operation_up addr = make_operation (std::move (str));