Skip to content

[libc++] Properly guard std::filesystem with >= C++17 #72701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libcxx/benchmarks/filesystem.bench.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <filesystem>

#include "GenerateInput.h"
#include "benchmark/benchmark.h"
#include "filesystem_include.h"
#include "test_iterators.h"

namespace fs = std::filesystem;

static const size_t TestNumInputs = 1024;

template <class GenInputs>
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__chrono/file_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace chrono
{

// [time.clock.file], type file_clock
using file_clock = _VSTD_FS::_FilesystemClock;
using file_clock = filesystem::_FilesystemClock;

template<class _Duration>
using file_time = time_point<file_clock, _Duration>;
Expand Down
11 changes: 2 additions & 9 deletions libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -825,19 +825,12 @@ typedef __char32_t char32_t;
# define _LIBCPP_END_NAMESPACE_STD }}
# define _VSTD std

# if _LIBCPP_STD_VER >= 17
# define _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM \
_LIBCPP_BEGIN_NAMESPACE_STD inline namespace __fs { namespace filesystem {
# else
# define _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM \
_LIBCPP_BEGIN_NAMESPACE_STD namespace __fs { namespace filesystem {
# endif
# define _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM _LIBCPP_BEGIN_NAMESPACE_STD \
inline namespace __fs { namespace filesystem {

# define _LIBCPP_END_NAMESPACE_FILESYSTEM _LIBCPP_END_NAMESPACE_STD }}
// clang-format on

# define _VSTD_FS std::__fs::filesystem

# if __has_attribute(__enable_if__)
# define _LIBCPP_PREFERRED_OVERLOAD __attribute__((__enable_if__(true, "")))
# endif
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/copy_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

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

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_COPY_OPTIONS_H
32 changes: 16 additions & 16 deletions libcxx/include/__filesystem/directory_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
_LIBCPP_PUSH_MACROS
#include <__undef_macros>

#if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH

class directory_entry {
typedef _VSTD_FS::path _Path;
typedef filesystem::path _Path;

public:
// constructors and destructors
Expand Down Expand Up @@ -104,11 +104,11 @@ class directory_entry {
operator const _Path&() const noexcept { return __p_; }

_LIBCPP_INLINE_VISIBILITY
bool exists() const { return _VSTD_FS::exists(file_status{__get_ft()}); }
bool exists() const { return filesystem::exists(file_status{__get_ft()}); }

_LIBCPP_INLINE_VISIBILITY
bool exists(error_code& __ec) const noexcept {
return _VSTD_FS::exists(file_status{__get_ft(&__ec)});
return filesystem::exists(file_status{__get_ft(&__ec)});
}

_LIBCPP_INLINE_VISIBILITY
Expand Down Expand Up @@ -144,11 +144,11 @@ class directory_entry {
}

_LIBCPP_INLINE_VISIBILITY
bool is_other() const { return _VSTD_FS::is_other(file_status{__get_ft()}); }
bool is_other() const { return filesystem::is_other(file_status{__get_ft()}); }

_LIBCPP_INLINE_VISIBILITY
bool is_other(error_code& __ec) const noexcept {
return _VSTD_FS::is_other(file_status{__get_ft(&__ec)});
return filesystem::is_other(file_status{__get_ft(&__ec)});
}

_LIBCPP_INLINE_VISIBILITY
Expand Down Expand Up @@ -366,7 +366,7 @@ class directory_entry {
case _IterNonSymlink:
case _RefreshNonSymlink:
file_status __st(__data_.__type_);
if (__ec && !_VSTD_FS::exists(__st))
if (__ec && !filesystem::exists(__st))
*__ec = make_error_code(errc::no_such_file_or_directory);
else if (__ec)
__ec->clear();
Expand All @@ -386,7 +386,7 @@ class directory_entry {
case _RefreshNonSymlink:
case _RefreshSymlink: {
file_status __st(__data_.__type_);
if (__ec && !_VSTD_FS::exists(__st))
if (__ec && !filesystem::exists(__st))
*__ec = make_error_code(errc::no_such_file_or_directory);
else if (__ec)
__ec->clear();
Expand Down Expand Up @@ -434,15 +434,15 @@ class directory_entry {
case _IterNonSymlink:
case _IterSymlink:
case _RefreshSymlinkUnresolved:
return _VSTD_FS::__file_size(__p_, __ec);
return filesystem::__file_size(__p_, __ec);
case _RefreshSymlink:
case _RefreshNonSymlink: {
error_code __m_ec;
file_status __st(__get_ft(&__m_ec));
__handle_error("in directory_entry::file_size", __ec, __m_ec);
if (_VSTD_FS::exists(__st) && !_VSTD_FS::is_regular_file(__st)) {
errc __err_kind = _VSTD_FS::is_directory(__st) ? errc::is_a_directory
: errc::not_supported;
if (filesystem::exists(__st) && !filesystem::is_regular_file(__st)) {
errc __err_kind = filesystem::is_directory(__st) ? errc::is_a_directory
: errc::not_supported;
__handle_error("in directory_entry::file_size", __ec,
make_error_code(__err_kind));
}
Expand All @@ -459,7 +459,7 @@ class directory_entry {
case _IterNonSymlink:
case _IterSymlink:
case _RefreshSymlinkUnresolved:
return _VSTD_FS::__hard_link_count(__p_, __ec);
return filesystem::__hard_link_count(__p_, __ec);
case _RefreshSymlink:
case _RefreshNonSymlink: {
error_code __m_ec;
Expand All @@ -478,13 +478,13 @@ class directory_entry {
case _IterNonSymlink:
case _IterSymlink:
case _RefreshSymlinkUnresolved:
return _VSTD_FS::__last_write_time(__p_, __ec);
return filesystem::__last_write_time(__p_, __ec);
case _RefreshSymlink:
case _RefreshNonSymlink: {
error_code __m_ec;
file_status __st(__get_ft(&__m_ec));
__handle_error("in directory_entry::last_write_time", __ec, __m_ec);
if (_VSTD_FS::exists(__st) &&
if (filesystem::exists(__st) &&
__data_.__write_time_ == file_time_type::min())
__handle_error("in directory_entry::last_write_time", __ec,
make_error_code(errc::value_too_large));
Expand Down Expand Up @@ -518,7 +518,7 @@ _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
#endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)

_LIBCPP_POP_MACROS

Expand Down
8 changes: 4 additions & 4 deletions libcxx/include/__filesystem/directory_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# pragma GCC system_header
#endif

#if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

Expand Down Expand Up @@ -156,14 +156,14 @@ _LIBCPP_END_NAMESPACE_FILESYSTEM

template <>
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
inline constexpr bool _VSTD::ranges::enable_borrowed_range<_VSTD_FS::directory_iterator> = true;
inline constexpr bool _VSTD::ranges::enable_borrowed_range<std::filesystem::directory_iterator> = true;

template <>
_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
inline constexpr bool _VSTD::ranges::enable_view<_VSTD_FS::directory_iterator> = true;
inline constexpr bool _VSTD::ranges::enable_view<std::filesystem::directory_iterator> = true;

#endif // _LIBCPP_STD_VER >= 20

#endif // !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
#endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)

#endif // _LIBCPP___FILESYSTEM_DIRECTORY_ITERATOR_H
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/directory_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

Expand Down Expand Up @@ -73,6 +73,6 @@ inline directory_options& operator^=(directory_options& __lhs,

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_DIRECTORY_OPTIONS_H
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/file_status.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

Expand Down Expand Up @@ -71,6 +71,6 @@ class _LIBCPP_EXPORTED_FROM_ABI file_status {

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_FILE_STATUS_H
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/file_time_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

typedef chrono::time_point<_FilesystemClock> file_time_type;

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_FILE_TIME_TYPE_H
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/file_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

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

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_FILE_TYPE_H
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/filesystem_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

Expand Down Expand Up @@ -84,6 +84,6 @@ __throw_filesystem_error(_Args&&...) {

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_FILESYSTEM_ERROR_H
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# pragma GCC system_header
#endif

#if !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

Expand Down Expand Up @@ -196,6 +196,6 @@ _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
#endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)

#endif // _LIBCPP___FILESYSTEM_OPERATIONS_H
14 changes: 7 additions & 7 deletions libcxx/include/__filesystem/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

Expand Down Expand Up @@ -617,7 +617,7 @@ class _LIBCPP_EXPORTED_FROM_ABI path {
_EnableIfPathable<_Source> append(const _Source& __src) {
using _Traits = __is_pathable<_Source>;
using _CVT = _PathCVT<_SourceChar<_Source> >;
bool __source_is_absolute = _VSTD_FS::__is_separator(_Traits::__first_or_null(__src));
bool __source_is_absolute = filesystem::__is_separator(_Traits::__first_or_null(__src));
if (__source_is_absolute)
__pn_.clear();
else if (has_filename())
Expand All @@ -632,7 +632,7 @@ class _LIBCPP_EXPORTED_FROM_ABI path {
typedef typename iterator_traits<_InputIt>::value_type _ItVal;
static_assert(__can_convert_char<_ItVal>::value, "Must convertible");
using _CVT = _PathCVT<_ItVal>;
if (__first != __last && _VSTD_FS::__is_separator(*__first))
if (__first != __last && filesystem::__is_separator(*__first))
__pn_.clear();
else if (has_filename())
__pn_ += preferred_separator;
Expand Down Expand Up @@ -1083,14 +1083,14 @@ _LIBCPP_END_NAMESPACE_FILESYSTEM
_LIBCPP_BEGIN_NAMESPACE_STD

template <>
struct _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY hash<_VSTD_FS::path> : __unary_function<_VSTD_FS::path, size_t> {
_LIBCPP_HIDE_FROM_ABI size_t operator()(_VSTD_FS::path const& __p) const noexcept {
return _VSTD_FS::hash_value(__p);
struct _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY hash<filesystem::path> : __unary_function<filesystem::path, size_t> {
_LIBCPP_HIDE_FROM_ABI size_t operator()(filesystem::path const& __p) const noexcept {
return filesystem::hash_value(__p);
}
};

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_PATH_H
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/path_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

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

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_PATH_ITERATOR_H
4 changes: 2 additions & 2 deletions libcxx/include/__filesystem/perm_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG
#if _LIBCPP_STD_VER >= 17

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

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

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___FILESYSTEM_PERM_OPTIONS_H
Loading