[libcxx-commits] [libcxx] [libc++][Android] Restrict use of mblen/towctrans/wctrans (PR #116147)

Ryan Prichard via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 15 17:18:51 PST 2025


https://github.com/rprichard updated https://github.com/llvm/llvm-project/pull/116147

>From b66919bc3356be017394cfd50bd06b776166b66a Mon Sep 17 00:00:00 2001
From: Ryan Prichard <rprichard at google.com>
Date: Wed, 13 Nov 2024 18:12:11 -0800
Subject: [PATCH 1/3] [libc++][Android] Restrict use of mblen/towctrans/wctrans

These functions weren't added until API 26 (Android 8.0), but libc++ is
supported for API 21 and up.

These APIs are undeclared as of r.android.com/3216959.
---
 libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp          | 3 +++
 libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp  | 3 +++
 .../test/std/language.support/support.runtime/cstdlib.pass.cpp | 3 +++
 libcxx/test/std/strings/c.strings/cwctype.pass.cpp             | 3 +++
 4 files changed, 12 insertions(+)

diff --git a/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp b/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
index 587c6b6e10ddb6..8199fabe3d1a68 100644
--- a/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
+++ b/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
@@ -141,7 +141,10 @@ int main(int, char**) {
     wchar_t* pw = 0;
     const wchar_t* pwc = 0;
     char* pc = 0;
+    // mblen was added in Android API 26.
+#if !defined(__ANDROID__) || __ANDROID_API__ >= 26
     ASSERT_SAME_TYPE(int,    decltype(mblen("",0)));
+#endif
     ASSERT_SAME_TYPE(int,    decltype(mbtowc(pw,"",0)));
     ASSERT_SAME_TYPE(int,    decltype(wctomb(pc,L' ')));
     ASSERT_SAME_TYPE(size_t, decltype(mbstowcs(pw,"",0)));
diff --git a/libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp b/libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp
index 35b294532b2bd2..ebbec565c1ab7d 100644
--- a/libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp
+++ b/libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp
@@ -109,5 +109,8 @@ ASSERT_SAME_TYPE(int,       decltype(iswctype(w, wct)));
 ASSERT_SAME_TYPE(wctype_t,  decltype(wctype("")));
 ASSERT_SAME_TYPE(wint_t,    decltype(towlower(w)));
 ASSERT_SAME_TYPE(wint_t,    decltype(towupper(w)));
+// towctrans and wctrans were added in Android API 26.
+#if !defined(__ANDROID__) || __ANDROID_API__ >= 26
 ASSERT_SAME_TYPE(wint_t,    decltype(towctrans(w, wctr)));
 ASSERT_SAME_TYPE(wctrans_t, decltype(wctrans("")));
+#endif
diff --git a/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp b/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
index a1f7e1143a1e9b..5f33c72f85bfbe 100644
--- a/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
+++ b/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
@@ -141,7 +141,10 @@ int main(int, char**)
     wchar_t* pw = 0;
     const wchar_t* pwc = 0;
     char* pc = 0;
+    // mblen was added in Android API 26.
+#if !defined(__ANDROID__) || __ANDROID_API__ >= 26
     static_assert((std::is_same<decltype(std::mblen("",0)), int>::value), "");
+#endif
     static_assert((std::is_same<decltype(std::mbtowc(pw,"",0)), int>::value), "");
     static_assert((std::is_same<decltype(std::wctomb(pc,L' ')), int>::value), "");
     static_assert((std::is_same<decltype(std::mbstowcs(pw,"",0)), std::size_t>::value), "");
diff --git a/libcxx/test/std/strings/c.strings/cwctype.pass.cpp b/libcxx/test/std/strings/c.strings/cwctype.pass.cpp
index 5bc2531d6f6ac7..7460c36c357e7f 100644
--- a/libcxx/test/std/strings/c.strings/cwctype.pass.cpp
+++ b/libcxx/test/std/strings/c.strings/cwctype.pass.cpp
@@ -111,8 +111,11 @@ int main(int, char**) {
   ASSERT_SAME_TYPE(std::wctype_t, decltype(std::wctype("")));
   ASSERT_SAME_TYPE(std::wint_t, decltype(std::towlower(w)));
   ASSERT_SAME_TYPE(std::wint_t, decltype(std::towupper(w)));
+  // towctrans and wctrans were added in Android API 26.
+#if !defined(__ANDROID__) || __ANDROID_API__ >= 26
   ASSERT_SAME_TYPE(std::wint_t, decltype(std::towctrans(w, std::wctrans_t())));
   ASSERT_SAME_TYPE(std::wctrans_t, decltype(std::wctrans("")));
+#endif
 
   return 0;
 }

>From 137d8775753228787602120a0f292fd76a9803ed Mon Sep 17 00:00:00 2001
From: Ryan Prichard <rprichard at google.com>
Date: Wed, 13 Nov 2024 18:42:20 -0800
Subject: [PATCH 2/3] fix formatting

---
 libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp         | 4 ++--
 .../std/language.support/support.runtime/cstdlib.pass.cpp     | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp b/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
index 8199fabe3d1a68..82083734c7a263 100644
--- a/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
+++ b/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
@@ -142,9 +142,9 @@ int main(int, char**) {
     const wchar_t* pwc = 0;
     char* pc = 0;
     // mblen was added in Android API 26.
-#if !defined(__ANDROID__) || __ANDROID_API__ >= 26
+#  if !defined(__ANDROID__) || __ANDROID_API__ >= 26
     ASSERT_SAME_TYPE(int,    decltype(mblen("",0)));
-#endif
+#  endif
     ASSERT_SAME_TYPE(int,    decltype(mbtowc(pw,"",0)));
     ASSERT_SAME_TYPE(int,    decltype(wctomb(pc,L' ')));
     ASSERT_SAME_TYPE(size_t, decltype(mbstowcs(pw,"",0)));
diff --git a/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp b/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
index 5f33c72f85bfbe..a00bf13936ec87 100644
--- a/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
+++ b/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
@@ -142,9 +142,9 @@ int main(int, char**)
     const wchar_t* pwc = 0;
     char* pc = 0;
     // mblen was added in Android API 26.
-#if !defined(__ANDROID__) || __ANDROID_API__ >= 26
+#  if !defined(__ANDROID__) || __ANDROID_API__ >= 26
     static_assert((std::is_same<decltype(std::mblen("",0)), int>::value), "");
-#endif
+#  endif
     static_assert((std::is_same<decltype(std::mbtowc(pw,"",0)), int>::value), "");
     static_assert((std::is_same<decltype(std::wctomb(pc,L' ')), int>::value), "");
     static_assert((std::is_same<decltype(std::mbstowcs(pw,"",0)), std::size_t>::value), "");

>From a586d0e5ad4e598c2de0f53a8e4d898385c71173 Mon Sep 17 00:00:00 2001
From: Ryan Prichard <rprichard at google.com>
Date: Mon, 13 Jan 2025 17:03:23 -0800
Subject: [PATCH 3/3] Replace #if with UNSUPPORTED.

We can't use XFAIL yet because the old sysroot used in the Android CI
Docker image still declares these functions even for pre-API 26. The
new sysroot we want to switch stop drops the declarations, unless the
code is targeting API 26 or newer.
---
 libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp     | 8 +++++---
 .../std/depr/depr.c.headers/wctype_h.compile.pass.cpp     | 8 +++++---
 .../std/language.support/support.runtime/cstdlib.pass.cpp | 8 +++++---
 libcxx/test/std/strings/c.strings/cwctype.pass.cpp        | 8 +++++---
 4 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp b/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
index 82083734c7a263..662331558c121b 100644
--- a/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
+++ b/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp
@@ -8,6 +8,11 @@
 
 // test <stdlib.h>
 
+// mblen was added in Android API 26.
+// TODO: Switch from UNSUPPORTED to XFAIL once the Android CI Docker sysroot is
+// updated.
+// UNSUPPORTED: LIBCXX-ANDROID-FIXME && target={{.+}}-android{{(eabi)?(21|22|23|24|25)}}
+
 #include <stdlib.h>
 #include <cassert>
 #include <type_traits>
@@ -141,10 +146,7 @@ int main(int, char**) {
     wchar_t* pw = 0;
     const wchar_t* pwc = 0;
     char* pc = 0;
-    // mblen was added in Android API 26.
-#  if !defined(__ANDROID__) || __ANDROID_API__ >= 26
     ASSERT_SAME_TYPE(int,    decltype(mblen("",0)));
-#  endif
     ASSERT_SAME_TYPE(int,    decltype(mbtowc(pw,"",0)));
     ASSERT_SAME_TYPE(int,    decltype(wctomb(pc,L' ')));
     ASSERT_SAME_TYPE(size_t, decltype(mbstowcs(pw,"",0)));
diff --git a/libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp b/libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp
index ebbec565c1ab7d..4e2fb319336f1b 100644
--- a/libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp
+++ b/libcxx/test/std/depr/depr.c.headers/wctype_h.compile.pass.cpp
@@ -8,6 +8,11 @@
 
 // UNSUPPORTED: no-wide-characters
 
+// towctrans and wctrans were added in Android API 26.
+// TODO: Switch from UNSUPPORTED to XFAIL once the Android CI Docker sysroot is
+// updated.
+// UNSUPPORTED: LIBCXX-ANDROID-FIXME && target={{.+}}-android{{(eabi)?(21|22|23|24|25)}}
+
 // <wctype.h>
 
 #include <wctype.h>
@@ -109,8 +114,5 @@ ASSERT_SAME_TYPE(int,       decltype(iswctype(w, wct)));
 ASSERT_SAME_TYPE(wctype_t,  decltype(wctype("")));
 ASSERT_SAME_TYPE(wint_t,    decltype(towlower(w)));
 ASSERT_SAME_TYPE(wint_t,    decltype(towupper(w)));
-// towctrans and wctrans were added in Android API 26.
-#if !defined(__ANDROID__) || __ANDROID_API__ >= 26
 ASSERT_SAME_TYPE(wint_t,    decltype(towctrans(w, wctr)));
 ASSERT_SAME_TYPE(wctrans_t, decltype(wctrans("")));
-#endif
diff --git a/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp b/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
index a00bf13936ec87..9d3e6d892daf0d 100644
--- a/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
+++ b/libcxx/test/std/language.support/support.runtime/cstdlib.pass.cpp
@@ -8,6 +8,11 @@
 
 // test <cstdlib>
 
+// mblen was added in Android API 26.
+// TODO: Switch from UNSUPPORTED to XFAIL once the Android CI Docker sysroot is
+// updated.
+// UNSUPPORTED: LIBCXX-ANDROID-FIXME && target={{.+}}-android{{(eabi)?(21|22|23|24|25)}}
+
 #include <cstdlib>
 #include <cassert>
 #include <type_traits>
@@ -141,10 +146,7 @@ int main(int, char**)
     wchar_t* pw = 0;
     const wchar_t* pwc = 0;
     char* pc = 0;
-    // mblen was added in Android API 26.
-#  if !defined(__ANDROID__) || __ANDROID_API__ >= 26
     static_assert((std::is_same<decltype(std::mblen("",0)), int>::value), "");
-#  endif
     static_assert((std::is_same<decltype(std::mbtowc(pw,"",0)), int>::value), "");
     static_assert((std::is_same<decltype(std::wctomb(pc,L' ')), int>::value), "");
     static_assert((std::is_same<decltype(std::mbstowcs(pw,"",0)), std::size_t>::value), "");
diff --git a/libcxx/test/std/strings/c.strings/cwctype.pass.cpp b/libcxx/test/std/strings/c.strings/cwctype.pass.cpp
index 7460c36c357e7f..0deabf51ed59cc 100644
--- a/libcxx/test/std/strings/c.strings/cwctype.pass.cpp
+++ b/libcxx/test/std/strings/c.strings/cwctype.pass.cpp
@@ -10,6 +10,11 @@
 
 // UNSUPPORTED: no-wide-characters
 
+// towctrans and wctrans were added in Android API 26.
+// TODO: Switch from UNSUPPORTED to XFAIL once the Android CI Docker sysroot is
+// updated.
+// UNSUPPORTED: LIBCXX-ANDROID-FIXME && target={{.+}}-android{{(eabi)?(21|22|23|24|25)}}
+
 #include <cwctype>
 #include <type_traits>
 
@@ -111,11 +116,8 @@ int main(int, char**) {
   ASSERT_SAME_TYPE(std::wctype_t, decltype(std::wctype("")));
   ASSERT_SAME_TYPE(std::wint_t, decltype(std::towlower(w)));
   ASSERT_SAME_TYPE(std::wint_t, decltype(std::towupper(w)));
-  // towctrans and wctrans were added in Android API 26.
-#if !defined(__ANDROID__) || __ANDROID_API__ >= 26
   ASSERT_SAME_TYPE(std::wint_t, decltype(std::towctrans(w, std::wctrans_t())));
   ASSERT_SAME_TYPE(std::wctrans_t, decltype(std::wctrans("")));
-#endif
 
   return 0;
 }



More information about the libcxx-commits mailing list