From 6523ad7246fc27f0ec125ce65566368dfeb0dc6d Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 4 Nov 2022 09:34:08 -0500 Subject: [PATCH 1/4] Add support for the `"C-unwind"` ABI This allows using `"C-unwind"` as an ABI override if the rust target is nightly. --- bindgen-cli/options.rs | 2 +- .../expectations/tests/c-unwind-abi-override.rs | 16 ++++++++++++++++ .../tests/headers/c-unwind-abi-override.h | 5 +++++ bindgen/codegen/mod.rs | 15 +++++++++++++++ bindgen/features.rs | 5 ++++- bindgen/ir/function.rs | 4 ++++ 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 bindgen-tests/tests/expectations/tests/c-unwind-abi-override.rs create mode 100644 bindgen-tests/tests/headers/c-unwind-abi-override.h diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index c186cfd92c..6206fbfcb6 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -570,7 +570,7 @@ where .help("Deduplicates extern blocks."), Arg::new("override-abi") .long("override-abi") - .help("Overrides the ABI of functions matching . The value must be of the shape : where can be one of C, stdcall, fastcall, thiscall, aapcs or win64.") + .help("Overrides the ABI of functions matching . The value must be of the shape : where can be one of C, stdcall, fastcall, thiscall, aapcs, win64 or C-unwind.") .value_name("override") .multiple_occurrences(true) .number_of_values(1), diff --git a/bindgen-tests/tests/expectations/tests/c-unwind-abi-override.rs b/bindgen-tests/tests/expectations/tests/c-unwind-abi-override.rs new file mode 100644 index 0000000000..127e7950fb --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/c-unwind-abi-override.rs @@ -0,0 +1,16 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +extern "C-unwind" { + pub fn foo(); +} +extern "C-unwind" { + pub fn bar(); +} +extern "C" { + pub fn baz(); +} diff --git a/bindgen-tests/tests/headers/c-unwind-abi-override.h b/bindgen-tests/tests/headers/c-unwind-abi-override.h new file mode 100644 index 0000000000..76c7fac5da --- /dev/null +++ b/bindgen-tests/tests/headers/c-unwind-abi-override.h @@ -0,0 +1,5 @@ +// bindgen-flags: --override-abi="foo|bar=C-unwind" --rust-target=nightly + +void foo(); +void bar(); +void baz(); diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 4e90102d36..d0c191360e 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -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; diff --git a/bindgen/features.rs b/bindgen/features.rs index 4f05b9eb07..9f6535aa40 100644 --- a/bindgen/features.rs +++ b/bindgen/features.rs @@ -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 ); } diff --git a/bindgen/ir/function.rs b/bindgen/ir/function.rs index 8a70d60ac6..52346f6c88 100644 --- a/bindgen/ir/function.rs +++ b/bindgen/ir/function.rs @@ -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) From 1de90b663659080dca98644bcf7bd7676dab9fc4 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 4 Nov 2022 09:39:54 -0500 Subject: [PATCH 2/4] fix help for `--override-abi` --- bindgen-cli/options.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index 6206fbfcb6..90f38cb2f9 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -570,7 +570,7 @@ where .help("Deduplicates extern blocks."), Arg::new("override-abi") .long("override-abi") - .help("Overrides the ABI of functions matching . The value must be of the shape : where can be one of C, stdcall, fastcall, thiscall, aapcs, win64 or C-unwind.") + .help("Overrides the ABI of functions matching . The value must be of the shape = where can be one of C, stdcall, fastcall, thiscall, aapcs, win64 or C-unwind.") .value_name("override") .multiple_occurrences(true) .number_of_values(1), From ed8b68ca0d2f60ea80a1bb6f826a86c52fcc40e2 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 4 Nov 2022 10:09:24 -0500 Subject: [PATCH 3/4] do c-unwind tests in nightly --- ...-unwind-abi-override.rs => c-unwind-abi-override-nightly.rs} | 2 ++ ...{c-unwind-abi-override.h => c-unwind-abi-override-nightly.h} | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) rename bindgen-tests/tests/expectations/tests/{c-unwind-abi-override.rs => c-unwind-abi-override-nightly.rs} (79%) rename bindgen-tests/tests/headers/{c-unwind-abi-override.h => c-unwind-abi-override-nightly.h} (52%) diff --git a/bindgen-tests/tests/expectations/tests/c-unwind-abi-override.rs b/bindgen-tests/tests/expectations/tests/c-unwind-abi-override-nightly.rs similarity index 79% rename from bindgen-tests/tests/expectations/tests/c-unwind-abi-override.rs rename to bindgen-tests/tests/expectations/tests/c-unwind-abi-override-nightly.rs index 127e7950fb..6b34192fe4 100644 --- a/bindgen-tests/tests/expectations/tests/c-unwind-abi-override.rs +++ b/bindgen-tests/tests/expectations/tests/c-unwind-abi-override-nightly.rs @@ -4,6 +4,8 @@ non_camel_case_types, non_upper_case_globals )] +#![cfg(feature = "nightly")] +#![feature(abi_thiscall)] extern "C-unwind" { pub fn foo(); diff --git a/bindgen-tests/tests/headers/c-unwind-abi-override.h b/bindgen-tests/tests/headers/c-unwind-abi-override-nightly.h similarity index 52% rename from bindgen-tests/tests/headers/c-unwind-abi-override.h rename to bindgen-tests/tests/headers/c-unwind-abi-override-nightly.h index 76c7fac5da..ac79a420c0 100644 --- a/bindgen-tests/tests/headers/c-unwind-abi-override.h +++ b/bindgen-tests/tests/headers/c-unwind-abi-override-nightly.h @@ -1,4 +1,4 @@ -// bindgen-flags: --override-abi="foo|bar=C-unwind" --rust-target=nightly +// 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(); From 37a53d0eee29fda5fb83901d7c7e84790ba824f1 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 4 Nov 2022 10:19:04 -0500 Subject: [PATCH 4/4] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index baa5cb204e..5f559a3fe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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