Add support for the "C-unwind" ABI (#2334)

* Add support for the `"C-unwind"` ABI

This allows using `"C-unwind"` as an ABI override if the rust target is
nightly.
This commit is contained in:
Christian Poveda Ruiz 2022-11-04 10:19:28 -05:00 committed by GitHub
parent 9c32b46048
commit 7c26cd218d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 2 deletions

View File

@ -150,6 +150,8 @@
## Added
* new feature: `--override-abi` flag to override the ABI used by functions
matching a regular expression.
* new feature: allow using the `C-unwind` ABI in `--override-abi` on nightly
rust.
## Changed

View File

@ -570,7 +570,7 @@ where
.help("Deduplicates extern blocks."),
Arg::new("override-abi")
.long("override-abi")
.help("Overrides the ABI of functions matching <regex>. The <override> value must be of the shape <abi>:<regex> where <abi> can be one of C, stdcall, fastcall, thiscall, aapcs or win64.")
.help("Overrides the ABI of functions matching <regex>. The <override> value must be of the shape <regex>=<abi> where <abi> can be one of C, stdcall, fastcall, thiscall, aapcs, win64 or C-unwind.")
.value_name("override")
.multiple_occurrences(true)
.number_of_values(1),

View File

@ -0,0 +1,18 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]
#![cfg(feature = "nightly")]
#![feature(abi_thiscall)]
extern "C-unwind" {
pub fn foo();
}
extern "C-unwind" {
pub fn bar();
}
extern "C" {
pub fn baz();
}

View File

@ -0,0 +1,5 @@
// bindgen-flags: --override-abi="foo|bar=C-unwind" --rust-target=nightly --raw-line '#![cfg(feature = "nightly")]' --raw-line '#![feature(abi_thiscall)]'
void foo();
void bar();
void baz();

View File

@ -2483,6 +2483,9 @@ impl MethodCodegen for Method {
ClangAbi::Known(Abi::Vectorcall) => {
ctx.options().rust_features().vectorcall_abi
}
ClangAbi::Known(Abi::CUnwind) => {
ctx.options().rust_features().c_unwind_abi
}
_ => true,
};
@ -4009,6 +4012,12 @@ impl TryToRustTy for FunctionSig {
warn!("Skipping function with vectorcall ABI that isn't supported by the configured Rust target");
Ok(proc_macro2::TokenStream::new())
}
ClangAbi::Known(Abi::CUnwind)
if !ctx.options().rust_features().c_unwind_abi =>
{
warn!("Skipping function with C-unwind ABI that isn't supported by the configured Rust target");
Ok(proc_macro2::TokenStream::new())
}
_ => Ok(quote! {
unsafe extern #abi fn ( #( #arguments ),* ) #ret
}),
@ -4120,6 +4129,12 @@ impl CodeGenerator for Function {
warn!("Skipping function with vectorcall ABI that isn't supported by the configured Rust target");
return None;
}
ClangAbi::Known(Abi::CUnwind)
if !ctx.options().rust_features().c_unwind_abi =>
{
warn!("Skipping function with C-unwind ABI that isn't supported by the configured Rust target");
return None;
}
ClangAbi::Known(Abi::Win64) if signature.is_variadic() => {
warn!("Skipping variadic function with Win64 ABI that isn't supported");
return None;

View File

@ -133,6 +133,7 @@ macro_rules! rust_target_base {
/// Nightly rust
/// * `thiscall` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/42202))
/// * `vectorcall` calling convention (no tracking issue)
/// * `c_unwind` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/74990))
=> Nightly => nightly;
);
}
@ -242,6 +243,7 @@ rust_feature_def!(
Nightly {
=> thiscall_abi;
=> vectorcall_abi;
=> c_unwind_abi;
}
);
@ -291,7 +293,8 @@ mod test {
f_nightly.maybe_uninit &&
f_nightly.repr_align &&
f_nightly.thiscall_abi &&
f_nightly.vectorcall_abi
f_nightly.vectorcall_abi &&
f_nightly.c_unwind_abi
);
}

View File

@ -188,6 +188,8 @@ pub enum Abi {
Aapcs,
/// The "win64" ABI.
Win64,
/// The "C-unwind" ABI.
CUnwind,
}
impl FromStr for Abi {
@ -202,6 +204,7 @@ impl FromStr for Abi {
"vectorcall" => Ok(Self::Vectorcall),
"aapcs" => Ok(Self::Aapcs),
"win64" => Ok(Self::Win64),
"C-unwind" => Ok(Self::CUnwind),
_ => Err(format!("Invalid or unknown ABI {:?}", s)),
}
}
@ -217,6 +220,7 @@ impl std::fmt::Display for Abi {
Self::Vectorcall => "vectorcall",
Self::Aapcs => "aapcs",
Self::Win64 => "win64",
Self::CUnwind => "C-unwind",
};
s.fmt(f)