Hi, please see the PR log for details. My symtab patch made testcase anon6.C to fail because it tests that unused static var is output into the assembly and we now optimize it out.
Adding attribute used however shows problem that the var suddenly becomes WEAK and COMDAT. It seems to me that this is bug in C++ FE. Watchpointing the decl I found that the decl is first correctly brought static by constrain_visibility but later it is exported again by mark_decl_instantiated. I think mark_decl_instantiated is wrong. I am not at all sure if the following patch is proper fix though. After this patch at least GCC agrees with clang on the testcase. Bootstrapped/regtested x86_64-linux, OK? Honza PR c++/57551 * cp/pt.c (mark_decl_instantiated): Do not export explicit instantiations of anonymous namespace templates. * g++.dg/ext/visibility/anon6.C: Update testcase. Index: cp/pt.c =================================================================== --- cp/pt.c (revision 199698) +++ cp/pt.c (working copy) @@ -17402,6 +17402,13 @@ mark_decl_instantiated (tree result, int if (TREE_ASM_WRITTEN (result)) return; + /* For anonymous namespace we don't need to do anything. */ + if (decl_anon_ns_mem_p (result)) + { + gcc_assert (!TREE_PUBLIC (result)); + return; + } + if (TREE_CODE (result) != FUNCTION_DECL) /* The TREE_PUBLIC flag for function declarations will have been set correctly by tsubst. */ Index: testsuite/g++.dg/ext/visibility/anon6.C =================================================================== --- testsuite/g++.dg/ext/visibility/anon6.C (revision 199698) +++ testsuite/g++.dg/ext/visibility/anon6.C (working copy) @@ -1,6 +1,8 @@ // PR c++/33094 // { dg-final { scan-assembler "1BIiE1cE" } } // { dg-final { scan-assembler-not "globl.*1BIiE1cE" } } +// { dg-final { scan-assembler-not "comdat" } } +// { dg-final { scan-assembler-not "weak" } } // { dg-final { scan-assembler-not "1CIiE1cE" } } // Test that B<int>::c is emitted as an internal symbol, and C<int>::c is @@ -18,7 +20,7 @@ namespace template <typename T> class B { - static const T c = 0; + __attribute__ ((__used__)) static const T c = 0; }; template <typename T> const T B<T>::c;