From d43919bf887530dfcbf85a76d60f1a698641731d Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 15 Apr 2020 21:01:42 +0100 Subject: [PATCH] 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. --- libstdc++-v3/ChangeLog | 11 +++ libstdc++-v3/include/bits/fs_dir.h | 23 +++++- libstdc++-v3/include/bits/fs_fwd.h | 4 + libstdc++-v3/include/bits/fs_path.h | 27 +++++-- .../27_io/filesystem/path/compare/compare.cc | 2 +- .../27_io/filesystem/path/compare/lwg2936.cc | 2 +- .../27_io/filesystem/path/compare/path.cc | 2 +- .../27_io/filesystem/path/compare/strings.cc | 2 +- .../27_io/filesystem/path/nonmember/cmp.cc | 79 ++++++++++++++++++ .../filesystem/path/nonmember/cmp_c++20.cc | 80 +++++++++++++++++++ 10 files changed, 216 insertions(+), 16 deletions(-) create mode 100644 libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc create mode 100644 libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ac446acaaac..f9e4c2d87dd 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,16 @@ 2020-04-15 Jonathan Wakely + * 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 df03f892f60..686dfce6e5f 100644 --- a/libstdc++-v3/include/bits/fs_dir.h +++ b/libstdc++-v3/include/bits/fs_dir.h @@ -36,6 +36,10 @@ # include # include +#if __cplusplus > 201703L +# include // 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; @@ -272,18 +281,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 symlink_status(error_code& __ec) const noexcept { 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 6363eca867c..d94cc414906 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 fb6e8a5247f..ee6ab15cc4c 100644 --- a/libstdc++-v3/include/bits/fs_path.h +++ b/libstdc++-v3/include/bits/fs_path.h @@ -46,6 +46,10 @@ #include #include +#if __cplusplus > 201703L +# include +#endif + #if defined(_WIN32) && !defined(__CYGWIN__) # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1 # include @@ -451,6 +455,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 88a9d0c3203..3c9974aeacc 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 // . -// 8.4.8 path compare [path.compare] +// C++17 30.10.8.4.8 path compare [fs.path.compare] #include #include 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 44ebac7b07a..b9978edd8f7 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 // . -// 8.4.8 path compare [path.compare] +// C++17 30.10.8.4.8 path compare [fs.path.compare] #include #include 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 c58722cdc69..6f4166b641d 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 // . -// 8.4.8 path compare [path.compare] +// C++17 30.10.8.4.8 path compare [fs.path.compare] #include #include 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 e014665b6d7..e9c900c49e4 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 // . -// 8.4.8 path compare [path.compare] +// C++17 30.10.8.4.8 path compare [fs.path.compare] #include #include 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 00000000000..d9adfad0a3e --- /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 +// . + +// C++17 30.10.8.6 path non-member functions [fs.path.nonmember] + +#include +#include +#include + +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 00000000000..34e8bf841c2 --- /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 +// . + +// C++20 29.11.7.7 Non-member functions [fs.path.nonmember] + +#include +#include +#include + +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(); +} -- 2.30.2