libstdc++: Add comparison operators to std::filesystem types
authorJonathan Wakely <jwakely@redhat.com>
Wed, 15 Apr 2020 20:01:42 +0000 (21:01 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 15 Apr 2020 20:01:42 +0000 (21:01 +0100)
Some more C++20 changes from P1614R2, "The Mothership has Landed".

* include/bits/fs_dir.h (file_status): Define operator== for C++20.
(directory_entry): Define operator<=> and remove redundant comparison
operators for C++20.
* include/bits/fs_fwd.h (space_info): Define operator== for C++20.
* include/bits/fs_path.h (path): Define operator<=> and remove
redundant comparison operators for C++20.
* testsuite/27_io/filesystem/path/compare/compare.cc: Fix comment.
* testsuite/27_io/filesystem/path/compare/lwg2936.cc: Likewise.
* testsuite/27_io/filesystem/path/compare/path.cc: Likewise.
* testsuite/27_io/filesystem/path/compare/strings.cc: Likewise.

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/fs_dir.h
libstdc++-v3/include/bits/fs_fwd.h
libstdc++-v3/include/bits/fs_path.h
libstdc++-v3/testsuite/27_io/filesystem/path/compare/compare.cc
libstdc++-v3/testsuite/27_io/filesystem/path/compare/lwg2936.cc
libstdc++-v3/testsuite/27_io/filesystem/path/compare/path.cc
libstdc++-v3/testsuite/27_io/filesystem/path/compare/strings.cc
libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc [new file with mode: 0644]

index ac446ac..f9e4c2d 100644 (file)
@@ -1,5 +1,16 @@
 2020-04-15  Jonathan Wakely  <jwakely@redhat.com>
 
+       * include/bits/fs_dir.h (file_status): Define operator== for C++20.
+       (directory_entry): Define operator<=> and remove redundant comparison
+       operators for C++20.
+       * include/bits/fs_fwd.h (space_info): Define operator== for C++20.
+       * include/bits/fs_path.h (path): Define operator<=> and remove
+       redundant comparison operators for C++20.
+       * testsuite/27_io/filesystem/path/compare/compare.cc: Fix comment.
+       * testsuite/27_io/filesystem/path/compare/lwg2936.cc: Likewise.
+       * testsuite/27_io/filesystem/path/compare/path.cc: Likewise.
+       * testsuite/27_io/filesystem/path/compare/strings.cc: Likewise.
+
        * include/bits/allocator.h (operator!=): Do not define for C++20.
        * include/bits/locale_classes.h (operator!=): Likewise.
        * include/bits/std_function.h (operator==(nullptr_t, const function&))
index df03f89..686dfce 100644 (file)
 # include <bits/unique_ptr.h>
 # include <bits/shared_ptr.h>
 
+#if __cplusplus > 201703L
+# include <compare>    // std::strong_ordering
+#endif
+
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -72,6 +76,11 @@ namespace filesystem
     void       type(file_type __ft) noexcept { _M_type = __ft; }
     void       permissions(perms __prms) noexcept { _M_perms = __prms; }
 
+#if __cpp_lib_three_way_comparison
+    friend bool
+    operator==(const file_status&, const file_status&) noexcept = default;
+#endif
+
   private:
     file_type  _M_type;
     perms      _M_perms;
@@ -273,18 +282,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
     { return filesystem::symlink_status(_M_path, __ec); }
 
     bool
-    operator< (const directory_entry& __rhs) const noexcept
-    { return _M_path < __rhs._M_path; }
-
-    bool
     operator==(const directory_entry& __rhs) const noexcept
     { return _M_path == __rhs._M_path; }
 
+#if __cpp_lib_three_way_comparison
+    strong_ordering
+    operator<=>(const directory_entry& __rhs) const noexcept
+    { return _M_path <=> __rhs._M_path; }
+#else
     bool
     operator!=(const directory_entry& __rhs) const noexcept
     { return _M_path != __rhs._M_path; }
 
     bool
+    operator< (const directory_entry& __rhs) const noexcept
+    { return _M_path < __rhs._M_path; }
+
+    bool
     operator<=(const directory_entry& __rhs) const noexcept
     { return _M_path <= __rhs._M_path; }
 
@@ -295,6 +309,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
     bool
     operator>=(const directory_entry& __rhs) const noexcept
     { return _M_path >= __rhs._M_path; }
+#endif
 
   private:
     friend class _Dir;
index 6363eca..d94cc41 100644 (file)
@@ -66,6 +66,10 @@ _GLIBCXX_END_NAMESPACE_CXX11
     uintmax_t capacity;
     uintmax_t free;
     uintmax_t available;
+
+#if __cpp_impl_three_way_comparison >= 201907L
+    friend bool operator==(const space_info&, const space_info&) = default;
+#endif
   };
 
   enum class file_type : signed char {
index fb6e8a5..ee6ab15 100644 (file)
 #include <bits/shared_ptr.h>
 #include <bits/unique_ptr.h>
 
+#if __cplusplus > 201703L
+# include <compare>
+#endif
+
 #if defined(_WIN32) && !defined(__CYGWIN__)
 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
 # include <algorithm>
@@ -452,6 +456,20 @@ namespace __detail
     // non-member operators
 
     /// Compare paths
+    friend bool operator==(const path& __lhs, const path& __rhs) noexcept
+    { return __lhs.compare(__rhs) == 0; }
+
+#if __cpp_lib_three_way_comparison
+    /// Compare paths
+    friend strong_ordering
+    operator<=>(const path& __lhs, const path& __rhs) noexcept
+    { return __lhs.compare(__rhs) <=> 0; }
+#else
+    /// Compare paths
+    friend bool operator!=(const path& __lhs, const path& __rhs) noexcept
+    { return !(__lhs == __rhs); }
+
+    /// Compare paths
     friend bool operator<(const path& __lhs, const path& __rhs) noexcept
     { return __lhs.compare(__rhs) < 0; }
 
@@ -466,14 +484,7 @@ namespace __detail
     /// Compare paths
     friend bool operator>=(const path& __lhs, const path& __rhs) noexcept
     { return !(__lhs < __rhs); }
-
-    /// Compare paths
-    friend bool operator==(const path& __lhs, const path& __rhs) noexcept
-    { return __lhs.compare(__rhs) == 0; }
-
-    /// Compare paths
-    friend bool operator!=(const path& __lhs, const path& __rhs) noexcept
-    { return !(__lhs == __rhs); }
+#endif
 
     /// Append one path to another
     friend path operator/(const path& __lhs, const path& __rhs)
index 88a9d0c..3c9974a 100644 (file)
@@ -18,7 +18,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 8.4.8 path compare [path.compare]
+// C++17 30.10.8.4.8 path compare [fs.path.compare]
 
 #include <filesystem>
 #include <testsuite_hooks.h>
index 44ebac7..b9978ed 100644 (file)
@@ -18,7 +18,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 8.4.8 path compare [path.compare]
+// C++17 30.10.8.4.8 path compare [fs.path.compare]
 
 #include <filesystem>
 #include <testsuite_hooks.h>
index c58722c..6f4166b 100644 (file)
@@ -18,7 +18,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 8.4.8 path compare [path.compare]
+// C++17 30.10.8.4.8 path compare [fs.path.compare]
 
 #include <filesystem>
 #include <testsuite_hooks.h>
index e014665..e9c900c 100644 (file)
@@ -18,7 +18,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 8.4.8 path compare [path.compare]
+// C++17 30.10.8.4.8 path compare [fs.path.compare]
 
 #include <filesystem>
 #include <testsuite_hooks.h>
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc
new file mode 100644 (file)
index 0000000..d9adfad
--- /dev/null
@@ -0,0 +1,79 @@
+// { dg-options "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// C++17 30.10.8.6 path non-member functions [fs.path.nonmember]
+
+#include <filesystem>
+#include <testsuite_hooks.h>
+#include <testsuite_fs.h>
+
+using std::filesystem::path;
+
+void
+test01()
+{
+  path p("/foo/bar");
+  VERIFY( p == p );
+  VERIFY( p == "/foo//bar" );
+
+  path q("/foo/baz");
+  VERIFY( p < q );
+  VERIFY( q > p );
+
+  path r("/foo/bar/.");
+  VERIFY( p < r );
+
+  VERIFY( path("a/b/") == path("a/b//") );
+}
+
+void
+test02()
+{
+  const path p0 = "/a/a/b/b";
+  for (const path& p : __gnu_test::test_paths)
+  {
+    VERIFY( p.compare(p) == 0 );
+    int cmp = p.compare(p0);
+    if (cmp == 0)
+      VERIFY( p0 == p );
+    else if (cmp < 0)
+      VERIFY( p0 > p );
+    else if (cmp > 0)
+      VERIFY( p0 < p );
+  }
+}
+
+void
+test03()
+{
+  VERIFY( path("/") == path("////") );
+  VERIFY( path("/a") > path("/") );
+  VERIFY( path("/") < path("/a") );
+  VERIFY( path("/ab") > path("/a") );
+  VERIFY( path("/ab") > path("/a/b") );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+}
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc
new file mode 100644 (file)
index 0000000..34e8bf8
--- /dev/null
@@ -0,0 +1,80 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// C++20 29.11.7.7 Non-member functions [fs.path.nonmember]
+
+#include <filesystem>
+#include <testsuite_hooks.h>
+#include <testsuite_fs.h>
+
+using std::filesystem::path;
+
+void
+test01()
+{
+  path p("/foo/bar");
+  VERIFY( p == p );
+  VERIFY( p == "/foo//bar" );
+  VERIFY( std::is_eq(p <=> p) );
+  VERIFY( std::is_eq(p <=> "/foo//bar") );
+
+  path q("/foo/baz");
+  VERIFY( p < q );
+  VERIFY( q > p );
+  VERIFY( std::is_lt(p <=> q) );
+  VERIFY( std::is_gt(q <=> p) );
+
+  path r("/foo/bar/.");
+  VERIFY( p < r );
+  VERIFY( std::is_lt(p <=> r) );
+
+  VERIFY( path("a/b/") == path("a/b//") );
+  VERIFY( std::is_eq(path("a/b/") <=> path("a/b//")) );
+}
+
+void
+test02()
+{
+  const path p0 = "/a/a/b/b";
+  for (const path& p : __gnu_test::test_paths)
+  {
+    VERIFY( std::is_eq(p <=> p) );
+    VERIFY( (p <=> p0) == (p.compare(p0) <=> 0) );
+    VERIFY( (p0 <=> p) == (p0.compare(p) <=> 0) );
+  }
+}
+
+void
+test03()
+{
+  VERIFY( std::is_eq(path("/") <=> path("////")) );
+  VERIFY( std::is_gt(path("/a") <=> path("/")) );
+  VERIFY( std::is_lt(path("/") <=> path("/a")) );
+  VERIFY( std::is_gt(path("/ab") <=> path("/a")) );
+  VERIFY( std::is_gt(path("/ab") <=> path("/a/b")) );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+}