]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Don't use stoken in rust-parse.c
authorTom Tromey <tom@tromey.com>
Fri, 19 Dec 2025 14:33:30 +0000 (07:33 -0700)
committerTom Tromey <tom@tromey.com>
Mon, 22 Dec 2025 20:24:39 +0000 (13:24 -0700)
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 <simon.marchi@efficios.com>
gdb/parse.c
gdb/parser-defs.h
gdb/rust-parse.c

index f59d3e0f2d05d18ee98df5f7f63a090831b69b1d..7385cf6adcba537dce4152ee02b354e090f852f2 100644 (file)
@@ -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)
     {
index f5618f3a9ce82210779ff0f9ce88d1557b14457b..d44baa11a1989911381a1a0889aa4941e51a9780 100644 (file)
@@ -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);
index 2256cf89aa487c998b2424f4e5fa1570c12d6709..98dc75167e1fb6b1c817fbbf7d99ef157ff776a8 100644 (file)
@@ -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));