5903 Commits

Author SHA1 Message Date
Louis Dionne
4c4606a743
[libc++] Add missing assertion in std::span constructor (#118396)
The (iterator, size) constructor should ensure that it gets passed a
valid range when the size is not 0.

Fixes #107789
2024-12-09 11:51:07 -05:00
Nikolas Klauser
ce7771902d
[libc++][C++03] Update include guards (#109001)
This patch updates the includes guards of the C++ 03 headers.

This is part of https://discourse.llvm.org/t/rfc-freezing-c-03-headers-in-libc.
2024-12-09 17:34:37 +01:00
Hui
f22ecdd9b9
[libc++] Move out flat_map::iterator (for reusing it in flat_multimap) (#117445) 2024-12-09 09:22:27 -05:00
Louis Dionne
0e34f3f496
[libc++] Extract a clean base support API for std::atomic (#118129)
This patch documents the underlying API for implementing atomics on a
platform.

This doesn't change the operations that std::atomic is based on, but it
reorganizes the C11 / GCC implementation split to make it clearer what's
the base support layer and what's not.
2024-12-09 09:14:17 -05:00
cmtice
384e69a914
[libc++] Add _LIBCPP_NODEBUG on internal allocator trait aliases (#118835)
Put _LIBCPP_NODEBUG on the new allocator trait aliases introduced in
https://github.com/llvm/llvm-project/pull/115654. This prevents a large
increase in the gdb_index size that was introduced by that PR.
2024-12-06 08:53:56 -05:00
serge-sans-paille
f7ff3cde96
[libc++] Fix sub-overflow in std::gcd implementation (#117984)
Fix #117249
2024-12-06 07:57:49 +00:00
Louis Dionne
2393ab65ed
[libc++] Fix unintended ABI break in associative containers with reference comparators (#118685)
While reference comparators are a terrible idea and it's not entirely
clear whether they are supported, fixing the unintended ABI break is
straightforward so we should do it as a first step.

Fixes #118559
2024-12-05 15:56:00 -05:00
Louis Dionne
c8060423fe
[libc++] Drop dependency on __functional/operations.h from <atomic> (#117302)
This should reduce the preprocessed size of the atomic header and other
headers in the synchronization library.
2024-12-03 16:57:17 -05:00
Louis Dionne
3d437893c3
[libc++] Re-enable Clang-tidy checks in the CI (#110026)
Now that we've gained control of our CI docker image again,
we can re-enable the clang-tidy tests.
2024-12-02 14:26:28 -05:00
Nikolas Klauser
04cc492ec3 [libc++][NFC] Remove unused macros from <__configuration/availability.h> 2024-11-30 13:18:33 +01:00
Benjamin Kramer
59f57be94f Revert "[libc++] Simplify the implementation of reserve() and shrink_to_fit() (#113453)"
This reverts commit d648eed5899c4be10f1f7866eebef2bc171e673f. Breaks
anything that relies on sized deallocation, e.g. asan and tcmalloc.
2024-11-29 13:18:28 +01:00
Nikolas Klauser
d648eed589
[libc++] Simplify the implementation of reserve() and shrink_to_fit() (#113453)
Since we changed the implementation of `reserve(size_type)` to only ever
extend,
it doesn't make a ton of sense anymore to have `__shrink_or_extend`,
since the code
paths of `reserve` and `shrink_to_fit` are now almost completely
separate.

This patch splits up `__shrink_or_extend` so that the individual parts
are in `reserve`
and `shrink_to_fit` depending on where they are needed.
2024-11-28 23:07:45 +01:00
Louis Dionne
81c88135e4
[libc++] Use the __strtoNUM functions from __locale instead of the old API (#118029)
The commit where I switched from using the old locale base API to the
new functions defined inside the __locale namespace forgot to update
references to the strtoNUM functions. This patch fixes that.
2024-11-28 16:44:46 -05:00
Peng Liu
056153f36e
Optimize vector::assign for InputIterator-only pair inputs (#113852)
This PR optimizes the input iterator overload of `assign(_InputIterator,
_InputIterator)` in `std::vector<_Tp, _Allocator>` by directly assigning
to already initialized memory, rather than first destroying existing
elements and then constructing new ones. By eliminating unnecessary
destruction and construction, the proposed algorithm enhances the
performance by up to 2x for trivial element types (e.g.,
`std::vector<int>`), up to 2.6x for non-trivial element types like
`std::vector<std::string>`, and up to 3.4x for more complex non-trivial
types (e.g., `std::vector<std::vector<int>>`).

###  Google Benchmarks

Benchmark tests (`libcxx/test/benchmarks/vector_operations.bench.cpp`)
were conducted for the `assign()` implementations before and after this
patch. The tests focused on trivial element types like
`std::vector<int>`, and non-trivial element types such as
`std::vector<std::string>` and `std::vector<std::vector<int>>`.



#### Before
```
-------------------------------------------------------------------------------------------------
Benchmark                                                       Time             CPU   Iterations
-------------------------------------------------------------------------------------------------
BM_AssignInputIterIter/vector_int/1024/1024                  1157 ns         1169 ns       608188
BM_AssignInputIterIter<32>/vector_string/1024/1024          14559 ns        14710 ns        47277
BM_AssignInputIterIter<32>/vector_vector_int/1024/1024      26846 ns        27129 ns        25925
```


#### After
```
-------------------------------------------------------------------------------------------------
Benchmark                                                       Time             CPU   Iterations
-------------------------------------------------------------------------------------------------
BM_AssignInputIterIter/vector_int/1024/1024                   561 ns          566 ns      1242251
BM_AssignInputIterIter<32>/vector_string/1024/1024           5604 ns         5664 ns       128365
BM_AssignInputIterIter<32>/vector_vector_int/1024/1024       7927 ns         8012 ns        88579
```
2024-11-28 20:52:59 +01:00
Peng Liu
c5cd1e958c
[libc++] Add exception guard for vector<bool>::__init_with_sentinel (#115491)
As a drive-by, also improve the test coverage for throwing exceptions
in vector<bool> constructors.
2024-11-28 09:09:54 -05:00
Louis Dionne
d681e1030f
[libc++] Refactor atomic_wait using lambdas (#115746)
Now that we've dropped support for older C++ dialects in the
synchronization library, we can use lambdas to clarify some of the code
used to implement atomic_wait.
2024-11-27 14:49:57 -05:00
Nico Weber
a94cec5212
Revert "[libc++] Remove workaround which allows setting _LIBCPP_OVERRIDABLE_FUNC_VIS externally (#113139)" (#117779)
This reverts commit 2e686d6d17c4cc7608510a856055e6ca79fcb917.

See https://github.com/llvm/llvm-project/issues/117571
2024-11-26 18:45:03 -05:00
Nikolas Klauser
7ae61a36f9
[libc++] Add __detected_or_t and use it to implement some of the allocator traits aliases (#115654)
This simplifies the implementation a bit, since we don't need a lot of
the `__has_x` classes anymore. We just need two template aliases to
implement the `allocator_traits` aliases now.
2024-11-26 23:53:26 +01:00
Peng Liu
8458bbe594
[libc++] Fix capacity increase issue with shrink_to_fit for __split_buffer (#117720)
This PR fixes the issue where `__split_buffer::shrink_to_fit` may
unexpectedly increase the capacity, similar to the issue for
`std::vector` in #97895. The fix follows the same approach
used in #97895 for `std::vector`.
2024-11-26 16:38:47 -05:00
Peng Liu
5ce981e76d
[libc++] Refactor vector move constructor with allocator (#116449)
This PR simplifies the implementation of std::vector's move constructor
with an alternative allocator by invoking __init_with_size() instead of
calling assign(), which ultimately calls __assign_with_size(). The
advantage of using __init_with_size() lies in its internal use of
an exception guard, which simplifies the code. Furthermore, from a
semantic standpoint, it is more intuitive for a constructor to call
an initialization function than an assignment function.
2024-11-26 16:00:14 -05:00
Nikolas Klauser
99fd1c5536 [libc++][NFC] Don't add legacy transitive includes in <__chrono/duration.h> 2024-11-25 17:40:54 +01:00
Louis Dionne
afae1a5f32
[libc++] Remove _LIBCPP_DISABLE_AVAILABILITY macro (#112952)
This was slated for removal years ago, so now's a good time to remove it.
2024-11-23 20:02:15 -05:00
Nikolas Klauser
4a8329cd7d
[libc++] Granularize <mutex> includes (#117068) 2024-11-23 12:21:23 +01:00
Jay Foad
d6fc7d3ab1 Fix typo "intead" 2024-11-21 14:48:38 +00:00
Nikolas Klauser
9ebc6f5d6d
[libc++] Include headers in <thread> conditionally (#116539) 2024-11-20 23:07:20 +01:00
Louis Dionne
3a63407686
[libc++] Make __atomic_base into an implementation detail of std::atomic (#115764)
The __atomic_base base class is only useful to conditionalize the
operations we provide inside std::atomic. It shouldn't be used directly
from other places in the library which can use std::atomic directly
instead.

Since we've granularized our includes, using std::atomic directly should
not make much of a difference compile-time wise.

This patch starts using std::atomic directly from other classes like
std::barrier and std::latch. Changing this shouldn't be an ABI break
since both classes have the same size and layout.

The benefits of this patch are isolating other parts of the code base
from implementation details of std::atomic and simplifying the mental
model for std::atomic's layers of implementation by making it clear that
__atomic_base is only an implementation detail of std::atomic.
2024-11-20 00:35:14 +01:00
Louis Dionne
ec67ad594b [libc++][NFC] Format <string> 2024-11-18 23:23:46 +01:00
Nikolas Klauser
85ef9666c8
[libc++] Avoid including all of <thread> in <future> (#116541) 2024-11-18 20:04:05 +01:00
Nikolas Klauser
de5e4ebb5a
[libc++] Remove transitive includes from empty headers (#116295)
This removes transitive includes that are only in a header that is empty
in a given C++ version.
2024-11-18 19:59:47 +01:00
Louis Dionne
748a29f052 [libc++][NFC] Fix incorrect include guard 2024-11-18 14:29:14 +01:00
Louis Dionne
5a48162dc8
[libc++] Remove unnecessary std::vector accessors (#114423)
Now that we don't use __compressed_pair anymore inside std::vector, we
can remove some unnecessary accessors. This is a mechanical replacement
of the __alloc() and __end_cap() accessors, and similar for
std::vector<bool>.

Note that I consistently used this->__alloc_ instead of just __alloc_
since most of the code in <vector> uses that pattern to access members.
I don't think this is necessary anymore (and I'm even less certain I
like this), but I went for consistency with surrounding code. If we want
to change that, we can do a follow-up mechanical change.
2024-11-18 10:48:09 +01:00
A. Jiang
b5bc528c14
[libc++] Guard __pad_and_output with _LIBCPP_HAS_LOCALIZATION (#116580)
This fixes errors for no-localization builds (possibly introduced by
#116223).
2024-11-18 17:03:29 +08:00
Nikolas Klauser
7c010bfdc5
[libc++] Remove some unnecessary boilerplate in <__chrono/duration.h> (#116238) 2024-11-17 12:23:05 +01:00
Nikolas Klauser
811186764d [libc++] Fix a few problems found by clang-tidy 2024-11-17 11:07:41 +01:00
Nikolas Klauser
dedc515999
[libc++] Avoid including <string> in <mutex> (#116254) 2024-11-16 19:25:03 +01:00
Nikolas Klauser
adb80d8a4c
[libc++] Address post-commit comments for __scope_guard (#116291)
Fixes #116204
2024-11-16 19:23:58 +01:00
Nikolas Klauser
764834d63d
[libc++] Remove <istream> and <ostream> includes from <iomanip> (#116223)
This reduces the include time of `<filesystem>` by ~50ms.
2024-11-16 18:20:55 +01:00
Nikolas Klauser
1636580b0a
[libc++] Avoid including <ostream> in <fstream> and <strstream> (#116014)
This reduces the include time of `<fstream>` from ~800ms to ~500ms.
2024-11-16 18:19:17 +01:00
Nikolas Klauser
46d8aa8d6a
[libc++] Make __throw_ member functions static (#116233)
Fixes #116092
2024-11-14 22:35:20 +01:00
Louis Dionne
5c3befb91c [libc++] Add forgotten call to std::__to_address in __uninitialized_allocator_relocate 2024-11-14 14:07:57 +01:00
Louis Dionne
8fde648aad
[libc++] Remove obsolete accessors in std::list and std::forward_list (#115748)
We don't need these accessors anymore now that we stopped using
compressed-pair.
2024-11-14 13:09:11 +01:00
Louis Dionne
6721bcfd1b
[libc++] Accept iterators instead of raw pointers in __uninitialized_allocator_relocate (#114552)
This generalizes the algorithm a bit. Unfortunately, we can't make 
the call sites cleaner inside std::vector because the arguments being
passed can all be fancy pointers, which may not be contiguous iterators.
2024-11-14 11:22:48 +01:00
Nikolas Klauser
67b81e2120
[libc++] Split __shared_count out of <__memory/shared_ptr.h> (#115943)
`__shared_count` is used in a few places where `shared_ptr` isn't. This
avoids a bunch of transitive includes needed for the implementation of
`shared_ptr` in these places.
2024-11-13 12:14:02 +01:00
Nikolas Klauser
b69ddbc628
[libc++] Make variables in templates inline (#115785)
The variables are all `constexpr`, which implies `inline`. Since they
aren't `constexpr` in C++03 they're also not `inline` there. Because of
that we define them out-of-line currently. Instead we can use the C++17
extension of `inline` variables, which results in the same weak
definitions of the variables but without having all the boilerplate.
2024-11-13 11:57:16 +01:00
Peng Liu
c7df10643b
Unify naming of internal pointer members in std::vector and std::__split_buffer (#115517)
Related to PR #114423, this PR proposes to unify the naming of the
internal pointer members in `std::vector` and `std::__split_buffer` for
consistency and clarity.

Both `std::vector` and `std::__split_buffer` originally used a
`__compressed_pair<pointer, allocator_type>` member named `__end_cap_`
to store an internal capacity pointer and an allocator. However,
inconsistent naming changes have been made in both classes:
- `std::vector` now uses `__cap_` and `__alloc_` for its internal
pointer and allocator members.
- In contrast, `std::__split_buffer` retains the name `__end_cap_` for
the capacity pointer, along with `__alloc_`.

This inconsistency between the names `__cap_` and `__end_cap_` has
caused confusions (especially to myself when I was working on both
classes). I suggest unifying these names by renaming `__end_cap_` to
`__cap_` in `std::__split_buffer`.
2024-11-13 11:08:08 +01:00
Nikolas Klauser
a2042521a0
[libc++] Remove _AlgPolicy from std::copy and algorithms using std::copy (#115887)
`std::copy` doesn't use the `_AlgPolicy` for anything other than calling
itself with it, so we can just remove the argument. This also removes
the need in a few other algorithms which had an `_AlgPolicy` argument
only to call `copy`.
2024-11-12 23:03:52 +01:00
Nikolas Klauser
36fa8bdfa0
[libc++][NFC] Remove unused functions from <__split_buffer> (#115735) 2024-11-12 22:55:59 +01:00
Nikolas Klauser
5b67372aec [libc++] Remove a few unused includes from <__algorithm/find_end.h> 2024-11-12 22:11:15 +01:00
Nikolas Klauser
e3c958a9a4
[libc++] Replace template structs with template variables in <ratio> (#115782)
This avoids a bit of boilerplate.
2024-11-12 15:15:18 +01:00
Nikolas Klauser
5098b56d22
[libc++] Introduce a standalone __scope_guard and use it in <string> (#114867)
This introduces a new `__scope_guard` without any fancy features. The
scope guard is used in `<string>` to simplify some of the ASan
annotations (especially by making it harder to forget them where
exceptions are thrown).
2024-11-12 00:00:34 +01:00