diff options
author | Tom Tromey <tromey@adacore.com> | 2021-10-04 08:44:22 -0600 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2021-10-04 13:45:38 -0600 |
commit | 3456e70c9d69da8d62a0ea1f8c2e643648afc654 (patch) | |
tree | 7959a8dc460685d2e8e3197478eddb4430d48708 /gdb/cp-support.c | |
parent | e133de4984cef9acb32fd756c607aa2fa1d090fb (diff) | |
download | gdb-3456e70c9d69da8d62a0ea1f8c2e643648afc654.zip gdb-3456e70c9d69da8d62a0ea1f8c2e643648afc654.tar.gz gdb-3456e70c9d69da8d62a0ea1f8c2e643648afc654.tar.bz2 |
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.
Diffstat (limited to 'gdb/cp-support.c')
-rw-r--r-- | gdb/cp-support.c | 29 |
1 files changed, 12 insertions, 17 deletions
diff --git a/gdb/cp-support.c b/gdb/cp-support.c index fb4c836..367fb6a 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -661,10 +661,9 @@ cp_canonicalize_string (const char *string) static std::unique_ptr<demangle_parse_info> 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 to trees. */ 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 name. */ - demangled_name = gdb_demangle (mangled_name, options); + gdb::unique_xmalloc_ptr<char> demangled_name = gdb_demangle (mangled_name, + options); if (demangled_name == NULL) return NULL; /* If we could demangle the name, parse it to build the component tree. */ 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) - { - xfree (demangled_name); - return NULL; - } + return NULL; - *demangled_p = demangled_name; + *demangled_p = std::move (demangled_name); return info; } @@ -709,7 +706,7 @@ char * cp_class_name_from_physname (const char *physname) { void *storage = NULL; - char *demangled_name = NULL; + gdb::unique_xmalloc_ptr<char> demangled_name; gdb::unique_xmalloc_ptr<char> ret; struct demangle_component *ret_comp, *prev_comp, *cur_comp; std::unique_ptr<demangle_parse_info> info; @@ -789,7 +786,6 @@ cp_class_name_from_physname (const char *physname) } xfree (storage); - xfree (demangled_name); return ret.release (); } @@ -857,7 +853,7 @@ char * method_name_from_physname (const char *physname) { void *storage = NULL; - char *demangled_name = NULL; + gdb::unique_xmalloc_ptr<char> demangled_name; gdb::unique_xmalloc_ptr<char> ret; struct demangle_component *ret_comp; 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); xfree (storage); - xfree (demangled_name); return ret.release (); } @@ -1604,10 +1599,10 @@ report_failed_demangle (const char *name, bool core_dump_allowed, /* A wrapper for bfd_demangle. */ -char * +gdb::unique_xmalloc_ptr<char> gdb_demangle (const char *name, int options) { - char *result = NULL; + gdb::unique_xmalloc_ptr<char> result; int crash_signal = 0; #ifdef HAVE_WORKING_FORK @@ -1636,7 +1631,7 @@ gdb_demangle (const char *name, int options) #endif if (crash_signal == 0) - result = bfd_demangle (NULL, name, options); + result.reset (bfd_demangle (NULL, name, options)); #ifdef HAVE_WORKING_FORK if (catch_demangler_crashes) |