aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2020-04-15 21:01:42 +0100
committerJonathan Wakely <jwakely@redhat.com>2020-04-15 21:01:42 +0100
commitd43919bf887530dfcbf85a76d60f1a698641731d (patch)
treefd0682814c8b6fd473f42e16c538703b11b0686e
parenteef00439e6723e089e74cd374474e0eac0a9f513 (diff)
downloadgcc-d43919bf887530dfcbf85a76d60f1a698641731d.zip
gcc-d43919bf887530dfcbf85a76d60f1a698641731d.tar.gz
gcc-d43919bf887530dfcbf85a76d60f1a698641731d.tar.bz2
libstdc++: Add comparison operators to std::filesystem types
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.
-rw-r--r--libstdc++-v3/ChangeLog11
-rw-r--r--libstdc++-v3/include/bits/fs_dir.h23
-rw-r--r--libstdc++-v3/include/bits/fs_fwd.h4
-rw-r--r--libstdc++-v3/include/bits/fs_path.h27
-rw-r--r--libstdc++-v3/testsuite/27_io/filesystem/path/compare/compare.cc2
-rw-r--r--libstdc++-v3/testsuite/27_io/filesystem/path/compare/lwg2936.cc2
-rw-r--r--libstdc++-v3/testsuite/27_io/filesystem/path/compare/path.cc2
-rw-r--r--libstdc++-v3/testsuite/27_io/filesystem/path/compare/strings.cc2
-rw-r--r--libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc79
-rw-r--r--libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc80
10 files changed, 216 insertions, 16 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index ac446ac..f9e4c2d 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -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&))
diff --git a/libstdc++-v3/include/bits/fs_dir.h b/libstdc++-v3/include/bits/fs_dir.h
index df03f89..686dfce 100644
--- a/libstdc++-v3/include/bits/fs_dir.h
+++ b/libstdc++-v3/include/bits/fs_dir.h
@@ -36,6 +36,10 @@
# 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;
diff --git a/libstdc++-v3/include/bits/fs_fwd.h b/libstdc++-v3/include/bits/fs_fwd.h
index 6363eca..d94cc41 100644
--- a/libstdc++-v3/include/bits/fs_fwd.h
+++ b/libstdc++-v3/include/bits/fs_fwd.h
@@ -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 {
diff --git a/libstdc++-v3/include/bits/fs_path.h b/libstdc++-v3/include/bits/fs_path.h
index fb6e8a5..ee6ab15 100644
--- a/libstdc++-v3/include/bits/fs_path.h
+++ b/libstdc++-v3/include/bits/fs_path.h
@@ -46,6 +46,10 @@
#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)
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/compare/compare.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/compare/compare.cc
index 88a9d0c..3c9974a 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/path/compare/compare.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/compare/compare.cc
@@ -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/compare/lwg2936.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/compare/lwg2936.cc
index 44ebac7..b9978ed 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/path/compare/lwg2936.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/compare/lwg2936.cc
@@ -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/compare/path.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/compare/path.cc
index c58722c..6f4166b 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/path/compare/path.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/compare/path.cc
@@ -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/compare/strings.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/compare/strings.cc
index e014665..e9c900c 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/path/compare/strings.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/compare/strings.cc
@@ -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
index 0000000..d9adfad
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc
@@ -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
index 0000000..34e8bf8
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc
@@ -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();
+}