Use unique_xmalloc_ptr<char> when demangling

I noticed that some methods in language_defn could use
unique_xmalloc_ptr<char> rather than a plain 'char *'.  This patch
implements this change, fixing up the fallout and changing
gdb_demangle to also return this type.  In one spot, std::string is
used to simplify some related code, and in another, an auto_obstack is
used to avoid manual management.

Regression tested on x86-64 Fedora 34.
This commit is contained in:
Tom Tromey 2021-10-04 08:44:22 -06:00
parent e133de4984
commit 3456e70c9d
23 changed files with 118 additions and 136 deletions

View file

@ -1324,12 +1324,6 @@ ada_decode_symbol (const struct general_symbol_info *arg)
return *resultp; return *resultp;
} }
static char *
ada_la_decode (const char *encoded, int options)
{
return xstrdup (ada_decode (encoded).c_str ());
}
/* Arrays */ /* Arrays */
@ -13116,8 +13110,9 @@ public:
} }
/* See language.h. */ /* See language.h. */
bool sniff_from_mangled_name (const char *mangled, bool sniff_from_mangled_name
char **out) const override (const char *mangled,
gdb::unique_xmalloc_ptr<char> *out) const override
{ {
std::string demangled = ada_decode (mangled); std::string demangled = ada_decode (mangled);
@ -13155,9 +13150,10 @@ public:
/* See language.h. */ /* See language.h. */
char *demangle_symbol (const char *mangled, int options) const override gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override
{ {
return ada_la_decode (mangled, options); return make_unique_xstrdup (ada_decode (mangled).c_str ());
} }
/* See language.h. */ /* See language.h. */

View file

@ -944,8 +944,9 @@ public:
} }
/* See language.h. */ /* See language.h. */
bool sniff_from_mangled_name (const char *mangled, bool sniff_from_mangled_name
char **demangled) const override (const char *mangled,
gdb::unique_xmalloc_ptr<char> *demangled) const override
{ {
*demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI); *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
return *demangled != NULL; return *demangled != NULL;
@ -953,7 +954,8 @@ public:
/* See language.h. */ /* See language.h. */
char *demangle_symbol (const char *mangled, int options) const override gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override
{ {
return gdb_demangle (mangled, options); return gdb_demangle (mangled, options);
} }

View file

@ -674,16 +674,16 @@ is_type_conversion_operator (struct type *type, int i, int j)
form. Even the author of this function feels that writing little form. Even the author of this function feels that writing little
parsers like this everywhere is stupid. */ parsers like this everywhere is stupid. */
static char * static const char *
remove_qualifiers (char *qid) remove_qualifiers (const char *qid)
{ {
int quoted = 0; /* Zero if we're not in quotes; int quoted = 0; /* Zero if we're not in quotes;
'"' if we're in a double-quoted string; '"' if we're in a double-quoted string;
'\'' if we're in a single-quoted string. */ '\'' if we're in a single-quoted string. */
int depth = 0; /* Number of unclosed parens we've seen. */ int depth = 0; /* Number of unclosed parens we've seen. */
char *parenstack = (char *) alloca (strlen (qid)); char *parenstack = (char *) alloca (strlen (qid));
char *scan; const char *scan;
char *last = 0; /* The character after the rightmost const char *last = 0; /* The character after the rightmost
`::' token we've seen so far. */ `::' token we've seen so far. */
for (scan = qid; *scan; scan++) for (scan = qid; *scan; scan++)
@ -1257,7 +1257,6 @@ c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
{ {
const char *mangled_name; const char *mangled_name;
gdb::unique_xmalloc_ptr<char> mangled_name_holder; gdb::unique_xmalloc_ptr<char> mangled_name_holder;
char *demangled_name;
const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j); const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
int is_full_physname_constructor = int is_full_physname_constructor =
TYPE_FN_FIELD_CONSTRUCTOR (f, j) TYPE_FN_FIELD_CONSTRUCTOR (f, j)
@ -1311,9 +1310,9 @@ c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
else else
mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j); mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
demangled_name = gdb::unique_xmalloc_ptr<char> demangled_name
gdb_demangle (mangled_name, = gdb_demangle (mangled_name,
DMGL_ANSI | DMGL_PARAMS); DMGL_ANSI | DMGL_PARAMS);
if (demangled_name == NULL) if (demangled_name == NULL)
{ {
/* In some cases (for instance with the HP /* In some cases (for instance with the HP
@ -1340,9 +1339,9 @@ c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
} }
else else
{ {
char *p; const char *p;
char *demangled_no_class const char *demangled_no_class
= remove_qualifiers (demangled_name); = remove_qualifiers (demangled_name.get ());
/* Get rid of the `static' appended by the /* Get rid of the `static' appended by the
demangler. */ demangler. */
@ -1350,19 +1349,12 @@ c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
if (p != NULL) if (p != NULL)
{ {
int length = p - demangled_no_class; int length = p - demangled_no_class;
char *demangled_no_static; std::string demangled_no_static (demangled_no_class,
length);
demangled_no_static fputs_filtered (demangled_no_static.c_str (), stream);
= (char *) xmalloc (length + 1);
strncpy (demangled_no_static,
demangled_no_class, length);
*(demangled_no_static + length) = '\0';
fputs_filtered (demangled_no_static, stream);
xfree (demangled_no_static);
} }
else else
fputs_filtered (demangled_no_class, stream); fputs_filtered (demangled_no_class, stream);
xfree (demangled_name);
} }
fprintf_filtered (stream, ";\n"); fprintf_filtered (stream, ";\n");

View file

@ -661,10 +661,9 @@ cp_canonicalize_string (const char *string)
static std::unique_ptr<demangle_parse_info> static std::unique_ptr<demangle_parse_info>
mangled_name_to_comp (const char *mangled_name, int options, mangled_name_to_comp (const char *mangled_name, int options,
void **memory, char **demangled_p) void **memory,
gdb::unique_xmalloc_ptr<char> *demangled_p)
{ {
char *demangled_name;
/* If it looks like a v3 mangled name, then try to go directly /* If it looks like a v3 mangled name, then try to go directly
to trees. */ to trees. */
if (mangled_name[0] == '_' && mangled_name[1] == 'Z') if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
@ -684,22 +683,20 @@ mangled_name_to_comp (const char *mangled_name, int options,
/* If it doesn't, or if that failed, then try to demangle the /* If it doesn't, or if that failed, then try to demangle the
name. */ name. */
demangled_name = gdb_demangle (mangled_name, options); gdb::unique_xmalloc_ptr<char> demangled_name = gdb_demangle (mangled_name,
options);
if (demangled_name == NULL) if (demangled_name == NULL)
return NULL; return NULL;
/* If we could demangle the name, parse it to build the component /* If we could demangle the name, parse it to build the component
tree. */ tree. */
std::unique_ptr<demangle_parse_info> info std::unique_ptr<demangle_parse_info> info
= cp_demangled_name_to_comp (demangled_name, NULL); = cp_demangled_name_to_comp (demangled_name.get (), NULL);
if (info == NULL) if (info == NULL)
{ return NULL;
xfree (demangled_name);
return NULL;
}
*demangled_p = demangled_name; *demangled_p = std::move (demangled_name);
return info; return info;
} }
@ -709,7 +706,7 @@ char *
cp_class_name_from_physname (const char *physname) cp_class_name_from_physname (const char *physname)
{ {
void *storage = NULL; void *storage = NULL;
char *demangled_name = NULL; gdb::unique_xmalloc_ptr<char> demangled_name;
gdb::unique_xmalloc_ptr<char> ret; gdb::unique_xmalloc_ptr<char> ret;
struct demangle_component *ret_comp, *prev_comp, *cur_comp; struct demangle_component *ret_comp, *prev_comp, *cur_comp;
std::unique_ptr<demangle_parse_info> info; std::unique_ptr<demangle_parse_info> info;
@ -789,7 +786,6 @@ cp_class_name_from_physname (const char *physname)
} }
xfree (storage); xfree (storage);
xfree (demangled_name);
return ret.release (); return ret.release ();
} }
@ -857,7 +853,7 @@ char *
method_name_from_physname (const char *physname) method_name_from_physname (const char *physname)
{ {
void *storage = NULL; void *storage = NULL;
char *demangled_name = NULL; gdb::unique_xmalloc_ptr<char> demangled_name;
gdb::unique_xmalloc_ptr<char> ret; gdb::unique_xmalloc_ptr<char> ret;
struct demangle_component *ret_comp; struct demangle_component *ret_comp;
std::unique_ptr<demangle_parse_info> info; std::unique_ptr<demangle_parse_info> info;
@ -875,7 +871,6 @@ method_name_from_physname (const char *physname)
ret = cp_comp_to_string (ret_comp, 10); ret = cp_comp_to_string (ret_comp, 10);
xfree (storage); xfree (storage);
xfree (demangled_name);
return ret.release (); return ret.release ();
} }
@ -1604,10 +1599,10 @@ report_failed_demangle (const char *name, bool core_dump_allowed,
/* A wrapper for bfd_demangle. */ /* A wrapper for bfd_demangle. */
char * gdb::unique_xmalloc_ptr<char>
gdb_demangle (const char *name, int options) gdb_demangle (const char *name, int options)
{ {
char *result = NULL; gdb::unique_xmalloc_ptr<char> result;
int crash_signal = 0; int crash_signal = 0;
#ifdef HAVE_WORKING_FORK #ifdef HAVE_WORKING_FORK
@ -1636,7 +1631,7 @@ gdb_demangle (const char *name, int options)
#endif #endif
if (crash_signal == 0) if (crash_signal == 0)
result = bfd_demangle (NULL, name, options); result.reset (bfd_demangle (NULL, name, options));
#ifdef HAVE_WORKING_FORK #ifdef HAVE_WORKING_FORK
if (catch_demangler_crashes) if (catch_demangler_crashes)

View file

@ -188,6 +188,6 @@ extern struct cmd_list_element *maint_cplus_cmd_list;
/* A wrapper for bfd_demangle. */ /* A wrapper for bfd_demangle. */
char *gdb_demangle (const char *name, int options); gdb::unique_xmalloc_ptr<char> gdb_demangle (const char *name, int options);
#endif /* CP_SUPPORT_H */ #endif /* CP_SUPPORT_H */

View file

@ -51,7 +51,7 @@ d_main_name (void)
/* Implements the la_demangle language_defn routine for language D. */ /* Implements the la_demangle language_defn routine for language D. */
char * gdb::unique_xmalloc_ptr<char>
d_demangle (const char *symbol, int options) d_demangle (const char *symbol, int options)
{ {
return gdb_demangle (symbol, options | DMGL_DLANG); return gdb_demangle (symbol, options | DMGL_DLANG);
@ -126,8 +126,9 @@ public:
} }
/* See language.h. */ /* See language.h. */
bool sniff_from_mangled_name (const char *mangled, bool sniff_from_mangled_name
char **demangled) const override (const char *mangled,
gdb::unique_xmalloc_ptr<char> *demangled) const override
{ {
*demangled = d_demangle (mangled, 0); *demangled = d_demangle (mangled, 0);
return *demangled != NULL; return *demangled != NULL;
@ -135,7 +136,8 @@ public:
/* See language.h. */ /* See language.h. */
char *demangle_symbol (const char *mangled, int options) const override gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override
{ {
return d_demangle (mangled, options); return d_demangle (mangled, options);
} }

View file

@ -62,7 +62,8 @@ extern int d_parse (struct parser_state *);
extern const char *d_main_name (void); extern const char *d_main_name (void);
extern char *d_demangle (const char *mangled, int options); extern gdb::unique_xmalloc_ptr<char> d_demangle (const char *mangled,
int options);
extern const struct builtin_d_type *builtin_d_type (struct gdbarch *); extern const struct builtin_d_type *builtin_d_type (struct gdbarch *);

View file

@ -10174,9 +10174,8 @@ dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
to look up their definition from their declaration so to look up their definition from their declaration so
the only disadvantage remains the minimal symbol variant the only disadvantage remains the minimal symbol variant
`long name(params)' does not have the proper inferior type. */ `long name(params)' does not have the proper inferior type. */
demangled.reset (gdb_demangle (mangled, demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
(DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
| DMGL_RET_DROP)));
} }
if (demangled) if (demangled)
canon = demangled.get (); canon = demangled.get ();

View file

@ -67,7 +67,8 @@ public:
/* See language.h. */ /* See language.h. */
char *demangle_symbol (const char *mangled, int options) const override gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override
{ {
/* We could support demangling here to provide module namespaces /* We could support demangling here to provide module namespaces
also for inferiors with only minimal symbol table (ELF symbols). also for inferiors with only minimal symbol table (ELF symbols).

View file

@ -160,7 +160,6 @@ is_cplus_marker (int c)
static void static void
demangle_command (const char *args, int from_tty) demangle_command (const char *args, int from_tty)
{ {
char *demangled;
const char *name; const char *name;
const char *arg_start; const char *arg_start;
int processing_args = 1; int processing_args = 1;
@ -202,12 +201,10 @@ demangle_command (const char *args, int from_tty)
else else
lang = current_language; lang = current_language;
demangled = language_demangle (lang, name, DMGL_ANSI | DMGL_PARAMS); gdb::unique_xmalloc_ptr<char> demangled
= language_demangle (lang, name, DMGL_ANSI | DMGL_PARAMS);
if (demangled != NULL) if (demangled != NULL)
{ printf_filtered ("%s\n", demangled.get ());
printf_filtered ("%s\n", demangled);
xfree (demangled);
}
else else
error (_("Can't demangle \"%s\""), name); error (_("Can't demangle \"%s\""), name);
} }

View file

@ -3078,8 +3078,8 @@ check_stub_method (struct type *type, int method_id, int signature_id)
struct gdbarch *gdbarch = type->arch (); struct gdbarch *gdbarch = type->arch ();
struct fn_field *f; struct fn_field *f;
char *mangled_name = gdb_mangle_name (type, method_id, signature_id); char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
char *demangled_name = gdb_demangle (mangled_name, gdb::unique_xmalloc_ptr<char> demangled_name
DMGL_PARAMS | DMGL_ANSI); = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
char *argtypetext, *p; char *argtypetext, *p;
int depth = 0, argcount = 1; int depth = 0, argcount = 1;
struct field *argtypes; struct field *argtypes;
@ -3087,7 +3087,7 @@ check_stub_method (struct type *type, int method_id, int signature_id)
/* Make sure we got back a function string that we can use. */ /* Make sure we got back a function string that we can use. */
if (demangled_name) if (demangled_name)
p = strchr (demangled_name, '('); p = strchr (demangled_name.get (), '(');
else else
p = NULL; p = NULL;
@ -3178,8 +3178,6 @@ check_stub_method (struct type *type, int method_id, int signature_id)
argtypes, argcount, p[-2] == '.'); argtypes, argcount, p[-2] == '.');
mtype->set_is_stub (false); mtype->set_is_stub (false);
TYPE_FN_FIELD_STUB (f, signature_id) = 0; TYPE_FN_FIELD_STUB (f, signature_id) = 0;
xfree (demangled_name);
} }
/* This is the external interface to check_stub_method, above. This /* This is the external interface to check_stub_method, above. This

View file

@ -189,7 +189,7 @@ gnuv2_value_rtti_type (struct value *v, int *full, LONGEST *top, int *using_enc)
struct type *rtti_type; struct type *rtti_type;
CORE_ADDR vtbl; CORE_ADDR vtbl;
struct bound_minimal_symbol minsym; struct bound_minimal_symbol minsym;
char *demangled_name, *p; char *p;
const char *linkage_name; const char *linkage_name;
struct type *btype; struct type *btype;
struct type *known_type_vptr_basetype; struct type *known_type_vptr_basetype;
@ -248,14 +248,15 @@ gnuv2_value_rtti_type (struct value *v, int *full, LONGEST *top, int *using_enc)
return NULL; return NULL;
/* If we just skip the prefix, we get screwed by namespaces. */ /* If we just skip the prefix, we get screwed by namespaces. */
demangled_name=gdb_demangle(linkage_name,DMGL_PARAMS|DMGL_ANSI); gdb::unique_xmalloc_ptr<char> demangled_name
p = strchr (demangled_name, ' '); = gdb_demangle(linkage_name,DMGL_PARAMS|DMGL_ANSI);
p = strchr (demangled_name.get (), ' ');
if (p) if (p)
*p = '\0'; *p = '\0';
/* Lookup the type for the name. */ /* Lookup the type for the name. */
/* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */ /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
rtti_type = cp_lookup_rtti_type (demangled_name, NULL); rtti_type = cp_lookup_rtti_type (demangled_name.get (), NULL);
if (rtti_type == NULL) if (rtti_type == NULL)
return NULL; return NULL;

View file

@ -644,17 +644,14 @@ gnuv3_print_method_ptr (const gdb_byte *contents,
possible paths to the method based on the adjustment. */ possible paths to the method based on the adjustment. */
if (physname) if (physname)
{ {
char *demangled_name = gdb_demangle (physname, gdb::unique_xmalloc_ptr<char> demangled_name
DMGL_ANSI | DMGL_PARAMS); = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
fprintf_filtered (stream, "&virtual "); fprintf_filtered (stream, "&virtual ");
if (demangled_name == NULL) if (demangled_name == NULL)
fputs_filtered (physname, stream); fputs_filtered (physname, stream);
else else
{ fputs_filtered (demangled_name.get (), stream);
fputs_filtered (demangled_name, stream);
xfree (demangled_name);
}
return; return;
} }
} }

View file

@ -333,12 +333,9 @@ unpack_mangled_go_symbol (const char *mangled_name,
This demangler can't work in all situations, This demangler can't work in all situations,
thus not too much effort is currently put into it. */ thus not too much effort is currently put into it. */
char * gdb::unique_xmalloc_ptr<char>
go_language::demangle_symbol (const char *mangled_name, int options) const go_language::demangle_symbol (const char *mangled_name, int options) const
{ {
struct obstack tempbuf;
char *result;
char *name_buf;
const char *package_name; const char *package_name;
const char *object_name; const char *object_name;
const char *method_type_package_name; const char *method_type_package_name;
@ -348,15 +345,16 @@ go_language::demangle_symbol (const char *mangled_name, int options) const
if (mangled_name == NULL) if (mangled_name == NULL)
return NULL; return NULL;
name_buf = unpack_mangled_go_symbol (mangled_name, gdb::unique_xmalloc_ptr<char> name_buf
&package_name, &object_name, (unpack_mangled_go_symbol (mangled_name,
&method_type_package_name, &package_name, &object_name,
&method_type_object_name, &method_type_package_name,
&method_type_is_pointer); &method_type_object_name,
&method_type_is_pointer));
if (name_buf == NULL) if (name_buf == NULL)
return NULL; return NULL;
obstack_init (&tempbuf); auto_obstack tempbuf;
/* Print methods as they appear in "method expressions". */ /* Print methods as they appear in "method expressions". */
if (method_type_package_name != NULL) if (method_type_package_name != NULL)
@ -380,10 +378,7 @@ go_language::demangle_symbol (const char *mangled_name, int options) const
} }
obstack_grow_str0 (&tempbuf, ""); obstack_grow_str0 (&tempbuf, "");
result = xstrdup ((const char *) obstack_finish (&tempbuf)); return make_unique_xstrdup ((const char *) obstack_finish (&tempbuf));
obstack_free (&tempbuf, NULL);
xfree (name_buf);
return result;
} }
/* Given a Go symbol, return its package or NULL if unknown. /* Given a Go symbol, return its package or NULL if unknown.

View file

@ -94,8 +94,9 @@ public:
/* See language.h. */ /* See language.h. */
bool sniff_from_mangled_name (const char *mangled, bool sniff_from_mangled_name
char **demangled) const override (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
const override
{ {
*demangled = demangle_symbol (mangled, 0); *demangled = demangle_symbol (mangled, 0);
return *demangled != NULL; return *demangled != NULL;
@ -103,7 +104,8 @@ public:
/* See language.h. */ /* See language.h. */
char *demangle_symbol (const char *mangled, int options) const override; gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override;
/* See language.h. */ /* See language.h. */

View file

@ -551,7 +551,7 @@ skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
more flexible demangler for the languages that need it. more flexible demangler for the languages that need it.
FIXME: Sometimes the demangler is invoked when we don't know the FIXME: Sometimes the demangler is invoked when we don't know the
language, so we can't use this everywhere. */ language, so we can't use this everywhere. */
char * gdb::unique_xmalloc_ptr<char>
language_demangle (const struct language_defn *current_language, language_demangle (const struct language_defn *current_language,
const char *mangled, int options) const char *mangled, int options)
{ {
@ -771,7 +771,8 @@ public:
/* See language.h. */ /* See language.h. */
char *demangle_symbol (const char *mangled, int options) const override gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override
{ {
/* The auto language just uses the C++ demangler. */ /* The auto language just uses the C++ demangler. */
return gdb_demangle (mangled, options); return gdb_demangle (mangled, options);

View file

@ -430,15 +430,16 @@ struct language_defn
The resulting string should be of the form that will be The resulting string should be of the form that will be
installed into a symbol. */ installed into a symbol. */
virtual bool sniff_from_mangled_name (const char *mangled, virtual bool sniff_from_mangled_name
char **demangled) const (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled) const
{ {
*demangled = nullptr; *demangled = nullptr;
return false; return false;
} }
/* Return demangled language symbol version of MANGLED, or NULL. */ /* Return demangled language symbol version of MANGLED, or NULL. */
virtual char *demangle_symbol (const char *mangled, int options) const virtual gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const
{ {
return nullptr; return nullptr;
} }
@ -791,8 +792,9 @@ extern const char *language_str (enum language);
extern CORE_ADDR skip_language_trampoline (struct frame_info *, CORE_ADDR pc); extern CORE_ADDR skip_language_trampoline (struct frame_info *, CORE_ADDR pc);
/* Return demangled language symbol, or NULL. */ /* Return demangled language symbol, or NULL. */
extern char *language_demangle (const struct language_defn *current_language, extern gdb::unique_xmalloc_ptr<char> language_demangle
const char *mangled, int options); (const struct language_defn *current_language,
const char *mangled, int options);
/* Return information about whether TYPE should be passed /* Return information about whether TYPE should be passed
(and returned) by reference at the language level. */ (and returned) by reference at the language level. */

View file

@ -1473,10 +1473,11 @@ minimal_symbol_reader::install ()
if (!msym->name_set) if (!msym->name_set)
{ {
/* This will be freed later, by compute_and_set_names. */ /* This will be freed later, by compute_and_set_names. */
char *demangled_name gdb::unique_xmalloc_ptr<char> demangled_name
= symbol_find_demangled_name (msym, msym->linkage_name ()); = symbol_find_demangled_name (msym, msym->linkage_name ());
msym->set_demangled_name msym->set_demangled_name
(demangled_name, &m_objfile->per_bfd->storage_obstack); (demangled_name.release (),
&m_objfile->per_bfd->storage_obstack);
msym->name_set = 1; msym->name_set = 1;
} }
/* This mangled_name_hash computation has to be outside of /* This mangled_name_hash computation has to be outside of

View file

@ -251,8 +251,9 @@ public:
} }
/* See language.h. */ /* See language.h. */
bool sniff_from_mangled_name (const char *mangled, bool sniff_from_mangled_name
char **demangled) const override (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
const override
{ {
*demangled = demangle_symbol (mangled, 0); *demangled = demangle_symbol (mangled, 0);
return *demangled != NULL; return *demangled != NULL;
@ -260,7 +261,8 @@ public:
/* See language.h. */ /* See language.h. */
char *demangle_symbol (const char *mangled, int options) const override; gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override;
/* See language.h. */ /* See language.h. */
@ -318,7 +320,7 @@ public:
/* See declaration of objc_language::demangle_symbol above. */ /* See declaration of objc_language::demangle_symbol above. */
char * gdb::unique_xmalloc_ptr<char>
objc_language::demangle_symbol (const char *mangled, int options) const objc_language::demangle_symbol (const char *mangled, int options) const
{ {
char *demangled, *cp; char *demangled, *cp;
@ -376,7 +378,7 @@ objc_language::demangle_symbol (const char *mangled, int options) const
*cp++ = ']'; /* closing right brace */ *cp++ = ']'; /* closing right brace */
*cp++ = 0; /* string terminator */ *cp++ = 0; /* string terminator */
return demangled; return gdb::unique_xmalloc_ptr<char> (demangled);
} }
else else
return nullptr; /* Not an objc mangled name. */ return nullptr; /* Not an objc mangled name. */

View file

@ -84,8 +84,9 @@ public:
/* See language.h. */ /* See language.h. */
bool sniff_from_mangled_name (const char *mangled, bool sniff_from_mangled_name
char **demangled) const override (const char *mangled, gdb::unique_xmalloc_ptr<char> *demangled)
const override
{ {
*demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI); *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
return *demangled != NULL; return *demangled != NULL;
@ -93,7 +94,8 @@ public:
/* See language.h. */ /* See language.h. */
char *demangle_symbol (const char *mangled, int options) const override gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
int options) const override
{ {
return gdb_demangle (mangled, options); return gdb_demangle (mangled, options);
} }

View file

@ -821,11 +821,11 @@ create_demangled_names_hash (struct objfile_per_bfd_storage *per_bfd)
/* See symtab.h */ /* See symtab.h */
char * gdb::unique_xmalloc_ptr<char>
symbol_find_demangled_name (struct general_symbol_info *gsymbol, symbol_find_demangled_name (struct general_symbol_info *gsymbol,
const char *mangled) const char *mangled)
{ {
char *demangled = NULL; gdb::unique_xmalloc_ptr<char> demangled;
int i; int i;
if (gsymbol->language () == language_unknown) if (gsymbol->language () == language_unknown)
@ -931,8 +931,8 @@ general_symbol_info::compute_and_set_names (gdb::string_view linkage_name,
linkage_name_copy = linkage_name; linkage_name_copy = linkage_name;
if (demangled_name.get () == nullptr) if (demangled_name.get () == nullptr)
demangled_name.reset demangled_name
(symbol_find_demangled_name (this, linkage_name_copy.data ())); = symbol_find_demangled_name (this, linkage_name_copy.data ());
/* Suppose we have demangled_name==NULL, copy_name==0, and /* Suppose we have demangled_name==NULL, copy_name==0, and
linkage_name_copy==linkage_name. In this case, we already have the linkage_name_copy==linkage_name. In this case, we already have the
@ -1858,9 +1858,10 @@ demangle_for_lookup (const char *name, enum language lang,
lookup, so we can always binary search. */ lookup, so we can always binary search. */
if (lang == language_cplus) if (lang == language_cplus)
{ {
char *demangled_name = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS); gdb::unique_xmalloc_ptr<char> demangled_name
= gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
if (demangled_name != NULL) if (demangled_name != NULL)
return storage.set_malloc_ptr (demangled_name); return storage.set_malloc_ptr (std::move (demangled_name));
/* If we were given a non-mangled name, canonicalize it /* If we were given a non-mangled name, canonicalize it
according to the language (so far only for C++). */ according to the language (so far only for C++). */
@ -1870,16 +1871,16 @@ demangle_for_lookup (const char *name, enum language lang,
} }
else if (lang == language_d) else if (lang == language_d)
{ {
char *demangled_name = d_demangle (name, 0); gdb::unique_xmalloc_ptr<char> demangled_name = d_demangle (name, 0);
if (demangled_name != NULL) if (demangled_name != NULL)
return storage.set_malloc_ptr (demangled_name); return storage.set_malloc_ptr (std::move (demangled_name));
} }
else if (lang == language_go) else if (lang == language_go)
{ {
char *demangled_name gdb::unique_xmalloc_ptr<char> demangled_name
= language_def (language_go)->demangle_symbol (name, 0); = language_def (language_go)->demangle_symbol (name, 0);
if (demangled_name != NULL) if (demangled_name != NULL)
return storage.set_malloc_ptr (demangled_name); return storage.set_malloc_ptr (std::move (demangled_name));
} }
return name; return name;

View file

@ -592,8 +592,8 @@ extern CORE_ADDR get_symbol_address (const struct symbol *sym);
then set the language appropriately. The returned name is allocated then set the language appropriately. The returned name is allocated
by the demangler and should be xfree'd. */ by the demangler and should be xfree'd. */
extern char *symbol_find_demangled_name (struct general_symbol_info *gsymbol, extern gdb::unique_xmalloc_ptr<char> symbol_find_demangled_name
const char *mangled); (struct general_symbol_info *gsymbol, const char *mangled);
/* Return true if NAME matches the "search" name of SYMBOL, according /* Return true if NAME matches the "search" name of SYMBOL, according
to the symbol's language. */ to the symbol's language. */

View file

@ -2345,8 +2345,6 @@ void
fprintf_symbol_filtered (struct ui_file *stream, const char *name, fprintf_symbol_filtered (struct ui_file *stream, const char *name,
enum language lang, int arg_mode) enum language lang, int arg_mode)
{ {
char *demangled;
if (name != NULL) if (name != NULL)
{ {
/* If user wants to see raw output, no problem. */ /* If user wants to see raw output, no problem. */
@ -2356,12 +2354,9 @@ fprintf_symbol_filtered (struct ui_file *stream, const char *name,
} }
else else
{ {
demangled = language_demangle (language_def (lang), name, arg_mode); gdb::unique_xmalloc_ptr<char> demangled
fputs_filtered (demangled ? demangled : name, stream); = language_demangle (language_def (lang), name, arg_mode);
if (demangled != NULL) fputs_filtered (demangled ? demangled.get () : name, stream);
{
xfree (demangled);
}
} }
} }
} }