[all-commits] [llvm/llvm-project] a9b2e9: [libc][cpp::string] Allocate fewer temp strings in...

Jackson Stogel via All-commits all-commits at lists.llvm.org
Tue Jul 21 00:28:05 PDT 2026


  Branch: refs/heads/users/jtstogel/realpath/string-view-assign
  Home:   https://github.com/llvm/llvm-project
  Commit: a9b2e94c4c307fba1dbfe350a91967b3e9d6cf7a
      https://github.com/llvm/llvm-project/commit/a9b2e94c4c307fba1dbfe350a91967b3e9d6cf7a
  Author: jtstogel <jtstogel at gmail.com>
  Date:   2026-07-21 (Tue, 21 Jul 2026)

  Changed paths:
    M libc/src/__support/CPP/CMakeLists.txt
    M libc/src/__support/CPP/string.h
    M libc/test/UnitTest/LibcTest.cpp
    M libc/test/src/__support/CPP/CMakeLists.txt
    M libc/test/src/__support/CPP/string_test.cpp
    M libc/test/src/stdlib/realpath_test.cpp
    M utils/bazel/llvm-project-overlay/libc/BUILD.bazel
    M utils/bazel/llvm-project-overlay/libc/test/src/__support/CPP/BUILD.bazel

  Log Message:
  -----------
  [libc][cpp::string] Allocate fewer temp strings in operator= and +=

This PR generally updates `cpp::string` to avoid incidental allocations. Specifically, it:

- Updates `opreator=(string_view)` to avoid allocating a temporary string: https://github.com/llvm/llvm-project/blob/67ebc4b221c3e94028b33004cd5cd08deee95048/libc/src/__support/CPP/string.h#L106-L108
- Changes `operator+=(const string&)` to accept a `string_view` so that strings may be appended without allocation.
- Makes the `string(string_view)` constructor explicit. Before, there were non-obvious allocations because of the implicit conversion.

This PR assumes that self-assignment and self-appends are not supported with `cpp::string`, and adds debug asserts preventing them. Currently, assignment doesn't work. `cpp::string s = "abc"; s = s;` will just zero out the string. This PR is a slight regression in that `operator+=(const cpp::string&)` previously worked, but now it errors in debug mode, and would result in a use-after-free without the assert statement. I don't think self-append/self-assignment is something `cpp::string` needs to support, so I omitted it, but if a reviewer thinks this behavior is important happy to amend this PR.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list