Skip to content

Commit

Permalink
De-duplicate and improve definition of core::ffi::c_char
Browse files Browse the repository at this point in the history
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 (llvm/llvm-project#115957).

Fixes: #129945
  • Loading branch information
arichardson committed Nov 15, 2024
1 parent 76fd471 commit 5e82b5e
Showing 1 changed file with 24 additions and 53 deletions.
77 changes: 24 additions & 53 deletions library/core/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5e82b5e

Please sign in to comment.