Commit graph

305 commits

Author SHA1 Message Date
William Tran-Viet
7ded638ea3
[libc++] Granularize <optional> (#206644)
Certain headers require `optional<T&>`, so it may be beneficial to split
out `optional<T>` and `optional<T&>`. This can allow consumers to only
bring in the `optional` flavour it needs..

- Parcel out the respective pieces into their own header.
- Certain sources rely on transitive includes brought in by
`<optional>`, so they're kept there for now, and only tests have been
fixed.

---------

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2026-07-30 11:12:15 +08:00
Hui
de962dda12
[libc++] Implement P0528R3 std::atomic CAS for types with padding (#76180)
Fixes #99984

Implement P0528R3 The Curious Case of Padding Bits, Featuring Atomic
Compare-and-Exchange


The unit test padding.pass.cpp is assisted by Cursor. However, all tests
have been modified to test what I wanted to test

---------

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2026-07-28 07:15:19 +01:00
Michael G. Kazakov
150f33ce28
[libc++][pstl] Implementation of a parallel std::mismatch() based on __pstl::__parallel_find() (#209291)
This PR adds a parallel version of `std::mismatch` as one of the backend
operations.
It also provides an implementation based on `__pstl::__parallel_find()`
which does the heavy lifting.
`libdispatch` and `std_thread` backends expose this implementation under
their backend tags, while the `serial` backend redirects the calls to
the serial `std::mismatch`.

4 flavours of the function are exposed: 3-legged, 3-legged with
predicate, 4-legged, 4-legged with predicate.
3-legged flavours are implemented in the `default` (`composition`)
backend by redirecting the call to the 4-legged flavours.

Included tests check that:

- Semantics of the iterator-only functions is correct.
- Semantics of the predicated functions is correct.
- Custom types are supported by the implementation.
- The functions correctly SFINAE out when the first argument is not an
execution policy.
- The `nodiscard` policy is followed.
- The `noexcept` policy is followed.
- `static_assert` verifies iterators' categories.

Part of #99938.
2026-07-26 22:51:39 +08:00
Hui
a434fe87cc
[libc++] Implement std::function_ref (#186692)
Papers:
- P0792R14: function_ref: a type-erased callable reference
- P3948R1 constant_wrapper is the only tool needed for passing constant
expressions via function arguments
- P3961R1 Less double indirection in function_ref (RU-220)

LWG issues:
- LWG4256: Incorrect constraints for function_ref constructors from
nontype_t
- LWG4425: CTAD function_ref of data member pointer should produce
noexcept signature

Fixes #189606
Fixes #105376
Fixes #189604
Fixes #171321
Fixes #171387

---------

Co-authored-by: Xiaoyang Liu <siujoeng.lau@gmail.com>
Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2026-07-25 17:44:55 +01:00
Nikolas Klauser
1b2efd2e07
[libc++] Split the multidimensional algorithms out of uninitialized_algorithms.h (#207447)
While these algorithms are conceptually related to other
`uninitialized_*` algorithms, they're likely only ever going to be used
by `shared_ptr`. `uninitialized_algorithms.h` itself is also getting
rather large, so it makes sense to split the multidimensional versions
into their own header.

Fixes #207417
2026-07-06 09:28:56 +02:00
Nikolas Klauser
a68147f0f0
[libc++] Use std::to_chars to format thread::id and canonicalize the representation across platforms (#181624)
This makes printing `thread::id` faster, since we avoid a bunch of
boilerplate code that isn't optimized away. It also avoids including
`<sstream>` and `<__locale>`, cutting the time to parse `<thread>` in
half.

This also changes the output when printing the id on a platform that
uses a pointer as the underlying type. I don't think that's a problem,
since the thread id isn't in any way stable. It also makes the output
consistent across all platforms we support.
2026-07-05 19:36:39 +02:00
Christopher Di Bella
bd338806e4
[libc++] Add a size-based representation for vector in the unstable ABI (#155330)
This commit adds an alternative representation for `std::vector` in a similar
manner to `__split_buffer` in #139632. This alternative representation was
measured to provide significant performance gains over the classic representation.

To do so, we introduce a layout type that tracks implementation details that
differ between the pointer-based vector and size-based vector representations.
`vector` does not parameterise its layout type, unlike `__split_buffer`.
`__split_buffer` is used by both `vector` and `deque`, and being able to take
an ABI break on `vector` doesn't automatically mean that users can also tolerate
a break on `deque`.

This new vector representation can be opted into by defining the
_LIBCPP_ABI_VECTOR_LAYOUT_SIZE_BASED macro when configuring the
library's ABI.

This change was the target of this RFC: https://discourse.llvm.org/t/adding-a-size-based-vector-to-libc-s-unstable-abi
2026-07-01 15:12:36 -04:00
Nikolas Klauser
28f08a6f64
[libc++] Don't make libc++ headers system headers in the modulemap (#205509)
We currently have `[system]` attributes in the modulemap, which
automatically makes the headers inside the module system headers. This
disables warnings when running the tests, which we generally don't want.
We also already have `#pragma GCC system_header` everywhere and use
`-isystem`, so there isn't much need for the tag in the modulemap as
well.
2026-07-01 14:52:50 +02:00
Nikolas Klauser
78af5e2b68
[libc++] Split the PSTL configuration macros into a separate header (#205531)
This continues the effort to split `<__config>` into self-contained
detail headers.
2026-06-26 20:36:05 +02:00
William Tran-Viet
8a531c3608
[libc++] Implement P1885R12: <text_encoding> (#141312)
Resolves #105373, resolves #118371 and resolves #105332

- Implements `<text_encoding>`
- Adds availability macros for LLVM 23.
- The data is stored in three tables:
  - One giant string split by null-terminators to represent the aliases
- An index table which stores indexes into the string, each entry
representing the first character of an alias
- Text encoding data, which stores an index to the index table, the MIB,
and the number of aliases the encoding has.

Storing it in the above manner allows us to make significant savings in
binary file size and required runtime storage for the data.

As required by the LLVM Project's AI use policy:
- The implementation for `__get_locale_encoding(const char*)` for
Windows has been developed with the assistance of AI.

---------

Co-authored-by: A. Jiang <de34@live.cn>
2026-06-25 07:46:11 +08:00
Nikolas Klauser
caea959905
[libc++] Remove _Lazy (#202303)
We don't actually need to evaluate anything as lazily as `_Lazy` does or
we can achieve the same amount of laziness in other ways, so we can get
rid of it. This was required previously in some places due to a Clang
bug, which has been resolved and implemented by all compilers we
support.
2026-06-10 16:59:50 +02:00
Nikolas Klauser
fc9bf89cfd
[libc++] Simplify some meta programming in <variant> (#201538)
Specifically, this avoids `__dependent_type` and `__type_identity`
instantiations, reducing compile times a bit.
2026-06-09 09:12:46 +02:00
Nikolas Klauser
ce5b2e8764
[libc++] Drop transitive includes by default (#195509)
This patch removes the unused transitive includes by default.
`_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23` can be defined to keep the
transitive includes around for an easier transition. The macro will be
removed in LLVM 24.

This patch implements
https://discourse.llvm.org/t/rfc-remove-unused-transitive-includes-from-the-libc-headers/90157
2026-06-05 09:45:49 +02:00
Nhat Nguyen
866945c202
[libc++][ranges] P2542R8: Implement views::concat (#120920)
Closes #105419
Closes #105348
Closes #105349
Closes #171314

Assisted-by: Chatgpt. 
I use AI to help me write some tests, however. I have reviewed the code
I submit in the tests.

---------

Co-authored-by: A. Jiang <de34@live.cn>
Co-authored-by: Hristo Hristov <hghristov.rmm@gmail.com>
Co-authored-by: Hristo Hristov <zingam@outlook.com>
2026-05-30 23:26:16 +01:00
Nikolas Klauser
0d6aac78ad
[libc++] Remove workarounds for __{add,remove}_pointer on AppleClang (#199821)
We've updated the supported AppleClang version, so we can drop those
workarounds now.

This also removes `__is_referenceable_v`, since it's no longer used.
2026-05-27 11:11:55 +02:00
Nikolas Klauser
182ae96a82
[libc++] Port The OpenBSD localization to the new locale API (#194317) 2026-05-19 12:41:42 +02:00
Nikolas Klauser
e925b357e1
[libc++] Introduce a private version of in_out_result and use it for copy/move algorithms (#198086)
This patch introduces a new `__in_out_result`, which is an internal
back-ported version of `in_out_result`, and is convertible to that when
it exists. This improves the readability of the code, since it replaces
uses of `first` and `second` with `__in_` and `__out_`, making it clear
which iterator is accessed.

Other algorithms will be updated in separate patches.
2026-05-17 10:06:05 +02:00
Nikolas Klauser
597e4ac7fb
[libc++] Replace __is_primary_template with a concept (#197646)
`__is_primary_template` is only used in C++20 and later, so we can
replace it with a concept, which is significantly faster than the
current SFINAE machinery. In my testig this reduces the time to
instantiate `std::ranges::find_first_of` from ~48ms to ~40ms.
This also inlines the single remaining use of `_IsValidExpansion`.
2026-05-15 20:31:02 +02:00
Nikolas Klauser
2e2d90b986
[libc++] Introduce implicit and explicit ABI annotations (#193045)
This patch introduces `_LIBCPP_{BEGIN/END}_EXPLICIT_ABI_ANNOTATIONS` and
marks everything within an
`_LIBCPP_{BEGIN,END}_UNVERSIONED_NAMESPACE_STD` (and any derivatives
like `_LIBCPP_{BEGIN,END}_NAMESPACE_STD`) implicitly by default. This
allows us to drop `_LIBCPP_HIDE_FROM_ABI` in most of the code base,
except for functions which shouldn't be `_LIBCPP_HIDE_FROM_ABI`.

This patch doesn't remove any `_LIBCPP_HIDE_FROM_ABI`s, since we have
over 13k of them in the code base. Actually dropping them will happen
over some time to avoid too many merge conflicts.
2026-05-08 12:21:31 +02:00
Nikolas Klauser
f7329e61ad
[libc++] Remove libc++'s own fenv.h (#194629)
Our own `fenv.h` only `#undef`s macros which the C header might define.
None of the libcs we support define any of these functions as macros, so
we can simply drop the header.
2026-05-04 08:28:45 +02:00
Nikolas Klauser
be8e0842a1
[libc++] Remove libc++'s own ctype.h (#194615)
Our own `ctype.h` only `#undef`s macros which the C header might define.
None of the libcs we support define any of these functions as macros, so
we can simply drop the header.
2026-05-04 08:28:24 +02:00
Nikolas Klauser
124ab73043
[libc++] Remove libc++'s own inttypes.h (#193716)
The header does three things:
- it defines `__STDC_FORMAT_MACROS` for compatibility with glibc
versions we don't support anymore
- it includes `<stdint.h>`, which is already required to be included by
the C standard
- it `#undef`s two macros which might be defined by the libc. We don't
provide a `<stdint.h>` header ourselves, which might provide these
macros as well, so these are already not defined by any libcs we
support.

So this header should be safe to remove.
2026-04-30 11:00:29 +02:00
Jackson Stogel
7226f424bf
[libc++] Allow building libc++ using llvm-libc without locale support (#187603)
This PR allows libc++ to be configured to use LLVM-libc without
requiring locale-variants of libc functions.

This PR updates libc++ to use `<llvm-libc-types/mbstate_t.h>`
when building against LLVM-libc to avoid pulling in all of `wchar.h`.

There is some code duplication in this PR. The code in `formatting.h` is
similar to the implementation in `fuchsia.h`, except that the new code
does not use thread-local locales via `__locale_guard`:

673002f325/libcxx/include/__locale_dir/support/fuchsia.h (L133)

If reviewers would like, I'm happy to refactor to create more shared
headers, similar to how `no_locale` is set up. I've avoided doing so in
this PR, but lmk. It looks like there's three general ways these
passthrough functions are defined for unix systems:
- No locale (e.g. Fuchsia's `strtod` ignores locales entirely).
- Thread-local locale (e.g. other Fuchsia functions use `__locale_guard`
set up locales via `uselocale`).
- Native locale-specific function variant (i.e. the proxy just uses the
*_l variant, like `toupper_l`).
2026-04-29 13:06:14 -07:00
Will Hawkins
2f28e1db53
[libc++] Implement P1899 ranges::stride_view (#65200)
Implement `ranges::stride_view` in libc++. This PR was migrated from
Phabricator (https://reviews.llvm.org/D156924).

Closes #105198

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
Co-authored-by: A. Jiang <de34@live.cn>
2026-04-25 06:51:37 -04:00
Hristo Hristov
2039a51881
[libc++][ranges] P2164R9: Implements views::enumerate (#73617)
This patch implements https://wg21.link/P2164R9 `std::views::enumerate`.
This PR was migrated from https://reviews.llvm.org/D157193.

Implemented papers and LWG issues:
- https://wg21.link/P2164R9: views::enumerate
- https://wg21.link/LWG3919: enumerate_view may invoke UB for sized common non-forward underlying ranges
- https://wg21.link/LWG3914: Inconsistent template-head of ranges::enumerate_view
- https://wg21.link/LWG3912: enumerate_view::iterator::operator- should be noexcept

Closes #105251
Closes #105315
Closes #105280
Closes #105279
2026-04-22 08:42:38 -04:00
Hui
b799d7e8f8
[libc++] Implement std::constant_wrapper (#191695)
Fixes #148179
Fixes #189605
Fixes #189865
Fixes #171370
Fixes #189856
Fixes #189838
Fixes #171359
2026-04-21 11:45:59 -04:00
Nikolas Klauser
9b8635f324
[libc++][NFC] Move namespace macros into a detail header (#193003)
This is another step in splitting up `<__config>` to make it easier to
understand what's going on and have logically separate parts of the
configuration properly separated.
2026-04-20 20:05:59 +02:00
Nikolas Klauser
1729480d24
[libc++] Reduce the amount of formatter code included in <vector> (#178683)
`formatter::parse` and `formatter::format` only have to accept
`basic_format_parse_context` and `basic_format_context` respectively,
which are only guaranteed to be provided via `<format>`. This allows us
to only declare the functions for `formatter<bool>` in `<vector>` and
define them if `<format>` is included. This reduces the amount of time
it takes to parse `<vector>` by ~15% on my system.
2026-03-10 09:34:15 +01:00
Nikolas Klauser
3519aaecef Reapply "[libc++] Add test_iterators.h to the modulemap (#181351)"
Another test committed recently was missing an include.

This reverts commit ab88d9af53.
2026-02-16 08:09:55 +01:00
Aiden Grossman
ab88d9af53 Revert "[libc++] Add test_iterators.h to the modulemap (#181351)"
This reverts commit 04ef765b65.

This was causing buildbot failures for premerge:
1. https://lab.llvm.org/staging/#/builders/192/builds/20135
2026-02-16 02:28:39 +00:00
Nikolas Klauser
04ef765b65
[libc++] Add test_iterators.h to the modulemap (#181351)
and fix all the missing includes found by it.
2026-02-15 22:05:55 +01:00
Hui
d7a24d30f6
[libc++] Implement ranges::shift_right (#177847)
Implement the `ranges::shift_right` algorithm from
[P2440R1](https://wg21.link/P2440R1).

Fixes #134062
Fixes #105184
2026-02-14 17:34:44 +00: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
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
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
Hui
2e53764f2d
[libc++] Introduce a native timed wait in the synchronization library (#172214)
Fixes #172137
2026-01-20 09:59:57 -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
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 776c09c212.
2026-01-15 10:16:45 +01: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
6189512f73
[libc++] Optimize std::find_if (#167697)
```
Benchmark                       4ecfaa602f    80d5ac247d34    Difference    % Difference
----------------------------  --------------  --------------  ------------  --------------
bm_find_if_autovectorization         1901.51          306.12      -1595.39         -83.90%
```
2026-01-13 11:13:54 +01:00
Hui
27cbe6e8f6
[libc++] Refactor atomic_waitable_traits into its own header and remove pre 20 support (#173157)
It makes sense to decouple traits from the things they describe, since
that's the purpose of traits. Furthermore, this will make it possible to
reuse these traits from other places in the library (e.g. <semaphore>)
outside of the synchronization library.

Pre-C++20 support is removed as a drive-by since the synchronization
library does not support pre-C++20 anymore. This makes it possible to
simplify a few constructs by using concepts.

Requested in https://github.com/llvm/llvm-project/pull/172214#discussion_r2635968291
2026-01-06 13:19:25 -05:00
Hui
fc4661aa11
[libc++] Implement adjacent_transform (#168208)
This patch implements std::ranges::adjacent_transform_view. This is part
of P2321R2 tracked
by #105169.
2025-12-21 09:08:09 +00:00
Janet Cobb
ef190061d3
[libc++][concepts] P2404R3: Move-only types for equality_comparable_with, totally_ordered_with, and three_way_comparable_with (#99420)
This implements all of [P2404R3](https://wg21.link/p2404r3)'s concept
changes.

---------

Co-authored-by: A. Jiang <de34@live.cn>
Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2025-12-19 17:56:32 +08:00
Xing Xue
50ae726bb3
[libc++][AIX] Move to new locale APIs (#172068)
This patch moves to the new locale base APIs for AIX.

Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de>
2025-12-18 08:27:10 -05:00
Hui
1e15dbe311
[libc++] Implement adjacent_view (#165089) 2025-12-13 19:33:32 +00:00
xiaoyang-sde
fa79e0a400
[libc++][ranges] implement ranges::elements_of (#91414)
## Introduction

This patch implements `ranges::elements_of` from
[P2502R2](https://wg21.link/P2502R2). Specializations of `elements_of`
encapsulate a range and act as a tag in overload sets to disambiguate
when a range should be treated as a sequence rather than a single value.

```cpp
template <bool YieldElements>
std::generator<std::any> f(std::ranges::input_range auto &&r) {
  if constexpr (YieldElements) {
    co_yield std::ranges::elements_of(r);
  } else {
    co_yield r;
  }
}
```

## Reference

- [P2502R2: `std::generator`: Synchronous Coroutine Generator for
Ranges](https://wg21.link/P2502R2)
- [[range.elementsof]](https://eel.is/c++draft/range.elementsof)

Partially addresses #105226

---------

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
Co-authored-by: A. Jiang <de34@live.cn>
2025-12-13 19:44:45 +08:00
Hristo Hristov
4125e73cdc
[libc++] Applied [[nodiscard]] to hash<shared_ptr>, hash<unique_ptr>, etc. (#170674)
`[[nodiscard]]` should be applied to functions where discarding the
return value is most likely a correctness issue.

- https://libcxx.llvm.org/CodingGuidelines.html


1. `hash<shared_ptr>`, `hash<unique_ptr>`, `std::integer_sequence<>`
etc.
2. Also implements fixes to
https://github.com/llvm/llvm-project/issues/169634 on the go (issues
discovered during current implementation)

---------

Co-authored-by: A. Jiang <de34@live.cn>
Co-authored-by: Hristo Hristov <zingam@outlook.com>
2025-12-08 06:56:33 +02:00
Chenguang Wang
72402e8b67
[libc++] Support using Newlib as libc with locale support. (#167962)
Tested on Newlib v4.5.0. The newlib.h was mostly copied from linux.h
within the same directory.

Newlib locale support was initially added in http://llvm.org/D5385,
but removed in #113721 , because of revision http://llvm.org/D49927.
2025-12-05 14:56:22 +00:00
Nico Weber
e05fffbbc5 Revert "[Clang] Add __builtin_common_reference (#121199)"
This reverts commit 3b9e203364.
Causes not-yet-understood semantic differences, see commits
on #121199.
2025-12-02 19:37:16 -05:00
Nikolas Klauser
3b9e203364
[Clang] Add __builtin_common_reference (#121199) 2025-12-01 15:42:33 +01:00