Introduce obstack_strndup

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.
This commit is contained in:
Tom Tromey 2019-07-13 12:13:15 -06:00
parent efba19b06a
commit 0cf9feb996
8 changed files with 43 additions and 23 deletions

View 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.

View 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;

View 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)

View 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);
}
}

View 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,

View 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
{

View 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. */

View 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)