On 9/6/26 10:50 AM, Léo Hardt wrote:
This patch was approved by Caroline Tice, the maintainer of VTV.
https://gcc.gnu.org/pipermail/gcc-patches/2026-September/730139.html
I recreated it so that the diffs match the line numbers in the
current code. Since I don't have write access, could someone push it?
Corrected patch below the dotted line.
Thanks!
Please use -- 8< -- to separate email intro from commit message for git am.
--------
vtv: Fix Memory Leak and Other Improvements
Currently, vtable_find_or_create_map_decl, given a class decl,
calls get_mangled_vtable_map_var_name (from mangle.cc) to:
(1) create a VTV map entry for the given class; and
(2) return the expected DECL_ASSEMBLER_NAME of its VTV tracker.
Here are some improvements for that logic:
a. (2) was only needed once for each class, and in that case, we
call get_identifier (var_name). Now we only generate the identifier
iff it is needed.
b. Since get_identifier copies data,var_name was always leaked.
This is now done without heap allocation.
c. get_identifier was called twice for the same symbol, which
costs an extra hash table lookup. Now we store it in a variable.
d. "vtable-class-hierarchy" and "mangle" depend on each other.
This is just because (1) is done inside mangle.cc. By moving that
statement to vtable-class-hierarchy, we avoid one extra circular
dependency.
The effect of vtable_find_or_create_map_decl has not changed.
Successfully bootstrapped with "--enable-vtable-verify".
gcc/cp/ChangeLog:
* cp-tree.h (get_mangled_vtable_map_var_name): Only get the
name, without side effects.
(get_mangled_type_name_if_anon): Split off from function above.
* mangle.cc (get_mangled_type_name_if_anon): Only get the name,
without side effects.
git gcc-verify rejects these ChangeLog entries due to extra spaces after
the initial tab. And the Signed-off-by seems to have gotten lost since
your v2.
diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc
index b2697f27c32..2fe580b463b 100644
--- a/gcc/cp/mangle.cc
+++ b/gcc/cp/mangle.cc
@@ -5417,40 +5416,41 @@ mangle_template_parm_object (tree expr)
}
/* Given a CLASS_TYPE, such as a record for std::bad_exception this
- function generates a mangled name for the vtable map variable of
- the class type. For example, if the class type is
- "std::bad_exception", the mangled name for the class is
- "St13bad_exception". This function would generate the name
- "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE", which unmangles as:
- "_VTV<std::bad_exception>::__vtable_map". */
+ function returns NULL_TREE if TYPE_NAME (CLASS_TYPE) is not from
+ an anonymous namespace. Otherwise, returns the mangled type name.
+ */
*/ goes at the end of the last line, not on a line by itself.
+tree
+get_mangled_type_name_if_anon(tree class_type)
git gcc-style flags this missing space before (, along with other true
positives.
+ {
+ gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
+ tree class_id = DECL_ASSEMBLER_NAME (TYPE_NAME (class_type));
+ bool is_anon = strstr (IDENTIFIER_POINTER (class_id), "<anon>") != NULL;
+ return is_anon ? get_mangled_id (TYPE_NAME (class_type)) : NULL_TREE;
+}
-char *
-get_mangled_vtable_map_var_name (tree class_type)
+/* Given a CLASS_TYPE_ASSEMBLER_NAME, such as of the TYPE_NAME of a
How about
"Given CLASS_TYPE_ASSEMBLER_NAME, the DECL_ASSEMBLER_NAME of the
TYPE_NAME of a class,"
+ record for std::bad_exception, this function generates a mangled
+ identifier for the vtable map variable of the class type.
+ For example, if the class type is "std::bad_exception", the mangled
+ name for the class is "St13bad_exception". This function would
+ generate the name "_ZN4_VTVISt13bad_exceptionE12__vtable_mapE",
+ which unmangles as "_VTV<std::bad_exception>::__vtable_map". */
+
+tree
+get_mangled_vtable_map_var_name (tree class_type_assembler_name)
diff --git a/gcc/cp/vtable-class-hierarchy.cc b/gcc/cp/vtable-class-
hierarchy.cc
index 2e52ea3c4a0..37ce5623806 100644
--- a/gcc/cp/vtable-class-hierarchy.cc
+++ b/gcc/cp/vtable-class-hierarchy.cc
@@ -1204,15 +1204,22 @@ vtv_generate_init_routine (void)
struct vtbl_map_node *
vtable_find_or_create_map_decl (tree base_type)
{
- char *var_name = NULL;
struct vtbl_map_node *vtable_map_node = NULL;
+ tree base_type_name = DECL_ASSEMBLER_NAME (TYPE_NAME (base_type));
This variable should move after the early exit along with
mangled_base_type_name, since DECL_ASSEMBLER_NAME might cause mangling.
/* Verify the type has an associated vtable. */
if (!TYPE_BINFO (base_type) || !BINFO_VTABLE (TYPE_BINFO (base_type)))
return NULL;
/* Create map lookup symbol for base class */
- var_name = get_mangled_vtable_map_var_name (base_type);
+ tree mangled_base_type_name = get_mangled_type_name_if_anon (base_type);
Jason