279 Commits

Author SHA1 Message Date
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
Nikolas Klauser
c166a9c713
[libc++] Add #if 0 block to all the top-level headers (#119234)
Including The frozen C++03 headers results in a lot of formatting
changes in the main headers, so this splits these changes into a
separate commit instead.

This is part of
https://discourse.llvm.org/t/rfc-freezing-c-03-headers-in-libc.
2024-12-10 16:02:12 +01: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
Nikolas Klauser
2e43a304f1
[libc++] Granularize <vector> (#99705)
|                    | old time | new time |
| ------------------ | -------- | -------- |
| functional - c++23 | 416ms    | 225ms    |
| random - c++23     | 513ms    | 392ms    |
| vector - c++17     | 206ms    | 100ms    |
2024-10-25 11:28:46 +02:00
Louis Dionne
8a7318eb00
[libc++] Refactor vector::push_back to use vector::emplace (#113481)
This removes some duplicate code. I suspect this was originally written
that way because vector::emplace didn't exist in C++03 mode, which
stopped being relevant when Clang implemented rvalue references in
C++03.
2024-10-24 16:19:21 +02:00
Peng Liu
b263a71c2b
[libc++] Refactor vector constructors to eliminate code duplication (#113193)
This PR refactors the std::vector's initializer_list constructors to
reduce code duplication. The constructors now call `__init_with_size`
directly, reducing code duplication and improving readability and
maintainability.
2024-10-22 12:21:31 -04:00
Hui
8f9cce0bef
[libc++] Add container_traits (prework for std::flat_map) (#109578)
This PR is extracted from
https://github.com/llvm/llvm-project/pull/98643, as per code review
request
https://github.com/llvm/llvm-project/pull/98643#discussion_r1768967793
2024-10-12 18:29:59 +01:00
Nikolas Klauser
ba87515fea
[libc++][RFC] Always define internal feature test macros (#89178)
Currently, the library-internal feature test macros are only defined if
the feature is not available, and always have the prefix
`_LIBCPP_HAS_NO_`. This patch changes that, so that they are always
defined and have the prefix `_LIBCPP_HAS_` instead. This changes the
canonical use of these macros to `#if _LIBCPP_HAS_FEATURE`, which means
that using an undefined macro (e.g. due to a missing include) is
diagnosed now. While this is rather unlikely currently, a similar change
in `<__configuration/availability.h>` caught a few bugs. This also
improves readability, since it removes the double-negation of `#ifndef
_LIBCPP_HAS_NO_FEATURE`.

The current patch only touches the macros defined in `<__config>`. If
people are happy with this approach, I'll make a follow-up PR to also
change the macros defined in `<__config_site>`.
2024-10-12 09:49:52 +02: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
Nikolas Klauser
27c83382d8
[libc++] Replace __compressed_pair with [[no_unique_address]] (#76756)
This significantly simplifies the code, improves compile times and
improves the object layout of types using `__compressed_pair` in the
unstable ABI. The only downside is that this is extremely ABI sensitive
and pedantically breaks the ABI for empty final types, since the address
of the subobject may change. The ABI of the whole object should not be
affected.

Fixes #91266
Fixes #93069
2024-09-16 11:08:57 +02:00
Nikolas Klauser
17e0686ab1
[libc++][NFC] Use [[__nodiscard__]] unconditionally (#80454)
`__has_cpp_attribute(__nodiscard__)` is always true now, so we might as
well replace `_LIBCPP_NODISCARD`. It's one less macro that can result in
bad diagnostics.
2024-09-12 21:18:43 +02:00
Nikolas Klauser
748023dc32
[libc++][NFC] Replace _LIBCPP_NORETURN and TEST_NORETURN with [[noreturn]] (#80455)
`[[__noreturn__]]` is now always available, so we can simply use the
attribute directly instead of through a macro.
2024-09-11 08:59:46 +02:00
Louis Dionne
c1a8283fcc
[libc++][modules] Move __noexcept_move_assign_container out of __type_traits (#107140)
That header depends on allocator traits, which is fundamentally tied to
`<memory>`, not to `<type_traits>`. This breaks a cycle betweeen
__type_traits and __memory.
2024-09-04 11:18:30 -04:00
Louis Dionne
348e74139a [libc++][NFC] Run clang-format on libcxx/include
This re-formats a few headers that had become out-of-sync with respect
to formatting since we ran clang-format on the whole codebase. There's
surprisingly few instances of it.
2024-08-30 12:09:36 -04:00
Louis Dionne
85561dd594 [libc++] Fix bounded iterator hardening mode in C++03 mode 2024-08-26 17:17:27 -04:00
Nikolas Klauser
710664341d
[libc++][NFC] Don't explicitly provide propagate_on_container_swap when calling __swap_allocator (#105980)
`__swap_allocator` does this automatically when not providing it
explicitly, so this is just more code without any benefit.
2024-08-26 18:44:16 +02:00
Mark de Wever
6cea8189b0
[libc++][vector] Updates LWG3778 status. (#99818)
The work was done in a8ae392718313e64e2d7bc092b00f51476c074b2 which
implements parts of

  N4258: Cleaning-up noexcept in the Library

However that's paper Summary of Proposed Changes contains
  No change in vector<bool>

This patch updates the status and as a few NFC cleanups. The fixed
version is based on the release page and might be off.

Updates
- LWG3778 vector<bool> missing exception specifications
2024-07-23 19:33:18 +02:00
David Benjamin
bcf9fb9802
[libc++][hardening] Use bounded iterators in std::vector and std::string (#78929)
~~NB: This PR depends on #78876. Ignore the first commit when reviewing,
and don't merge it until #78876 is resolved. When/if #78876 lands, I'll
clean this up.~~

This partially restores parity with the old, since removed debug build.
We now can re-enable a bunch of the disabled tests. Some things of note:

- `bounded_iter`'s converting constructor has never worked. It needs a
friend declaration to access the other `bound_iter` instantiation's
private fields.

- The old debug iterators also checked that callers did not try to
compare iterators from different objects. `bounded_iter` does not
currently do this, so I've left those disabled. However, I think we
probably should add those. See
https://github.com/llvm/llvm-project/issues/78771#issuecomment-1902999181

- The `std::vector` iterators are bounded up to capacity, not size. This
makes for a weaker safety check. This is because the STL promises not to
invalidate iterators when appending up to the capacity. Since we cannot
retroactively update all the iterators on `push_back()`, I've instead
sized it to the capacity. This is not as good, but at least will stop
the iterator from going off the end of the buffer.

There was also no test for this, so I've added one in the `std`
directory.

- `std::string` has two ambiguities to deal with. First, I opted not to
size it against the capacity. https://eel.is/c++draft/string.require#4
says iterators are invalidated on an non-const operation. Second,
whether the iterator can reach the NUL terminator. The previous debug
tests and the special-case in https://eel.is/c++draft/string.access#2
suggest no. If either of these causes widespread problems, I figure we
can revisit.

- `resize_and_overwrite.pass.cpp` assumed `std::string`'s iterator
supported `s.begin().base()`, but I see no promise of this in the
standard. GCC also doesn't support this. I fixed the test to use
`std::to_address`.

- `alignof.compile.pass.cpp`'s pointer isn't enough of a real pointer.
(It needs to satisfy `NullablePointer`, `LegacyRandomAccessIterator`,
and `LegacyContiguousIterator`.) `__bounded_iter` seems to instantiate
enough to notice. I've added a few more bits to satisfy it.

Fixes #78805
2024-07-22 22:44:25 -07:00
Mark de Wever
f65d7fdcf8
[libc++][vector] Fixes shrink_to_fit. (#97895)
This assures shrink_to_fit does not increase the allocated size.

Partly addresses https://github.com/llvm/llvm-project/issues/95161

---------

Co-authored-by: Mital Ashok <mital.vaja@googlemail.com>
2024-07-20 12:55:44 +02:00
Hristo Hristov
d043e4c322
[libc++] Restore __synth_three_way lambda (#90398)
Restore `__synth_three_way` lambda to match the Standard. 
GH-57222 is done, restoring the Standard wording implementation should
be possible.


df28d4412c/libcxx/include/__compare/synth_three_way.h (L28)

According to comment
https://github.com/llvm/llvm-project/issues/59513#issuecomment-2068338762,
GH-59513 is not a blocker.

Co-authored-by: Hristo Hristov <zingam@outlook.com>
2024-07-07 20:49:10 +03:00
Hui
79e8a59523
[libc++] Move allocator assertion into allocator_traits (#94750)
There is code duplication in all containers that static_assert the
allocator matches the allocator requirements in the spec. This check can
be moved into a more centralised place.
2024-06-25 10:13:48 -05:00
Louis Dionne
e2c2ffbe7a
[libc++][NFC] Run clang-format on libcxx/include again (#95874)
As time went by, a few files have become mis-formatted w.r.t.
clang-format. This was made worse by the fact that formatting was not
being enforced in extensionless headers. This commit simply brings all
of libcxx/include in-line with clang-format again.

We might have to do this from time to time as we update our clang-format
version, but frankly this is really low effort now that we've formatted
everything once.
2024-06-18 09:13:45 -04:00
Nikolas Klauser
cb41740187
[libc++] Refactor<__type_traits/is_swappable.h> (#86822)
This changes the `is_swappable` implementation to use variable templates
first and basing the class templates on that. This avoids instantiating
them when the `_v` versions are used, which are generally less resource
intensive.
2024-06-18 11:01:43 +02:00
Nikolas Klauser
bbe4a80605
[libc++] Enable modernize-use-equals-delete (#93293)
Differential Revision: https://reviews.llvm.org/D121213
2024-06-18 10:51:57 +02:00
Nikolas Klauser
6b4b29f859
[libc++][NFC] Remove unnecessary parens in static_asserts (#95605)
These were required a long time ago due to `static_assert` not actually
being available in C++03. Now `static_assert` is simply mapped to
`_Static_assert` in C++03, making the additional parens unnecessary.
2024-06-18 10:45:30 +02:00
Konstantin Varlamov
0cfdce854d
[libc++] Guard transitive include of <locale> with availability macro (#95686)
This is a follow-up to https://github.com/llvm/llvm-project/pull/80282.
The transitive includes of `<locale>` in `<vector>` were all guarded by
the availability macro -- the new include should also be guarded,
otherwise any users who compile with localization disabled will start
getting errors trying to include `<vector>`.
2024-06-17 09:30:41 -04:00
Nikolas Klauser
1ba8ed0cd7
[libc++] Mark more types as trivially relocatable (#89724)
Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
2024-06-17 13:09:04 +02:00
Louis Dionne
04f01a2b9c
[libc++] Make the __availability header a sub-header of __config (#93083)
In essence, this header has always been related to configuration of
the library but we didn't want to put it inside <__config> due to
complexity reasons. Now that we have sub-headers in <__config>, we
can move <__availability> to it and stop including it everywhere since
we already obtain the required macros via <__config>.
2024-05-28 18:29:11 -07:00
Mateusz Zych
c7e9b4918e
[libc++][vector] Inline remaining constructors filling vector with the same value (#82068)
Placing physically next to each other remaining constructors filling
vector with the same value
will make code better, since they all have nearly identical
implementation, which needs to be kept in sync.

Co-authored-by: Mark de Wever <koraq@xs4all.nl>
2024-05-22 10:13:29 +02:00
Nikolas Klauser
83bc7b5771
[libc++] Remove _LIBCPP_DISABLE_NODISCARD_EXTENSIONS and refactor the tests (#87094)
This also adds a few tests that were missing.
2024-04-22 22:13:58 +02:00
Nikolas Klauser
61f1f13002
[libc++][NFC] Move basic ASan annotation functions into a utility header (#87220) 2024-04-13 18:24:12 +02:00
Nikolas Klauser
316634ff59
[libc++] Remove <queue> and <stack> includes from <format> (#85520)
This reduces the include time of <format> from 691ms to 556ms.
2024-03-29 12:06:09 +01:00
Nikolas Klauser
580f60484e
[libc++][NFC] Merge is{,_nothrow,_trivially}{,_copy,_move,_default}{_assignable,_constructible} (#85308)
These headers have become very small by using compiler builtins, often
containing only two declarations. This merges these headers, since
there doesn't seem to be much of a benefit keeping them separate.

Specifically, `is_{,_nothrow,_trivially}{assignable,constructible}` are
kept and the `copy`, `move` and `default` versions of these type traits
are moved in to the respective headers.
2024-03-18 08:29:44 +01:00
Nikolas Klauser
4528c44d0a
[libc++] Remove <tuple> include from <__format/concepts.h> (#80214)
This also moves `tuple_size_v` into `tuple_size` as a drive-by.
2024-03-14 12:04:41 +01:00
Louis Dionne
37dca605c9
[libc++] Clean up includes of <__assert> (#80091)
Originally, we used __libcpp_verbose_abort to handle assertion failures.
That function was declared from all public headers. Since we don't use
that mechanism anymore, we don't need to declare __libcpp_verbose_abort
from all public headers, and we can clean up a lot of unnecessary
includes.

This patch also moves the definition of the various assertion categories
to the <__assert> header, since we now rely on regular IWYU for these
assertion macros.

rdar://105510916
2024-02-29 10:12:22 -05:00
Nikolas Klauser
76a2472715
[libc++] Refactor more __enable_ifs to the canonical style (#81457)
This brings the code base closer to having only a single style of
`enable_if`s.
2024-02-20 01:47:38 +01:00
Nikolas Klauser
544f610d53
[libc++] Use __is_pointer_in_range inside vector::insert (#80624) 2024-02-08 19:22:16 +01:00
Louis Dionne
3e33b6f5de [libc++][NFC] Reformat a few files that had gotten mis-formatted
Those appear to be oversights when committing patches
in the last few months.
2024-02-08 10:12:42 -05:00
Nikolas Klauser
4e112e5c1c
Reapply "[libc++] Optimize vector growing of trivially relocatable types" (#80558)
This reapplies #76657. Non-trivial elements didn't get destroyed
previously. This fixes the bug and adds tests for all the vector
insertion functions.
2024-02-04 00:28:29 +01:00
Kirill Stoimenov
2352fdd202 Revert "[libc++] Optimize vector growing of trivially relocatable types (#76657)"
Broke sanitizer bots: https://lab.llvm.org/buildbot/#/builders/5/builds/40641

This reverts commit 67eee4a029797c09129889c3655416d1be487cfe.
2024-02-02 20:43:51 +00:00
Nikolas Klauser
67eee4a029
[libc++] Optimize vector growing of trivially relocatable types (#76657)
This patch introduces a new trait to represent whether a type is
trivially
relocatable, and uses that trait to optimize the growth of a std::vector
of trivially relocatable objects.

```
--------------------------------------------------
Benchmark                           old        new
--------------------------------------------------
bm_grow<int>                    1354 ns    1301 ns
bm_grow<std::string>            5584 ns    3370 ns
bm_grow<std::unique_ptr<int>>   3506 ns    1994 ns
bm_grow<std::deque<int>>       27114 ns   27209 ns
```

This also changes to order of moving and destroying the objects when
growing the vector. This should not affect our conformance.
2024-02-02 17:13:55 +01:00
Nikolas Klauser
ffb3589b8c
[libc++] Remove transitive <locale> include from <vector> (#80282)
This reduces the time to include `<vector>` from 468ms to 367ms.
2024-02-02 11:33:08 +01:00
Louis Dionne
f89d707e5f
[libc++] Accept __VA_ARGS__ in conditional _NOEXCEPT_ macro (#79877)
This prevents having to use double parentheses in common cases.
2024-01-30 08:33:48 -05:00
Tacet
2b3cdd69f7
[ASan][libc++][NFC] refactor vector annotations arguments (#78322)
This commit simplifies ASan helper functions in `std::vector` by
removing arguments which can be calculated later.

Short term it improves readability of helper functions in `std::vector`.

Long term it aims to help with a bigger refactor of container
annotations.
2024-01-17 08:50:10 +01:00
Tacet
82b38e83cf
[ASan][libc++] Optimization of container annotations (#76082)
This commit implements conditional compilation for ASan helper code.

As convey to me by @EricWF, string benchmarks with UBSan have been
experiencing significant performance hit after the commit with ASan
string annotations. This is likely due to the fact that no-op ASan code
is not optimized out with UBSan. To address this issue, this commit
conditionalizes the inclusion of ASan helper function bodies using
`#ifdef` directives. This approach allows us to selectively include only
the ASan code when it's actually required, thereby enhancing
optimizations and improving performance.

While issue was noticed in string benchmarks, I expect same overhead
(just less noticeable) in other containers, therefore `std::vector` and
`std::deque` have same changes.

To see impact of that change run `string.libcxx.out` with UBSan and
`--benchmark_filter=BM_StringAssign` or
`--benchmark_filter=BM_StringConstruct`.
2023-12-21 17:26:10 -05:00
Louis Dionne
9783f28cbb
[libc++] Format the code base (#74334)
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
2023-12-18 14:01:33 -05:00
Louis Dionne
f0d4811690
[libc++] Add CI job for testing macOS C++03 (#75355)
It's not that I have much love for C++03, but we should ensure that it
works. Some recent changes broke this configuration because slightly
older Clang versions don't support attribute syntax in C++03 mode.
2023-12-13 18:05:36 -05:00
Tacet
02f4b36ad5
[libc++] Refactor of ASan annotation functions (#74023)
This commit refactors the ASan annotation functions in libc++ to reduce
unnecessary code duplication. Additionally it adds a small optimization.

- Eliminates two redundant function versions by utilizing the
`[[maybe_unused]]` attribute and guarding function bodies with `#ifndef
_LIBCPP_HAS_NO_ASAN`.
- Introduces an additional guard to an auxiliary function, allowing the
removal of a no-ops function body. This approach avoids relying on the
optimizer for code elimination.

Fixes #73043
2023-12-05 13:27:08 -05:00
Louis Dionne
b18a46e35d
[libc++][NFC] Add a few clang-format annotations (#74352)
This is in preparation for clang-formatting the whole code base. These
annotations are required either to avoid clang-format bugs or because
the manually formatted code is significantly more readable than the
clang-formatted alternative. All in all, it seems like very few
annotations are required, which means that clang-format is doing a very
good job in most cases.
2023-12-04 15:17:31 -05:00
Martijn Vels
6fe4e033f0 [libc++] Optimize vector push_back to avoid continuous load and store of end pointer
Credits: this change is based on analysis and a proof of concept by
gerbens@google.com.

Before, the compiler loses track of end as 'this' and other references
possibly escape beyond the compiler's scope. This can be see in the
generated assembly:

     16.28 │200c80:   mov     %r15d,(%rax)
     60.87 │200c83:   add     $0x4,%rax
           │200c87:   mov     %rax,-0x38(%rbp)
      0.03 │200c8b: → jmpq    200d4e
      ...
      ...
      1.69 │200d4e:   cmp     %r15d,%r12d
           │200d51: → je      200c40
     16.34 │200d57:   inc     %r15d
      0.05 │200d5a:   mov     -0x38(%rbp),%rax
      3.27 │200d5e:   mov     -0x30(%rbp),%r13
      1.47 │200d62:   cmp     %r13,%rax
           │200d65: → jne     200c80

We fix this by always explicitly storing the loaded local and pointer
back at the end of push back. This generates some slight source 'noise',
but creates nice and compact fast path code, i.e.:

     32.64 │200760:   mov    %r14d,(%r12)
      9.97 │200764:   add    $0x4,%r12
      6.97 │200768:   mov    %r12,-0x38(%rbp)
     32.17 │20076c:   add    $0x1,%r14d
      2.36 │200770:   cmp    %r14d,%ebx
           │200773: → je     200730
      8.98 │200775:   mov    -0x30(%rbp),%r13
      6.75 │200779:   cmp    %r13,%r12
           │20077c: → jne    200760

Now there is a single store for the push_back value (as before), and a
single store for the end without a reload (dependency).

For fully local vectors, (i.e., not referenced elsewhere), the capacity
load and store inside the loop could also be removed, but this requires
more substantial refactoring inside vector.

Differential Revision: https://reviews.llvm.org/D80588
2023-10-02 09:12:37 -04:00