[libcxx-commits] [libcxx] [libcxx] P2937R0: Freestanding: Remove strtok (PR #146290)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jun 29 12:47:24 PDT 2025
https://github.com/dywoq created https://github.com/llvm/llvm-project/pull/146290
Link to ['P2937R0: Freestanding: Remove strtok'](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2937r0.html)
>From 96e28740abe407fc02e17b0e6cbc8f35a3cd0fc4 Mon Sep 17 00:00:00 2001
From: dywoq <aleks.koyf at gmail.com>
Date: Sun, 29 Jun 2025 21:59:10 +0300
Subject: [PATCH 1/4] [libcxx cstring] make strtok not freestanding
---
libcxx/include/cstring | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/libcxx/include/cstring b/libcxx/include/cstring
index 4aa14feeec280..f7f38d359b602 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
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;
>From 311aac1e91dd219e9dd7566be234ee9e7bf50a3b Mon Sep 17 00:00:00 2001
From: dywoq <aleks.koyf at gmail.com>
Date: Sun, 29 Jun 2025 22:07:38 +0300
Subject: [PATCH 2/4] [libcxx] change the if/endif macro condition in cstring
---
libcxx/include/cstring | 2 +-
.../freestanding/strtok_removed.fail.cpp | 17 +++++++++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
create mode 100644 libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
diff --git a/libcxx/include/cstring b/libcxx/include/cstring
index f7f38d359b602..3828b33c9ee8b 100644
--- a/libcxx/include/cstring
+++ b/libcxx/include/cstring
@@ -97,7 +97,7 @@ 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
+#if __STDC_HOSTED__ == 1 || _LIBCPP_STD_VER < 26
using ::strtok _LIBCPP_USING_IF_EXISTS;
#endif
using ::memset _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
new file mode 100644
index 0000000000000..f4ff87c27f65b
--- /dev/null
+++ b/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
@@ -0,0 +1,17 @@
+// this test confirms that std::strtok is not available in a freestanding C++ environment.
+// it is expected to fail compilation when -ffreestanding is enabled.
+
+// 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
>From b42c795142090ae783b0e1d6b90f19b8f4bd0009 Mon Sep 17 00:00:00 2001
From: dywoq <aleks.koyf at gmail.com>
Date: Sun, 29 Jun 2025 22:21:20 +0300
Subject: [PATCH 3/4] [libcxx] remove comment in cstring.freestanding test
---
libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp | 3 ---
1 file changed, 3 deletions(-)
diff --git a/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp b/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
index f4ff87c27f65b..18c5f069e9a7e 100644
--- a/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
+++ b/libcxx/test/std/cstring/freestanding/strtok_removed.fail.cpp
@@ -1,6 +1,3 @@
-// this test confirms that std::strtok is not available in a freestanding C++ environment.
-// it is expected to fail compilation when -ffreestanding is enabled.
-
// UNSUPPORTED: libcpp-has-no-cstring
// REQUIRES: freestanding
// XFAIL: *
>From e4d20cec84dd168bdbb6b98caa40000d033af10f Mon Sep 17 00:00:00 2001
From: dywoq <aleks.koyf at gmail.com>
Date: Sun, 29 Jun 2025 22:41:26 +0300
Subject: [PATCH 4/4] [libcxx] add feature test macro
__cpp_lib_freestanding_cstring
---
libcxx/include/version | 3 +++
1 file changed, 3 insertions(+)
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)
More information about the libcxx-commits
mailing list