Introduce obstack_strndup
authorTom Tromey <tom@tromey.com>
Sat, 13 Jul 2019 18:13:15 +0000 (12:13 -0600)
committerTom Tromey <tom@tromey.com>
Wed, 7 Aug 2019 02:08:48 +0000 (20:08 -0600)
This introduces obstack_strndup and changes gdb to use it.

Note that obstack_strndup works like savestring, and not exactly like
xstrndup.  The difference is that obstack_strndup uses the passed-in
length, while xstrndup uses strnlen to choose the length.

gdb/ChangeLog
2019-08-06  Tom Tromey  <tom@tromey.com>

* stabsread.c (patch_block_stabs, read_one_struct_field)
(read_enum_type): Use obstack_strndup.
* rust-exp.y (rust_parser::copy_name): Use obstack_strndup.
* gdb_obstack.h (obstack_strndup): Use obstack_strndup.
* dwarf2read.c (guess_full_die_structure_name)
(anonymous_struct_prefix): Use obstack_strndup.
* dbxread.c (cp_set_block_scope): Use obstack_strndup.
* c-exp.y (yylex): Use obstack_strndup.
* ada-exp.y (write_object_renaming, write_ambiguous_var)
(write_var_or_type): Use obstack_strndup.

gdb/ChangeLog
gdb/ada-exp.y
gdb/c-exp.y
gdb/dbxread.c
gdb/dwarf2read.c
gdb/gdb_obstack.h
gdb/rust-exp.y
gdb/stabsread.c

index 31cd69dc85da5483dc76e25b97499c278bd3582d..82e0819c136dc42702e74c0714b399b5ab437436 100644 (file)
@@ -1,3 +1,16 @@
+2019-08-06  Tom Tromey  <tom@tromey.com>
+
+       * stabsread.c (patch_block_stabs, read_one_struct_field)
+       (read_enum_type): Use obstack_strndup.
+       * rust-exp.y (rust_parser::copy_name): Use obstack_strndup.
+       * gdb_obstack.h (obstack_strndup): Use obstack_strndup.
+       * dwarf2read.c (guess_full_die_structure_name)
+       (anonymous_struct_prefix): Use obstack_strndup.
+       * dbxread.c (cp_set_block_scope): Use obstack_strndup.
+       * c-exp.y (yylex): Use obstack_strndup.
+       * ada-exp.y (write_object_renaming, write_ambiguous_var)
+       (write_var_or_type): Use obstack_strndup.
+
 2019-08-06  Tom Tromey  <tom@tromey.com>
 
        * symfile.c (reread_symbols): Use obstack_strdup.
index 2c8f3d6572effc46d9da4370c3893961ff8f2319..f36aabaf15001a3dcc796f8521bcf14bb98d5807 100644 (file)
@@ -812,8 +812,8 @@ write_object_renaming (struct parser_state *par_state,
   if (orig_left_context == NULL)
     orig_left_context = get_selected_block (NULL);
 
-  name = (char *) obstack_copy0 (&temp_parse_space, renamed_entity,
-                                renamed_entity_len);
+  name = obstack_strndup (&temp_parse_space, renamed_entity,
+                         renamed_entity_len);
   ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info);
   if (sym_info.symbol == NULL)
     error (_("Could not find renamed variable: %s"), ada_decode (name));
@@ -881,9 +881,8 @@ write_object_renaming (struct parser_state *par_state,
            if (end == NULL)
              end = renaming_expr + strlen (renaming_expr);
 
-           index_name
-             = (char *) obstack_copy0 (&temp_parse_space, renaming_expr,
-                                       end - renaming_expr);
+           index_name = obstack_strndup (&temp_parse_space, renaming_expr,
+                                         end - renaming_expr);
            renaming_expr = end;
 
            ada_lookup_encoded_symbol (index_name, orig_left_context,
@@ -1107,8 +1106,7 @@ write_ambiguous_var (struct parser_state *par_state,
 
   memset (sym, 0, sizeof (struct symbol));
   SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN;
-  SYMBOL_LINKAGE_NAME (sym)
-    = (const char *) obstack_copy0 (&temp_parse_space, name, len);
+  SYMBOL_LINKAGE_NAME (sym) = obstack_strndup (&temp_parse_space, name, len);
   SYMBOL_LANGUAGE (sym) = language_ada;
 
   write_exp_elt_opcode (par_state, OP_VAR_VALUE);
@@ -1206,8 +1204,7 @@ write_var_or_type (struct parser_state *par_state,
 
   encoded_name = ada_encode (name0.ptr);
   name_len = strlen (encoded_name);
-  encoded_name
-    = (char *) obstack_copy0 (&temp_parse_space, encoded_name, name_len);
+  encoded_name = obstack_strndup (&temp_parse_space, encoded_name, name_len);
   for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
     {
       int tail_index;
index 439a72a10de426d4e8cbf853178b4baef0720411..e0abbd83ddb0ef32c4e99a2ea812313cf8ef721f 100644 (file)
@@ -3291,9 +3291,9 @@ yylex (void)
   if (checkpoint > 0)
     {
       current.value.sval.ptr
-       = (const char *) obstack_copy0 (&cpstate->expansion_obstack,
-                                       current.value.sval.ptr,
-                                       current.value.sval.length);
+       = obstack_strndup (&cpstate->expansion_obstack,
+                          current.value.sval.ptr,
+                          current.value.sval.length);
 
       token_fifo[0] = current;
       if (checkpoint > 1)
index 0b370cd2e3324fb61403ea879bdc1f40def01fc1..e339d1f7ecfaff594b0393080e38b7b7e8a77b61 100644 (file)
@@ -2354,8 +2354,7 @@ cp_set_block_scope (const struct symbol *symbol,
       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
       unsigned int prefix_len = cp_entire_prefix_len (name);
 
-      block_set_scope (block,
-                      (const char *) obstack_copy0 (obstack, name, prefix_len),
+      block_set_scope (block, obstack_strndup (obstack, name, prefix_len),
                       obstack);
     }
 }
index 6d28924e79bddfe76276b3658b70ffd49a0febc6..1c26f8355f53e218a7a1fc2a10c944e0c5320ebe 100644 (file)
@@ -22334,7 +22334,7 @@ guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
                      if (actual_name_len > die_name_len + 2
                          && actual_name[actual_name_len
                                         - die_name_len - 1] == ':')
-                       name = (char *) obstack_copy0 (
+                       name = obstack_strndup (
                          &objfile->per_bfd->storage_obstack,
                          actual_name, actual_name_len - die_name_len - 2);
                    }
@@ -22378,9 +22378,9 @@ anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
     return "";
 
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
-  return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
-                                DW_STRING (attr),
-                                &base[-1] - DW_STRING (attr));
+  return obstack_strndup (&objfile->per_bfd->storage_obstack,
+                         DW_STRING (attr),
+                         &base[-1] - DW_STRING (attr));
 }
 
 /* Return the name of the namespace/class that DIE is defined within,
index db849a06444c2c22138a39dc42157b1fc8149dfd..3d6a01099fe6599412968c909428776d4bf2f293 100644 (file)
@@ -105,6 +105,17 @@ obstack_strdup (struct obstack *obstackp, const std::string &string)
                                 string.size ());
 }
 
+/* Duplicate the first N characters of STRING, returning a
+   \0-terminated string that's allocated on the obstack OBSTACKP.
+   Note that exactly N characters are copied, even if STRING is
+   shorter.  */
+
+static inline char *
+obstack_strndup (struct obstack *obstackp, const char *string, size_t n)
+{
+  return (char *) obstack_copy0 (obstackp, string, n);
+}
+
 /* An obstack that frees itself on scope exit.  */
 struct auto_obstack : obstack
 {
index bf6566b8d2954aa7a5ae034c8322e735f6496580..60ce2cde057f21e64d6c357e1f4c981bdc964814 100644 (file)
@@ -1011,7 +1011,7 @@ static const struct token_info operator_tokens[] =
 const char *
 rust_parser::copy_name (const char *name, int len)
 {
-  return (const char *) obstack_copy0 (&obstack, name, len);
+  return obstack_strndup (&obstack, name, len);
 }
 
 /* Helper function to make an stoken from a C string.  */
index 23da5f94ebbbdb52477714a8c8786effe3c21107..48c88ded55374298b96da0543fd71198a24285fc 100644 (file)
@@ -426,8 +426,8 @@ patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,
              SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
              SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
              SYMBOL_SET_LINKAGE_NAME
-               (sym, (char *) obstack_copy0 (&objfile->objfile_obstack,
-                                             name, pp - name));
+               (sym, obstack_strndup (&objfile->objfile_obstack,
+                                      name, pp - name));
              pp += 2;
              if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
                {
@@ -2841,7 +2841,7 @@ read_one_struct_field (struct stab_field_info *fip, const char **pp,
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
 
   fip->list->field.name
-    = (const char *) obstack_copy0 (&objfile->objfile_obstack, *pp, p - *pp);
+    = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
   *pp = p + 1;
 
   /* This means we have a visibility for a field coming.  */
@@ -3641,7 +3641,7 @@ read_enum_type (const char **pp, struct type *type,
       p = *pp;
       while (*p != ':')
        p++;
-      name = (char *) obstack_copy0 (&objfile->objfile_obstack, *pp, p - *pp);
+      name = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
       *pp = p + 1;
       n = read_huge_number (pp, ',', &nbits, 0);
       if (nbits != 0)