diff options
author | Eric Fiselier <eric@efcs.ca> | 2018-12-21 04:09:01 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2018-12-21 04:09:01 +0000 |
commit | 8cf803bd99b588b1baae20efca8e239adb4512d6 (patch) | |
tree | bf704770e18533e1f604db75463475395945f383 /libcxx | |
parent | 114384b825673d9c709431269afc5edde88930f0 (diff) |
Implement LWG 3065: Make path operators friends.
This prevents things like:
using namespace std::filesystem;
auto x = L"a/b" == std::string("a/b");
Diffstat (limited to 'libcxx')
5 files changed, 89 insertions, 39 deletions
diff --git a/libcxx/include/filesystem b/libcxx/include/filesystem index 06a0eb33c4e..af713a06358 100644 --- a/libcxx/include/filesystem +++ b/libcxx/include/filesystem @@ -1151,6 +1151,31 @@ public: return __is; } + friend _LIBCPP_INLINE_VISIBILITY bool operator==(const path& __lhs, const path& __rhs) noexcept { + return __lhs.compare(__rhs) == 0; + } + friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const path& __lhs, const path& __rhs) noexcept { + return __lhs.compare(__rhs) != 0; + } + friend _LIBCPP_INLINE_VISIBILITY bool operator<(const path& __lhs, const path& __rhs) noexcept { + return __lhs.compare(__rhs) < 0; + } + friend _LIBCPP_INLINE_VISIBILITY bool operator<=(const path& __lhs, const path& __rhs) noexcept { + return __lhs.compare(__rhs) <= 0; + } + friend _LIBCPP_INLINE_VISIBILITY bool operator>(const path& __lhs, const path& __rhs) noexcept { + return __lhs.compare(__rhs) > 0; + } + friend _LIBCPP_INLINE_VISIBILITY bool operator>=(const path& __lhs, const path& __rhs) noexcept { + return __lhs.compare(__rhs) >= 0; + } + + friend _LIBCPP_INLINE_VISIBILITY path operator/(const path& __lhs, + const path& __rhs) { + path __result(__lhs); + __result /= __rhs; + return __result; + } private: inline _LIBCPP_INLINE_VISIBILITY path& __assign_view(__string_view const& __s) noexcept { @@ -1167,43 +1192,6 @@ inline _LIBCPP_INLINE_VISIBILITY void swap(path& __lhs, path& __rhs) noexcept { _LIBCPP_FUNC_VIS size_t hash_value(const path& __p) noexcept; -inline _LIBCPP_INLINE_VISIBILITY bool operator==(const path& __lhs, - const path& __rhs) noexcept { - return __lhs.compare(__rhs) == 0; -} - -inline _LIBCPP_INLINE_VISIBILITY bool operator!=(const path& __lhs, - const path& __rhs) noexcept { - return __lhs.compare(__rhs) != 0; -} - -inline _LIBCPP_INLINE_VISIBILITY bool operator<(const path& __lhs, - const path& __rhs) noexcept { - return __lhs.compare(__rhs) < 0; -} - -inline _LIBCPP_INLINE_VISIBILITY bool operator<=(const path& __lhs, - const path& __rhs) noexcept { - return __lhs.compare(__rhs) <= 0; -} - -inline _LIBCPP_INLINE_VISIBILITY bool operator>(const path& __lhs, - const path& __rhs) noexcept { - return __lhs.compare(__rhs) > 0; -} - -inline _LIBCPP_INLINE_VISIBILITY bool operator>=(const path& __lhs, - const path& __rhs) noexcept { - return __lhs.compare(__rhs) >= 0; -} - -inline _LIBCPP_INLINE_VISIBILITY path operator/(const path& __lhs, - const path& __rhs) { - path __result(__lhs); - __result /= __rhs; - return __result; -} - template <class _Source> _LIBCPP_INLINE_VISIBILITY typename enable_if<__is_pathable<_Source>::value, path>::type diff --git a/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/append_op.fail.cpp b/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/append_op.fail.cpp new file mode 100644 index 00000000000..e0b3a959c29 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/append_op.fail.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + +#include "filesystem_include.hpp" + +using namespace fs; + +struct ConvToPath { + operator fs::path() const { + return ""; + } +}; + +int main() { + ConvToPath LHS, RHS; + (void)(LHS / RHS); // expected-error {{invalid operands to binary expression}} +}
\ No newline at end of file diff --git a/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/append_op.pass.cpp b/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/append_op.pass.cpp index 09498bf2135..2a291d61da7 100644 --- a/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/append_op.pass.cpp +++ b/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/append_op.pass.cpp @@ -20,7 +20,6 @@ #include "test_macros.h" #include "filesystem_test_helper.hpp" - // This is mainly tested via the member append functions. int main() { @@ -29,4 +28,7 @@ int main() path p2("def"); path p3 = p1 / p2; assert(p3 == "abc/def"); + + path p4 = p1 / "def"; + assert(p4 == "abc/def"); } diff --git a/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/comparison_ops.fail.cpp b/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/comparison_ops.fail.cpp new file mode 100644 index 00000000000..00eafe532d0 --- /dev/null +++ b/libcxx/test/std/input.output/filesystems/class.path/path.nonmember/comparison_ops.fail.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// <filesystem> + + +#include "filesystem_include.hpp" + +using namespace fs; + +struct ConvToPath { + operator fs::path() const { + return ""; + } +}; + +int main() { + ConvToPath LHS, RHS; + (void)(LHS == RHS); // expected-error {{invalid operands to binary expression}} + (void)(LHS != RHS); // expected-error {{invalid operands to binary expression}} + (void)(LHS < RHS); // expected-error {{invalid operands to binary expression}} + (void)(LHS <= RHS); // expected-error {{invalid operands to binary expression}} + (void)(LHS > RHS); // expected-error {{invalid operands to binary expression}} + (void)(LHS >= RHS); // expected-error {{invalid operands to binary expression}} +}
\ No newline at end of file diff --git a/libcxx/www/cxx2a_status.html b/libcxx/www/cxx2a_status.html index 88aa56e38a8..1a072c9b054 100644 --- a/libcxx/www/cxx2a_status.html +++ b/libcxx/www/cxx2a_status.html @@ -270,7 +270,7 @@ <tr><td><a href="https://wg21.link/LWG3037">3037</a></td><td><tt>polymorphic_allocator</tt> and incomplete types</td><td>San Diego</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG3038">3038</a></td><td><tt>polymorphic_allocator::allocate</tt> should not allow integer overflow to create vulnerabilities</td><td>San Diego</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG3054">3054</a></td><td><tt>uninitialized_copy</tt> appears to not be able to meet its exception-safety guarantee</td><td>San Diego</td><td></td></tr> - <tr><td><a href="https://wg21.link/LWG3065">3065</a></td><td>LWG 2989 missed that all <tt>path</tt>'s other operators should be hidden friends as well</td><td>San Diego</td><td></td></tr> + <tr><td><a href="https://wg21.link/LWG3065">3065</a></td><td>LWG 2989 missed that all <tt>path</tt>'s other operators should be hidden friends as well</td><td>San Diego</td><td>Complete</td></tr> <tr><td><a href="https://wg21.link/LWG3096">3096</a></td><td><tt>path::lexically_relative</tt> is confused by trailing slashes</td><td>San Diego</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG3116">3116</a></td><td><tt><i>OUTERMOST_ALLOC_TRAITS</i></tt> needs <tt>remove_reference_t</tt></td><td>San Diego</td><td></td></tr> <tr><td><a href="https://wg21.link/LWG3122">3122</a></td><td><tt>__cpp_lib_chrono_udls</tt> was accidentally dropped</td><td>San Diego</td><td><i>We've already made the update; but we don't support all the test macros. When we do, this will be closed</i></td></tr> |