]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++/modules: Avoid name clashes when streaming internal labels [PR98375,PR118904]
authorNathaniel Shead <nathanieloshead@gmail.com>
Tue, 20 May 2025 15:18:49 +0000 (01:18 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Fri, 27 Jun 2025 14:35:00 +0000 (00:35 +1000)
commit0337e3c2743ca0c57da8c6b78b725a7d83f0b721
tree20b969942276a78f648081da7f69e1d69dc96f97
parent48a9567e4100fd6a734089087c139d51b63b7531
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 <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
13 files changed:
gcc/cp/cp-gimplify.cc
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/src-loc-1.h [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/src-loc-1_a.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/src-loc-1_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/src-loc-1_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/ubsan-1_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/ubsan-1_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ubsan/module-1-aux.cc [new file with mode: 0644]
gcc/testsuite/g++.dg/ubsan/module-1.C [new file with mode: 0644]
gcc/tree.cc
gcc/tree.h
gcc/ubsan.cc