]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: modules and local static
authorJason Merrill <jason@redhat.com>
Tue, 26 Nov 2024 12:50:49 +0000 (07:50 -0500)
committerJason Merrill <jason@redhat.com>
Wed, 27 Nov 2024 04:53:37 +0000 (23:53 -0500)
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.

gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/static-3_a.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/static-3_b.C [new file with mode: 0644]

index e5955081d4d3148b6d512fccb55b445a36cc4459..ddede0fdd437cc6a8ca723c078ea186357a2c83a 100644 (file)
@@ -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_a.H b/gcc/testsuite/g++.dg/modules/static-3_a.H
new file mode 100644 (file)
index 0000000..e5a014e
--- /dev/null
@@ -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;
+}
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 (file)
index 0000000..70180f8
--- /dev/null
@@ -0,0 +1,8 @@
+// { dg-additional-options -fmodules }
+
+import "static-3_a.H";
+
+int main()
+{
+  afn();
+}