summaryrefslogtreecommitdiff
path: root/libcxx/test/std/strings/basic.string/string.ops
AgeCommit message (Collapse)Author
2025-09-25[libc++] Fold __search_substring into _Traits::find in case the second ↵Nikolas Klauser
string has length 1 (#160076) Apple M4: ``` Benchmark Baseline Candidate Difference % Difference ----------------------------------------------------------- ---------- ----------- ------------ -------------- BM_string_literal/1024 16.99 16.79 -0.21 -1.21 BM_string_literal/128 3.44 3.34 -0.10 -2.88 BM_string_literal/16 1.80 1.69 -0.11 -5.93 BM_string_literal/2048 38.44 38.38 -0.07 -0.17 BM_string_literal/256 5.77 5.65 -0.12 -2.02 BM_string_literal/32 2.03 1.92 -0.11 -5.44 BM_string_literal/4096 73.92 73.74 -0.18 -0.25 BM_string_literal/512 9.49 9.41 -0.08 -0.84 BM_string_literal/64 2.59 2.45 -0.14 -5.38 BM_string_literal/8 1.79 1.69 -0.11 -5.90 BM_string_literal/8192 132.09 131.81 -0.28 -0.21 ```
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>
2023-12-13[ASan][libc++] std::basic_string annotations (#72677)Tacet
This commit introduces basic annotations for `std::basic_string`, mirroring the approach used in `std::vector` and `std::deque`. Initially, only long strings with the default allocator will be annotated. Short strings (_SSO - short string optimization_) and strings with non-default allocators will be annotated in the near future, with separate commits dedicated to enabling them. The process will be similar to the workflow employed for enabling annotations in `std::deque`. **Please note**: these annotations function effectively only when libc++ and libc++abi dylibs are instrumented (with ASan). This aligns with the prevailing behavior of Memory Sanitizer. To avoid breaking everything, this commit also appends `_LIBCPP_INSTRUMENTED_WITH_ASAN` to `__config_site` whenever libc++ is compiled with ASan. If this macro is not defined, string annotations are not enabled. However, linking a binary that does **not** annotate strings with a dynamic library that annotates strings, is not permitted. Originally proposed here: https://reviews.llvm.org/D132769 Related patches on Phabricator: - Turning on annotations for short strings: https://reviews.llvm.org/D147680 - Turning on annotations for all allocators: https://reviews.llvm.org/D146214 This PR is a part of a series of patches extending AddressSanitizer C++ container overflow detection capabilities by adding annotations, similar to those existing in `std::vector` and `std::deque` collections. These enhancements empower ASan to effectively detect instances where the instrumented program attempts to access memory within a collection's internal allocation that remains unused. This includes cases where access occurs before or after the stored elements in `std::deque`, or between the `std::basic_string`'s size (including the null terminator) and capacity bounds. The introduction of these annotations was spurred by a real-world software bug discovered by Trail of Bits, involving an out-of-bounds memory access during the comparison of two strings using the `std::equals` function. This function was taking iterators (`iter1_begin`, `iter1_end`, `iter2_begin`) to perform the comparison, using a custom comparison function. When the `iter1` object exceeded the length of `iter2`, an out-of-bounds read could occur on the `iter2` object. Container sanitization, upon enabling these annotations, would effectively identify and flag this potential vulnerability. This Pull Request introduces basic annotations for `std::basic_string`. Long strings exhibit structural similarities to `std::vector` and will be annotated accordingly. Short strings are already implemented, but will be turned on separately in a forthcoming commit. Look at [a comment](https://github.com/llvm/llvm-project/pull/72677#issuecomment-1850554465) below to read about SSO issues at current moment. Due to the functionality introduced in [D132522](https://github.com/llvm/llvm-project/commit/dd1b7b797a116eed588fd752fbe61d34deeb24e4), the `__sanitizer_annotate_contiguous_container` function now offers compatibility with all allocators. However, enabling this support will be done in a subsequent commit. For the time being, only strings with the default allocator will be annotated. If you have any questions, please email: - advenam.tacet@trailofbits.com - disconnect3d@trailofbits.com
2023-12-12Add `std::basic_string` test cases (#74830)Tacet
Extend `std::basic_string` tests to cover more buffer situations and length in general, particularly non-SSO cases after SSO test cases (changing buffers). This commit is a side effect of working on tests for ASan annotations. Related PR: https://github.com/llvm/llvm-project/pull/72677
2023-09-27[libc++] Refactor string unit tests to ease addition of new allocatorsLouis Dionne
While doing this, I also found a few tests that were either clearly incorrect (e.g. testing the wrong function) or that lacked basic test coverage like testing std::string itself (e.g. the test was only checking std::basic_string with a custom allocator). In these cases, I did a few conservative drive-by changes. Differential Revision: https://reviews.llvm.org/D140550 Co-authored-by: Brendan Emery <brendan.emery@esrlabs.com>
2023-09-01[libc++] Apply clang formatting to all string unit testsBrendan Emery
This applies clang-format to the std::string unit tests in preparation for landing https://reviews.llvm.org/D140550. Differential Revision: https://reviews.llvm.org/D140612
2023-06-13[libc++] Mark slow tests as unsupported on GCCLouis Dionne
Some tests in our test suite are unbelievably slow on GCC due to the use of the always_inline attribute. See [1] for more details. This patch introduces the GCC-ALWAYS_INLINE-FIXME lit feature to disable tests that are plagued by that issue. At the same time, it moves several existing tests from ad-hoc `UNSUPPORTED: gcc-12` markup to the new GCC-ALWAYS_INLINE-FIXME feature, and marks the slowest tests reported by the CI as `UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME`. [1]: https://discourse.llvm.org/t/rfc-stop-supporting-extern-instantiations-with-gcc/71277/1 Differential Revision: https://reviews.llvm.org/D152736
2023-01-09[libc++][test] Make some string tests MSVC-friendlyCasey Carter
* Using one-or-two letter names for globals is asking for shadowing warnings. * MSVCSTL's container proxy allocations strike again * MSVCSTL's `<string>` doesn't define `std::out_of_range` * `basic_string::substr` takes two arguments of type `size_type`. Let's use that type instead of `size_t` and `ptrdiff_t` to avoid narrowing warnings. Differential Revision: https://reviews.llvm.org/D141253
2022-12-01[libc++][NFC] Avoid most-vexing parseLouis Dionne
2022-11-29[libc++] Don't pass the allocator in substr()Nikolas Klauser
This bug was dicoved when implementing P2438R2. Fixes #57190 Reviewed By: ldionne, Mordante, #libc Spies: libcxx-commits Differential Revision: https://reviews.llvm.org/D138069
2022-11-02[libc++] Implement P2438R2 (std::string::substr() &&)Nikolas Klauser
This doesn't affect our ABI because `std::string::substr()` isn't in the dylib and the mangling of `substr() const` and `substr() const&` are different. Reviewed By: ldionne, Mordante, var-const, avogelsgesang, #libc Spies: arphaman, huixie90, libcxx-commits Differential Revision: https://reviews.llvm.org/D131668
2022-08-26[libc++][NFC] Remove some of the code duplication in the string testsNikolas Klauser
Reviewed By: ldionne, #libc, huixie90 Spies: huixie90, libcxx-commits, arphaman Differential Revision: https://reviews.llvm.org/D131856
2022-06-13[libcxx][AIX] Switch build compiler to clangJake Egan
This patch switches the build compiler for AIX from ibm-clang to clang. ibm-clang++_r has `-pthread` by default, but clang for AIX doesn't, so `-pthread` had to be added to the test config. A bunch of tests now pass, so the `XFAIL` was removed. This patch also switch the build to use the visibility support available in clang-15 to control symbols exported by the shared library (AIX traditionally uses explicit export lists for this purpose). Reviewed By: #libc, #libc_abi, daltenty, #libunwind, ldionne Differential Revision: https://reviews.llvm.org/D127470
2022-04-27[libc++] Implement P0980R1 (constexpr std::string)Nikolas Klauser
Reviewed By: #libc, ldionne Spies: daltenty, sdasgup3, ldionne, arichardson, MTC, ChuanqiXu, mehdi_amini, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes, tatianashp, rdzhabarov, teijeong, cota, dcaballe, Chia-hungDuan, wrengr, wenzhicui, arphaman, Mordante, miscco, Quuxplusone, smeenai, libcxx-commits Differential Revision: https://reviews.llvm.org/D110598
2022-02-11[libc++] Prepare string.ops for constexprNikolas Klauser
Reviewed By: ldionne, #libc Spies: libcxx-commits Differential Revision: https://reviews.llvm.org/D119490
2021-02-09[libc++] Add `noexcept` to `string::find` and similar members.zoecarver
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
2020-04-10[libc++] NFC: Clean up a lot of old Lit featuresLouis Dionne
The libc++ test suite has a lot of old Lit features used to XFAIL tests and mark them as UNSUPPORTED. Many of them are to workaround problems on old compilers or old platforms. As time goes by, it is good to go and clean those up to simplify the configuration of the test suite, and also to reflect the testing reality. It's not useful to have markup that gives the impression that e.g. clang-3.3 is supported, when we don't really test on it anymore (and hence several new tests probably don't have the necessary markup on them).
2020-04-03[libc++] Remove useless nothing_to_do.pass.cpp testsLouis Dionne
The testing script used to test libc++ historically did not like directories without any testing files, so these tests had been added. Since this is not necessary anymore, we can now remove these files. This has the benefit that the total number of tests reflects the real number of tests more closely, and we also skip some unnecessary work (especially relevant when running tests over SSH). However, some nothing_to_do.pass.cpp tests actually serve the purpose of documenting that an area of the Standard doesn't need to be tested, or is tested elsewhere. These files are not removed by this commit. Removal done with: import os import itertools for (dirpath, dirnames, filenames) in itertools.chain(os.walk('./libcxx/test'), os.walk('./libcxxabi/test')): if len(filenames + dirnames) > 1 and \ any(p == 'nothing_to_do.pass.cpp' for p in filenames): os.remove(os.path.join(dirpath, 'nothing_to_do.pass.cpp'))
2019-10-22[libcxx][test][NFC] Fix comment typos.Stephan T. Lavavej
(Testing git commit access.)
2019-05-31Add include for 'test_macros.h' to all the tests that were missing them. ↵Marshall Clow
Thanks to Zoe for the (big, but simple) patch. NFC intended. llvm-svn: 362252
2019-02-27[libc++] Mark several tests as XFAIL on macosx10.7Louis Dionne
Those tests fail when linking against a new dylib but running against macosx10.7. I believe this is caused by a duplicate definition of the RTTI for exception classes in libc++.dylib and libc++abi.dylib, but this matter still needs some investigation. This issue was not caught previously because all the tests always linked against the same dylib used for running (because LIT made it impossible to do otherwise before r349171). rdar://problem/46809586 llvm-svn: 354940
2019-02-04Support tests in freestandingJF Bastien
Summary: Freestanding is *weird*. The standard allows it to differ in a bunch of odd manners from regular C++, and the committee would like to improve that situation. I'd like to make libc++ behave better with what freestanding should be, so that it can be a tool we use in improving the standard. To do that we need to try stuff out, both with "freestanding the language mode" and "freestanding the library subset". Let's start with the super basic: run the libc++ tests in freestanding, using clang as the compiler, and see what works. The easiest hack to do this: In utils/libcxx/test/config.py add: self.cxx.compile_flags += ['-ffreestanding'] Run the tests and they all fail. Why? Because in freestanding `main` isn't special. This "not special" property has two effects: main doesn't get mangled, and main isn't allowed to omit its `return` statement. The first means main gets mangled and the linker can't create a valid executable for us to test. The second means we spew out warnings (ew) and the compiler doesn't insert the `return` we omitted, and main just falls of the end and does whatever undefined behavior (if you're luck, ud2 leading to non-zero return code). Let's start my work with the basics. This patch changes all libc++ tests to declare `main` as `int main(int, char**` so it mangles consistently (enabling us to declare another `extern "C"` main for freestanding which calls the mangled one), and adds `return 0;` to all places where it was missing. This touches 6124 files, and I apologize. The former was done with The Magic Of Sed. The later was done with a (not quite correct but decent) clang tool: https://gist.github.com/jfbastien/793819ff360baa845483dde81170feed This works for most tests, though I did have to adjust a few places when e.g. the test runs with `-x c`, macros are used for main (such as for the filesystem tests), etc. Once this is in we can create a freestanding bot which will prevent further regressions. After that, we can start the real work of supporting C++ freestanding fairly well in libc++. <rdar://problem/47754795> Reviewers: ldionne, mclow.lists, EricWF Subscribers: christof, jkorous, dexonsmith, arphaman, miyuki, libcxx-commits Differential Revision: https://reviews.llvm.org/D57624 llvm-svn: 353086
2019-01-19Update more file headers across all of the LLVM projects in the monorepoChandler Carruth
to reflect the new license. These used slightly different spellings that defeated my regular expressions. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351648
2018-07-02Implement LWG 2946, 3075 and 3076. Reviewed as https://reviews.llvm.org/D48616Marshall Clow
llvm-svn: 336132
2017-11-21Change (void) casts to TEST_IGNORE_NODISCARD, as requested by Eric. Reviewed ↵Billy Robert O'Neal III
as https://reviews.llvm.org/D40065 llvm-svn: 318804
2017-11-15Tolerate [[nodiscard]] annotations in the STL. Reviewed as ↵Billy Robert O'Neal III
https://reviews.llvm.org/D39033 llvm-svn: 318276
2017-09-26[libcxx] [test] Fix unused local typedef warnings.Stephan T. Lavavej
llvm-svn: 314259
2017-09-07Add even more string_view tests. These found some bugs in the default ↵Marshall Clow
parameter value for rfind/find_last_of/find_last_not_of llvm-svn: 312693
2016-11-29Protect std::string tests under libcpp-no-exceptionsRoger Ferrer Ibanez
Skip tests that expect an exception be thrown and/or disable unreachable catch handlers. Differential Revision: https://reviews.llvm.org/D26612 llvm-svn: 288158
2016-11-24Reverting wrong diffRoger Ferrer Ibanez
I managed to confuse me with two reviews of the same thing and ended commiting the wrong one. llvm-svn: 287868
2016-11-24Protect std::string tests under libcpp-no-exceptionsRoger Ferrer Ibanez
Skip tests that expect an exception be thrown and/or disable unreachable catch handlers. Differential Revision: https://reviews.llvm.org/D26608 llvm-svn: 287865
2016-11-01Protect exceptional paths under libcpp-no-exceptionsRoger Ferrer Ibanez
These tests are of the form try { action-that-may-throw assert(!exceptional-condition) assert(some-other-facts) } catch (relevant-exception) { assert(exceptional-condition) } Under libcpp-no-exceptions there is still value in verifying some-other-facts while avoiding the exceptional case. So for these tests just conditionally check some-other-facts if exceptional-condition is false. When exception are supported make sure that a true exceptional-condition throws an exception Differential Revision: https://reviews.llvm.org/D26136 llvm-svn: 285697
2016-09-24Fix a few static_asserts that need extra parens on -03Marshall Clow
llvm-svn: 282343
2016-09-24Implement proposed resolution for LWG#2758. Reviewed as D24446. Normally, I ↵Marshall Clow
would wait for these to be voted upon at a committee meeting (November), but the current draft standard is broken, and this should fix it. (And if it doesn't, we want to know about it soonest) llvm-svn: 282342
2016-07-21Implement std::string_view as described in http://wg21.link/P0254R1. ↵Marshall Clow
Reviewed as https://reviews.llvm.org/D21459 llvm-svn: 276238
2016-06-14Replace __cplusplus comparisons and dialect __has_feature checks with ↵Eric Fiselier
TEST_STD_VER. This is a huge cleanup that helps make the libc++ test suite more portable. Patch from STL@microsoft.com. Thanks STL! llvm-svn: 272716
2016-04-28Guard libc++ specific c.__invariants() tests in LIBCPP_ASSERT macrosEric Fiselier
llvm-svn: 267947
2016-03-08Remove a couple tabs that crept inMarshall Clow
llvm-svn: 262932
2016-03-08Implement P0272R1: Give 'std::string' a non-const '.data()' member functionMarshall Clow
llvm-svn: 262931
2015-11-10Make it possible to build a no-exceptions variant of libcxx.Asiri Rathnayake
Fixes a small omission in libcxx that prevents libcxx being built when -DLIBCXX_ENABLE_EXCEPTIONS=0 is specified. This patch adds XFAILS to all those tests that are currently failing on the new -fno-exceptions library variant. Follow-up patches will update the tests (progressively) to cope with the new library variant. Change-Id: I4b801bd8d8e4fe7193df9e55f39f1f393a8ba81a llvm-svn: 252598
2014-12-20Move test into test/std subdirectory.Eric Fiselier
llvm-svn: 224658