about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Richardson <alexrichardson@google.com>2024-11-12 17:27:52 -0800
committerAlex Richardson <alexrichardson@google.com>2024-12-10 08:33:29 -0800
commit028ca8e6160bc193cf6e119b9f22c7f81ff5dbde (patch)
tree812e35e8fcb19cb1f38aea1ce849db76f7e36d22
parent33c245b9e98bc91e18ea1c5033824f4c6f92766f (diff)
downloadrust-028ca8e6160bc193cf6e119b9f22c7f81ff5dbde.tar.gz
rust-028ca8e6160bc193cf6e119b9f22c7f81ff5dbde.zip
De-duplicate and improve definition of core::ffi::c_char
Instead of having a list of unsigned char targets for each OS, follow the
logic Clang uses and instead set the value based on architecture with
a special case for Darwin and Windows operating systems. This makes it
easier to support new operating systems targeting Arm/AArch64 without
having to modify this config statement for each new OS. The new list does
not quite match Clang since I noticed a few bugs in the Clang
implementation (https://github.com/llvm/llvm-project/issues/115957).

Fixes: https://github.com/rust-lang/rust/issues/129945
-rw-r--r--library/core/src/ffi/mod.rs77
1 files changed, 24 insertions, 53 deletions
diff --git a/library/core/src/ffi/mod.rs b/library/core/src/ffi/mod.rs
index dc107c5d22c..a9be4ebea4e 100644
--- a/library/core/src/ffi/mod.rs
+++ b/library/core/src/ffi/mod.rs
@@ -91,59 +91,30 @@ pub type c_ssize_t = isize;
 
 mod c_char_definition {
     cfg_if! {
-        // These are the targets on which c_char is unsigned.
-        if #[cfg(any(
-            all(
-                target_os = "linux",
-                any(
-                    target_arch = "aarch64",
-                    target_arch = "arm",
-                    target_arch = "hexagon",
-                    target_arch = "powerpc",
-                    target_arch = "powerpc64",
-                    target_arch = "s390x",
-                    target_arch = "riscv64",
-                    target_arch = "riscv32",
-                    target_arch = "csky"
-                )
-            ),
-            all(target_os = "android", any(target_arch = "aarch64", target_arch = "arm")),
-            all(target_os = "l4re", target_arch = "x86_64"),
-            all(
-                any(target_os = "freebsd", target_os = "openbsd", target_os = "rtems"),
-                any(
-                    target_arch = "aarch64",
-                    target_arch = "arm",
-                    target_arch = "powerpc",
-                    target_arch = "powerpc64",
-                    target_arch = "riscv64"
-                )
-            ),
-            all(
-                target_os = "netbsd",
-                any(
-                    target_arch = "aarch64",
-                    target_arch = "arm",
-                    target_arch = "powerpc",
-                    target_arch = "riscv64"
-                )
-            ),
-            all(
-                target_os = "vxworks",
-                any(
-                    target_arch = "aarch64",
-                    target_arch = "arm",
-                    target_arch = "powerpc64",
-                    target_arch = "powerpc"
-                )
-            ),
-            all(
-                target_os = "fuchsia",
-                any(target_arch = "aarch64", target_arch = "riscv64")
-            ),
-            all(target_os = "nto", target_arch = "aarch64"),
-            target_os = "horizon",
-            target_os = "aix",
+        // These are the targets on which c_char is unsigned. Usually the
+        // signedness is the same for all target_os values on a given
+        // architecture but there are some exceptions (see isSignedCharDefault()
+        // in clang/lib/Driver/ToolChains/Clang.cpp):
+        // - PowerPC uses unsigned char for all targets except Darwin
+        // - Arm/AArch64 uses unsigned char except for Darwin and Windows
+        // - L4RE builds with -funsigned-char on all targets
+        if #[cfg(all(
+            not(windows),
+            not(target_vendor = "apple"),
+            any(
+                target_arch = "aarch64",
+                target_arch = "arm",
+                target_arch = "csky",
+                target_arch = "hexagon",
+                target_arch = "msp430",
+                target_arch = "powerpc",
+                target_arch = "powerpc64",
+                target_arch = "riscv64",
+                target_arch = "riscv32",
+                target_arch = "s390x",
+                target_arch = "xtensa",
+                target_os = "l4re",
+            )
         ))] {
             pub type c_char = u8;
         } else {