Commit Graph

245 Commits

Author SHA1 Message Date
Nikolas Klauser 1cfa485769 [libc++] Implement P1165R1 (Make stateful allocator propagation more consistent)
Reviewed By: Quuxplusone, ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D119112
2022-02-17 22:00:48 +01:00
Louis Dionne f87aa19be6 [libc++] Move everything related solely to _LIBCPP_ASSERT to its own file
This is the first step towards disentangling the debug mode and assertions
in libc++. This patch doesn't make any functional change: it simply moves
_LIBCPP_ASSERT-related stuff to its own file so as to make it clear that
libc++ assertions and the debug mode are different things. Future patches
will make it possible to enable assertions without enabling the debug
mode.

Differential Revision: https://reviews.llvm.org/D119769
2022-02-16 12:49:50 -05:00
Nikolas Klauser 2e2f3158c6 [libc++] Granularize algorithm includes
Reviewed By: Mordante, ldionne, Quuxplusone, #libc, #libc_abi

Spies: #libc_vendors, libcxx-commits, miyuki

Differential Revision: https://reviews.llvm.org/D119667
2022-02-16 04:12:22 +01:00
Louis Dionne 5c53afe5aa [libc++] Temporarily silence failing debug mode test
Also, fix the actual code so that the test would pass if we fixed the
issue that the method is instantiated in the dylib, and hence the debug
assertion will never fire except if the debug mode is enabled when the
dylib is being compiled.
2022-02-15 15:49:29 -05:00
Nikolas Klauser 169a66eac8 [libc++] Remove __functional_base
Reviewed By: ldionne, Quuxplusone, #libc

Spies: Mordante, mgorny, libcxx-commits, arichardson, llvm-commits, arphaman

Differential Revision: https://reviews.llvm.org/D119439
2022-02-11 19:16:01 +01:00
Nikolas Klauser 679f8a885b [libc++] Move fpos into its own header
For some reason `<string>` defines `std::fpos`, which should be defined in `<ios>`.

Reviewed By: Quuxplusone, Mordante, #libc

Spies: libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D118914
2022-02-04 21:53:43 +01:00
Arthur O'Dwyer fa6b9e4010 [libc++] Normalize all our '#pragma GCC system_header', and regression-test.
Now we'll notice if a header forgets to include this magic phrase.

Differential Revision: https://reviews.llvm.org/D118800
2022-02-04 12:27:19 -05:00
Nikolas Klauser 5173f43cc8 [libc++] Remove the std::string base class
Removing the base class of std::basic_string is not an ABI break, so we can remove any references to it from the header.

Reviewed By: ldionne, Mordante, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D118733
2022-02-03 22:57:23 +01:00
Nikolas Klauser 014a673441 [libc++] Remove std::basic_string's base class in ABIv2
Remove `std::basic_string`'s base class in ABI version 2

Reviewed By: Quuxplusone, ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D116334
2022-01-25 00:21:53 +01:00
Nikolas Klauser 52f37c24c3 [libc++][NFC] remove this-> when calling member functions in <string>
remove `this->` when calling member functions

Reviewed By: Quuxplusone, Mordante, ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D116324
2022-01-25 00:21:51 +01:00
Louis Dionne 0407ab4114 [libc++] Make sure basic_string::reserve(n) never shrinks in all Standard modes
Since basic_string::reserve(n) is instantiated in the shared library but also
available to the compiler for inlining, its definition should not depend on
things like the Standard mode in use. Indeed, that flag may not match between
how the shared library is compiled and how users are compiling their own code,
resulting in ODR violations.

However, note that we retain the behavior of basic_string::reserve() to
shrink the string for backwards compatibility reasons. While it would
technically be conforming to not shrink, we believe user expectation is
for it to shrink, and so existing code might have been written based on
that assumption. We prefer to not break such code, even though that makes
basic_string::reserve() and basic_string::reserve(0) not equivalent anymore.

Fixes llvm-project#53170

Differential Revision: https://reviews.llvm.org/D117332
2022-01-24 15:43:13 -05:00
Nikolas Klauser 4822447522 [libc++] basic_string::resize_and_overwrite: Adopt LWG3645 (Not voted in yet)
Adopt LWG3645, which fixes the value categories of basic_string::resize_and_overwrite
https://timsong-cpp.github.io/lwg-issues/3645

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D116815
2022-01-20 18:41:09 +01:00
Nikolas Klauser fcfc0e7ad3 [libc++] Introduce __fits_in_sso()
Introduce `__fits_in_sso()` to put the constexpr tests into a central place.

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D116487
2022-01-11 23:20:15 +01:00
Nikolas Klauser e3cf70502c [libc++] Introduce __debug_db_insert_c()
There are a lot of
```
#if _LIBCPP_DEBUG_LEVEL == 2
    __get_db()->__insert_c(this);
#endif
```

This patch introduces `__debug_db_insert_c()` to put the `#if` in one central place.

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D116947
2022-01-11 23:11:26 +01:00
Nikolas Klauser bec50db2ed [libc++] Implement P1072R10 (std::basic_string::resize_and_overwrite)
Reviewed By: Quuxplusone, #libc, Mordante

Spies: mzeren-vmw, ckennelly, arichardson, ldionne, Mordante, libcxx-commits, Quuxplusone

Differential Revision: https://reviews.llvm.org/D113013
2022-01-07 00:09:16 +01:00
Nikolas Klauser 5675b6112a [libc++] Disable _LIBCPP_DEBUG_ASSERT during constant evaluation
Disable `_LIBCPP_DEBUG_ASSERT` and debug iterators in <string> during constant evaluation

Reviewed By: ldionne, #libc

Spies: goncharov, libcxx-commits

Differential Revision: https://reviews.llvm.org/D115788
2021-12-17 11:15:53 +01:00
Nikolas Klauser af88bc153d [libc++][NFC] Use _LIBCPP_DEBUG_ASSERT in <string>
Use `_LIBCPP_DEBUG_ASSERT` instead of `_LIBCPP_ASSERT` and guarding it with `LIBCPP_DEBUG_LEVEL == 2`

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D115765
2021-12-15 08:39:42 +01:00
Louis Dionne bf39e7dc6c [libc++] Fix wrongly non-inline basic_string::shrink_to_fit
As explained in https://stackoverflow.com/a/70339311/627587, the fact
that shrink_to_fit wasn't defined as inline lead to issues when explicitly
instantiating basic_string. While explicit instantiations are always
somewhat brittle, this one was clearly a bug on our end.

Differential Revision: https://reviews.llvm.org/D115656
2021-12-14 11:12:04 -05:00
Nikolas Klauser f2eab339b9 [libc++][NFC] Remove goto from std::string
Remove `goto` from `std::string`

Reviewed By: Quuxplusone, ldionne, #libc, nilayvaish

Spies: nilayvaish, libcxx-commits

Differential Revision: https://reviews.llvm.org/D115598
2021-12-14 12:35:02 +01:00
Nikolas Klauser d2b0df35af [libc++][NFC] Update namespace comments in include/
update the namspace comments in include/

Reviewed By: ldionne, #libc

Spies: smeenai, libcxx-commits

Differential Revision: https://reviews.llvm.org/D114947
2021-12-02 21:06:59 +01:00
Louis Dionne eb8650a757 [runtimes][NFC] Remove filenames at the top of the license notice
We've stopped doing it in libc++ for a while now because these names
would end up rotting as we move things around and copy/paste stuff.
This cleans up all the existing files so as to stop the spreading
as people copy-paste headers around.
2021-11-17 16:30:52 -05:00
Louis Dionne 4eda928660 [libc++] Add missed comment in https://reviews.llvm.org/D113910 2021-11-16 11:36:06 -05:00
Nilay Vaish d17d89f4eb [libc++] Remove not needed call to __is_long()
The string is known to be long since __grow_by unconditionally calls
__set_long_cap().

Differential Revision: https://reviews.llvm.org/D113910
2021-11-16 11:26:13 -05:00
Louis Dionne f4c1258d56 [libc++] Add an option to disable wide character support in libc++
Some embedded platforms do not wish to support the C library functionality
for handling wchar_t because they have no use for it. It makes sense for
libc++ to work properly on those platforms, so this commit adds a carve-out
of functionality for wchar_t.

Unfortunately, unlike some other carve-outs (e.g. random device), this
patch touches several parts of the library. However, despite the wide
impact of this patch, I still think it is important to support this
configuration since it makes it much simpler to port libc++ to some
embedded platforms.

Differential Revision: https://reviews.llvm.org/D111265
2021-10-12 06:08:23 -04:00
Louis Dionne 84b0b52b03 [libc++] Refactor how basic_string and vector hoist exception-throwing functions
In basic_string and vector, we've been encapsulating all exception
throwing code paths in helper functions of a base class, which are defined
in the compiled library. For example, __vector_base_common defines two
methods, __throw_length_error() and __throw_out_of_range(), and the class
is externally instantiated in the library. This was done a long time ago,
but after investigating, I believe the goal of the current design was to:

1. Encapsulate the code to throw an exception (which is non-trivial) in
   an externally-defined function so that the important code paths that
   call it (e.g. vector::at) are free from that code. Basically, the
   intent is for the "hot" code path to contain a single conditional jump
   (based on checking the error condition) to an externally-defined function,
   which handles all the exception-throwing business.

2. Avoid defining this exception-throwing function once per instantiation
   of the class template. In other words, we want a single copy of
   __throw_length_error even if we have vector<int>, vector<char>, etc.

3. Encapsulate the passing of the container-specific string (i.e. "vector"
   and "basic_string") to the underlying exception-throwing function
   so that object files don't contain those duplicated string literals.
   For example, we'd like to have a single "vector" string literal for
   passing to `std::__throw_length_error` in the library, instead of
   having one per translation unit.

However, the way this is achieved right now has two problems:

- Using a base class and exporting it is really weird - I've been confused
  about this ever since I first saw it. It's just a really unusual way of
  achieving the above goals. Also, it's made even worse by the fact that
  the definitions of __throw_length_error and __throw_out_of_range appear
  in the headers despite always being intended to be defined in the compiled
  library (via the extern template instantiation).

- We end up exporting those functions as weak symbols, which isn't great
  for load times. Instead, it would be better to export those as strong
  symbols from the library.

This patch fixes those issues while retaining ABI compatibility (e.g. we
still export the exact same symbols as before). Note that we need to
keep the base classes as-is to avoid breaking the ABI of someone who
might inherit from std::basic_string or std::vector.

Differential Revision: https://reviews.llvm.org/D111173
2021-10-05 20:53:40 -04:00
Arthur O'Dwyer ae0e037f53 [libc++] Simplify the _LIBCPP_CONSTEXPR markings on starts_with() etc.
This came out of review comments on D110598.

Differential Revision: https://reviews.llvm.org/D110637
2021-09-29 19:00:58 -04:00
Louis Dionne 71752e0008 [libc++][NFC] Remove #endif comments for really small conditionals on _LIBCPP_HAS_NO_UNICODE_CHARS
We generally don't put a comment on the #endif when the #if block is so small
that it's unambiguous what the #endif refers to.
2021-09-09 11:25:10 -04:00
Louis Dionne b4e88d4db1 [libc++][NFC] Rename _EnableIf to __enable_if_t for consistency
In other places in the code, we use lowercase spelling for things that
are not available in prior standards.

Differential Revision: https://reviews.llvm.org/D109435
2021-09-08 15:20:58 -04:00
Arthur O'Dwyer 16bf43398a [libc++] Comma-operator-proof a lot of algorithm/container code.
Detected by evil-izing the widely used `MoveOnly` testing type.
I had to patch some tests that were themselves using its comma operator,
but I think that's a worthwhile cost in order to catch more places
in our headers that needed comma-proofing.

The trick here is that even `++ptr, SomeClass()` can find a comma operator
by ADL, if `ptr` is of type `Evil*`. (A comma between two operands
of non-class-or-enum type is always treated as the built-in
comma, without ADL. But if either operand is class-or-enum, then
ADL happens for _both_ operands' types.)

Differential Revision: https://reviews.llvm.org/D109414
2021-09-08 13:34:01 -04:00
Louis Dionne 4e0ea2cf2e [libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:

     candidate template ignored: substitution failure [with _Args =
     <ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'

Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:

     candidate template ignored: requirement 'is_invocable_v<
          std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
          std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
          ContiguousView>' was not satisfied [with _Args = <ContiguousView>]

Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.

Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.

I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).

Differential Revision: https://reviews.llvm.org/D108216
2021-09-08 09:09:28 -04:00
Louis Dionne a4cb5aefd5 [libc++] Remove some workarounds for unsupported GCC and Clang versions
There is a lot more we can do, in particular in <type_traits>, but this
removes some workarounds that were gated on checking a specific compiler
version.

Differential Revision: https://reviews.llvm.org/D108923
2021-09-01 10:57:14 -04:00
Louis Dionne f3bc0e51ab [libc++] Bypass calling exception-throwing functions in the dylib with -fno-exceptions
basic_string and vector currently have a hard dependency on the compiled
library because they need to call __vector_base_common::__throw_xxx(),
which are externally instantiated in the compiled library. That makes
sense when exceptions are enabled (because we're trying to localize the
exception-throwing code to the compiled library), but it doesn't really
make sense when exceptions are disabled, and the __throw_xxx functions
are just calling abort() anyways.

This patch simply overrides the __throw_xxx() functions so that they
don't rely on the compiled library when exceptions are disabled.

Differential Revision: https://reviews.llvm.org/D108389
2021-08-20 08:38:58 -04:00
Louis Dionne 0166690401 [libc++] Remove workarounds for the lack of deduction guides in C++17
All supported compilers have supported deduction guides in C++17 for a
while, so this isn't necessary anymore.

Differential Revision: https://reviews.llvm.org/D108213
2021-08-18 08:57:25 -04:00
Marek Kurdej 775caa58fc [libc++] [c++2b] [P2166] Prohibit string and string_view construction from nullptr.
* https://wg21.link/P2166

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D106801
2021-07-27 16:20:21 +02:00
Arthur O'Dwyer a8d1182f66 [libc++] Remove some _LIBCPP_CXX03_LANG from iostreams headers.
With the STL containers, I didn't enable move operations in C++03 mode
because that would change the overload resolution for things that today
are copy operations. With iostreams, though, the copy operations aren't
present at all, and so I see no problem with enabling move operations
even in (Clang's greatly extended) C++03 mode.

Clang's C++03 mode does not support delegating constructors.

Differential Revision: https://reviews.llvm.org/D104310
2021-06-28 12:55:26 -04:00
Louis Dionne f32f3db9fc [libc++] Split the various iterator types out of <iterator>
Differential Revision: https://reviews.llvm.org/D104669
2021-06-28 12:25:40 -04:00
Arthur O'Dwyer bfbd73f87d [libc++] Alphabetize and include-what-you-use. NFCI.
Differential Revision: https://reviews.llvm.org/D102781
2021-05-29 19:54:48 -04:00
Arthur O'Dwyer db9425cb06 [libc++] [LIBCXX-DEBUG-FIXME] Fix an iterator-invalidation issue in string::assign.
This appears to be a bug in our string::assign: when assigning into
a longer string, from a shorter snippet of itself, we invalidate
iterators before doing the copy. We should invalidate them afterward.
Also drive-by improve the formatting of a function header.

Differential Revision: https://reviews.llvm.org/D101675
2021-05-05 16:20:53 -04:00
Arthur O'Dwyer e87479b00f [libc++] Remove the special logic for "noexcept iterators" in basic_string.
This reverts a large chunk of http://reviews.llvm.org/D15862 ,
and also fixes bugs in `insert`, `append`, and `assign`, which are now regression-tested.
(Thanks to Tim Song for pointing out the bug in `append`!)

Before this patch, we did a special dance in `append`, `assign`, and `insert`
(but not `replace`). All of these require the strong exception guarantee,
even when the user-provided InputIterator might have throwing operations.

The naive way to accomplish this is to construct a temporary string and
then append/assign/insert from the temporary; i.e., finish all the potentially
throwing and self-inspecting InputIterator operations *before* starting to
modify self. But this is slow, so we'd like to skip it when possible.

The old code (D15682) attempted to check that specific iterator operations
were nothrow: it assumed that if the iterator operations didn't throw, then
it was safe to iterate the input range multiple times and therefore it was
safe to use the fast-path non-naive version. This was wrong for two reasons:
(1) the old code checked the wrong operations (e.g. checked noexceptness of `==`,
but the code that ran used `!=`), and (2) the conversion of value_type to char
could still throw, or inspect the contents of self.

The new code is much simpler, although still much more complicated than it
really could be. We'll likely revisit this codepath at some point, but for now
this patch suffices to get it passing all the new regression tests.

The added tests all fail before this patch, and succeed afterward.
See https://quuxplusone.github.io/blog/2021/04/17/pathological-string-appends/

Differential Revision: https://reviews.llvm.org/D98573
2021-04-26 16:22:43 -04:00
Arthur O'Dwyer 5c40c994c3 [libc++] s/_LIBCPP_NO_HAS_CHAR8_T/_LIBCPP_HAS_NO_CHAR8_T/g
This was raised in D94511.

Differential Revision: https://reviews.llvm.org/D100736
2021-04-21 12:49:07 -04:00
Louis Dionne 4cd6ca102a [libc++] NFC: Normalize `#endif //` comment indentation 2021-04-20 12:03:32 -04:00
Arthur O'Dwyer 2d0f1fa472 [libc++] Header inclusion tests.
As mandated by the Standard's various synopses, e.g. [iterator.synopsis].
Searching the TeX source for '#include' is a good way to find all of these
mandates.

The new tests are all autogenerated by utils/generate_header_inclusion_tests.py.
I was SHOCKED by how many mandates there are, and how many of them
libc++ wasn't conforming with.

Differential Revision: https://reviews.llvm.org/D99309
2021-04-06 15:31:56 -04:00
Arthur O'Dwyer 199d2ebeed [libc++] Use _EnableIf and __iter_value_type consistently. NFCI.
Specifically, use these metafunctions consistently in areas that are
about to be affected by P1518R2's changes.

This is the NFCI part of https://reviews.llvm.org/D97742 .
The functional-change part is still waiting for P1518R2 to be
officially merged into the working draft.
2021-03-29 09:22:52 -04:00
Marek Kurdej 28f82bec7f [libc++] [C++20] [P0482] Add missing tests and synopses for char8_t.
Left to finish P0482:
* <cuchar> header.
* Parts of <memory_resource> concerning char8_t. Also, tests for hash<pmr::*string>.

Reviewed By: ldionne, #libc, Quuxplusone

Differential Revision: https://reviews.llvm.org/D99184
2021-03-23 18:45:31 +01:00
Martin Storsjö c793f68d9b [libcxx] Don't use dllimport for a static member in a template
This fixes clang warnings (that are treated as errors when running
the test suite):

libcxx/include/string:4409:59: error: definition of dllimport static field [-Werror,-Wdllimport-static-field-def]
               basic_string<_CharT, _Traits, _Allocator>::npos;

The warning is normally not visible as long as the libc++ headers
are treated as system headers.

The same construct is always an error in MSVC.

(One _LIBCPP_FUNC_VIS was added in
2d8f23f571, which broke DLL builds.
59919c4d6b fixed this by adding another
_LIBCPP_FUNC_VIS on the declaration for consistency, but the underlying
issue remained, that one can't use dllimport here.)

Differential Revision: https://reviews.llvm.org/D97168
2021-03-04 08:55:27 +02:00
zoecarver dea74b2820 [libc++] Add `noexcept` to `string::find` and similar members.
Adds `noexcept` to `string_view`/`string::find` and similar members
(`rfind`, etc.). See discussion in D95251. Refs D95821.

Reviewed By: curdeius, ldionne

Differential Revision: https://reviews.llvm.org/D95848
2021-02-09 11:47:40 -08:00
Wim Leflere 6ac9cb2a7c [libc++][P1679] add string contains
C++23 string contains implementation and tests

Paper: https://wg21.link/P1679R3
Standard (string): https://eel.is/c++draft/string.contains
Standard (string_view): https://eel.is/c++draft/string.view.ops#lib:contains,basic_string_view

Differential Revision: https://reviews.llvm.org/D93912
2021-01-19 14:35:07 -05:00
Marek Kurdej 044b892c79 [libc++] Use c++20 instead of c++2a consistently.
* The only exception is that the flag -std=c++2a is still used not to break compatibility with older compilers (clang <= 9, gcc <= 9).
* Bump _LIBCPP_STD_VER for C++20 to 20 and use 21 for the future standard (C++2b).

That's a preparation step to add c++2b support to libc++.

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D93383
2021-01-07 13:11:33 +01:00
Arthur O'Dwyer 3696227c10 [libc++] ADL-proof by adding _VSTD:: qualifications to memmove etc.
Generally these calls aren't vulnerable to ADL because they involve only
primitive types. The ones in <list> and <vector> drag in namespace std
but that's OK; the ones in <fstream> and <strstream> are vulnerable
iff `CharT` is an enum type, which seems far-fetched.
But absolutely zero of them *need* ADL to happen; so in my opinion
they should all be consistently qualified, just like calls to any
other (non-user-customizable) functions in namespace std.

Also: Include <cstring> and <cwchar> in <__string>.
We seemed to be getting lucky that <memory> included <iterator>
included <iosfwd> included <wchar.h>. That gave us the
global-namespace `wmemmove`, but not `_VSTD::wmemmove`.
This is now fixed.

I didn't touch these headers:
<ext/__hash> uses strlen, safely
<support/ibm/locale_mgmt_aix.h> uses memcpy, safely
<string.h> uses memchr and strchr, safely
<wchar.h> uses wcschr, safely
<__bsd_locale_fallbacks.h> uses wcsnrtombs, safely

Differential Revision: https://reviews.llvm.org/D93061
2020-12-10 22:03:12 -05:00
Richard Smith 2a2c228c7a Add new 'preferred_name' attribute.
This attribute permits a typedef to be associated with a class template
specialization as a preferred way of naming that class template
specialization. This permits us to specify that (for example) the
preferred way to express 'std::basic_string<char>' is as 'std::string'.

The attribute is applied to the various class templates in libc++ that have
corresponding well-known typedef names.

This is a re-commit. The previous commit was reverted because it exposed
a pre-existing bug that has since been fixed / worked around; see
PR48434.

Differential Revision: https://reviews.llvm.org/D91311
2020-12-09 12:22:35 -08:00