Skip to content

Commit

Permalink
[libc++] Format the code base (llvm#74334)
Browse files Browse the repository at this point in the history
This patch runs clang-format on all of libcxx/include and libcxx/src, in
accordance with the RFC discussed at [1]. Follow-up patches will format
the benchmarks, the test suite and remaining parts of the code. I'm
splitting this one into its own patch so the diff is a bit easier to
review.

This patch was generated with:

   find libcxx/include libcxx/src -type f \
      | grep -v 'module.modulemap.in' \
      | grep -v 'CMakeLists.txt' \
      | grep -v 'README.txt' \
      | grep -v 'libcxx.imp' \
      | grep -v '__config_site.in' \
      | xargs clang-format -i

A Git merge driver is available in libcxx/utils/clang-format-merge-driver.sh
to help resolve merge and rebase issues across these formatting changes.

[1]: https://discourse.llvm.org/t/rfc-clang-formatting-all-of-libc-once-and-for-all
  • Loading branch information
ldionne authored Dec 18, 2023
1 parent e5c523e commit 9783f28
Show file tree
Hide file tree
Showing 542 changed files with 67,543 additions and 84,889 deletions.
142 changes: 142 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
libcxx/src/**/*.cpp merge=libcxx-reformat
libcxx/include/**/*.h merge=libcxx-reformat

# Explicitly handle files with no extension
libcxx/include/__availability merge=libcxx-reformat
libcxx/include/__bit_reference merge=libcxx-reformat
libcxx/include/__config merge=libcxx-reformat
libcxx/include/__hash_table merge=libcxx-reformat
libcxx/include/__locale merge=libcxx-reformat
libcxx/include/__node_handle merge=libcxx-reformat
libcxx/include/__split_buffer merge=libcxx-reformat
libcxx/include/__std_clang_module merge=libcxx-reformat
libcxx/include/__threading_support merge=libcxx-reformat
libcxx/include/__tree merge=libcxx-reformat
libcxx/include/__verbose_abort merge=libcxx-reformat
libcxx/include/algorithm merge=libcxx-reformat
libcxx/include/any merge=libcxx-reformat
libcxx/include/array merge=libcxx-reformat
libcxx/include/atomic merge=libcxx-reformat
libcxx/include/barrier merge=libcxx-reformat
libcxx/include/bit merge=libcxx-reformat
libcxx/include/bitset merge=libcxx-reformat
libcxx/include/cassert merge=libcxx-reformat
libcxx/include/ccomplex merge=libcxx-reformat
libcxx/include/cctype merge=libcxx-reformat
libcxx/include/cerrno merge=libcxx-reformat
libcxx/include/cfenv merge=libcxx-reformat
libcxx/include/cfloat merge=libcxx-reformat
libcxx/include/charconv merge=libcxx-reformat
libcxx/include/chrono merge=libcxx-reformat
libcxx/include/cinttypes merge=libcxx-reformat
libcxx/include/ciso646 merge=libcxx-reformat
libcxx/include/climits merge=libcxx-reformat
libcxx/include/clocale merge=libcxx-reformat
libcxx/include/cmath merge=libcxx-reformat
libcxx/include/codecvt merge=libcxx-reformat
libcxx/include/compare merge=libcxx-reformat
libcxx/include/complex merge=libcxx-reformat
libcxx/include/concepts merge=libcxx-reformat
libcxx/include/condition_variable merge=libcxx-reformat
libcxx/include/coroutine merge=libcxx-reformat
libcxx/include/csetjmp merge=libcxx-reformat
libcxx/include/csignal merge=libcxx-reformat
libcxx/include/cstdarg merge=libcxx-reformat
libcxx/include/cstdbool merge=libcxx-reformat
libcxx/include/cstddef merge=libcxx-reformat
libcxx/include/cstdint merge=libcxx-reformat
libcxx/include/cstdio merge=libcxx-reformat
libcxx/include/cstdlib merge=libcxx-reformat
libcxx/include/cstring merge=libcxx-reformat
libcxx/include/ctgmath merge=libcxx-reformat
libcxx/include/ctime merge=libcxx-reformat
libcxx/include/cuchar merge=libcxx-reformat
libcxx/include/cwchar merge=libcxx-reformat
libcxx/include/cwctype merge=libcxx-reformat
libcxx/include/deque merge=libcxx-reformat
libcxx/include/exception merge=libcxx-reformat
libcxx/include/execution merge=libcxx-reformat
libcxx/include/expected merge=libcxx-reformat
libcxx/include/experimental/__config merge=libcxx-reformat
libcxx/include/experimental/__memory merge=libcxx-reformat
libcxx/include/experimental/deque merge=libcxx-reformat
libcxx/include/experimental/forward_list merge=libcxx-reformat
libcxx/include/experimental/iterator merge=libcxx-reformat
libcxx/include/experimental/list merge=libcxx-reformat
libcxx/include/experimental/map merge=libcxx-reformat
libcxx/include/experimental/memory merge=libcxx-reformat
libcxx/include/experimental/memory_resource merge=libcxx-reformat
libcxx/include/experimental/propagate_const merge=libcxx-reformat
libcxx/include/experimental/regex merge=libcxx-reformat
libcxx/include/experimental/set merge=libcxx-reformat
libcxx/include/experimental/simd merge=libcxx-reformat
libcxx/include/experimental/string merge=libcxx-reformat
libcxx/include/experimental/type_traits merge=libcxx-reformat
libcxx/include/experimental/unordered_map merge=libcxx-reformat
libcxx/include/experimental/unordered_set merge=libcxx-reformat
libcxx/include/experimental/utility merge=libcxx-reformat
libcxx/include/experimental/vector merge=libcxx-reformat
libcxx/include/ext/__hash merge=libcxx-reformat
libcxx/include/ext/hash_map merge=libcxx-reformat
libcxx/include/ext/hash_set merge=libcxx-reformat
libcxx/include/filesystem merge=libcxx-reformat
libcxx/include/format merge=libcxx-reformat
libcxx/include/forward_list merge=libcxx-reformat
libcxx/include/fstream merge=libcxx-reformat
libcxx/include/functional merge=libcxx-reformat
libcxx/include/future merge=libcxx-reformat
libcxx/include/initializer_list merge=libcxx-reformat
libcxx/include/iomanip merge=libcxx-reformat
libcxx/include/ios merge=libcxx-reformat
libcxx/include/iosfwd merge=libcxx-reformat
libcxx/include/iostream merge=libcxx-reformat
libcxx/include/istream merge=libcxx-reformat
libcxx/include/iterator merge=libcxx-reformat
libcxx/include/latch merge=libcxx-reformat
libcxx/include/limits merge=libcxx-reformat
libcxx/include/list merge=libcxx-reformat
libcxx/include/locale merge=libcxx-reformat
libcxx/include/map merge=libcxx-reformat
libcxx/include/mdspan merge=libcxx-reformat
libcxx/include/memory merge=libcxx-reformat
libcxx/include/memory_resource merge=libcxx-reformat
libcxx/include/mutex merge=libcxx-reformat
libcxx/include/new merge=libcxx-reformat
libcxx/include/numbers merge=libcxx-reformat
libcxx/include/numeric merge=libcxx-reformat
libcxx/include/optional merge=libcxx-reformat
libcxx/include/ostream merge=libcxx-reformat
libcxx/include/print merge=libcxx-reformat
libcxx/include/queue merge=libcxx-reformat
libcxx/include/random merge=libcxx-reformat
libcxx/include/ranges merge=libcxx-reformat
libcxx/include/ratio merge=libcxx-reformat
libcxx/include/regex merge=libcxx-reformat
libcxx/include/scoped_allocator merge=libcxx-reformat
libcxx/include/semaphore merge=libcxx-reformat
libcxx/include/set merge=libcxx-reformat
libcxx/include/shared_mutex merge=libcxx-reformat
libcxx/include/source_location merge=libcxx-reformat
libcxx/include/span merge=libcxx-reformat
libcxx/include/sstream merge=libcxx-reformat
libcxx/include/stack merge=libcxx-reformat
libcxx/include/stdexcept merge=libcxx-reformat
libcxx/include/stop_token merge=libcxx-reformat
libcxx/include/streambuf merge=libcxx-reformat
libcxx/include/string merge=libcxx-reformat
libcxx/include/string_view merge=libcxx-reformat
libcxx/include/strstream merge=libcxx-reformat
libcxx/include/syncstream merge=libcxx-reformat
libcxx/include/system_error merge=libcxx-reformat
libcxx/include/thread merge=libcxx-reformat
libcxx/include/tuple merge=libcxx-reformat
libcxx/include/type_traits merge=libcxx-reformat
libcxx/include/typeindex merge=libcxx-reformat
libcxx/include/typeinfo merge=libcxx-reformat
libcxx/include/unordered_map merge=libcxx-reformat
libcxx/include/unordered_set merge=libcxx-reformat
libcxx/include/utility merge=libcxx-reformat
libcxx/include/valarray merge=libcxx-reformat
libcxx/include/variant merge=libcxx-reformat
libcxx/include/vector merge=libcxx-reformat
libcxx/include/version merge=libcxx-reformat
20 changes: 7 additions & 13 deletions libcxx/include/__algorithm/binary_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,16 @@
_LIBCPP_BEGIN_NAMESPACE_STD

template <class _ForwardIterator, class _Tp, class _Compare>
_LIBCPP_NODISCARD_EXT inline
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
bool
binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
{
__first = std::lower_bound<_ForwardIterator, _Tp, __comp_ref_type<_Compare> >(__first, __last, __value, __comp);
return __first != __last && !__comp(__value, *__first);
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp) {
__first = std::lower_bound<_ForwardIterator, _Tp, __comp_ref_type<_Compare> >(__first, __last, __value, __comp);
return __first != __last && !__comp(__value, *__first);
}

template <class _ForwardIterator, class _Tp>
_LIBCPP_NODISCARD_EXT inline
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
bool
binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
{
return std::binary_search(__first, __last, __value, __less<>());
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
return std::binary_search(__first, __last, __value, __less<>());
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
66 changes: 28 additions & 38 deletions libcxx/include/__algorithm/comp_ref_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,42 @@
_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Compare>
struct __debug_less
{
_Compare &__comp_;
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI __debug_less(_Compare& __c) : __comp_(__c) {}
struct __debug_less {
_Compare& __comp_;
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI __debug_less(_Compare& __c) : __comp_(__c) {}

template <class _Tp, class _Up>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
bool operator()(const _Tp& __x, const _Up& __y)
{
bool __r = __comp_(__x, __y);
if (__r)
__do_compare_assert(0, __y, __x);
return __r;
}
template <class _Tp, class _Up>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Up& __y) {
bool __r = __comp_(__x, __y);
if (__r)
__do_compare_assert(0, __y, __x);
return __r;
}

template <class _Tp, class _Up>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
bool operator()(_Tp& __x, _Up& __y)
{
bool __r = __comp_(__x, __y);
if (__r)
__do_compare_assert(0, __y, __x);
return __r;
}
template <class _Tp, class _Up>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(_Tp& __x, _Up& __y) {
bool __r = __comp_(__x, __y);
if (__r)
__do_compare_assert(0, __y, __x);
return __r;
}

template <class _LHS, class _RHS>
_LIBCPP_CONSTEXPR_SINCE_CXX14
inline _LIBCPP_HIDE_FROM_ABI
decltype((void)std::declval<_Compare&>()(
std::declval<_LHS &>(), std::declval<_RHS &>()))
__do_compare_assert(int, _LHS & __l, _RHS & __r) {
_LIBCPP_ASSERT_UNCATEGORIZED(!__comp_(__l, __r),
"Comparator does not induce a strict weak ordering");
(void)__l;
(void)__r;
}
template <class _LHS, class _RHS>
_LIBCPP_CONSTEXPR_SINCE_CXX14 inline _LIBCPP_HIDE_FROM_ABI decltype((void)std::declval<_Compare&>()(
std::declval<_LHS&>(), std::declval<_RHS&>()))
__do_compare_assert(int, _LHS& __l, _RHS& __r) {
_LIBCPP_ASSERT_UNCATEGORIZED(!__comp_(__l, __r), "Comparator does not induce a strict weak ordering");
(void)__l;
(void)__r;
}

template <class _LHS, class _RHS>
_LIBCPP_CONSTEXPR_SINCE_CXX14
inline _LIBCPP_HIDE_FROM_ABI
void __do_compare_assert(long, _LHS &, _RHS &) {}
template <class _LHS, class _RHS>
_LIBCPP_CONSTEXPR_SINCE_CXX14 inline _LIBCPP_HIDE_FROM_ABI void __do_compare_assert(long, _LHS&, _RHS&) {}
};

// Pass the comparator by lvalue reference. Or in debug mode, using a
// debugging wrapper that stores a reference.
# if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
#if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
template <class _Comp>
using __comp_ref_type = __debug_less<_Comp>;
#else
Expand Down
16 changes: 6 additions & 10 deletions libcxx/include/__algorithm/copy_backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ struct __copy_backward_loop {

struct __copy_backward_trivial {
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
template <class _In, class _Out,
__enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0>
template <class _In, class _Out, __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
operator()(_In* __first, _In* __last, _Out* __result) const {
return std::__copy_backward_trivial_impl(__first, __last, __result);
Expand All @@ -124,16 +123,13 @@ __copy_backward(_BidirectionalIterator1 __first, _Sentinel __last, _Bidirectiona
}

template <class _BidirectionalIterator1, class _BidirectionalIterator2>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
_BidirectionalIterator2
copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
_BidirectionalIterator2 __result)
{
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator2
copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, _BidirectionalIterator2 __result) {
static_assert(std::is_copy_constructible<_BidirectionalIterator1>::value &&
std::is_copy_constructible<_BidirectionalIterator1>::value, "Iterators must be copy constructible.");
std::is_copy_constructible<_BidirectionalIterator1>::value,
"Iterators must be copy constructible.");

return std::__copy_backward<_ClassicAlgPolicy>(
std::move(__first), std::move(__last), std::move(__result)).second;
return std::__copy_backward<_ClassicAlgPolicy>(std::move(__first), std::move(__last), std::move(__result)).second;
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
23 changes: 9 additions & 14 deletions libcxx/include/__algorithm/copy_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,16 @@

_LIBCPP_BEGIN_NAMESPACE_STD

template<class _InputIterator, class _OutputIterator, class _Predicate>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
_OutputIterator
copy_if(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _Predicate __pred)
{
for (; __first != __last; ++__first)
{
if (__pred(*__first))
{
*__result = *__first;
++__result;
}
template <class _InputIterator, class _OutputIterator, class _Predicate>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred) {
for (; __first != __last; ++__first) {
if (__pred(*__first)) {
*__result = *__first;
++__result;
}
return __result;
}
return __result;
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
25 changes: 11 additions & 14 deletions libcxx/include/__algorithm/copy_move_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,19 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _From, class _To>
struct __can_lower_copy_assignment_to_memmove {
static const bool value =
// If the types are always bitcastable, it's valid to do a bitwise copy between them.
__is_always_bitcastable<_From, _To>::value &&
// Reject conversions that wouldn't be performed by the regular built-in assignment (e.g. between arrays).
is_trivially_assignable<_To&, const _From&>::value &&
// `memmove` doesn't accept `volatile` pointers, make sure the optimization SFINAEs away in that case.
!is_volatile<_From>::value &&
!is_volatile<_To>::value;
// If the types are always bitcastable, it's valid to do a bitwise copy between them.
__is_always_bitcastable<_From, _To>::value &&
// Reject conversions that wouldn't be performed by the regular built-in assignment (e.g. between arrays).
is_trivially_assignable<_To&, const _From&>::value &&
// `memmove` doesn't accept `volatile` pointers, make sure the optimization SFINAEs away in that case.
!is_volatile<_From>::value && !is_volatile<_To>::value;
};

template <class _From, class _To>
struct __can_lower_move_assignment_to_memmove {
static const bool value =
__is_always_bitcastable<_From, _To>::value &&
is_trivially_assignable<_To&, _From&&>::value &&
!is_volatile<_From>::value &&
!is_volatile<_To>::value;
__is_always_bitcastable<_From, _To>::value && is_trivially_assignable<_To&, _From&&>::value &&
!is_volatile<_From>::value && !is_volatile<_To>::value;
};

// `memmove` algorithms implementation.
Expand Down Expand Up @@ -95,8 +92,8 @@ struct __can_rewrap<_InIter,
_Sent,
_OutIter,
// Note that sentinels are always copy-constructible.
__enable_if_t< is_copy_constructible<_InIter>::value &&
is_copy_constructible<_OutIter>::value > > : true_type {};
__enable_if_t< is_copy_constructible<_InIter>::value && is_copy_constructible<_OutIter>::value > >
: true_type {};

template <class _Algorithm,
class _InIter,
Expand All @@ -108,7 +105,7 @@ __unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) {
auto __range = std::__unwrap_range(__first, std::move(__last));
auto __result = _Algorithm()(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__out_first));
return std::make_pair(std::__rewrap_range<_Sent>(std::move(__first), std::move(__result.first)),
std::__rewrap_iter(std::move(__out_first), std::move(__result.second)));
std::__rewrap_iter(std::move(__out_first), std::move(__result.second)));
}

template <class _Algorithm,
Expand Down
Loading

0 comments on commit 9783f28

Please sign in to comment.