Use unique_xmalloc_ptr in apply_ext_lang_type_printers

This changes apply_ext_lang_type_printers to use unique_xmalloc_ptr,
removing some manual memory management.  Regression tested on x86-64
Fedora 36.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
This commit is contained in:
Tom Tromey
2023-04-06 09:05:58 -06:00
parent b4f767131f
commit 55af06a70e
5 changed files with 19 additions and 19 deletions

View File

@@ -137,7 +137,7 @@ struct extension_language_ops
struct ext_lang_type_printers *); struct ext_lang_type_printers *);
/* Try to pretty-print TYPE. If successful the pretty-printed type is /* Try to pretty-print TYPE. If successful the pretty-printed type is
stored in *PRETTIED_TYPE, and the caller must free it. stored in *PRETTIED_TYPE.
Returns EXT_LANG_RC_OK upon success, EXT_LANG_RC_NOP if the type Returns EXT_LANG_RC_OK upon success, EXT_LANG_RC_NOP if the type
is not recognized, and EXT_LANG_RC_ERROR if an error was encountered. is not recognized, and EXT_LANG_RC_ERROR if an error was encountered.
This function has a bit of a funny name, since it actually applies This function has a bit of a funny name, since it actually applies
@@ -146,7 +146,8 @@ struct extension_language_ops
enum ext_lang_rc (*apply_type_printers) enum ext_lang_rc (*apply_type_printers)
(const struct extension_language_defn *, (const struct extension_language_defn *,
const struct ext_lang_type_printers *, const struct ext_lang_type_printers *,
struct type *, char **prettied_type); struct type *,
gdb::unique_xmalloc_ptr<char> *prettied_type);
/* Called after a type has been printed to give the type pretty-printer /* Called after a type has been printed to give the type pretty-printer
mechanism an opportunity to clean up. */ mechanism an opportunity to clean up. */

View File

@@ -418,13 +418,13 @@ ext_lang_type_printers::ext_lang_type_printers ()
returning the result of the first one that succeeds. returning the result of the first one that succeeds.
If there was an error, or if no printer succeeds, then NULL is returned. */ If there was an error, or if no printer succeeds, then NULL is returned. */
char * gdb::unique_xmalloc_ptr<char>
apply_ext_lang_type_printers (struct ext_lang_type_printers *printers, apply_ext_lang_type_printers (struct ext_lang_type_printers *printers,
struct type *type) struct type *type)
{ {
for (const struct extension_language_defn *extlang : extension_languages) for (const struct extension_language_defn *extlang : extension_languages)
{ {
char *result = NULL; gdb::unique_xmalloc_ptr<char> result;
enum ext_lang_rc rc; enum ext_lang_rc rc;
if (extlang->ops == nullptr if (extlang->ops == nullptr
@@ -435,7 +435,7 @@ apply_ext_lang_type_printers (struct ext_lang_type_printers *printers,
switch (rc) switch (rc)
{ {
case EXT_LANG_RC_OK: case EXT_LANG_RC_OK:
gdb_assert (result != NULL); gdb_assert (result != nullptr);
return result; return result;
case EXT_LANG_RC_ERROR: case EXT_LANG_RC_ERROR:
return NULL; return NULL;

View File

@@ -282,8 +282,8 @@ extern void eval_ext_lang_from_control_command (struct command_line *cmd);
extern void auto_load_ext_lang_scripts_for_objfile (struct objfile *); extern void auto_load_ext_lang_scripts_for_objfile (struct objfile *);
extern char *apply_ext_lang_type_printers (struct ext_lang_type_printers *, extern gdb::unique_xmalloc_ptr<char> apply_ext_lang_type_printers
struct type *); (struct ext_lang_type_printers *, struct type *);
extern int apply_ext_lang_val_pretty_printer extern int apply_ext_lang_val_pretty_printer
(struct value *value, struct ui_file *stream, int recurse, (struct value *value, struct ui_file *stream, int recurse,

View File

@@ -112,7 +112,8 @@ static void gdbpy_start_type_printers (const struct extension_language_defn *,
struct ext_lang_type_printers *); struct ext_lang_type_printers *);
static enum ext_lang_rc gdbpy_apply_type_printers static enum ext_lang_rc gdbpy_apply_type_printers
(const struct extension_language_defn *, (const struct extension_language_defn *,
const struct ext_lang_type_printers *, struct type *, char **); const struct ext_lang_type_printers *, struct type *,
gdb::unique_xmalloc_ptr<char> *);
static void gdbpy_free_type_printers (const struct extension_language_defn *, static void gdbpy_free_type_printers (const struct extension_language_defn *,
struct ext_lang_type_printers *); struct ext_lang_type_printers *);
static void gdbpy_set_quit_flag (const struct extension_language_defn *); static void gdbpy_set_quit_flag (const struct extension_language_defn *);
@@ -1700,7 +1701,7 @@ gdbpy_start_type_printers (const struct extension_language_defn *extlang,
/* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE /* If TYPE is recognized by some type printer, store in *PRETTIED_TYPE
a newly allocated string holding the type's replacement name, and return a newly allocated string holding the type's replacement name, and return
EXT_LANG_RC_OK. The caller is responsible for freeing the string. EXT_LANG_RC_OK.
If there's a Python error return EXT_LANG_RC_ERROR. If there's a Python error return EXT_LANG_RC_ERROR.
Otherwise, return EXT_LANG_RC_NOP. Otherwise, return EXT_LANG_RC_NOP.
This is the extension_language_ops.apply_type_printers "method". */ This is the extension_language_ops.apply_type_printers "method". */
@@ -1708,7 +1709,8 @@ gdbpy_start_type_printers (const struct extension_language_defn *extlang,
static enum ext_lang_rc static enum ext_lang_rc
gdbpy_apply_type_printers (const struct extension_language_defn *extlang, gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
const struct ext_lang_type_printers *ext_printers, const struct ext_lang_type_printers *ext_printers,
struct type *type, char **prettied_type) struct type *type,
gdb::unique_xmalloc_ptr<char> *prettied_type)
{ {
PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers; PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers;
gdb::unique_xmalloc_ptr<char> result; gdb::unique_xmalloc_ptr<char> result;
@@ -1763,7 +1765,7 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
return EXT_LANG_RC_ERROR; return EXT_LANG_RC_ERROR;
} }
*prettied_type = result.release (); *prettied_type = std::move (result);
return EXT_LANG_RC_OK; return EXT_LANG_RC_OK;
} }

View File

@@ -309,7 +309,6 @@ const char *
typedef_hash_table::find_global_typedef (const struct type_print_options *flags, typedef_hash_table::find_global_typedef (const struct type_print_options *flags,
struct type *t) struct type *t)
{ {
char *applied;
void **slot; void **slot;
struct decl_field tf, *new_tf; struct decl_field tf, *new_tf;
@@ -334,14 +333,12 @@ typedef_hash_table::find_global_typedef (const struct type_print_options *flags,
*slot = new_tf; *slot = new_tf;
applied = apply_ext_lang_type_printers (flags->global_printers, t); gdb::unique_xmalloc_ptr<char> applied
= apply_ext_lang_type_printers (flags->global_printers, t);
if (applied != NULL) if (applied != nullptr)
{ new_tf->name = obstack_strdup (&flags->global_typedefs->m_storage,
new_tf->name = obstack_strdup (&flags->global_typedefs->m_storage, applied.get ());
applied);
xfree (applied);
}
return new_tf->name; return new_tf->name;
} }