https://gcc.gnu.org/g:2fd9aef1db1a4260ee823bc3a3d4cfc22e95c543

commit r15-5714-g2fd9aef1db1a4260ee823bc3a3d4cfc22e95c543
Author: Jason Merrill <ja...@redhat.com>
Date:   Tue Nov 26 07:50:49 2024 -0500

    c++: modules and local static
    
    Here we weren't emitting the guard variable for 'a'  when we emitted 'afn'
    in the importer, because we only treated inline variables as needing that.
    Fixed by generalizing to vague_linkage_p.
    
    But we need to specifically exempt vtables, because the rest of the module
    code handles them specially and expects them to be DECL_EXTERNAL.
    
    gcc/cp/ChangeLog:
    
            * module.cc (trees_out::core_bools): Check vague_linkage_p.
            (has_definition): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/static-3_b.C: New test.
            * g++.dg/modules/static-3_a.H: New test.

Diff:
---
 gcc/cp/module.cc                          | 23 ++++++++++++-----------
 gcc/testsuite/g++.dg/modules/static-3_b.C |  8 ++++++++
 gcc/testsuite/g++.dg/modules/static-3_a.H | 13 +++++++++++++
 3 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index e5955081d4d3..ddede0fdd437 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -5521,16 +5521,13 @@ trees_out::core_bools (tree t, bits_out& bits)
 
            case VAR_DECL:
              if (TREE_PUBLIC (t)
-                 && !(TREE_STATIC (t)
-                      && DECL_FUNCTION_SCOPE_P (t)
-                      && DECL_DECLARED_INLINE_P (DECL_CONTEXT (t)))
-                 && !DECL_VAR_DECLARED_INLINE_P (t))
+                 && DECL_VTABLE_OR_VTT_P (t))
+               /* We handle vtable linkage specially.  */
                is_external = true;
-             break;
-
+             gcc_fallthrough ();
            case FUNCTION_DECL:
              if (TREE_PUBLIC (t)
-                 && !DECL_DECLARED_INLINE_P (t))
+                 && !vague_linkage_p (t))
                is_external = true;
              break;
            }
@@ -11950,11 +11947,15 @@ has_definition (tree decl)
               since there's no TU to emit them in otherwise.  */
            return true;
 
-         if (!decl_maybe_constant_var_p (decl)
-             && !DECL_INLINE_VAR_P (decl))
-           return false;
+         if (decl_maybe_constant_var_p (decl))
+           /* We might need its constant value.  */
+           return true;
 
-         return true;
+         if (vague_linkage_p (decl))
+           /* These are emitted as needed.  */
+           return true;
+
+         return false;
        }
       break;
 
diff --git a/gcc/testsuite/g++.dg/modules/static-3_b.C 
b/gcc/testsuite/g++.dg/modules/static-3_b.C
new file mode 100644
index 000000000000..70180f8eb323
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/static-3_b.C
@@ -0,0 +1,8 @@
+// { dg-additional-options -fmodules }
+
+import "static-3_a.H";
+
+int main()
+{
+  afn();
+}
diff --git a/gcc/testsuite/g++.dg/modules/static-3_a.H 
b/gcc/testsuite/g++.dg/modules/static-3_a.H
new file mode 100644
index 000000000000..e5a014e93732
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/static-3_a.H
@@ -0,0 +1,13 @@
+// { dg-additional-options -fmodules }
+// { dg-module-do link }
+
+inline int i;
+
+struct A {
+  A() { ++i; }
+};
+
+inline A& afn() {
+  static A a;
+  return a;
+}

Reply via email to