Skip to content

Commit 752b7f0

Browse files
committed
Let LLVM mangle the link name of static wrappers
1 parent 9f90b32 commit 752b7f0

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
int foo__extern(void) asm("foo__extern");
21
int foo__extern(void) { return foo(); }
3-
int bar__extern(void) asm("bar__extern");
42
int bar__extern(void) { return bar(); }
5-
int takes_ptr__extern(int *arg) asm("takes_ptr__extern");
63
int takes_ptr__extern(int *arg) { return takes_ptr(arg); }
7-
int takes_fn_ptr__extern(int (*f) (int)) asm("takes_fn_ptr__extern");
84
int takes_fn_ptr__extern(int (*f) (int)) { return takes_fn_ptr(f); }
9-
int takes_fn__extern(int (f) (int)) asm("takes_fn__extern");
105
int takes_fn__extern(int (f) (int)) { return takes_fn(f); }
11-
int takes_alias__extern(func f) asm("takes_alias__extern");
126
int takes_alias__extern(func f) { return takes_alias(f); }
13-
int takes_qualified__extern(const int *const *arg) asm("takes_qualified__extern");
147
int takes_qualified__extern(const int *const *arg) { return takes_qualified(arg); }
15-
enum foo takes_enum__extern(const enum foo f) asm("takes_enum__extern");
168
enum foo takes_enum__extern(const enum foo f) { return takes_enum(f); }

bindgen-tests/tests/expectations/tests/wrap-static-fns.rs

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen/codegen/helpers.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use quote::TokenStreamExt;
77

88
pub(crate) mod attributes {
99
use proc_macro2::{Ident, Span, TokenStream};
10-
use std::str::FromStr;
10+
use std::{borrow::Cow, str::FromStr};
1111

1212
pub(crate) fn repr(which: &str) -> TokenStream {
1313
let which = Ident::new(which, Span::call_site());
@@ -62,10 +62,15 @@ pub(crate) mod attributes {
6262
}
6363
}
6464

65-
pub(crate) fn link_name(name: &str) -> TokenStream {
65+
pub(crate) fn link_name<const MANGLE: bool>(name: &str) -> TokenStream {
6666
// LLVM mangles the name by default but it's already mangled.
6767
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
68-
let name = format!("\u{1}{}", name);
68+
let name: Cow<'_, str> = if MANGLE {
69+
name.into()
70+
} else {
71+
format!("\u{1}{}", name).into()
72+
};
73+
6974
quote! {
7075
#[link_name = #name]
7176
}
@@ -151,8 +156,8 @@ pub(crate) mod ast_ty {
151156
}
152157
}
153158
None => {
154-
if ctx.options().use_core &&
155-
ctx.options().rust_features.core_ffi_c_void
159+
if ctx.options().use_core
160+
&& ctx.options().rust_features.core_ffi_c_void
156161
{
157162
quote! { ::core::ffi::c_void }
158163
} else {
@@ -172,8 +177,8 @@ pub(crate) mod ast_ty {
172177
}
173178
}
174179
None => {
175-
if ctx.options().use_core &&
176-
ctx.options().rust_features().core_ffi_c
180+
if ctx.options().use_core
181+
&& ctx.options().rust_features().core_ffi_c
177182
{
178183
quote! {
179184
::core::ffi::#ident

bindgen/codegen/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ impl CodeGenerator for Var {
751751
link_name,
752752
None,
753753
) {
754-
attrs.push(attributes::link_name(link_name));
754+
attrs.push(attributes::link_name::<false>(link_name));
755755
}
756756

757757
let maybe_mut = if self.is_const() {
@@ -4155,7 +4155,7 @@ impl CodeGenerator for Function {
41554155
Some(abi),
41564156
)
41574157
{
4158-
attributes.push(attributes::link_name(link_name));
4158+
attributes.push(attributes::link_name::<false>(link_name));
41594159
has_link_name_attr = true;
41604160
}
41614161

@@ -4169,7 +4169,7 @@ impl CodeGenerator for Function {
41694169

41704170
if is_internal && ctx.options().wrap_static_fns && !has_link_name_attr {
41714171
let name = canonical_name.clone() + ctx.wrap_static_fns_suffix();
4172-
attributes.push(attributes::link_name(&name));
4172+
attributes.push(attributes::link_name::<true>(&name));
41734173
}
41744174

41754175
let ident = ctx.rust_ident(canonical_name);

bindgen/codegen/serialize.rs

-6
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ impl<'a> CSerialize<'a> for Function {
105105
// The function's return type
106106
let ret_ty = signature.return_type();
107107

108-
// Write `ret_ty wrap_name(args) asm("wrap_name");`
109-
ret_ty.serialize(ctx, (), stack, writer)?;
110-
write!(writer, " {}(", wrap_name)?;
111-
serialize_args(&args, ctx, writer)?;
112-
writeln!(writer, ") asm(\"{}\");", wrap_name)?;
113-
114108
// Write `ret_ty wrap_name(args) { return name(arg_names)' }`
115109
ret_ty.serialize(ctx, (), stack, writer)?;
116110
write!(writer, " {}(", wrap_name)?;

0 commit comments

Comments
 (0)