From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 03/13] gdb: modernize mangled_name_to_comp and cp_class_name_from_physname
Date: Tue, 20 Oct 2026 12:17:25 -0400 [thread overview]
Message-ID: <20261020162054.2140671-4-simon.marchi@efficios.com> (raw)
In-Reply-To: <20261020162054.2140671-1-simon.marchi@efficios.com>
- Move gdb_cplus_demangle_v3_components above mangled_name_to_comp,
removing the need for a forward declaration.
- Use nullptr instead of NULL, and fix the indentation of a return
statement in mangled_name_to_comp.
- In cp_class_name_from_physname, declare variables where they are
initialized, use bool instead of int for the loop flag, use for loops
instead of while loops to reduce the scope of the loop variables, and
return early when no class name is found.
Change-Id: I35be6a6907b15e353a5042e77f4fc6d168039c92
---
gdb/cp-support.c | 95 +++++++++++++++++++++---------------------------
1 file changed, 42 insertions(+), 53 deletions(-)
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 80f3f14c4086..3ddbc4083745 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -75,9 +75,6 @@ static void
canonicalization_ftype *finder,
void *data);
-static demangle_parse_info_up gdb_cplus_demangle_v3_components
- (const char *mangled, int options);
-
/* A convenience function to copy STRING into OBSTACK, returning a pointer
to the newly allocated string and saving the number of bytes saved in LEN.
@@ -664,6 +661,25 @@ cp_canonicalize_string (const char *string)
return us;
}
+/* A wrapper for cplus_demangle_v3_components that forces
+ DMGL_VERBOSE. */
+
+static demangle_parse_info_up
+gdb_cplus_demangle_v3_components (const char *mangled, int options)
+{
+ void *storage;
+ demangle_component *tree
+ = cplus_demangle_v3_components (mangled, options | DMGL_VERBOSE,
+ &storage);
+ if (tree == nullptr)
+ return nullptr;
+
+ auto info = std::make_unique<demangle_parse_info> ();
+ info->tree = tree;
+ info->demangler_storage.reset (storage);
+ return info;
+}
+
/* Convert a mangled name to a demangle_component tree. OPTIONS is passed to
the demangler.
@@ -685,18 +701,17 @@ mangled_name_to_comp (const char *mangled_name, int options)
/* If it doesn't, or if that failed, then try to demangle the
name. */
- gdb::unique_xmalloc_ptr<char> demangled_name = gdb_demangle (mangled_name,
- options);
- if (demangled_name == NULL)
- return NULL;
+ gdb::unique_xmalloc_ptr<char> demangled_name
+ = gdb_demangle (mangled_name, options);
+ if (demangled_name == nullptr)
+ return nullptr;
/* If we could demangle the name, parse it to build the component
tree. */
demangle_parse_info_up info
= cp_demangled_name_to_comp (demangled_name.get ());
-
- if (info == NULL)
- return NULL;
+ if (info == nullptr)
+ return nullptr;
info->demangled_name = std::move (demangled_name);
return info;
@@ -707,20 +722,15 @@ mangled_name_to_comp (const char *mangled_name, int options)
gdb::unique_xmalloc_ptr<char>
cp_class_name_from_physname (const char *physname)
{
- gdb::unique_xmalloc_ptr<char> ret;
- struct demangle_component *ret_comp, *prev_comp, *cur_comp;
demangle_parse_info_up info = mangled_name_to_comp (physname, DMGL_ANSI);
- int done;
-
if (info == nullptr)
return nullptr;
- done = 0;
- ret_comp = info->tree;
+ demangle_component *ret_comp = info->tree;
/* First strip off any qualifiers, if we have a function or
method. */
- while (!done)
+ for (bool done = false; !done;)
switch (ret_comp->type)
{
case DEMANGLE_COMPONENT_CONST:
@@ -733,7 +743,7 @@ cp_class_name_from_physname (const char *physname)
ret_comp = d_left (ret_comp);
break;
default:
- done = 1;
+ done = true;
break;
}
@@ -749,10 +759,9 @@ cp_class_name_from_physname (const char *physname)
/* What we have now should be a name, possibly qualified.
Additional qualifiers could live in the left subtree or the right
subtree. Find the last piece. */
- done = 0;
- prev_comp = NULL;
- cur_comp = ret_comp;
- while (!done)
+ demangle_component *prev_comp = nullptr;
+ demangle_component *cur_comp = ret_comp;
+ for (bool done = false; !done;)
switch (cur_comp->type)
{
case DEMANGLE_COMPONENT_QUAL_NAME:
@@ -766,24 +775,23 @@ cp_class_name_from_physname (const char *physname)
case DEMANGLE_COMPONENT_DTOR:
case DEMANGLE_COMPONENT_OPERATOR:
case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
- done = 1;
+ done = true;
break;
default:
- done = 1;
- cur_comp = NULL;
+ done = true;
+ cur_comp = nullptr;
break;
}
- if (cur_comp != NULL && prev_comp != NULL)
- {
- /* We want to discard the rightmost child of PREV_COMP. */
- *prev_comp = *d_left (prev_comp);
- /* The ten is completely arbitrary; we don't have a good
- estimate. */
- ret = cp_comp_to_string (ret_comp, 10);
- }
+ if (cur_comp == nullptr || prev_comp == nullptr)
+ return nullptr;
- return ret;
+ /* We want to discard the rightmost child of PREV_COMP. */
+ *prev_comp = *d_left (prev_comp);
+
+ /* The ten is completely arbitrary; we don't have a good
+ estimate. */
+ return cp_comp_to_string (ret_comp, 10);
}
/* Return the child of COMP which is the basename of a method,
@@ -1650,25 +1658,6 @@ gdb_cplus_demangle_print (int options,
estimated_length, &allocated_size));
}
-/* A wrapper for cplus_demangle_v3_components that forces
- DMGL_VERBOSE. */
-
-static demangle_parse_info_up
-gdb_cplus_demangle_v3_components (const char *mangled, int options)
-{
- void *storage;
- demangle_component *tree
- = cplus_demangle_v3_components (mangled, options | DMGL_VERBOSE,
- &storage);
- if (tree == nullptr)
- return nullptr;
-
- auto info = std::make_unique<demangle_parse_info> ();
- info->tree = tree;
- info->demangler_storage.reset (storage);
- return info;
-}
-
/* See cp-support.h. */
unsigned int
--
2.56.0
next prev parent reply other threads:[~2026-10-20 16:22 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-20 16:17 [PATCH 00/13] Make the C++ name parser recognize more char literal forms Simon Marchi
2026-10-20 16:17 ` [PATCH 01/13] gdb: make demangle_parse_info own the memory its tree points into Simon Marchi
2026-10-20 16:17 ` [PATCH 02/13] gdb: remove p_allocated_size parameter of gdb_cplus_demangle_print Simon Marchi
2026-10-20 16:17 ` Simon Marchi [this message]
2026-10-20 16:17 ` [PATCH 04/13] gdb: fix crash in "maint canonicalize" without argument Simon Marchi
2026-10-20 16:17 ` [PATCH 05/13] gdb: move C++ name parser parsing selftest to its own test Simon Marchi
2026-10-20 16:17 ` [PATCH 06/13] gdb: check exact results in the canonicalize selftest Simon Marchi
2026-10-21 15:21 ` Tom Tromey
2026-10-21 15:48 ` Simon Marchi
2026-10-21 16:56 ` Tom Tromey
2026-10-21 17:01 ` Simon Marchi
2026-10-21 17:48 ` Tom Tromey
2026-10-22 16:28 ` Simon Marchi
2026-10-20 16:17 ` [PATCH 07/13] gdb: extract lex_char_literal from cpname_yylex Simon Marchi
2026-10-21 15:22 ` Tom Tromey
2026-10-20 16:17 ` [PATCH 08/13] gdb: remove \^ escape sequence support from C++ name parser Simon Marchi
2026-10-20 16:17 ` [PATCH 09/13] gdb: remove backslash-newline handling from cp_parse_escape Simon Marchi
2026-10-20 16:17 ` [PATCH 10/13] gdb: fix out-of-bounds reads in lex_char_literal Simon Marchi
2026-10-20 16:17 ` [PATCH 11/13] gdb: handle hexadecimal escapes in C++ name parser Simon Marchi
2026-10-20 16:17 ` [PATCH 12/13] gdb: handle character literal encoding prefixes " Simon Marchi
2026-10-20 16:17 ` [PATCH 13/13] gdb: handle universal character names " Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261020162054.2140671-4-simon.marchi@efficios.com \
--to=simon.marchi@efficios.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox