]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Stream definitions for implicit instantiations [PR114170]
authorNathaniel Shead <nathanieloshead@gmail.com>
Fri, 1 Mar 2024 00:08:23 +0000 (11:08 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Fri, 1 Mar 2024 22:04:16 +0000 (09:04 +1100)
commit852b58552991099141f9df5782e1f28d8606af9d
tree9d323a1d3b8177202cbe7905232a07975365aea1
parent2823b4d96d9ec4ad4e67e5e8edaa1b060a467491
c++: Stream definitions for implicit instantiations [PR114170]

An implicit instantiation has an initializer depending on whether
DECL_INITIALIZED_P is set (like normal VAR_DECLs) which needs to be
written to ensure that consumers of header modules properly emit
definitions for these instantiations. This patch ensures that we
correctly fallback to checking this flag when DECL_INITIAL is not set
for a template instantiation.

For variables with non-trivial dynamic initialization, DECL_INITIAL can
be empty after 'split_nonconstant_init' but DECL_INITIALIZED_P is still
set; we need to check the latter to determine if we need to go looking
for a definition to emit (often in 'static_aggregates' here). This is
the case in the linked testcase.

However, for template specialisations (not instantiations?) we primarily
care about DECL_INITIAL; if the variable has initialization depending on
a template parameter then we'll need to emit that definition even though
it doesn't yet have DECL_INITIALIZED_P set; this is the case in e.g.

  template <int N> int value = N;

As a drive-by fix, also ensures that the count of initializers matches
the actual number of initializers written. This doesn't seem to be
necessary for correctness in the current testsuite, but feels wrong and
makes debugging harder when initializers aren't properly written for
other reasons.

PR c++/114170

gcc/cp/ChangeLog:

* module.cc (has_definition): Fall back to DECL_INITIALIZED_P
when DECL_INITIAL is not set on a template.
(module_state::write_inits): Only increment count when
initializers are actually written.

gcc/testsuite/ChangeLog:

* g++.dg/modules/var-tpl-2_a.H: New test.
* g++.dg/modules/var-tpl-2_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/var-tpl-2_a.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/var-tpl-2_b.C [new file with mode: 0644]