6735 Commits

Author SHA1 Message Date
Nikolas Klauser
9d23031032
[libc++] Only make comparators transparent in __tree if they don't cause a conversion (#179453)
We're currently unwrapping `less<T>` even if the `key_type` isn't `T`.
This causes the removal of an implicit conversion to `const T&` if the
types mismatch. Making `less<T>` transparent in that case changes
overload resolution and makes it fail potentially.

Fixes #179319
2026-02-10 13:26:55 +01:00
Vinay Deshmukh
70b7245990
[libc++] Prepare for PR #134330 by migrating to std::__static_fancy_pointer_cast (#180546)
To reduce the noise in #134330 for `libcxx/include/__tree`, we migrate
from certain `static_cast` to `std::__static_fancy_pointer_cast` in this
separate patch.

This change is needed to properly work with fancy pointers like
`min_pointer` during constant evaluation for `std::map`

Co-authored-by: Joseph Huber <huberjn@outlook.com>
2026-02-10 12:39:12 +01:00
Christopher Di Bella
4d6913e14f
[libcxx] Rename __split_buffer alias template to _SplitBuffer (#180284)
`-Wchanges-meaning` is a GCC warning that catches shadowing in more
contexts. While a bit annoying here, it's a helpful warning. As such, we
need to rename the `__split_buffer` alias template in `std::vector` so
that we don't trip it up.
2026-02-06 23:22:32 +00:00
Christopher Di Bella
f735aa061b
[libc++] Tighten __add_alignment_assumption return type (#180090)
`__add_alignment_assumption` unilaterally returned a `pointer`, even
when passed a `const_pointer`. This was surfaced by some (but not all)
CI jobs when rebasing `std::vector` to have a layout type.
2026-02-06 08:26:37 -05:00
A. Jiang
39dba7b354
[libc++] Fix module builds for <__algorithm/unwrap_range.h> (#179887)
3a653afd45709432181952c0ffdb53eceb0939ae removed the inclusion of
`<__utility/declval.h>` from `<__algorithm/unwrap_range.h>`. However,
`unwrap_range.h` still needs to use `std::declval`. So we should restore
the inclusion.

The building failure with Clang modules was already caught by CI.
2026-02-06 09:42:49 +08:00
Connector Switch
2d106844e7
[libcxx] Optimize ranges::fold_left_with_iter for segmented iterators (#177853)
Part of https://github.com/llvm/llvm-project/issues/102817.

This patch attempts to optimize the performance of
`ranges::fold_left_with_iter` for segmented iterators.

- before

```
# | rng::fold_left(vector<int>)/8             2.78 ns         2.78 ns    241953718
# | rng::fold_left(vector<int>)/32            12.2 ns         12.2 ns     57579851
# | rng::fold_left(vector<int>)/50            19.2 ns         19.2 ns     36487764
# | rng::fold_left(vector<int>)/8192          3226 ns         3226 ns       216811
# | rng::fold_left(vector<int>)/1048576     441842 ns       441839 ns         1592
# | rng::fold_left(deque<int>)/8              2.83 ns         2.83 ns    243888678
# | rng::fold_left(deque<int>)/32             16.6 ns         16.6 ns     42297458
# | rng::fold_left(deque<int>)/50             22.3 ns         22.3 ns     31387998
# | rng::fold_left(deque<int>)/8192           2492 ns         2492 ns       281637
# | rng::fold_left(deque<int>)/1048576      324936 ns       324936 ns         2154
# | rng::fold_left(list<int>)/8               2.54 ns         2.54 ns    275946635
# | rng::fold_left(list<int>)/32              16.2 ns         16.2 ns     42901634
# | rng::fold_left(list<int>)/50              54.7 ns         54.7 ns     12767450
# | rng::fold_left(list<int>)/8192           15154 ns        15154 ns        56744
# | rng::fold_left(list<int>)/1048576      4976906 ns      4976867 ns          158
```

- after

```
# | rng::fold_left(vector<int>)/8             2.74 ns         2.74 ns    255954900
# | rng::fold_left(vector<int>)/32            12.1 ns         12.1 ns     57843462
# | rng::fold_left(vector<int>)/50            19.2 ns         19.2 ns     36422594
# | rng::fold_left(vector<int>)/8192          3202 ns         3202 ns       218265
# | rng::fold_left(vector<int>)/1048576     435718 ns       435709 ns         1609
# | rng::fold_left(deque<int>)/8              2.52 ns         2.52 ns    277288254
# | rng::fold_left(deque<int>)/32             14.1 ns         14.1 ns     52244463
# | rng::fold_left(deque<int>)/50             16.2 ns         16.2 ns     43131857
# | rng::fold_left(deque<int>)/8192           1695 ns         1695 ns       415620
# | rng::fold_left(deque<int>)/1048576      277729 ns       277731 ns         2532
# | rng::fold_left(list<int>)/8               2.55 ns         2.55 ns    277025050
# | rng::fold_left(list<int>)/32              16.2 ns         16.2 ns     43058857
# | rng::fold_left(list<int>)/50              54.7 ns         54.7 ns     12705516
# | rng::fold_left(list<int>)/8192           15236 ns        15235 ns        56840
# | rng::fold_left(list<int>)/1048576      4827263 ns      4827147 ns          152
```
2026-02-05 21:12:36 +08:00
Nikolas Klauser
c9863ceacc
[libc++] Specialize allocator_traits for std::allocator (#160278)
This reduces the time to instantiate `allocator_traits<allocator<T>>`.
2026-02-05 11:43:47 +01:00
Nikolas Klauser
ccb58432e3
[libc++] Use views::reverse to implement ranges::reverse_copy (#177123)
We currently have a custom utility `__reverse_range`, which does
basically the same thing as `views::reverse` and the only place where we
use it is in `ranges::reverse_copy`. Instead of this special utility, we
can simply use `views::reverse`.

This has originally been introduced due to compile time concerns.
However, there doesn't seem to actually be a significant compile time
regression overall.
2026-02-04 20:58:16 +01:00
Connector Switch
a3f848057b
[libcxx] Modify std::__for_each{, _n} to accept r-values in __f (#179451)
This is necessary when optimizing algorithms for segmented iterators to
reduce boilerplate code.

related:
-
https://github.com/llvm/llvm-project/pull/177853#discussion_r2754820322
-
https://github.com/llvm/llvm-project/pull/164266#discussion_r2447129525
2026-02-04 23:15:21 +08:00
Nikolas Klauser
3a653afd45
[libc++] Simplify the implementation of __{un,re}wrap_range (#178381)
We can use a relatively simple `if constexpr` chain instead of SFINAE
and class template specialization, making the functions much simpler to
understand.
2026-02-04 10:31:31 +01:00
Nikolas Klauser
f537b4e518
[libc++] Avoid template instantiations in the duration aliases (#178182)
These templates are instantiated whenever `<__chrono/duration.h>` is
included, making these calculations quite costly. I also don't think
that folding the calculations decreases readability here (IMO it's
actually easier to read now), so I don't think there is a reason we
shouldn't do this.

`<__chrono/duration.h>` is currently (transitively) included in at least
(I stopped checking) `<algorithm>`, `<atomic>`, `<barrier>`, `<chrono>`,
`<condition_variable>`, `<future>`, `<iomanip>`, `<ios>`, `<iostream>`,
`<istream>`, `<latch>`, `<locale>`, `<mutex>`, `<ostream>`,
`<semaphore>`, `<shared_mutex>`, `<syncstream>` and `<thread>`
2026-02-04 10:26:48 +01:00
William Tran-Viet
7b6f1235b9
[libc++] Implement a type-safe iterator for optional (#154239)
Create a new `__capacity_aware_iterator` iterator type which wraps an
existing iterator, takes its container as a template parameter, and
encodes the maximum amount of elements the container can hold. The main
objective is to prevent iterator mixups between different containers
(e.g. `vector`).
2026-02-02 16:18:39 -05:00
A. Jiang
aa5428864e
[libc++] Short-cut constraints of single-argument any constructor (#177082)
When a default template argument of a function template uses
`std::is_copy_constructible<T>::value` and `T` is convertible from and
to `any`, the changes in 21dc73f6a46cd786394f10f5aef46ec4a2d26175 would
introduce constraint meta-recursion when compiling with Clang.

This patch short-cuts constraints of the related constructor to avoid
computing `is_copy_constructible<T>` when `decay_t<T>` is `any`, which
gets rid of constraint meta-recursion in the overload resolution of copy
construction of `T`.

Fixes #176877.
2026-02-02 17:48:55 +01:00
Walter Lee
09f8f22a64
[libc++] "Always" include_next for non C++ path in stdatomic.h (#178463)
In https://github.com/llvm/llvm-project/pull/176903, `#include
<__configuration/compiler.h>` is moved into the
`#ifdef _cplusplus` clause, so `_LIBCPP_COMPILER_CLANG_BASED` is no
longer set for C compiles. This causes a regression internally, where
when C compiles includes stdatomic.h, they no longer get the
corresponding C header.

C++ stdlib headers "shouldn't" be on the search patch for C compile, but
we do and so do lots of other people, so libc++ tends to support that.
This include_next for a C compile should be unconditional, not
conditional upon being Clang.
2026-01-30 10:01:51 -05:00
Nikolas Klauser
a03c35fad7
[libc++][NFC] Avoid checking that string::iterator is a contiguous iterator (#178636)
`__is_continuation` is only used in contexts where we already know that
the argument is a contiguous iterator. However, due to the context in
which it is used, we check it as soon as the header is included. The
`contiguous_iterator` check is quite expensive (~12ms on my system), so
avoiding it reduces compile times for quite a few headers, including
`<vector>`.
2026-01-30 14:29:12 +01:00
sohail
9bde15d76d
[libc++] Deprecate std::launch::any extension (#173397)
`std::launch::any` was a draft C++11 feature that was removed before the
final standard but it has remained in libc++ as an extension. This patch
marks it as deprecated and suggests using `std::launch::async |
std::launch::deferred` instead.

- Used `_LIBCPP_DEPRECATED_` to mark `std::launch::any` as deprecated
with an associated warning message recommending `std::launch::async |
std::launch::deferred` instead.
- Added a `.verify.cpp` test to validate the deprecation warning.
- Updated existing tests to avoid using the deprecated extension.
- Added note about deprecation in docs.

Fixes #173219

---------

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2026-01-30 13:51:52 +08:00
Christopher Di Bella
3222670c68
[libc++] Fix __split_buffer_size_layout bugs (#178341)
As `__split_buffer` doesn't have any unit tests, and because #139632
disassociated adding `__split_buffer` support for both pointer-based and
size-based layouts from its `vector` counterpart, libc++ had no way to
expose `__split_buffer_size_layout` bugs until the size-based vector
patch integrated #139632. This commit fixes the two problems that were
identified while working on #155330.
2026-01-29 13:24:01 -05:00
Nikolas Klauser
224a6ffbc7
[libc++] Refactor swap_ranges to use __specialized_algorithm for the vector<bool>::iterator specialization (#173384) 2026-01-29 11:11:51 +01:00
Nikolas Klauser
ba0cddb43e
[libc++][NFC] Remove _LIBCPP_FREESTANDING (#176907)
`_LIBCPP_FREESTANDING` isn't used and it is unclear what libc++'s
strategy is going to be for implementing freestanding at the moment.
Therefore, remove the macro.
2026-01-29 10:13:32 +01:00
Nikolas Klauser
562c0b002c
[libc++] Use the fast path for move assignment in __tree if the allocator is_always_equal (#177115)
This avoids instantiating some code that we know is dead. This is also a
prerequisite for #134330, since we avoid trying to `const_cast` in the
common case now.
2026-01-28 10:52:18 -05:00
Nikolas Klauser
9bdaebbeae
[libc++] Remove a bunch of unused includes from <atomic> detail headers (#178161) 2026-01-27 22:00:15 +01:00
Michael G. Kazakov
4ccdd2ffac
[libc++][pstl] Generic implementation of parallel std::is_sorted (#176129)
This PR implements a generic backend-agnostic parallel `std::is_sorted`
based on `std::transform_reduce`.

While this approach is suboptimal comparing a direct backend-specific
implementation, since it doesn't support early termination and requires
a reduction operation, it does show speedup when the dataset is large
enough and the comparator is not absolutely trivial.

Parent issue: #99938
2026-01-27 13:43:23 -05:00
Louis Dionne
20014885c3
[libc++] Honor __SANITIZER_DISABLE_CONTAINER_OVERFLOW__ in libc++ (#168955)
Address sanitizer recently got a new macro __SANITIZER_DISABLE_CONTAINER_OVERFLOW__
which is intended to disable container overflow checks in libraries (either the
standard library or user libraries that might provide such checks). This patch makes
libc++ honor that macro and, in addition, cleans up how these checks are enabled for
string (which is special) by introducing a macro just for string.

rdar://166234942
2026-01-27 16:04:03 +00:00
Nikolas Klauser
c48be319db
[libc++] Forward calls to ranges::swap_ranges to the 3-leg implementation if possible (#176762)
This allows us to make use of any optimizations to `std::swap_ranges` in
`ranges::swap_ranges`.

This patch also moves some code specific to `ranges::swap_ranges` into
`ranges_swap_ranges.h`.
2026-01-27 16:25:41 +01:00
Nikolas Klauser
05443a9807
[libc++] Refactor how we do amortized growth (#171117)
When doing amortized growth we currently have separate functions for
calculating the new capacity and allocating. However, we set the size to
the calculated capacity that way instead of the actually required size.
This makes the interface quite confusing, since the size has to be set
manually. Instead, this patch refactors the function to get the
amortized growth capacity to instead allocate and set the size
correctly.
2026-01-27 14:27:48 +01:00
Nikolas Klauser
8dfca82a7c
[libc++][NFC] Don't use std::distance in std::equal (#177113)
We don't need to use `std::distance`, since we know for a fact that we
have random access iterators in that place. Instead, we can just
subtract the iterators, avoiding a bunch of template machinery and
imrpoving compile times a bit.
2026-01-27 10:29:12 +01:00
Hristo Hristov
b232970172
[libc++][ranges] Updated [[nodiscard]] implementation for subrange and join_with_view (#176936)
Added or removed `[[nodiscard]]` according to the guidelines and updated
the tests.

 - https://libcxx.llvm.org/CodingGuidelines.html
 - https://wg21.link/range.subrange
 -  https://wg21.link/range.join.with.view

Towards #172124
2026-01-27 11:03:07 +02:00
Nikolas Klauser
2e5eb5d212
[libc++][NFC] Forward string observer functions when appropriate (#171120)
Instead of unwrapping the arguments to string's observer functions
everywhere, simply unwrap the specific argument and then forward all of
them to a single function which does the final unwrapping of `*this`.
This simplifies the code a bit.
2026-01-26 22:09:08 +01:00
Nikolas Klauser
b285745dc1
[libc++][NFC] Move attribute macros out of __config into a detail header (#176903)
This patch moves all macros which expand to attributes into a detail
header, including the relatively complex visibility and ABI-hiding
attributes. This also adds sections for attributes that are important to
not be ignored. At the bottom there is a catch-all section for
attributes which can safely be ignored and are only important for the
quality of implementation.
2026-01-26 15:31:25 -05:00
h-vetinari
62339468e0
[libc++] Do not redefine _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION (#177851)
Review in #176857 asked to remove this block and make
_LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION similar
to the other availability macros. This patch does this.
2026-01-26 12:56:23 -05:00
xiaoyang-sde
9311996261
[libc++][ranges] implement ranges::shift_left (#83231)
Implement the `ranges::shift_left` algorithm from
[P2440R1](https://wg21.link/P2440R1).

Closes: #134061

---------

Co-authored-by: Hui Xie <hui.xie1990@gmail.com>
Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2026-01-25 10:35:43 +00:00
Louis Dionne
a3ebe650d3
[libc++][NFC] Clang-format <vector> and remove unused __self alias (#177021)
These changes were extracted out of the size-based vector patch.

Co-authored-by: Christopher Di Bella <cjdb@google.com>
2026-01-23 09:17:20 -05:00
Hristo Hristov
8c8661e170
[libc++][NFC] Reformat subrange.h (#177118)
As a pre-requisite to https://github.com/llvm/llvm-project/pull/176936
2026-01-22 04:40:49 +02:00
Hristo Hristov
503db9859e
[libc++][NFC] Reformat wrap_iter.h (#177127)
As a pre-requisite to https://github.com/llvm/llvm-project/pull/172200
2026-01-22 04:40:28 +02:00
Hui
2e53764f2d
[libc++] Introduce a native timed wait in the synchronization library (#172214)
Fixes #172137
2026-01-20 09:59:57 -05:00
Nikolas Klauser
67cf9b8f82
[libc++] Remove _LIBCPP_CONSTINIT (#171802)
`_LIBCPP_CONSTINIT` is only ever used inside `src/`, so we can just
replace it with `constinit` instead.
2026-01-20 12:51:43 +01:00
Hristo Hristov
19c7eb358b
[libc++][chrono] Applied [[nodiscard]] to The Civil Calendar (#174968)
- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/time.cal

Towards #172124
2026-01-20 09:36:01 +02:00
A. Jiang
bf6e9867c7
[libc++] Update __cpp_lib_flat_set to 202511L according to P3567R2 (#176297)
The paper was already implemented in LLVM22 in cd13170aea2,
but the previous patch forgot to update `__cpp_lib_flat_set`.

Fixes #176232.
2026-01-19 15:27:56 -05:00
Hui
985d75a57a
[libc++] define FTM __cpp_lib_ranges_zip (#176569)
P2321R2 has been implemented in various PRs. Based on the discussion
in #105169, the last bit in iterator.concept.winc doesn't require
any changes, so we can actually mark this as done.

Fixes #105169
2026-01-19 15:18:57 -05:00
Nikolas Klauser
dd7348420d
[libc++][NFC] Use forward declarations in {in,}out_ptr.h (#176341)
This doesn't improve compile times currently, but it does make it clear
that we don't need the definition of `shared_ptr` or `unique_ptr` for
`{in,}out_ptr`. It also reduces compile times quite a bit if we ever use
these utilities in another header.
2026-01-19 15:13:47 -05:00
Ian Anderson
501645cbeb
[libc++] Remove libc++'s <float.h> (#175849)
The existence of <float.h> in libc++ leads to difficulties with modules,
especially when it comes to supporting the various __need macros
supported by the Clang builtin headers.

Fortunately, libc++'s <float.h> only defines FLT_EVAL_METHOD and
DECIMAL_DIG, which are already defined by the Clang builtin headers in
C++11 mode. Hence, removing the header entirely should have no impact,
apart from not defining FLT_EVAL_METHOD and DECIMAL_DIG in C++03 mode.
However, that is arguably something that libc++ shouldn't be doing: if
the underlying C library doesn't want to provide these definitions, then
libc++ shouldn't force it to.
2026-01-19 15:12:40 -05:00
Volodymyr Turanskyy
dc5e1d05bf
[libc++] Provide flag for RUNTIMES_USE_LIBC=llvm-libc (#174967)
There was no flag added for llvm-libc when picolibc and newlib were
provided in https://github.com/llvm/llvm-project/pull/147956 - the
missing flag breaks libc++ iostream support now because this check
9a8421fa61/libcxx/include/__config (L719)
fails unless an LLVM libc header is included.
2026-01-15 12:24:39 +00:00
Nikolas Klauser
4304106dc2
Reapply "[libc++] Optimize std::find_if" (#175903) (#175921)
#175913 removed that `__builtin_assume_dereferenceable(ptr, 0)` implies
`ptr != nullptr`, which should allow us to use the builtin with LLVM 23.

This reverts commit 776c09c212e945fdceeae240b42c38df3dd34727.
2026-01-15 10:16:45 +01:00
Fady Farag
d4469292bf
[libc++] Add parentheses around shift operations in bitwise expressions (#175407)
This improves consistency within files that already use this style.
2026-01-14 17:36:33 -05:00
Louis Dionne
1f5c445c4b
[libc++][NFC] Refactor __request_unbuffered_mode (#168950)
It's a bit awkward to pass the char* and the size to the function, which
then only decides whether it should be a no-op with it. Instead, only
call the function when the pointer is null and n is 0, which matches
more closely what the standard says with respect to calling pubsetbuf(0, 0).
2026-01-14 13:39:16 -05:00
Prabhu Rajasekaran
676fc58744
[libc++] Fix use of static in constexpr (#175667) 2026-01-14 06:08:14 -08:00
Nikolas Klauser
776c09c212
Revert "[libc++] Optimize std::find_if" (#175903)
`__builtin_assume_dereferenceable` currently implies that the pointer
given to it is non-null, even if the size is zero. This causes
miscompilations, since we consider [nullptr, nullptr) to be a valid
range.

Reverts llvm/llvm-project#167697
2026-01-14 11:15:21 +01:00
Nikolas Klauser
7581c7018b
[libc++] Simplify __unwrap_iter a bit (#175153)
`__unwrap_iter` doesn't need to SFINAE away, so we can just check inside
the function body whether an iterator is copy constructible. This
reduces the overload set, improving compile times a bit.
2026-01-13 17:01:02 +01:00
Hristo Hristov
6889c3863e
[libc++][ranges] Applied [[nodiscard]] to Range access (#173550)
`[[nodiscard]]` should be applied to functions where discarding the
return value is most likely a correctness issue.

- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/range.access

Towards #172124
2026-01-13 17:10:18 +02:00
Louis Dionne
53ad9cdc32 [libc++] Fix typo in math special functions
Extracted from #156270

Co-Authored-by: Austin Jiang <austinjiangboyu@gmail.com>
2026-01-13 08:47:09 -05:00