summaryrefslogtreecommitdiff
path: root/libcxx/include/string_view
AgeCommit message (Collapse)Author
2025-09-25[libc++][string] P3044R2: sub-`string_view` from `string` (#147095)Hristo Hristov
Implements [P3044R2](https://wg21.link/P3044R2) Note: `substr.pass.cpp` is refactored to accommodate the test of `basic_string_view`'s `subview` which is an alias of `substr` without changing the test cases. Closes #148140 # References - https://github.com/cplusplus/draft/pull/7975 - https://wg21.link/string.substr - https://wg21.link/string.view.ops --------- Co-authored-by: Hristo Hristov <zingam@outlook.com> Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de>
2025-09-04[libc++][NFC] Use llvm.org/PR to link to bug reports (#156288)Nikolas Klauser
We've built up quite a few links directly to github within the code base. We should instead use `llvm.org/PR<issue-number>` to link to bugs, since that is resilient to the bug tracker changing in the future. This is especially relevant for tests linking to bugs, since they will probably be there for decades to come. A nice side effect is that these links are significantly shorter than the GH links, making them much less of an eyesore. This patch also replaces a few links that linked to the old bugzilla instance on llvm.org.
2025-08-14[libc++] Remove assertions from <string_view> that are unreachable (#148598)Nikolas Klauser
When assertions are enabled it is impossible to construct a `string_view` which contains a null pointer and a non-zero size, so assertions where we check for that on an already constructed `string_view` are unreachable.
2025-07-19[libc++] Diagnose passing null pointers to a bunch of APIs (#148585)Nikolas Klauser
2025-04-19[libc++] Backport segmented iterator optimization for std::for_each to C++11 ↵Peng Liu
(#134960) Previously, the segmented iterator optimization for `std::for_each` was restricted to C++23 and later due to its dependency on `__movable_box`, which is not available in earlier standards. This patch eliminates that restriction, enabling consistent optimizations starting from C++11. By backporting this enhancement, we improve performance across older standards and create opportunities to extend similar optimizations to other algorithms by forwarding their calls to `std::for_each`.
2025-04-09[libc++] P3247R2: Deprecate `is_trivial(_v)` (#130573)A. Jiang
Requirements on character-like types are updated unconditionally, because `basic_string` does requires the default-constructibility. It might be possible to make `basic_string_view` support classes with non-public trivial default constructor, but this doesn't seem sensible. libcxxabi's `ItaniumDemangle.h` is also updated to avoid deprecated features.
2025-02-21[libc++] Qualify calls to nullary functions like __throw_foo (#122465)Louis Dionne
This is technically not necessary in most cases to prevent issues with ADL, but let's be consistent. This allows us to remove the libcpp-qualify-declval clang-tidy check, which is now enforced by the robust-against-adl clang-tidy check.
2024-12-21[libc++][C++03] Use `__cxx03/` headers in C++03 mode (#109002)Nikolas Klauser
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-13[libc++][NFC] Simplify the implementation of string and string_views ↵Nikolas Klauser
operator== (#117184)
2024-12-10[libc++] Add #if 0 block to all the top-level headers (#119234)Nikolas Klauser
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-11-06[libc++] Refactor the configuration macros to being always defined (#112094)Nikolas Klauser
This is a follow-up to #89178. This updates the `<__config_site>` macros.
2024-11-06[libc++] Remove <stddef.h> includes from the granularized <cstddef> headers ↵Nikolas Klauser
(#114788) We can define some of these aliases without having to include the system <stddef.h> and there doesn't seem to be much of a reason we shouldn't do it this way.
2024-10-31[libc++] Granularize <cstddef> includes (#108696)Nikolas Klauser
2024-10-12[libc++][RFC] Always define internal feature test macros (#89178)Nikolas Klauser
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-09-12[libc++][NFC] Use [[__nodiscard__]] unconditionally (#80454)Nikolas Klauser
`__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-08-26[libc++] Call basic_string_view's assume-valid constructor from basic_string ↵Christopher Di Bella
operations (#105863) `basic_string` frequently calls `basic_string_view(data(), size())`, which accounts for ~15% of the observed overhead when hardening is enabled. This commit removes unnecessary checks when `basic_string` is known to already have valid data, by bypassing the public constructor, so that we eliminate that overhead.
2024-07-23Suppress a redundant hardening check in basic_string_view::substr (#91804)David Benjamin
Fixes #91634. This could alternatively be done with an _LIBCPP_ASSUME, after https://github.com/llvm/llvm-project/pull/91801 lands, but would also require https://github.com/llvm/llvm-project/issues/91619 be fixed first. Given the dependencies, it seemed simplest to just make a private ctor.
2024-07-18[libc++] Include the rest of the detail headers by version in the umbrella ↵Nikolas Klauser
headers (#96032) This is a follow-up to #83740.
2024-06-23[libc++][NFC] Replace _NOEXCEPT and _LIBCPP_CONSTEXPR macros with the ↵Nikolas Klauser
keywords in C++11 code (#96387)
2024-06-18[libc++][NFC] Run clang-format on libcxx/include again (#95874)Louis Dionne
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[libc++][NFC] Remove unnecessary parens in static_asserts (#95605)Nikolas Klauser
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-04-22[libc++] Remove _LIBCPP_DISABLE_NODISCARD_EXTENSIONS and refactor the tests ↵Nikolas Klauser
(#87094) This also adds a few tests that were missing.
2024-03-11[libc++][hardening] Reclassify string_view(ptr, len)'s size assertion (#79297)David Benjamin
The comment makes this error condition sound less problematic than it is. If the length does not match the pointer's bounds, all bounds-checking in string_view goes wrong. A length over PTRDIFF_MAX cannot possibly be a correct bounds and was mostly an underflowed negative number cast to a size_t. The documentation for _LIBCPP_ASSERT_VALID_INPUT_RANGE discusses ranges being valid, including an iterator and a count, which seemed appropriate here.
2024-03-04[libc++] Use __wrap_iter in string_view and array in the unstable ABI (#74482)Louis Dionne
std::string_view and std::array iterators don't have to be raw pointers, and in fact other implementations don't represent them as raw pointers. Them being raw pointers in libc++ makes it easier for users to write non-portable code. This is bad in itself, but this is even worse when considering efforts like hardening where we want an easy ability to swap for a different iterator type. If users depend on iterators being raw pointers, this becomes a build break. Hence, this patch enables the use of __wrap_iter in the unstable ABI, creating a long term path towards making this the default. This patch may break code that assumes these iterators are raw pointers for people compiling with the unstable ABI. This patch also removes several assumptions that array iterators are raw pointers in the code base and in the test suite.
2024-02-29[libc++] Clean up includes of <__assert> (#80091)Louis Dionne
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-01-20[libc++][hardening] Categorize assertions that produce incorrect results ↵Konstantin Varlamov
(#77183) Introduce a new `argument-within-domain` category that covers cases where the given arguments make it impossible to produce a correct result (or create a valid object in case of constructors). While the incorrect result doesn't create an immediate problem within the library (like e.g. a null pointer dereference would), it always indicates a logic error in user code and is highly likely to lead to a bug in the program once the value is used.
2023-12-18[libc++] Format the code base (#74334)Louis Dionne
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-05[libc++] Replace uses of _VSTD:: by std:: (#74331)Louis Dionne
As part of the upcoming clang-formatting of libc++, this patch performs the long desired removal of the _VSTD macro. See https://discourse.llvm.org/t/rfc-clang-formatting-all-of-libc-once-and-for-all for the clang-format proposal.
2023-12-04[libc++][NFC] Add a few clang-format annotations (#74352)Louis Dionne
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[libc++] Rename _LIBCPP_INLINE_VISIBILITY to _LIBCPP_HIDE_FROM_ABI (#74095)Louis Dionne
In preparation for running clang-format on the whole code base, we are also removing mentions of the legacy _LIBCPP_INLINE_VISIBILITY macro in favor of the newer _LIBCPP_HIDE_FROM_ABI. We're still leaving the definition of _LIBCPP_INLINE_VISIBILITY to avoid creating needless breakage in case some older patches are checked-in with mentions of the old macro. After we branch for LLVM 18, we can do another pass to clean up remaining uses of the macro that might have gotten introduced by mistake (if any) and remove the macro itself at the same time. This is just a minor convenience to smooth out the transition as much as possible. See https://discourse.llvm.org/t/rfc-clang-formatting-all-of-libc-once-and-for-all for the clang-format proposal.
2023-11-07[libc++][hardening] Add `_LIBCPP_ASSERT_NON_NULL` to check for null pointers ↵Konstantin Varlamov
(#71428)
2023-09-18[libc++] [string_view] Remove operators made redundant by C++20 (#66206)Amirreza Ashouri
Thanks to Giuseppe D'Angelo for pointing this out on the cpplang Slack! The example implementation in https://eel.is/c++draft/string.view.comparison#example-1 was necessary when it was written, in C++17, but in C++20 we don't need that complexity anymore, because of the reversed candidates that are synthesized by the compiler.
2023-07-20[libc++][hardening] Categorize most assertions inside the container classes.varconst
This introduces: - `_LIBCPP_ASSERT_VALID_INPUT_RANGE`; - `_LIBCPP_ASSERT_VALID_CONTAINER_ACCESS`; - `_LIBCPP_ASSERT_VALID_ITERATOR_ACCESS`; - `_LIBCPP_ASSERT_VALID_ALLOCATOR`; - `_LIBCPP_ASSERT_INTERNAL`. Differential Revision: https://reviews.llvm.org/D155349
2023-07-14[libc++][NFC] Suppress -Wdeprecated-literal-operatorPo-yao Chang
Remove spaces between operator"" and identifier to suppress -Wdeprecated-literal-operator, and between operator and "" like how they are written in [string.view.literals] and [basic.string.literals]. Differential Revision: https://reviews.llvm.org/D155200
2023-06-28[libc++][hardening][NFC] Introduce `_LIBCPP_ASSERT_UNCATEGORIZED`.varconst
Replace most uses of `_LIBCPP_ASSERT` with `_LIBCPP_ASSERT_UNCATEGORIZED`. This is done as a prerequisite to introducing hardened mode to libc++. The idea is to make enabling assertions an opt-in with (somewhat) fine-grained controls over which categories of assertions are enabled. The vast majority of assertions are currently uncategorized; the new macro will allow turning on `_LIBCPP_ASSERT` (the underlying mechanism for all kinds of assertions) without enabling all the uncategorized assertions (in the future; this patch preserves the current behavior). Differential Revision: https://reviews.llvm.org/D153816
2023-06-27[libc++][hardening] Add an ABI macro `_LIBCPP_ABI_BOUNDED_ITERATORS`.varconst
Use the new macro instead of `_LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING`. Differential Revision: https://reviews.llvm.org/D153895
2023-06-27[libc++] __iterator/readable_traits.h isn't standaloneIan Anderson
`__iterator/readable_traits.h` can't be used by itself, intantiating `iter_value_t` requires `__iterator/iterator_traits.h`. `readable_traits.h` can't include `iterator_traits.h` though because `iterator_traits.h` requires `readable_traits.h`. Move `iter_value_t` to `__iterator/iterator_traits.h` so that both headers can work standalone. Reviewed By: Mordante, #libc Differential Revision: https://reviews.llvm.org/D153828
2023-05-23[libc++] Updates C++2b to C++23.Mark de Wever
During the ISO C++ Committee meeting plenary session the C++23 Standard has been voted as technical complete. This updates the reference to c++2b to c++23 and updates the __cplusplus macro. Note since we use clang-tidy 16 a small work-around is needed. Clang knows -std=c++23 but clang-tidy not so for now force the lit compiler flag to use -std=c++2b instead of -std=c++23. Reviewed By: #libc, philnik, jloser, ldionne Differential Revision: https://reviews.llvm.org/D150795
2023-05-07[libc++][Modules] Add missing includes and exportsIan Anderson
Several headers are missing includes for things they use. type_traits.is_enum needs to export type_traits.integral_constant so that clients can access its `value` member without explicitly including __type_traits/integral_constant.h themselves. Make `subrange_fwd` a peer submodule to `subrange` rather than a submodule of it, and have `subrange` export `subrange_fwd`. That will make it easier to programmatically generate modules for the private detail headers, and it will accomplish the same effect that __ranges/subrange.h will make subrange_kind visible. Reviewed By: Mordante, #libc Differential Revision: https://reviews.llvm.org/D150055
2023-05-06[libc++][format] Fixes vector<bool> requirements.Mark de Wever
Makes sure the formatter for the vector<bool>::reference is enabled when only the header <vector> is included. Before this change it required <vector> and <format> to be included. This violated the requirements in the Standard. Fixes: https://llvm.org/PR61314 Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D149543
2023-04-09[libc++] Remove <cstdlib> includesNikolas Klauser
We changed the `abort` calls when trying to throw exceptions in `-fno-exceptions` mode to `__verbose_abort` calls, which removes the dependency in most files. Reviewed By: ldionne, #libc Spies: dim, emaste, mikhail.ramalho, smeenai, libcxx-commits Differential Revision: https://reviews.llvm.org/D146076
2023-03-19[libc++] Assert that lengths fit in difference_typeDavid Benjamin
This can help flag accidentally passing in negative values into the `string_view` constructor. This aligns with a similar check in `absl::string_view`. Fixes https://github.com/llvm/llvm-project/issues/61100 Reviewed By: #libc, Mordante Differential Revision: https://reviews.llvm.org/D145981
2023-03-12[libc++] Granularize <exception>Nikolas Klauser
This patch also updates the moved code to the new style (i.e. formatted, replaced marcos and typedefs) Reviewed By: ldionne, #libc Spies: arichardson, libcxx-commits Differential Revision: https://reviews.llvm.org/D145095
2023-03-08[libc++] Granularize <type_traits> includesNikolas Klauser
Reviewed By: ldionne, #libc, #libc_abi Spies: #libc_vendors, smeenai, libcxx-commits Differential Revision: https://reviews.llvm.org/D145320
2023-02-16[libc++] LWG 3857: allow `string_view` conversion when only traits varyJoe Loser
The `basic_string_view` constructor accepting a contiguous range rejects converting between `basic_string_view` even when only the trait types vary. This prevents conversions for converting from `basic_string_view<C, T1>` and `basic_string<C, T1, A>` to `basic_string_view<C, T2>`. Recently, this constructor was made `explicit`, so there's no reason to really forbid this conversion anymore. Relax the restriction that the trait types need to match in this constructor. Differential Revision: https://reviews.llvm.org/D143972
2023-02-15[libc++][NFC] Replace _LIBCPP_STD_VER > x with _LIBCPP_STD_VER >= xNikolas Klauser
This change is almost fully mechanical. The only interesting change is in `generate_feature_test_macro_components.py` to generate `_LIBCPP_STD_VER >=` instead. To avoid churn in the git-blame this commit should be added to the `.git-blame-ignore-revs` once committed. Reviewed By: ldionne, var-const, #libc Spies: jloser, libcxx-commits, arichardson, arphaman, wenlei Differential Revision: https://reviews.llvm.org/D143962
2023-01-31[libc++] Use bounded iterators in std::string_view when the debug mode is ↵Louis Dionne
enabled Differential Revision: https://reviews.llvm.org/D142903
2022-12-23[libc++] Add custom clang-tidy checksNikolas Klauser
Reviewed By: #libc, ldionne Spies: jwakely, beanz, smeenai, cfe-commits, tschuett, avogelsgesang, Mordante, sstefan1, libcxx-commits, ldionne, mgorny, arichardson, miyuki Differential Revision: https://reviews.llvm.org/D131963
2022-11-28[libc++][NFC] Remove stray whitespace in string_viewLouis Dionne
2022-11-28[libc++][NFC] Use using declaration instead of typedef in string_viewLouis Dionne