[2/3] Don't use stoken in rust-parse.c
Commit Message
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.
---
gdb/parse.c | 30 +++++++++++++++++-------------
gdb/parser-defs.h | 4 ++++
gdb/rust-parse.c | 18 +++++++-----------
3 files changed, 28 insertions(+), 24 deletions(-)
Comments
On 2025-12-21 14:12, Tom Tromey wrote:
> 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.
LGTM.
Approved-By: Simon Marchi <simon.marchi@efficios.com>
Simon
@@ -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<expr::last_operation> (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<expr::register_operation> (copy_name (str));
+ push_new<expr::register_operation> (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)
{
@@ -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);
@@ -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<std::pair<std::string, operation_up>> field_v;
- size_t len = current_string_val.length;
+ size_t len = current_string_val.length ();
operation_up str = make_operation<string_operation> (get_string ());
operation_up addr
= make_operation<rust_unop_addr_operation> (std::move (str));