Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions bindgen-tests/tests/headers/c-unwind-abi-override-nightly.h
Original file line number Diff line number Diff line change
@@ -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();
15 changes: 15 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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
}),
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion bindgen/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
);
}
Expand Down Expand Up @@ -242,6 +243,7 @@ rust_feature_def!(
Nightly {
=> thiscall_abi;
=> vectorcall_abi;
=> c_unwind_abi;
}
);

Expand Down Expand Up @@ -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
);
}

Expand Down
4 changes: 4 additions & 0 deletions bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ pub enum Abi {
Aapcs,
/// The "win64" ABI.
Win64,
/// The "C-unwind" ABI.
CUnwind,
}

impl FromStr for Abi {
Expand All @@ -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)),
}
}
Expand All @@ -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)
Expand Down