https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118904
--- Comment #1 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Nathaniel Shead <nsh...@gcc.gnu.org>: https://gcc.gnu.org/g:0337e3c2743ca0c57da8c6b78b725a7d83f0b721 commit r16-1738-g0337e3c2743ca0c57da8c6b78b725a7d83f0b721 Author: Nathaniel Shead <nathanielosh...@gmail.com> Date: Wed May 21 01:18:49 2025 +1000 c++/modules: Avoid name clashes when streaming internal labels [PR98375,PR118904] The frontend creates some variables that need to be given unique names for the TU so that they can unambiguously be accessed. Historically this has been done with a global counter local to each place that needs an internal label, but this doesn't work with modules as depending on what declarations have been imported, some counter values may have already been used. This patch reworks the situation to instead have a single collection of counters for the TU, and a new function 'generate_internal_label' that gets the next label with given prefix using that counter. Modules streaming can then use this function to regenerate new names on stream-in for any such decls, guaranteeing uniqueness within the TU. These labels should only be used for internal entities so there should be no issues with the names differing from TU to TU; we will need to handle this if we ever start checking ODR of definitions we're merging but that's an issue for later. For proof of concept, this patch makes use of the new API for __builtin_source_location and ubsan; there are probably other places in the frontend where this change will need to be made as well. One other change this exposes is that both of these components rely on the definition of the VAR_DECLs they create, so stream that too for uncontexted variables. PR c++/98735 PR c++/118904 gcc/cp/ChangeLog: * cp-gimplify.cc (source_location_id): Remove. (fold_builtin_source_location): Use generate_internal_label. * module.cc (enum tree_tag): Add 'tt_internal_id' enumerator. (trees_out::tree_value): Adjust assertion, write definitions of uncontexted VAR_DECLs. (trees_in::tree_value): Read variable definitions. (trees_out::tree_node): Write internal labels, adjust assert. (trees_in::tree_node): Read internal labels. gcc/ChangeLog: * tree.cc (struct identifier_hash): New type. (struct identifier_count_traits): New traits. (internal_label_nums): New hash map. (generate_internal_label): New function. (prefix_for_internal_label): New function. * tree.h (IDENTIFIER_INTERNAL_P): New macro. (generate_internal_label): Declare. (prefix_for_internal_label): Declare. * ubsan.cc (ubsan_ids): Remove. (ubsan_type_descriptor): Use generate_internal_label. (ubsan_create_data): Likewise. gcc/testsuite/ChangeLog: * g++.dg/modules/src-loc-1.h: New test. * g++.dg/modules/src-loc-1_a.H: New test. * g++.dg/modules/src-loc-1_b.C: New test. * g++.dg/modules/src-loc-1_c.C: New test. * g++.dg/modules/ubsan-1_a.C: New test. * g++.dg/modules/ubsan-1_b.C: New test. * g++.dg/ubsan/module-1-aux.cc: New test. * g++.dg/ubsan/module-1.C: New test. Signed-off-by: Nathaniel Shead <nathanielosh...@gmail.com> Reviewed-by: Jason Merrill <ja...@redhat.com>