[PATCH 27/40] Make cp_remove_params return a unique_ptr

Pedro Alves
2017-06-02T12:23:10Z

I'm going to add a variant of cp_remove_params in a following patch,
and while at it, it seems nice to eliminate a few related cleanups as
preparatory work.

gdb/ChangeLog:
yyyy-mm-dd Pedro Alves <palves@redhat.com>

* cp-support.c (cp_remove_params): Return a gdb::unique_xmalloc_ptr.
Use bool.
(overload_list_add_symbol): Adjust to use gdb::unique_xmalloc_ptr.
* cp-support.h (cp_remove_params): Now returns a
gdb::unique_xmalloc_ptr.
* dwarf2read.c (find_slot_in_mapped_hash): Now returns bool.
Adjust to use gdb::unique_xmalloc_ptr.
(dw2_expand_symtabs_matching_symbol): Adjust to use
gdb::unique_xmalloc_ptr.
* psymtab.c (psymtab_search_name): Now returns a
gdb::unique_xmalloc_ptr.
(lookup_partial_symbol): Adjust to use gdb::unique_xmalloc_ptr.
* stack.c (find_frame_funname): Adjust to use
gdb::unique_xmalloc_ptr.
---
gdb/cp-support.c | 19 +++++++------------
gdb/cp-support.h | 2 +-
gdb/dwarf2read.c | 23 ++++++++---------------
gdb/psymtab.c | 31 ++++++++++---------------------
gdb/stack.c | 18 ++++++++----------
5 files changed, 34 insertions(+), 59 deletions(-)

diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 95e7cb8..0095c6d 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -837,10 +837,10 @@ cp_func_name (const char *full_name)
(optionally) a return type. Return the name of the function without
parameters or return type, or NULL if we can not parse the name. */

-char *
+gdb::unique_xmalloc_ptr<char>
cp_remove_params (const char *demangled_name)
{
- int done = 0;
+ bool done = false;
struct demangle_component *ret_comp;
std::unique_ptr<demangle_parse_info> info;
char *ret = NULL;
@@ -867,7 +867,7 @@ cp_remove_params (const char *demangled_name)
ret_comp = d_left (ret_comp);
break;
default:
- done = 1;
+ done = true;
break;
}

@@ -875,7 +875,7 @@ cp_remove_params (const char *demangled_name)
if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
ret = cp_comp_to_string (d_left (ret_comp), 10);

- return ret;
+ return gdb::unique_xmalloc_ptr<char> (ret);
}

/* Here are some random pieces of trivia to keep in mind while trying
@@ -1102,7 +1102,7 @@ overload_list_add_symbol (struct symbol *sym,
{
int newsize;
int i;
- char *sym_name;
+ gdb::unique_xmalloc_ptr<char> sym_name;

/* If there is no type information, we can't do anything, so
skip. */
@@ -1121,13 +1121,8 @@ overload_list_add_symbol (struct symbol *sym,
return;

/* skip symbols that cannot match */
- if (strcmp (sym_name, oload_name) != 0)
- {
- xfree (sym_name);
- return;
- }
-
- xfree (sym_name);
+ if (strcmp (sym_name.get (), oload_name) != 0)
+ return;

/* We have a match for an overload instance, so add SYM to the
current list of overload instances */
diff --git a/gdb/cp-support.h b/gdb/cp-support.h
index 3a42cd6..dd42415 100644
--- a/gdb/cp-support.h
+++ b/gdb/cp-support.h
@@ -95,7 +95,7 @@ extern unsigned int cp_entire_prefix_len (const char *name);

extern char *cp_func_name (const char *full_name);

-extern char *cp_remove_params (const char *demangled_name);
+extern gdb::unique_xmalloc_ptr<char> cp_remove_params (const char *qualified);

extern struct symbol **make_symbol_overload_list (const char *,
const char *);
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index e955131..dfa79aa 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -3168,16 +3168,17 @@ mapped_index_string_hash (int index_version, const void *p)

/* Find a slot in the mapped index INDEX for the object named NAME.
If NAME is found, set *VEC_OUT to point to the CU vector in the
- constant pool and return 1. If NAME cannot be found, return 0. */
+ constant pool and return true. If NAME cannot be found, return
+ false. */

-static int
+static bool
find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
offset_type **vec_out)
{
- struct cleanup *back_to = make_cleanup (null_cleanup, 0);
offset_type hash;
offset_type slot, step;
int (*cmp) (const char *, const char *);
+ gdb::unique_xmalloc_ptr<char> without_params;

if (current_language->la_language == language_cplus
|| current_language->la_language == language_fortran
@@ -3188,13 +3189,9 @@ find_slot_in_mapped_hash (struct mapped_index *index, const char *name,

if (strchr (name, '(') != NULL)
{
- char *without_params = cp_remove_params (name);
-
+ without_params = cp_remove_params (name);
if (without_params != NULL)
- {
- make_cleanup (xfree, without_params);
- name = without_params;
- }
+ name = without_params.get ();
}
}

@@ -3216,18 +3213,14 @@ find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
offset_type i = 2 * slot;
const char *str;
if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
- {
- do_cleanups (back_to);
- return 0;
- }
+ return false;

str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
if (!cmp (name, str))
{
*vec_out = (offset_type *) (index->constant_pool
+ MAYBE_SWAP (index->symbol_table[i + 1]));
- do_cleanups (back_to);
- return 1;
+ return true;
}

slot = (slot + step) & (index->symbol_table_slots - 1);
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index b853266..1a1929d 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -642,11 +642,9 @@ match_partial_symbol (struct objfile *objfile,
not contain any method/function instance information (since this would
force reading type information while reading psymtabs). Therefore,
if NAME contains overload information, it must be stripped before searching
- psymtabs.
+ psymtabs. */

- The caller is responsible for freeing the return result. */
-
-static char *
+static gdb::unique_xmalloc_ptr<char>
psymtab_search_name (const char *name)
{
switch (current_language->la_language)
@@ -655,7 +653,7 @@ psymtab_search_name (const char *name)
{
if (strchr (name, '('))
{
- char *ret = cp_remove_params (name);
+ gdb::unique_xmalloc_ptr<char> ret = cp_remove_params (name);

if (ret)
return ret;
@@ -667,7 +665,7 @@ psymtab_search_name (const char *name)
break;
}

- return xstrdup (name);
+ return gdb::unique_xmalloc_ptr<char> (xstrdup (name));
}

/* Look, in partial_symtab PST, for symbol whose natural name is NAME.
@@ -682,16 +680,14 @@ lookup_partial_symbol (struct objfile *objfile,
struct partial_symbol **top, **real_top, **bottom, **center;
int length = (global ? pst->n_global_syms : pst->n_static_syms);
int do_linear_search = 1;
- char *search_name;
- struct cleanup *cleanup;

if (length == 0)
return NULL;

- search_name = psymtab_search_name (name);
- cleanup = make_cleanup (xfree, search_name);
+ gdb::unique_xmalloc_ptr<char> search_name = psymtab_search_name (name);

- lookup_name_info lookup_name (search_name, symbol_name_match_type::FULL);
+ lookup_name_info lookup_name (search_name.get (),
+ symbol_name_match_type::FULL);

start = (global ?
objfile->global_psymbols.list + pst->globals_offset :
@@ -717,7 +713,7 @@ lookup_partial_symbol (struct objfile *objfile,
internal_error (__FILE__, __LINE__,
_("failed internal consistency check"));
if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center),
- search_name) >= 0)
+ search_name.get ()) >= 0)
{
top = center;
}
@@ -742,10 +738,7 @@ lookup_partial_symbol (struct objfile *objfile,
{
if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
SYMBOL_DOMAIN (*top), domain))
- {
- do_cleanups (cleanup);
- return *top;
- }
+ return *top;
top++;
}
}
@@ -760,14 +753,10 @@ lookup_partial_symbol (struct objfile *objfile,
if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
SYMBOL_DOMAIN (*psym), domain)
&& SYMBOL_MATCHES_SEARCH_NAME (*psym, lookup_name))
- {
- do_cleanups (cleanup);
- return *psym;
- }
+ return *psym;
}
}

- do_cleanups (cleanup);
return NULL;
}

diff --git a/gdb/stack.c b/gdb/stack.c
index 7f8a51c..cff4351 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -1106,15 +1106,15 @@ find_frame_funname (struct frame_info *frame, char **funname,
stored in the symbol table, but we stored a version
with DMGL_PARAMS turned on, and here we don't want to
display parameters. So remove the parameters. */
- char *func_only = cp_remove_params (print_name);
+ gdb::unique_xmalloc_ptr<char> func_only
+ = cp_remove_params (print_name);

if (func_only)
- *funname = func_only;
+ *funname = func_only.release ();
}

- /* If we didn't hit the C++ case above, set *funname here.
- This approach is taken to avoid having to install a
- cleanup in case cp_remove_params can throw. */
+ /* If we didn't hit the C++ case above, set *funname
+ here. */
if (*funname == NULL)
*funname = xstrdup (print_name);
}
@@ -1415,6 +1415,7 @@ frame_info (char *addr_exp, int from_tty)
/* Initialize it to avoid "may be used uninitialized" warning. */
CORE_ADDR caller_pc = 0;
int caller_pc_p = 0;
+ gdb::unique_xmalloc_ptr<char> func_only;

fi = parse_frame_specification (addr_exp, &selected_frame_p);
gdbarch = get_frame_arch (fi);
@@ -1448,13 +1449,10 @@ frame_info (char *addr_exp, int from_tty)
stored in the symbol table, but we stored a version
with DMGL_PARAMS turned on, and here we don't want to
display parameters. So remove the parameters. */
- char *func_only = cp_remove_params (funname);
+ func_only = cp_remove_params (funname);

if (func_only)
- {
- funname = func_only;
- make_cleanup (xfree, func_only);
- }
+ funname = func_only.get ();
}
}
else if (frame_pc_p)
--
2.5.5