761 Commits

Author SHA1 Message Date
Peng Liu
b350bc2c0b
[libc++] Verify forward_list self-merging is a no-op (#129985)
https://wg21.link/LWG3088 requires that `forward_list::merge()` is a no-op when passed
`*this`, which aligns with the behavior of `list::merge`. Although libc++'s implementation
of `forward_list::merge()` already meets this requirement, there were no tests to verify
this behavior. This patch adds the necessary tests to ensure that self-merging remains a
no-op and prevents any future regressions on this.

Closes #104942.
2025-03-19 12:28:11 -04:00
Peng Liu
70480fcff6
[libc++] Validate vector<bool> copy/move-assignment operators in realistic scenarios (#119817)
The existing tests for `vector<bool>` copy- and move-assignment
operators are limited to 3 bits only, which are inadequate to cover
realistic scenarios. Most `vector<bool>` operations have code paths that
are executed only when multiple storage words are involved, with each
storage word typically comprising 64 bits on a 64-bit platform.
Furthermore, the existing tests fail to cover all combinations
`POCCA`/`POCMA`, along with different allocator equality and/or
reallocation scenarios, leaving some critical code paths untested.

This patch enhances the test coverage by introducing new tests covering
up to 5 storage words, ensuring that partial words in the front or tail,
and whole words in the middle are all properly tested. Moreover, these
new tests ensure that the copy- and move-assignment operators are tested
under all combinations of `POCCA`/`POCMA` and various allocator equality
scenarios, both with or without reallocations.
2025-03-19 12:08:20 -04:00
Louis Dionne
5b9006550d [libc++][NFC] Fix incorrect main() signatures 2025-03-17 18:02:40 -04:00
A. Jiang
27e686c788
[libc++] Verify that LWG4140 is implemented (#128624)
According to the commit history, the constructors removed by LWG4140
have never been added to libc++.

Existence of non-public or deleted default constructor is observable,
this patch tests that there's no such default constructor at all.
2025-03-06 11:12:17 +08:00
A. Jiang
5bf1f03d1f
[libc++] Fix assignment in insertion into vector (#116001)
Changes:
- Avoid direct assignment in iterator-pair `insert` overload and
`insert_range`, except when the assignment is move assignment.
2025-03-04 09:06:49 +08:00
Nikolas Klauser
43401dd0b5
[libc++] Make .verify.cpp tests more robust against changing headers (#128703)
This is fixes the tests for the frozen headers, but is an improvement
either way.
2025-02-25 18:02:34 +01:00
Louis Dionne
2d66ab5e8e
[libc++][NFC] Run the container tests through clang-format (#126499)
We've been improving these the tests for vector quite a bit and we are
probably not done improving our container tests. Formatting everything
at once will make subsequent reviews easier.
2025-02-11 06:17:39 +01:00
A. Jiang
998f2422a5
[libc++][test] Fixes for hash<Emplaceable> and value discarding (#126566)
Currently `std::hash<Emplaceable>::operator()` relies implicit
conversion from `int` to `size_t`, which makes MSVC compelling. This PR
switches to use `static_cast`.

In `flat.map/flat.map.access/at_transparent.pass.cpp`, there's one
value-discarding use of `at` that wasn't marked `TEST_IGNORE_NODISCARD`.
This PR adds the missing `TEST_IGNORE_NODISCARD`.
2025-02-11 07:56:53 +08:00
Peng Liu
efa287dd8a
[libc++] Slightly simplify max_size and add new tests for vector (#119990)
This PR slightly simplifies the implementation of `vector<bool>::max_size`
and adds extensive tests for the `max_size()` function for both `vector<bool>`
and `vector<T>`. The main purposes of the new tests include:

- Verify correctness of `max_size()` under various `size_type` and
  `difference_type` definitions: check that `max_size()` works properly
  with allocators that have custom `size_type` and `difference_type`. This
  is particularly useful for `vector<bool>`, as different `size_type` lead
  to different `__storage_type` of different word lengths, resulting in
  varying `max_size()` values for `vector<bool>`. Additionally, different
  `difference_type` also sets different upper limit of `max_size()` for
  both `vector<bool>` and `std::vector`. These tests were previously
  missing.

- Eliminate incorrect implementations: Special tests are added to identify and
  reject incorrect implementations of `vector<bool>::max_size` that unconditionally
  return `std::min<size_type>(size-max, __internal_cap_to_external(allocator-max-size))`.
  This can cause overflow in the `__internal_cap_to_external()` call and lead
  to incorrect results. The new tests ensure that such incorrect
  implementations are identified.
2025-02-05 22:38:32 -05:00
Louis Dionne
accfbd4cb3
[libc++] Replace __is_trivially_relocatable by is_trivially_copyable (#124970)
The __is_trivially_relocatable builtin has semantics that do not
correspond to any current or future notion of trivial relocation.
Furthermore, it currently leads to incorrect optimizations for some
types on supported compilers:
- Clang on Windows where types with non-trivial destructors get
  incorrectly optimized
- AppleClang where types with non-trivial move constructors get
  incorrectly optimized

Until there is an agreed upon and bugfree implementation of what it
means to be trivially relocatable, it is safer to simply use trivially
copyable instead. This doesn't leave a lot of types behind and is
definitely correct.
2025-02-05 22:32:51 -05:00
Nikolas Klauser
fcc4ceb331
[libc++] Implement N4258(Cleaning-up noexcept in the Library) (#120312)
Fixes #99937
2025-01-30 21:10:56 +01:00
Nikolas Klauser
7f845cba2c
[libc++] Update the CI to Clang-20 and drop Clang-17 support (#117429) 2025-01-28 12:35:33 +01:00
Hui
def50f701f
[libc++] implement std::flat_multimap (#113835)
fixes https://github.com/llvm/llvm-project/issues/105190

---------

Co-authored-by: Hui Xie <huixie@Mac.broadband>
Co-authored-by: Hui Xie <huixie@Huis-MacBook-Pro.local>
2025-01-25 18:30:00 +00:00
A. Jiang
733a98db4a
[libc++] Fix input-only range handling for vector (#116157)
Changes:
- Carve out sized but input-only ranges for C++23.
- Call `std::move` for related functions when the iterator is possibly input-only.

Fixes #115727
2025-01-21 16:30:42 -05:00
Peng Liu
2d317d903a
[libc++] Fix no-op shrink_to_fit for vector<bool> (#120495)
This PR addresses an issue where the `shrink_to_fit` function in
`vector<bool>` is effectively a no-op, meaning it will never shrink the
capacity.

Fixes #122502
2025-01-21 16:28:58 -05:00
Peng Liu
0298e58c7d
[libc++] Optimize input_iterator-pair insert for std::vector (#113768)
As a follow-up to #113852, this PR optimizes the performance of the
`insert(const_iterator pos, InputIt first, InputIt last)` function for
`input_iterator`-pair inputs in `std::vector` for cases where
reallocation occurs during insertion. Additionally, this optimization
enhances exception safety by replacing the traditional `try-catch`
mechanism with a modern exception guard for the `insert` function.

The optimization targets cases where insertion trigger reallocation. In
scenarios without reallocation, the implementation remains unchanged.

Previous implementation
-----------------------
The previous implementation of `insert` is inefficient in reallocation
scenarios because it performs the following steps separately:
- `reserve()`: This leads to the first round of relocating old
elements to new memory;
- `rotate()`: This leads to the second round of reorganizing the
existing elements;
- Move-forward: Moves the elements after the insertion position to
their final positions.
- Insert: performs the actual insertion.

This approach results in a lot of redundant operations, requiring the
elements to undergo three rounds of relocations/reorganizations to be
placed in their final positions.

Proposed implementation
-----------------------
The proposed implementation jointly optimize the above 4 steps in the
previous implementation such that each element is placed in its final
position in just one round of relocation. Specifically, this
optimization reduces the total cost from 2 relocations + 1 std::rotate
call to just 1 relocation, without needing to call `std::rotate`,
thereby significantly improving overall performance.
2025-01-14 11:40:29 -05:00
Peng Liu
550d32f205
[libc++][test] Add exception tests for vector capacity operations (#118141)
As a follow-up to #117662, this PR provides a comprehensive set of
exception tests for the following capacity-related functions in
`std::vector`. Specifically, it includes tests for the following
functions:
- `reserve(size_type)`
- `resize(size_type)` and `resize(size_type, const_reference)`
- `shrink_to_fit()`

Previously, the exception safety tests for these functions were either
missing or inadequate. We need a thorough coverage of exception tests to
validate that these operations provide strong exception guarantees under
various exceptional scenarios.
2025-01-13 15:31:05 -05:00
Hui
162397f98d
[libc++] Replace stable_sort with sort in flat_map (#121431)
Fixes #120788
2025-01-13 09:09:29 -05:00
LoS
fb03ce7aad
[libc++] Fix test for vector data_const.pass.cpp (#122085)
The test contained some non-const accesses when we're really trying to test
that the method is const.
2025-01-09 11:22:46 -05:00
Peng Liu
6c06253b85
[libc++] Fix erroneous internal capacity evaluation in vector<bool> (#120577)
This PR fixes the erroneous internal capacity evaluation in
`vector<bool>`, which caused a subsequent SIGSEGV error when calling
`flip()` on `vector<bool>`. By fixing the internal capacity evaluation,
the SIGSEGV is automatically resolved.
2025-01-09 17:46:04 +08:00
Peng Liu
71e9a48227
[libc++] Mark vector<bool>::at() as constexpr to conform to C++20 standard (#121848)
Closes #121844.
2025-01-07 16:41:58 -05:00
Nikolas Klauser
b9a2658a3e
[libc++][C++03] Use __cxx03/ headers in C++03 mode (#109002)
This patch implements the forwarding to frozen C++03 headers as
discussed in
https://discourse.llvm.org/t/rfc-freezing-c-03-headers-in-libc. In the
RFC, we initially proposed selecting the right headers from the Clang
driver, however consensus seemed to steer towards handling this in the
library itself. This patch implements that direction.

At a high level, the changes basically amount to making each public
header look like this:

```
// inside <vector>
#ifdef _LIBCPP_CXX03_LANG
#  include <__cxx03/vector>
#else
  // normal <vector> content
#endif
```

In most cases, public headers are simple umbrella headers so there isn't
much code in the #else branch. In other cases, the #else branch contains
the actual implementation of the header.
2024-12-21 13:01:48 +01:00
Peng Liu
fafdf97047
[libc++] Simplify vector<bool>::flip() and add new tests (#119607)
This PR simplifies the internal bitwise logic of the `flip()` function
for `vector<bool>`, and creates new tests to validate the changes.
2024-12-19 11:48:51 -05:00
Peng Liu
4039a79de7
[libc++][test] Improve tests for assign in std::vector and vector<bool> (#119163)
This PR enhances the test coverage for std::vector::assign by adding new
tests for several important test cases that were previously missing, as
shown in the following table:

| test cases                        | forward_iterator | input_iterator |
|-----------------------------------|------------------|----------------|
| new_size > capacity()             | Yes              | Yes            |
| size() < new_size <= capacity()   | No               | No             |
| new_size <= size()                | No               | No             |

Similarly, no tests have previously covered `assign(InputIterator, InputIterator)`
and `assign(size_type, const value_type&)` for `vector<bool>`.

With this patch applied, all missing tests are covered.
2024-12-19 11:19:25 -05:00
Louis Dionne
62bdb85f9b [libc++][NFC] Fix incorrect comment for vector::assign(iter, iter) test 2024-12-13 09:35:56 -05: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
cd74ebaec6
[libc++] Make a few test helper constructors explicit (#118975) 2024-12-06 15:31:20 -05:00
Peng Liu
37797d3e80
[libc++][test] Fix and refactor exception tests for std::vector constructors (#117662)
The existing exceptions tests for `vector<T>` have several issues: some
tests did not throw exceptions at all, making them not useful for
exception-safety testing, and some tests did not throw exceptions at the
intended points, failing to serve their expected purpose. This PR fixes
those tests for vector's constructors. Morever, this PR extracted common
classes and utilities into a separate header file, and renamed those
classes using more descriptive names.
2024-12-06 09:03:17 -05:00
Peng Liu
a821937b6d
[libc++][test] Refactor increasing_allocator (#115671)
The increasing_allocator<T> class, originally introduced to test shrink_to_fit()
for std::vector, std::vector<bool>, and std::basic_string, has duplicated
definitions across several test files. Given the potential utility of this
class for capacity-related tests in various sequence containers, this patch
refactors the definition of increasing_allocator<T> into a single, reusable
location.
2024-12-05 15:44:05 -05: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
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
Louis Dionne
44ef12b020
[libc++] Refactor tests for hijacked address operator in vector (#117457)
This reduces the amount of boilerplate needed to implement the tests.
2024-11-26 14:33:56 -05:00
Louis Dionne
3a3517c5e9
[libc++] Improve the tests for vector::erase (#116265)
In particular, test everything with both a normal and a min_allocator,
add tests for a few corner cases and add tests with types that are
trivially relocatable. Also add tests that count the number of
assignments performed by vector::erase, since that is mandated by the
Standard.

This patch is a preparation for optimizing vector::erase.
2024-11-19 00:49:22 +01:00
Louis Dionne
965f3a95b9 [libc++][NFC] Run clang-format on vector::erase test
Since I am about to make significant changes to this test, run clang-format
on it before to avoid obscuring the review.
2024-11-14 14:13:51 +01:00
Louis Dionne
427a5cf105
[libc++] Add support for bounded iterators in std::array (#110729)
This patch introduces a new kind of bounded iterator that knows the size
of its valid range at compile-time, as in std::array. This allows computing
the end of the range from the start of the range and the size, which requires
storing only the start of the range in the iterator instead of both the start
and the size (or start and end). The iterator wrapper is otherwise identical
in design to the existing __bounded_iter.

Since this requires changing the type of the iterators returned by
std::array, this new bounded iterator is controlled by an ABI flag.

As a drive-by, centralize the tests for std::array::operator[] and add
missing tests for OOB operator[] on non-empty arrays.

Fixes #70864
2024-11-07 09:23:21 -05:00
Nikolas Klauser
c6f3b7bcd0
[libc++] Refactor the configuration macros to being always defined (#112094)
This is a follow-up to #89178. This updates the `<__config_site>`
macros.
2024-11-06 10:39:19 +01:00
Peng Liu
07443e9776
[libc++][test] Improve ThrowingT to Accurately Throw after throw_after > 1 Use (#114077)
This PR fixes the `ThrowingT` class, which currently fails to raise
exceptions after a specified number of copy construction operations. The
class is intended to throw in a controlled manner based on a specified
counter value `throw_after`. However, its current implementation of the
copy constructor fails to achieve this goal.

The problem arises because the copy constructor does not initialize the
`throw_after_n_` member, leaving `throw_after_n_` to default to `nullptr`
as defined by the in-class initializer. As a result, its copy constructor
always checks against `nullptr`, causing an immediate exception rather
than throwing after the specified number `throw_after` of uses. The fix
is straightforward: simply initialize the `throw_after_n_` member in the
member initializer list.

This issue was previously uncovered because all exception tests for
`std::vector` in `exceptions.pass.cpp` used a `throw_after` value of 1,
which coincidentally aligned with the class's behavior.
2024-11-05 11:24:05 -05:00
Joseph Huber
95a2eb70cf
[libcxx] Add testing configuration for GPU targets (#104515)
Summary:
The GPU runs these tests using the files built from the `libc` project.
These will be placed in `include/<triple>` and `lib/<triple>`. We use
the `amdhsa-loader` and `nvptx-loader` tools, which are also provided by
`libc`. These launch a kernel called `_start` which calls `main` so we
can pretend like GPU programs are normal terminal applications.

We force serial exeuction here, because `llvm-lit` runs way too many
processes in parallel, which has a bad habit of making the GPU drivers
hang or run out of resources. This allows the compilation to be run in
parallel while the jobs themselves are serialized via a file lock.

In the future this can likely be refined to accept user specified
architectures, or better handle including the root directory by exposing
that instead of just `include/<triple>/c++/v1/`.

This currently fails ~1% of the tests on AMDGPU and ~3% of the tests on
NVPTX. This will hopefully be reduced further, and later patches can
XFAIL a lot of them once it's down to a reasonable number.

Future support will likely want to allow passing in a custom
architecture instead of simply relying on `-mcpu=native`.
2024-11-04 12:58:23 -06:00
Hui
f467af6696
[libc++][test] add test coverage for flat_map::emplace_hint (#113773)
Not all the code path has been exercised by the tests for
`flat_map::emplace_hint`
Adding more test coverage.
At the same time, adding more test cases for `flat_map::emplace`
2024-11-02 09:42:26 +00:00
Nikolas Klauser
e99c4906e4
[libc++] Granularize <cstddef> includes (#108696) 2024-10-31 02:20:10 +01:00
Peng Liu
d3b98559be
Add exception guard for constructor vector(n, x, a) (#113086)
Added exception guard to the `vector(n, x, a)` constructor to enhance
exception safety. This change ensures that the `vector(n, x, a)`
constructor is consistent with other constructors, such as `vector(n)`,
`vector(n, x)`, `vector(n, a)`, in terms of exception safety.
2024-10-29 13:29:37 +08:00
Hui
0be1883c36
[libc++] Implement P0429R9 std::flat_map (#98643)
Around half of the tests are based on the tests Arthur O'Dwyer's
original implementation of std::flat_map, with modifications and
removals.

partially implement #105190
2024-10-26 18:58:53 +01:00
A. Jiang
397707f718
[libc++] __uglify non-conforming member typedef base (#112843)
Currently, libc++'s `bitset`, `forward_list`, and `list` have
non-conforming member typedef name `base`. The typedef is private, but
can cause ambiguity in name lookup.

Some other classes in libc++ that are either implementation details or
not precisely specified by the standard also have member typdef `base`.
I think this can still be conforming.

Follows up #80706 and #111127.
2024-10-18 23:27:12 +08:00
A. Jiang
432ba353d8
[libc++][test] Cover move construction of allocators again (#110375)
Previous PR #107344 fixed move constructor of `test_allocator` but
dropped test coverage for move construction in some cases. This PR
attempts to restore the test coverage.

Thanks @Quuxplusone for reminding.
2024-10-01 01:24:23 +08:00
A. Jiang
2d13302d38
[libc++][test] Confirm that P0508R0 has been implemented (#108172)
The paper was implemented by commit b0386a515b60c
(https://reviews.llvm.org/D46845) in LLVM 7.0. But it would be nice to
have test coverage for desired properties of `insert_return_type`.

Closes #99944
2024-09-16 15:34:40 -04:00
Louis Dionne
09e3a36058
[libc++][modules] Fix missing and incorrect includes (#108850)
This patch adds a large number of missing includes in the libc++ headers
and the test suite. Those were found as part of the effort to move
towards a mostly monolithic top-level std module.
2024-09-16 15:06:20 -04:00
Louis Dionne
33325524f5
[libc++][modules] Refactor poisoned_hash_helper (#108296)
The poisoned_hash_helper header was relying on an implicit forward
declaration of std::hash located in <type_traits>. When we improve the
modularization of the library, that causes issues, in addition to being
a fundamentally non-portable assumption in the test suite.

It turns out that the reason for relying on a forward declaration is to
be able to test that std::hash is *not* provided if we don't include any
header that provides it. But testing that is actually both non-portable
and not really useful.

Indeed, what harm does it make if additional headers provide std::hash
specializations? That would certainly be conforming -- the Standard
never requires an implementation to avoid providing a declaration when a
given header is included, instead it mandates what *must* be provided
for sure. In that spirit, it would be conforming for e.g. `<cstddef>` to
define the hash specializations if that was our desire. I also don't
read https://wg21.link/P0513R0 as going against that statement. Hence,
this patch just removes that test which doesn't carry its weight.

Fixes #56938
2024-09-12 15:07:49 -04:00
A. Jiang
46a76c3334
[libc++][test] LWG2593: Moved-from state of Allocators (#107344)
The resolution of LWG2593 didn't require the standard library
implementation to change. It merely strengthened requirements on
user-defined allocator types and allowed the implementation to make
stronger assumptions. The status is tentatively set to Nothing To Do.

However, `test_allocator` in libc++'s test suit needs to be fixed to
conform to the strengthened requirements.

Closes #100220.
2024-09-10 09:53:23 -04:00
Louis Dionne
5e19fd1720
[libc++][modules] Consolidate leaf modules into their own top-level modules (#107147)
Some modules are leaf modules in the sense that they are not used by any
other part of the headers. These leaf modules are easy to consolidate
since there is no risk to create a cycle. As a result of regrouping
these modules, several missing includes were found and fixed in this
patch.
2024-09-04 16:39:55 -04:00
A. Jiang
74e70bae15
[libc++] Disallow character types being index types of extents (#105832)
#78086 provided the trait we want to use for this: `__libcpp_integer`.

In some `libcxx/containers/views/mdspan` tests, improper uses of `char` 
are replaced with `signed char`. 

Fixes #73715
2024-08-27 16:41:55 -04:00