Skip to content

[libcxx][c++26] 2937R0: Freestanding: Remove strtok #146290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Conversation

dywoq
Copy link

@dywoq dywoq commented Jun 29, 2025

@dywoq dywoq requested a review from a team as a code owner June 29, 2025 19:47
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jun 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 29, 2025

@llvm/pr-subscribers-libcxx

Author: dywoq (dywoq)

Changes

Link to 'P2937R0: Freestanding: Remove strtok'


Full diff: https://github.com/llvm/llvm-project/pull/146290.diff

3 Files Affected:

  • (modified) libcxx/include/cstring (+3-1)
  • (modified) libcxx/include/version (+3)
  • (added) libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp (+14)
diff --git a/libcxx/include/cstring b/libcxx/include/cstring
index 4aa14feeec280..3828b33c9ee8b 100644
--- a/libcxx/include/cstring
+++ b/libcxx/include/cstring
@@ -47,7 +47,7 @@ const char* strrchr(const char* s, int c);
 size_t strspn(const char* s1, const char* s2);
 const char* strstr(const char* s1, const char* s2);
       char* strstr(      char* s1, const char* s2);
-char* strtok(char* restrict s1, const char* restrict s2);
+char* strtok(char* restrict s1, const char* restrict s2); // not freestanding in C++
 void* memset(void* s, int c, size_t n);
 char* strerror(int errnum);
 size_t strlen(const char* s);
@@ -97,7 +97,9 @@ using ::strpbrk _LIBCPP_USING_IF_EXISTS;
 using ::strrchr _LIBCPP_USING_IF_EXISTS;
 using ::strspn _LIBCPP_USING_IF_EXISTS;
 using ::strstr _LIBCPP_USING_IF_EXISTS;
+#if __STDC_HOSTED__ == 1 || _LIBCPP_STD_VER < 26
 using ::strtok _LIBCPP_USING_IF_EXISTS;
+#endif
 using ::memset _LIBCPP_USING_IF_EXISTS;
 using ::strerror _LIBCPP_USING_IF_EXISTS;
 using ::strlen _LIBCPP_USING_IF_EXISTS;
diff --git a/libcxx/include/version b/libcxx/include/version
index 91fe48351e161..dafbafefaa2ae 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -534,6 +534,7 @@ __cpp_lib_void_t                                        201411L <type_traits>
 # define __cpp_lib_to_underlying                        202102L
 // # define __cpp_lib_tuple_like                           202207L
 # define __cpp_lib_unreachable                          202202L
+# define __cpp_lib_freestanding_cstring                 202306L
 #endif
 
 #if _LIBCPP_STD_VER >= 26
@@ -606,6 +607,8 @@ __cpp_lib_void_t                                        201411L <type_traits>
 // # define __cpp_lib_tuple_like                           202311L
 # undef  __cpp_lib_variant
 # define __cpp_lib_variant                              202306L
+# undef __cpp_lib_freestanding_cstring
+# define __cpp_lib_freestanding_cstring                 202306L
 #endif
 
 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
diff --git a/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp b/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
new file mode 100644
index 0000000000000..18c5f069e9a7e
--- /dev/null
+++ b/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
@@ -0,0 +1,14 @@
+// UNSUPPORTED: libcpp-has-no-cstring
+// REQUIRES: freestanding
+// XFAIL: *
+
+// RUN: %clang %s -c -o /dev/null -ffreestanding --std=c++20 2>&1 | FileCheck %s
+
+#include <cstring> 
+
+int main() {
+  char s[] = "hello world";
+  char* tok = std::strtok(s, " ");
+  (void)tok;
+  return 0;
+}
\ No newline at end of file

@dywoq dywoq changed the title [libcxx] P2937R0: Freestanding: Remove strtok [libcxx][c++26] 2937R0: Freestanding: Remove strtok Jun 29, 2025
Copy link

github-actions bot commented Jun 30, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions ,cpp -- libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp libcxx/include/cstring libcxx/include/version libcxx/test/std/language.support/support.limits/support.limits.general/cstring.version.compile.pass.cpp libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
View the diff from clang-format here.
diff --git a/libcxx/include/cstring b/libcxx/include/cstring
index 8ebe9dc6a..e5551cf10 100644
--- a/libcxx/include/cstring
+++ b/libcxx/include/cstring
@@ -97,9 +97,9 @@ using ::strpbrk _LIBCPP_USING_IF_EXISTS;
 using ::strrchr _LIBCPP_USING_IF_EXISTS;
 using ::strspn _LIBCPP_USING_IF_EXISTS;
 using ::strstr _LIBCPP_USING_IF_EXISTS;
-#if __STDC_HOSTED__ == 1 || _LIBCPP_STD_VER < 26
+#  if __STDC_HOSTED__ == 1 || _LIBCPP_STD_VER < 26
 using ::strtok _LIBCPP_USING_IF_EXISTS;
-#endif
+#  endif
 using ::memset _LIBCPP_USING_IF_EXISTS;
 using ::strerror _LIBCPP_USING_IF_EXISTS;
 using ::strlen _LIBCPP_USING_IF_EXISTS;
diff --git a/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp b/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
index 0f0869380..030249add 100644
--- a/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
+++ b/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
@@ -4,10 +4,10 @@
 
 // RUN: %clang %s -c -o /dev/null -ffreestanding --std=c++2c 2>&1 | FileCheck %s
 
-#include <cstring> 
+#include <cstring>
 
 int main() {
-  char s[] = "hello world";
+  char s[]  = "hello world";
   char* tok = std::strtok(s, " ");
   (void)tok;
   return 0;


#include <cstring>

int main() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether this really works as main is only expected to be the entry in hosted environments.

Moreover, I think it's worthwhile to test all other freestanding functions in <cstring>/<string.h> together. Feature-test macro __cpp_lib_freestanding_cstring covers them.

Copy link
Contributor

@frederick-vs-ja frederick-vs-ja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please modify generate_feature_test_macro_components.py together and execute it to update <vesion> and corresponding test files.

@dywoq dywoq requested a review from frederick-vs-ja June 30, 2025 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants