]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Fix std::filesystem errors with -fkeep-inline-functions [PR108636]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 2 Feb 2023 14:06:40 +0000 (14:06 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 2 Feb 2023 16:18:37 +0000 (16:18 +0000)
With -fkeep-inline-functions there are linker errors when including
<filesystem>. This happens because there are some filesystem::path
constructors defined inline which call non-exported functions defined in
the library. That's usually not a problem, because those constructors
are only called by code that's also inside the library. But when the
header is compiled with -fkeep-inline-functions those inline functions
are emitted even though they aren't called. That then creates an
undefined reference to the other library internsl. The fix is to just
move the private constructors into the library where they are called.
That way they are never even seen by users, and so not compiled even if
-fkeep-inline-functions is used.

On trunk there is a second problem, which is that the new equality
operators for comparing directory iterators with default_sentinel use
the shared_ptr::operator bool() conversion operator. The shared_ptr
specializations used by directory iterators are explicitly instantiated
in the library, but the bool conversion operators are not exported. This
causes linker errors at -O0 or with -fkeep-inline-functions. That just
requires the conversion operators to be exported.

libstdc++-v3/ChangeLog:

PR libstdc++/108636
* config/abi/pre/gnu.ver (GLIBCXX_3.4.31): Export shared_ptr
conversion operators for directory iterator comparisons with
std::default_sentinel_t.
* include/bits/fs_path.h (path::path(string_view, _Type))
(path::_Cmpt::_Cmpt(string_view, _Type, size_t)): Move inline
definitions to ...
* src/c++17/fs_path.cc: ... here.
* testsuite/27_io/filesystem/path/108636.cc: New test.

libstdc++-v3/config/abi/pre/gnu.ver
libstdc++-v3/include/bits/fs_path.h
libstdc++-v3/src/c++17/fs_path.cc
libstdc++-v3/testsuite/27_io/filesystem/path/108636.cc [new file with mode: 0644]

index 72716414ccb021d81cde4e554add7a786edb72e1..34f23bcbce0fa0a1bdbc14f2e62540fff2897d11 100644 (file)
@@ -2504,6 +2504,13 @@ GLIBCXX_3.4.31 {
     _ZNSt6chrono9tzdb_list14const_iteratorppEi;
     _ZN9__gnu_cxx21zoneinfo_dir_overrideEv;
 
+    # __shared_ptr<directory_iterator::_Dir>::operator bool()
+    _ZNKSt12__shared_ptrINSt10filesystem4_DirELN9__gnu_cxx12_Lock_policyE[012]EEcvbEv;
+    _ZNKSt12__shared_ptrINSt10filesystem7__cxx114_DirELN9__gnu_cxx12_Lock_policyE[012]EEcvbEv;
+    # __shared_ptr<recursive_directory_iterator::_Dir_stack>::operator bool()
+    _ZNKSt12__shared_ptrINSt10filesystem28recursive_directory_iterator10_Dir_stackELN9__gnu_cxx12_Lock_policyE[012]EEcvbEv;
+    _ZNKSt12__shared_ptrINSt10filesystem7__cxx1128recursive_directory_iterator10_Dir_stackELN9__gnu_cxx12_Lock_policyE[012]EEcvbEv;
+
 } GLIBCXX_3.4.30;
 
 # Symbols in the support library (libsupc++) have their own tag.
index 1cbfaaa542790d268307b913e14de579fd8f461e..0d7bb10c1a022ca44b345e2315ded9591ee54a98 100644 (file)
@@ -596,12 +596,7 @@ namespace __detail
       _Multi = 0, _Root_name, _Root_dir, _Filename
     };
 
-    path(basic_string_view<value_type> __str, _Type __type)
-    : _M_pathname(__str)
-    {
-      __glibcxx_assert(__type != _Type::_Multi);
-      _M_cmpts.type(__type);
-    }
+    path(basic_string_view<value_type> __str, _Type __type);
 
     enum class _Split { _Stem, _Extension };
 
@@ -851,8 +846,7 @@ namespace __detail
 
   struct path::_Cmpt : path
   {
-    _Cmpt(basic_string_view<value_type> __s, _Type __t, size_t __pos)
-      : path(__s, __t), _M_pos(__pos) { }
+    _Cmpt(basic_string_view<value_type> __s, _Type __t, size_t __pos);
 
     _Cmpt() : _M_pos(-1) { }
 
index 93149c4b415bf918ce8f2a8ffa2f91528dd68185..aaea7d2725dbd3b1d286e650eb38bb249a536ff2 100644 (file)
@@ -187,6 +187,19 @@ struct path::_Parser
   { return origin + c.str.data() - input.data(); }
 };
 
+inline
+path::path(basic_string_view<value_type> __str, _Type __type)
+: _M_pathname(__str)
+{
+  __glibcxx_assert(__type != _Type::_Multi);
+  _M_cmpts.type(__type);
+}
+
+inline
+path::_Cmpt::_Cmpt(basic_string_view<value_type> __s, _Type __t, size_t __pos)
+: path(__s, __t), _M_pos(__pos)
+{ }
+
 struct path::_List::_Impl
 {
   using value_type = _Cmpt;
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/108636.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/108636.cc
new file mode 100644 (file)
index 0000000..d58de46
--- /dev/null
@@ -0,0 +1,8 @@
+// { dg-do link { target c++17 } }
+// { dg-options "-fkeep-inline-functions" }
+
+#include <filesystem>
+int main()
+{
+  // PR libstdc++/108636 - link failure with -fkeep-inline-functions
+}
This page took 0.069097 seconds and 5 git commands to generate.