Implement LWG 3065: Make path operators friends.

This prevents things like:

using namespace std::filesystem;
auto x = L"a/b" == std::string("a/b");

llvm-svn: 349884
This commit is contained in:
Eric Fiselier 2018-12-21 04:09:01 +00:00
parent 866885e12a
commit 49b183a9ec
5 changed files with 89 additions and 39 deletions

View File

@ -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

View File

@ -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}}
}

View File

@ -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");
}

View File

@ -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}}
}

View File

@ -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>