Skip to content

Commit ac8c9f1

Browse files
authored
[libc++] Properly guard std::filesystem with >= C++17 (llvm#72701)
<filesystem> is a C++17 addition. In C++11 and C++14 modes, we actually have all the code for <filesystem> but it is hidden behind a non-inline namespace __fs so it is not accessible. Instead of doing this unusual dance, just guard the code for filesystem behind a classic C++17 check like we normally do.
1 parent d105701 commit ac8c9f1

File tree

170 files changed

+490
-493
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+490
-493
lines changed

libcxx/benchmarks/filesystem.bench.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#include <filesystem>
2+
13
#include "GenerateInput.h"
24
#include "benchmark/benchmark.h"
3-
#include "filesystem_include.h"
45
#include "test_iterators.h"
56

7+
namespace fs = std::filesystem;
8+
69
static const size_t TestNumInputs = 1024;
710

811
template <class GenInputs>

libcxx/include/__chrono/file_clock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace chrono
3535
{
3636

3737
// [time.clock.file], type file_clock
38-
using file_clock = _VSTD_FS::_FilesystemClock;
38+
using file_clock = filesystem::_FilesystemClock;
3939

4040
template<class _Duration>
4141
using file_time = time_point<file_clock, _Duration>;

libcxx/include/__config

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -825,19 +825,12 @@ typedef __char32_t char32_t;
825825
# define _LIBCPP_END_NAMESPACE_STD }}
826826
# define _VSTD std
827827

828-
# if _LIBCPP_STD_VER >= 17
829-
# define _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM \
830-
_LIBCPP_BEGIN_NAMESPACE_STD inline namespace __fs { namespace filesystem {
831-
# else
832-
# define _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM \
833-
_LIBCPP_BEGIN_NAMESPACE_STD namespace __fs { namespace filesystem {
834-
# endif
828+
# define _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM _LIBCPP_BEGIN_NAMESPACE_STD \
829+
inline namespace __fs { namespace filesystem {
835830

836831
# define _LIBCPP_END_NAMESPACE_FILESYSTEM _LIBCPP_END_NAMESPACE_STD }}
837832
// clang-format on
838833

839-
# define _VSTD_FS std::__fs::filesystem
840-
841834
# if __has_attribute(__enable_if__)
842835
# define _LIBCPP_PREFERRED_OVERLOAD __attribute__((__enable_if__(true, "")))
843836
# endif

libcxx/include/__filesystem/copy_options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# pragma GCC system_header
1818
#endif
1919

20-
#ifndef _LIBCPP_CXX03_LANG
20+
#if _LIBCPP_STD_VER >= 17
2121

2222
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
2323

@@ -75,6 +75,6 @@ inline copy_options& operator^=(copy_options& __lhs, copy_options __rhs) {
7575

7676
_LIBCPP_END_NAMESPACE_FILESYSTEM
7777

78-
#endif // _LIBCPP_CXX03_LANG
78+
#endif // _LIBCPP_STD_VER >= 17
7979

8080
#endif // _LIBCPP___FILESYSTEM_COPY_OPTIONS_H

libcxx/include/__filesystem/directory_entry.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
_LIBCPP_PUSH_MACROS
3535
#include <__undef_macros>
3636

37-
#if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
37+
#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
3838

3939
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
4040

4141
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
4242

4343
class directory_entry {
44-
typedef _VSTD_FS::path _Path;
44+
typedef filesystem::path _Path;
4545

4646
public:
4747
// constructors and destructors
@@ -104,11 +104,11 @@ class directory_entry {
104104
operator const _Path&() const noexcept { return __p_; }
105105

106106
_LIBCPP_INLINE_VISIBILITY
107-
bool exists() const { return _VSTD_FS::exists(file_status{__get_ft()}); }
107+
bool exists() const { return filesystem::exists(file_status{__get_ft()}); }
108108

109109
_LIBCPP_INLINE_VISIBILITY
110110
bool exists(error_code& __ec) const noexcept {
111-
return _VSTD_FS::exists(file_status{__get_ft(&__ec)});
111+
return filesystem::exists(file_status{__get_ft(&__ec)});
112112
}
113113

114114
_LIBCPP_INLINE_VISIBILITY
@@ -144,11 +144,11 @@ class directory_entry {
144144
}
145145

146146
_LIBCPP_INLINE_VISIBILITY
147-
bool is_other() const { return _VSTD_FS::is_other(file_status{__get_ft()}); }
147+
bool is_other() const { return filesystem::is_other(file_status{__get_ft()}); }
148148

149149
_LIBCPP_INLINE_VISIBILITY
150150
bool is_other(error_code& __ec) const noexcept {
151-
return _VSTD_FS::is_other(file_status{__get_ft(&__ec)});
151+
return filesystem::is_other(file_status{__get_ft(&__ec)});
152152
}
153153

154154
_LIBCPP_INLINE_VISIBILITY
@@ -366,7 +366,7 @@ class directory_entry {
366366
case _IterNonSymlink:
367367
case _RefreshNonSymlink:
368368
file_status __st(__data_.__type_);
369-
if (__ec && !_VSTD_FS::exists(__st))
369+
if (__ec && !filesystem::exists(__st))
370370
*__ec = make_error_code(errc::no_such_file_or_directory);
371371
else if (__ec)
372372
__ec->clear();
@@ -386,7 +386,7 @@ class directory_entry {
386386
case _RefreshNonSymlink:
387387
case _RefreshSymlink: {
388388
file_status __st(__data_.__type_);
389-
if (__ec && !_VSTD_FS::exists(__st))
389+
if (__ec && !filesystem::exists(__st))
390390
*__ec = make_error_code(errc::no_such_file_or_directory);
391391
else if (__ec)
392392
__ec->clear();
@@ -434,15 +434,15 @@ class directory_entry {
434434
case _IterNonSymlink:
435435
case _IterSymlink:
436436
case _RefreshSymlinkUnresolved:
437-
return _VSTD_FS::__file_size(__p_, __ec);
437+
return filesystem::__file_size(__p_, __ec);
438438
case _RefreshSymlink:
439439
case _RefreshNonSymlink: {
440440
error_code __m_ec;
441441
file_status __st(__get_ft(&__m_ec));
442442
__handle_error("in directory_entry::file_size", __ec, __m_ec);
443-
if (_VSTD_FS::exists(__st) && !_VSTD_FS::is_regular_file(__st)) {
444-
errc __err_kind = _VSTD_FS::is_directory(__st) ? errc::is_a_directory
445-
: errc::not_supported;
443+
if (filesystem::exists(__st) && !filesystem::is_regular_file(__st)) {
444+
errc __err_kind = filesystem::is_directory(__st) ? errc::is_a_directory
445+
: errc::not_supported;
446446
__handle_error("in directory_entry::file_size", __ec,
447447
make_error_code(__err_kind));
448448
}
@@ -459,7 +459,7 @@ class directory_entry {
459459
case _IterNonSymlink:
460460
case _IterSymlink:
461461
case _RefreshSymlinkUnresolved:
462-
return _VSTD_FS::__hard_link_count(__p_, __ec);
462+
return filesystem::__hard_link_count(__p_, __ec);
463463
case _RefreshSymlink:
464464
case _RefreshNonSymlink: {
465465
error_code __m_ec;
@@ -478,13 +478,13 @@ class directory_entry {
478478
case _IterNonSymlink:
479479
case _IterSymlink:
480480
case _RefreshSymlinkUnresolved:
481-
return _VSTD_FS::__last_write_time(__p_, __ec);
481+
return filesystem::__last_write_time(__p_, __ec);
482482
case _RefreshSymlink:
483483
case _RefreshNonSymlink: {
484484
error_code __m_ec;
485485
file_status __st(__get_ft(&__m_ec));
486486
__handle_error("in directory_entry::last_write_time", __ec, __m_ec);
487-
if (_VSTD_FS::exists(__st) &&
487+
if (filesystem::exists(__st) &&
488488
__data_.__write_time_ == file_time_type::min())
489489
__handle_error("in directory_entry::last_write_time", __ec,
490490
make_error_code(errc::value_too_large));
@@ -518,7 +518,7 @@ _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
518518

519519
_LIBCPP_END_NAMESPACE_FILESYSTEM
520520

521-
#endif // !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
521+
#endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
522522

523523
_LIBCPP_POP_MACROS
524524

libcxx/include/__filesystem/directory_iterator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# pragma GCC system_header
3030
#endif
3131

32-
#if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
32+
#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
3333

3434
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
3535

@@ -156,14 +156,14 @@ _LIBCPP_END_NAMESPACE_FILESYSTEM
156156

157157
template <>
158158
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
159-
inline constexpr bool _VSTD::ranges::enable_borrowed_range<_VSTD_FS::directory_iterator> = true;
159+
inline constexpr bool _VSTD::ranges::enable_borrowed_range<std::filesystem::directory_iterator> = true;
160160

161161
template <>
162162
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
163-
inline constexpr bool _VSTD::ranges::enable_view<_VSTD_FS::directory_iterator> = true;
163+
inline constexpr bool _VSTD::ranges::enable_view<std::filesystem::directory_iterator> = true;
164164

165165
#endif // _LIBCPP_STD_VER >= 20
166166

167-
#endif // !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
167+
#endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
168168

169169
#endif // _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H

libcxx/include/__filesystem/directory_options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# pragma GCC system_header
1818
#endif
1919

20-
#ifndef _LIBCPP_CXX03_LANG
20+
#if _LIBCPP_STD_VER >= 17
2121

2222
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
2323

@@ -73,6 +73,6 @@ inline directory_options& operator^=(directory_options& __lhs,
7373

7474
_LIBCPP_END_NAMESPACE_FILESYSTEM
7575

76-
#endif // _LIBCPP_CXX03_LANG
76+
#endif // _LIBCPP_STD_VER >= 17
7777

7878
#endif // _LIBCPP___FILESYSTEM_DIRECTORY_OPTIONS_H

libcxx/include/__filesystem/file_status.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# pragma GCC system_header
2020
#endif
2121

22-
#ifndef _LIBCPP_CXX03_LANG
22+
#if _LIBCPP_STD_VER >= 17
2323

2424
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
2525

@@ -71,6 +71,6 @@ class _LIBCPP_EXPORTED_FROM_ABI file_status {
7171

7272
_LIBCPP_END_NAMESPACE_FILESYSTEM
7373

74-
#endif // _LIBCPP_CXX03_LANG
74+
#endif // _LIBCPP_STD_VER >= 17
7575

7676
#endif // _LIBCPP___FILESYSTEM_FILE_STATUS_H

libcxx/include/__filesystem/file_time_type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
# pragma GCC system_header
2020
#endif
2121

22-
#ifndef _LIBCPP_CXX03_LANG
22+
#if _LIBCPP_STD_VER >= 17
2323

2424
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
2525

2626
typedef chrono::time_point<_FilesystemClock> file_time_type;
2727

2828
_LIBCPP_END_NAMESPACE_FILESYSTEM
2929

30-
#endif // _LIBCPP_CXX03_LANG
30+
#endif // _LIBCPP_STD_VER >= 17
3131

3232
#endif // _LIBCPP___FILESYSTEM_FILE_TIME_TYPE_H

libcxx/include/__filesystem/file_type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# pragma GCC system_header
1818
#endif
1919

20-
#ifndef _LIBCPP_CXX03_LANG
20+
#if _LIBCPP_STD_VER >= 17
2121

2222
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
2323

@@ -38,6 +38,6 @@ enum class file_type : signed char {
3838

3939
_LIBCPP_END_NAMESPACE_FILESYSTEM
4040

41-
#endif // _LIBCPP_CXX03_LANG
41+
#endif // _LIBCPP_STD_VER >= 17
4242

4343
#endif // _LIBCPP___FILESYSTEM_FILE_TYPE_H

libcxx/include/__filesystem/filesystem_error.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# pragma GCC system_header
2525
#endif
2626

27-
#ifndef _LIBCPP_CXX03_LANG
27+
#if _LIBCPP_STD_VER >= 17
2828

2929
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
3030

@@ -84,6 +84,6 @@ __throw_filesystem_error(_Args&&...) {
8484

8585
_LIBCPP_END_NAMESPACE_FILESYSTEM
8686

87-
#endif // _LIBCPP_CXX03_LANG
87+
#endif // _LIBCPP_STD_VER >= 17
8888

8989
#endif // _LIBCPP___FILESYSTEM_FILESYSTEM_ERROR_H

libcxx/include/__filesystem/operations.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# pragma GCC system_header
2929
#endif
3030

31-
#if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
31+
#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
3232

3333
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
3434

@@ -196,6 +196,6 @@ _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
196196

197197
_LIBCPP_END_NAMESPACE_FILESYSTEM
198198

199-
#endif // !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
199+
#endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
200200

201201
#endif // _LIBCPP___FILESYSTEM_OPERATIONS_H

libcxx/include/__filesystem/path.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# pragma GCC system_header
3737
#endif
3838

39-
#ifndef _LIBCPP_CXX03_LANG
39+
#if _LIBCPP_STD_VER >= 17
4040

4141
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
4242

@@ -617,7 +617,7 @@ class _LIBCPP_EXPORTED_FROM_ABI path {
617617
_EnableIfPathable<_Source> append(const _Source& __src) {
618618
using _Traits = __is_pathable<_Source>;
619619
using _CVT = _PathCVT<_SourceChar<_Source> >;
620-
bool __source_is_absolute = _VSTD_FS::__is_separator(_Traits::__first_or_null(__src));
620+
bool __source_is_absolute = filesystem::__is_separator(_Traits::__first_or_null(__src));
621621
if (__source_is_absolute)
622622
__pn_.clear();
623623
else if (has_filename())
@@ -632,7 +632,7 @@ class _LIBCPP_EXPORTED_FROM_ABI path {
632632
typedef typename iterator_traits<_InputIt>::value_type _ItVal;
633633
static_assert(__can_convert_char<_ItVal>::value, "Must convertible");
634634
using _CVT = _PathCVT<_ItVal>;
635-
if (__first != __last && _VSTD_FS::__is_separator(*__first))
635+
if (__first != __last && filesystem::__is_separator(*__first))
636636
__pn_.clear();
637637
else if (has_filename())
638638
__pn_ += preferred_separator;
@@ -1083,14 +1083,14 @@ _LIBCPP_END_NAMESPACE_FILESYSTEM
10831083
_LIBCPP_BEGIN_NAMESPACE_STD
10841084

10851085
template <>
1086-
struct _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY hash<_VSTD_FS::path> : __unary_function<_VSTD_FS::path, size_t> {
1087-
_LIBCPP_HIDE_FROM_ABI size_t operator()(_VSTD_FS::path const& __p) const noexcept {
1088-
return _VSTD_FS::hash_value(__p);
1086+
struct _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY hash<filesystem::path> : __unary_function<filesystem::path, size_t> {
1087+
_LIBCPP_HIDE_FROM_ABI size_t operator()(filesystem::path const& __p) const noexcept {
1088+
return filesystem::hash_value(__p);
10891089
}
10901090
};
10911091

10921092
_LIBCPP_END_NAMESPACE_STD
10931093

1094-
#endif // _LIBCPP_CXX03_LANG
1094+
#endif // _LIBCPP_STD_VER >= 17
10951095

10961096
#endif // _LIBCPP___FILESYSTEM_PATH_H

libcxx/include/__filesystem/path_iterator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# pragma GCC system_header
2424
#endif
2525

26-
#ifndef _LIBCPP_CXX03_LANG
26+
#if _LIBCPP_STD_VER >= 17
2727

2828
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
2929

@@ -127,6 +127,6 @@ inline _LIBCPP_INLINE_VISIBILITY bool operator!=(const path::iterator& __lhs,
127127

128128
_LIBCPP_END_NAMESPACE_FILESYSTEM
129129

130-
#endif // _LIBCPP_CXX03_LANG
130+
#endif // _LIBCPP_STD_VER >= 17
131131

132132
#endif // _LIBCPP___FILESYSTEM_PATH_ITERATOR_H

libcxx/include/__filesystem/perm_options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# pragma GCC system_header
1818
#endif
1919

20-
#ifndef _LIBCPP_CXX03_LANG
20+
#if _LIBCPP_STD_VER >= 17
2121

2222
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
2323

@@ -68,6 +68,6 @@ inline perm_options& operator^=(perm_options& __lhs, perm_options __rhs) {
6868

6969
_LIBCPP_END_NAMESPACE_FILESYSTEM
7070

71-
#endif // _LIBCPP_CXX03_LANG
71+
#endif // _LIBCPP_STD_VER >= 17
7272

7373
#endif // _LIBCPP___FILESYSTEM_PERM_OPTIONS_H

0 commit comments

Comments
 (0)