Skip to content

Commit 4f8de62

Browse files
committed
Stop Rust from prepending underscore before '?' for win32
1 parent 75c5875 commit 4f8de62

13 files changed

+165
-7
lines changed

src/ir/function.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,14 @@ fn get_abi(cc: CXCallingConv) -> Abi {
130130

131131
fn mangling_hack_if_needed(ctx: &BindgenContext, symbol: &mut String) {
132132
if ctx.needs_mangling_hack() {
133-
symbol.remove(0);
133+
match symbol.chars().next().unwrap() {
134+
// Stripping leading underscore for all names on Darwin and
135+
// C linkage functions on Win32.
136+
'_' => { symbol.remove(0); }
137+
// Stop Rust from prepending underscore for variables on Win32.
138+
'?' => { symbol.insert(0, '\x01'); }
139+
_ => {}
140+
}
134141
}
135142
}
136143

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
extern "C" {
8+
pub fn foo();
9+
}
10+
#[repr(C)]
11+
#[derive(Debug, Default, Copy)]
12+
pub struct Foo {
13+
pub _address: u8,
14+
}
15+
extern "C" {
16+
#[link_name = "_ZN3Foo4sBarE"]
17+
pub static mut Foo_sBar: bool;
18+
}
19+
#[test]
20+
fn bindgen_test_layout_Foo() {
21+
assert_eq!(::std::mem::size_of::<Foo>() , 1usize , concat ! (
22+
"Size of: " , stringify ! ( Foo ) ));
23+
assert_eq! (::std::mem::align_of::<Foo>() , 1usize , concat ! (
24+
"Alignment of " , stringify ! ( Foo ) ));
25+
}
26+
impl Clone for Foo {
27+
fn clone(&self) -> Self { *self }
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
extern "C" {
8+
pub fn foo();
9+
}
10+
#[repr(C)]
11+
#[derive(Debug, Default, Copy)]
12+
pub struct Foo {
13+
pub _address: u8,
14+
}
15+
extern "C" {
16+
#[link_name = "_ZN3Foo4sBarE"]
17+
pub static mut Foo_sBar: bool;
18+
}
19+
#[test]
20+
fn bindgen_test_layout_Foo() {
21+
assert_eq!(::std::mem::size_of::<Foo>() , 1usize , concat ! (
22+
"Size of: " , stringify ! ( Foo ) ));
23+
assert_eq! (::std::mem::align_of::<Foo>() , 1usize , concat ! (
24+
"Alignment of " , stringify ! ( Foo ) ));
25+
}
26+
impl Clone for Foo {
27+
fn clone(&self) -> Self { *self }
28+
}

tests/expectations/tests/mangling-macos.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,22 @@
77
extern "C" {
88
pub fn foo();
99
}
10+
#[repr(C)]
11+
#[derive(Debug, Default, Copy)]
12+
pub struct Foo {
13+
pub _address: u8,
14+
}
15+
extern "C" {
16+
#[link_name = "_ZN3Foo4sBarE"]
17+
pub static mut Foo_sBar: bool;
18+
}
19+
#[test]
20+
fn bindgen_test_layout_Foo() {
21+
assert_eq!(::std::mem::size_of::<Foo>() , 1usize , concat ! (
22+
"Size of: " , stringify ! ( Foo ) ));
23+
assert_eq! (::std::mem::align_of::<Foo>() , 1usize , concat ! (
24+
"Alignment of " , stringify ! ( Foo ) ));
25+
}
26+
impl Clone for Foo {
27+
fn clone(&self) -> Self { *self }
28+
}

tests/expectations/tests/mangling-win32.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,22 @@
77
extern "C" {
88
pub fn foo();
99
}
10+
#[repr(C)]
11+
#[derive(Debug, Default, Copy)]
12+
pub struct Foo {
13+
pub _address: u8,
14+
}
15+
extern "C" {
16+
#[link_name = "\u{1}?sBar@Foo@@2_NA"]
17+
pub static mut Foo_sBar: bool;
18+
}
19+
#[test]
20+
fn bindgen_test_layout_Foo() {
21+
assert_eq!(::std::mem::size_of::<Foo>() , 1usize , concat ! (
22+
"Size of: " , stringify ! ( Foo ) ));
23+
assert_eq! (::std::mem::align_of::<Foo>() , 1usize , concat ! (
24+
"Alignment of " , stringify ! ( Foo ) ));
25+
}
26+
impl Clone for Foo {
27+
fn clone(&self) -> Self { *self }
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
extern "C" {
8+
pub fn foo();
9+
}
10+
#[repr(C)]
11+
#[derive(Debug, Default, Copy)]
12+
pub struct Foo {
13+
pub _address: u8,
14+
}
15+
extern "C" {
16+
#[link_name = "?sBar@Foo@@2_NA"]
17+
pub static mut Foo_sBar: bool;
18+
}
19+
#[test]
20+
fn bindgen_test_layout_Foo() {
21+
assert_eq!(::std::mem::size_of::<Foo>() , 1usize , concat ! (
22+
"Size of: " , stringify ! ( Foo ) ));
23+
assert_eq! (::std::mem::align_of::<Foo>() , 1usize , concat ! (
24+
"Alignment of " , stringify ! ( Foo ) ));
25+
}
26+
impl Clone for Foo {
27+
fn clone(&self) -> Self { *self }
28+
}

tests/headers/mangling-linux32.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: -- --target=i586-unknown-linux
2+
3+
extern "C" void foo();
4+
5+
struct Foo {
6+
static bool sBar;
7+
};

tests/headers/mangling-linux64.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: -- --target=x86_64-unknown-linux
2+
3+
extern "C" void foo();
4+
5+
struct Foo {
6+
static bool sBar;
7+
};

tests/headers/mangling-macos.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/headers/mangling-macos.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: -- --target=x86_64-apple-darwin
2+
3+
extern "C" void foo();
4+
5+
struct Foo {
6+
static bool sBar;
7+
};

tests/headers/mangling-win32.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/headers/mangling-win32.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: -- --target=i686-pc-win32
2+
3+
extern "C" void foo();
4+
5+
struct Foo {
6+
static bool sBar;
7+
};

tests/headers/mangling-win64.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: -- --target=x86_64-pc-win32
2+
3+
extern "C" void foo();
4+
5+
struct Foo {
6+
static bool sBar;
7+
};

0 commit comments

Comments
 (0)