Skip to content

Commit b6d8777

Browse files
committed
[libc++] LWG3171: implement operator<< for filesystem::directory_entry.
Differential Revision: https://reviews.llvm.org/D116642
1 parent cf35825 commit b6d8777

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

libcxx/docs/Status/Cxx2bIssues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"`3120 <https://wg21.link/LWG3120>`__","Unclear behavior of ``monotonic_buffer_resource::release()``","November 2020","",""
1818
"`3170 <https://wg21.link/LWG3170>`__","``is_always_equal`` added to ``std::allocator`` makes the standard library treat derived types as always equal","November 2020","",""
1919
"`3036 <https://wg21.link/LWG3036>`__","``polymorphic_allocator::destroy`` is extraneous","November 2020","",""
20-
"`3171 <https://wg21.link/LWG3171>`__","LWG2989 breaks ``directory_entry`` stream insertion","November 2020","",""
20+
"`3171 <https://wg21.link/LWG3171>`__","LWG2989 breaks ``directory_entry`` stream insertion","November 2020","|Complete|","14.0"
2121
"`3306 <https://wg21.link/LWG3306>`__","``ranges::advance`` violates its preconditions","November 2020","","","|ranges|"
2222
"`3403 <https://wg21.link/LWG3403>`__","Domain of ``ranges::ssize(E)`` doesn't ``match ranges::size(E)``","November 2020","","","|ranges|"
2323
"`3404 <https://wg21.link/LWG3404>`__","Finish removing subrange's conversions from pair-like","November 2020","","","|ranges|"

libcxx/include/__filesystem/directory_entry.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <chrono>
2424
#include <cstdint>
2525
#include <cstdlib>
26+
#include <iosfwd>
2627
#include <system_error>
2728

2829
_LIBCPP_PUSH_MACROS
@@ -239,6 +240,12 @@ class directory_entry {
239240
return __p_ >= __rhs.__p_;
240241
}
241242

243+
template <class _CharT, class _Traits>
244+
_LIBCPP_INLINE_VISIBILITY
245+
friend basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const directory_entry& __d) {
246+
return __os << __d.path();
247+
}
248+
242249
private:
243250
friend class directory_iterator;
244251
friend class recursive_directory_iterator;

libcxx/test/std/experimental/iterator/ostream.joiner/ostream.joiner.ops/ostream_joiner.op.assign.pass.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ int main(int, char**) {
8989
test<char, const char *, wchar_t> ('X', chars, chars+10, L"0X1X2X3X4X5X6X7X8X9");
9090
test<char, const int *, wchar_t> ('x', ints, ints+10, L"10x11x12x13x14x15x16x17x18x19");
9191
#endif
92+
// TODO(var-const): uncomment when it becomes possible to instantiate a `basic_ostream` object with a sized
93+
// character type (see https://llvm.org/PR53119).
9294
// test<char, const char *, char16_t>('X', chars, chars+10, u"0X1X2X3X4X5X6X7X8X9");
9395
// test<char, const int *, char16_t>('x', ints, ints+10, u"10x11x12x13x14x15x16x17x18x19");
9496
// test<char, const char *, char32_t>('X', chars, chars+10, U"0X1X2X3X4X5X6X7X8X9");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03
10+
// UNSUPPORTED: libcpp-has-no-localization
11+
12+
// <filesystem>
13+
//
14+
// class directory_entry
15+
//
16+
// template<class charT, class traits>
17+
// friend basic_ostream<charT, traits>&
18+
// operator<<(basic_ostream<charT, traits>& os, const directory_entry& d);
19+
20+
#include "filesystem_include.h"
21+
#include <cassert>
22+
#include <sstream>
23+
24+
#include "test_macros.h"
25+
#include "make_string.h"
26+
27+
MultiStringType InStr = MKSTR("abcdefg/\"hijklmnop\"/qrstuvwxyz/123456789");
28+
MultiStringType OutStr = MKSTR("\"abcdefg/\\\"hijklmnop\\\"/qrstuvwxyz/123456789\"");
29+
30+
template <class CharT>
31+
void TestOutput() {
32+
const char* input = static_cast<const char*>(InStr);
33+
const CharT* expected_output = static_cast<const CharT*>(OutStr);
34+
const fs::directory_entry dir = fs::directory_entry(fs::path(input));
35+
std::basic_stringstream<CharT> stream;
36+
37+
auto& result = stream << dir;
38+
assert(stream.str() == expected_output);
39+
assert(&result == &stream);
40+
}
41+
42+
int main(int, char**) {
43+
TestOutput<char>();
44+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
45+
TestOutput<wchar_t>();
46+
#endif
47+
// TODO(var-const): uncomment when it becomes possible to instantiate a `basic_ostream` object with a sized character
48+
// type (see https://llvm.org/PR53119).
49+
//TestOutput<char8_t>();
50+
//TestOutput<char16_t>();
51+
//TestOutput<char32_t>();
52+
53+
return 0;
54+
}

libcxx/test/std/input.output/filesystems/class.path/path.nonmember/path.io.pass.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ int main(int, char**) {
9494
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
9595
doIOTest<wchar_t>();
9696
#endif
97+
// TODO(var-const): uncomment when it becomes possible to instantiate a `basic_ostream` object with a sized character
98+
// type (see https://llvm.org/PR53119).
9799
//doIOTest<char16_t>();
98100
//doIOTest<char32_t>();
99101
test_LWG2989();

0 commit comments

Comments
 (0)