Skip to content

Commit 71ea973

Browse files
authored
Merge pull request rust-lang#5 from martinboehme/enum-and-typedef-original-name
Emit `bindgen_original_name` attribute also for enums and typedefs
2 parents e8f6cd3 + 201ed3c commit 71ea973

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+126
-2
lines changed

src/codegen/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,16 @@ impl CodeGenerator for Type {
914914
return;
915915
}
916916

917+
let mut attributes = Vec::new();
918+
if let Some(original_name) = item.original_name(ctx) {
919+
if name != original_name {
920+
attributes.push(attributes::original_name(&original_name));
921+
}
922+
}
923+
917924
tokens.append_all(match alias_style {
918925
AliasVariation::TypeAlias => quote! {
926+
#( #attributes )*
919927
pub type #rust_name
920928
},
921929
AliasVariation::NewType | AliasVariation::NewTypeDeref => {
@@ -925,8 +933,7 @@ impl CodeGenerator for Type {
925933
alias_style
926934
);
927935

928-
let mut attributes =
929-
vec![attributes::repr("transparent")];
936+
attributes.push(attributes::repr("transparent"));
930937
let derivable_traits = derives_of_item(item, ctx);
931938
if !derivable_traits.is_empty() {
932939
let derives: Vec<_> = derivable_traits.into();
@@ -2940,6 +2947,12 @@ impl CodeGenerator for Enum {
29402947

29412948
let mut attrs = vec![];
29422949

2950+
if let Some(original_name) = item.original_name(ctx) {
2951+
if name != original_name {
2952+
attrs.push(attributes::original_name(&original_name));
2953+
}
2954+
}
2955+
29432956
// TODO(emilio): Delegate this to the builders?
29442957
match variation {
29452958
EnumVariation::Rust { non_exhaustive } => {

tests/expectations/tests/381-decltype-alias.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
pub struct std_allocator_traits {
1212
pub _address: u8,
1313
}
14+
#[bindgen_original_name("allocator_traits::__size_type")]
1415
pub type std_allocator_traits___size_type<_Alloc> = _Alloc;

tests/expectations/tests/anon_enum_trait.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
pub struct DataType {
1111
pub _address: u8,
1212
}
13+
#[bindgen_original_name("DataType::value_type")]
1314
pub type DataType_value_type<_Tp> = _Tp;
15+
#[bindgen_original_name("DataType::work_type")]
1416
pub type DataType_work_type<_Tp> = DataType_value_type<_Tp>;
17+
#[bindgen_original_name("DataType::channel_type")]
1518
pub type DataType_channel_type<_Tp> = DataType_value_type<_Tp>;
19+
#[bindgen_original_name("DataType::vec_type")]
1620
pub type DataType_vec_type<_Tp> = DataType_value_type<_Tp>;
1721
pub const DataType_generic_type: DataType__bindgen_ty_1 =
1822
DataType__bindgen_ty_1::generic_type;

tests/expectations/tests/anon_union.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl TErrorResult_UnionState {
1717
TErrorResult_UnionState::HasMessage;
1818
}
1919
#[repr(i32)]
20+
#[bindgen_original_name("TErrorResult::UnionState")]
2021
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2122
pub enum TErrorResult_UnionState {
2223
HasMessage = 0,

tests/expectations/tests/anon_union_1_0.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub struct TErrorResult {
5959
pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
6060
TErrorResult_UnionState::HasMessage;
6161
#[repr(i32)]
62+
#[bindgen_original_name("TErrorResult::UnionState")]
6263
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
6364
pub enum TErrorResult_UnionState {
6465
HasMessage = 0,

tests/expectations/tests/builtin-template.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
non_upper_case_globals
66
)]
77

8+
#[bindgen_original_name("make_integer_sequence")]
89
pub type std_make_integer_sequence = u8;

tests/expectations/tests/call-conv-typedef.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![cfg(not(test))]
88

99
pub type void_fn = ::std::option::Option<unsafe extern "stdcall" fn()>;
10+
#[bindgen_original_name("fn")]
1011
pub type fn_ = ::std::option::Option<
1112
unsafe extern "stdcall" fn(id: ::std::os::raw::c_int) -> void_fn,
1213
>;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
#[repr(C)]
9+
#[derive(Debug, Default, Copy, Clone)]
10+
pub struct A {
11+
pub _address: u8,
12+
}
13+
pub const A_B_B1: A_B = 0;
14+
pub const A_B_B2: A_B = 1;
15+
#[bindgen_original_name("A::B")]
16+
pub type A_B = ::std::os::raw::c_uint;
17+
#[test]
18+
fn bindgen_test_layout_A() {
19+
assert_eq!(
20+
::std::mem::size_of::<A>(),
21+
1usize,
22+
concat!("Size of: ", stringify!(A))
23+
);
24+
assert_eq!(
25+
::std::mem::align_of::<A>(),
26+
1usize,
27+
concat!("Alignment of ", stringify!(A))
28+
);
29+
}

tests/expectations/tests/class_with_typedef.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ pub struct C {
1515
pub d: AnotherInt,
1616
pub other_ptr: *mut AnotherInt,
1717
}
18+
#[bindgen_original_name("C::MyInt")]
1819
pub type C_MyInt = ::std::os::raw::c_int;
20+
#[bindgen_original_name("C::Lookup")]
1921
pub type C_Lookup = *const ::std::os::raw::c_char;
2022
#[test]
2123
fn bindgen_test_layout_C() {

tests/expectations/tests/constify-module-enums-simple-nonamespace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)]
77

88
pub mod one_Foo {
9+
#[bindgen_original_name("Foo")]
910
pub type Type = ::std::os::raw::c_int;
1011
pub const Variant1: Type = 0;
1112
pub const Variant2: Type = 1;

tests/expectations/tests/constify-module-enums-types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ pub mod anon_enum {
2020
pub const Variant3: Type = 2;
2121
}
2222
pub mod ns1_foo {
23+
#[bindgen_original_name("foo")]
2324
pub type Type = ::std::os::raw::c_uint;
2425
pub const THIS: Type = 0;
2526
pub const SHOULD_BE: Type = 1;
2627
pub const A_CONSTANT: Type = 2;
2728
pub const ALSO_THIS: Type = 42;
2829
}
2930
pub mod ns2_Foo {
31+
#[bindgen_original_name("Foo")]
3032
pub type Type = ::std::os::raw::c_int;
3133
pub const Variant1: Type = 0;
3234
pub const Variant2: Type = 1;
@@ -205,6 +207,7 @@ impl Default for Baz {
205207
}
206208
}
207209
pub mod one_Foo {
210+
#[bindgen_original_name("Foo")]
208211
pub type Type = ::std::os::raw::c_int;
209212
pub const Variant1: Type = 0;
210213
pub const Variant2: Type = 1;

tests/expectations/tests/derive-debug-function-pointer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct Nice {
1111
pub pointer: Nice_Function,
1212
pub large_array: [::std::os::raw::c_int; 34usize],
1313
}
14+
#[bindgen_original_name("Nice::Function")]
1415
pub type Nice_Function =
1516
::std::option::Option<unsafe extern "C" fn(data: ::std::os::raw::c_int)>;
1617
#[test]

tests/expectations/tests/elaborated.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
non_upper_case_globals
66
)]
77

8+
#[bindgen_original_name("whatever_t")]
89
pub type whatever_whatever_t = ::std::os::raw::c_int;
910
extern "C" {
1011
#[link_name = "\u{1}_Z9somethingPKi"]

tests/expectations/tests/enum_in_template.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ pub struct Foo {
1212
}
1313
pub const Foo_Bar_A: Foo_Bar = 0;
1414
pub const Foo_Bar_B: Foo_Bar = 0;
15+
#[bindgen_original_name("Foo::Bar")]
1516
pub type Foo_Bar = i32;

tests/expectations/tests/enum_in_template_with_typedef.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
pub struct std_fbstring_core {
1212
pub _address: u8,
1313
}
14+
#[bindgen_original_name("fbstring_core::category_type")]
1415
pub type std_fbstring_core_category_type = u8;
1516
impl std_fbstring_core_Category {
1617
pub const Bar: std_fbstring_core_Category = std_fbstring_core_Category::Foo;
1718
}
1819
#[repr(u8)]
20+
#[bindgen_original_name("fbstring_core::Category")]
1921
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2022
pub enum std_fbstring_core_Category {
2123
Foo = 0,

tests/expectations/tests/eval-value-dependent.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
pub struct e {
1111
pub _address: u8,
1212
}
13+
#[bindgen_original_name("e::f")]
1314
pub type e_f<d> = d;

tests/expectations/tests/in_class_typedef.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
pub struct Foo {
1111
pub _address: u8,
1212
}
13+
#[bindgen_original_name("Foo::elem_type")]
1314
pub type Foo_elem_type<T> = T;
15+
#[bindgen_original_name("Foo::ptr_type")]
1416
pub type Foo_ptr_type<T> = *mut T;
1517
#[repr(C)]
1618
#[derive(Debug, Default, Copy, Clone)]

tests/expectations/tests/issue-1113-template-references.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl<K, V> Default for Entry<K, V> {
2323
pub struct nsBaseHashtable {
2424
pub _address: u8,
2525
}
26+
#[bindgen_original_name("nsBaseHashtable::EntryType")]
2627
pub type nsBaseHashtable_EntryType<K, V> = Entry<K, V>;
2728
#[repr(C)]
2829
#[derive(Debug, Copy, Clone)]

tests/expectations/tests/issue-1382-rust-primitive-types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
non_upper_case_globals
66
)]
77

8+
#[bindgen_original_name("i8")]
89
pub type i8_ = i8;
10+
#[bindgen_original_name("u8")]
911
pub type u8_ = u8;
12+
#[bindgen_original_name("i16")]
1013
pub type i16_ = i16;
14+
#[bindgen_original_name("u16")]
1115
pub type u16_ = u16;
16+
#[bindgen_original_name("i32")]
1217
pub type i32_ = i32;
18+
#[bindgen_original_name("u32")]
1319
pub type u32_ = u32;
20+
#[bindgen_original_name("i64")]
1421
pub type i64_ = i64;
22+
#[bindgen_original_name("u64")]
1523
pub type u64_ = u64;
1624
#[repr(C)]
1725
#[derive(Debug, Default, Copy, Clone)]

tests/expectations/tests/issue-493.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ impl<T> ::std::cmp::Eq for __BindgenUnionField<T> {}
5353
pub struct basic_string {
5454
pub _address: u8,
5555
}
56+
#[bindgen_original_name("basic_string::size_type")]
5657
pub type basic_string_size_type = ::std::os::raw::c_ulonglong;
58+
#[bindgen_original_name("basic_string::value_type")]
5759
pub type basic_string_value_type = ::std::os::raw::c_char;
60+
#[bindgen_original_name("basic_string::pointer")]
5861
pub type basic_string_pointer = *mut basic_string_value_type;
5962
#[repr(C)]
6063
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]

tests/expectations/tests/issue-493_1_0.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ impl<T> ::std::cmp::Eq for __BindgenUnionField<T> {}
5353
pub struct basic_string {
5454
pub _address: u8,
5555
}
56+
#[bindgen_original_name("basic_string::size_type")]
5657
pub type basic_string_size_type = ::std::os::raw::c_ulonglong;
58+
#[bindgen_original_name("basic_string::value_type")]
5759
pub type basic_string_value_type = ::std::os::raw::c_char;
60+
#[bindgen_original_name("basic_string::pointer")]
5861
pub type basic_string_pointer = *mut basic_string_value_type;
5962
#[repr(C)]
6063
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]

tests/expectations/tests/issue-544-stylo-creduce-2.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
pub struct Foo {
1010
pub member: Foo_SecondAlias,
1111
}
12+
#[bindgen_original_name("Foo::FirstAlias")]
1213
pub type Foo_FirstAlias = [u8; 0usize];
14+
#[bindgen_original_name("Foo::SecondAlias")]
1315
pub type Foo_SecondAlias = [u8; 0usize];
1416
impl Default for Foo {
1517
fn default() -> Self {

tests/expectations/tests/issue-569-non-type-template-params-causing-layout-test-failures.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum _bindgen_ty_1 {
1313
ENUM_VARIANT_1 = 0,
1414
ENUM_VARIANT_2 = 1,
1515
}
16+
#[bindgen_original_name("Alias")]
1617
pub type JS_Alias = u8;
1718
#[repr(C)]
1819
#[derive(Debug, Copy, Clone)]

tests/expectations/tests/issue-584-stylo-template-analysis-panic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub type RefPtr<T> = T;
1212
pub struct A {
1313
pub _address: u8,
1414
}
15+
#[bindgen_original_name("A::a")]
1516
pub type A_a = b;
1617
#[test]
1718
fn bindgen_test_layout_A() {

tests/expectations/tests/issue-638-stylo-cannot-find-T-in-this-scope.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct UsesRefPtrWithAliasedTypeParam<U> {
2222
pub member: RefPtr<UsesRefPtrWithAliasedTypeParam_V<U>>,
2323
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
2424
}
25+
#[bindgen_original_name("UsesRefPtrWithAliasedTypeParam::V")]
2526
pub type UsesRefPtrWithAliasedTypeParam_V<U> = U;
2627
impl<U> Default for UsesRefPtrWithAliasedTypeParam<U> {
2728
fn default() -> Self {

tests/expectations/tests/issue-645-cannot-find-type-T-in-this-scope.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct HasRefPtr<T> {
1313
pub refptr_member: RefPtr<HasRefPtr_TypedefOfT<T>>,
1414
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
1515
}
16+
#[bindgen_original_name("HasRefPtr::TypedefOfT")]
1617
pub type HasRefPtr_TypedefOfT<T> = T;
1718
impl<T> Default for HasRefPtr<T> {
1819
fn default() -> Self {

tests/expectations/tests/issue-674-1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod root {
1717
pub struct Maybe {
1818
pub _address: u8,
1919
}
20+
#[bindgen_original_name("Maybe::ValueType")]
2021
pub type Maybe_ValueType<T> = T;
2122
}
2223
#[repr(C)]

tests/expectations/tests/issue-674-2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod root {
1717
pub struct Rooted {
1818
pub _address: u8,
1919
}
20+
#[bindgen_original_name("Rooted::ElementType")]
2021
pub type Rooted_ElementType<T> = T;
2122
}
2223
#[repr(C)]

tests/expectations/tests/issue-674-3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod root {
1414
pub struct nsRefPtrHashtable {
1515
pub _address: u8,
1616
}
17+
#[bindgen_original_name("nsRefPtrHashtable::UserDataType")]
1718
pub type nsRefPtrHashtable_UserDataType<PtrType> = *mut PtrType;
1819
#[repr(C)]
1920
#[derive(Debug, Default, Copy, Clone)]

tests/expectations/tests/issue-820-unused-template-param-in-alias.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
non_upper_case_globals
66
)]
77

8+
#[bindgen_original_name("Foo::self_type")]
89
pub type Foo_self_type = u8;

tests/expectations/tests/libclang-9/issue-769-bad-instantiation-test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod root {
2020
unsafe { ::std::mem::zeroed() }
2121
}
2222
}
23+
#[bindgen_original_name("AutoValueVector::Alias")]
2324
pub type AutoValueVector_Alias = ::std::os::raw::c_int;
2425
#[test]
2526
fn __bindgen_test_layout_Rooted_open0_int_close0_instantiation() {

tests/expectations/tests/nsBaseHashtable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ pub struct nsTHashtable {
2121
pub struct nsBaseHashtable {
2222
pub _address: u8,
2323
}
24+
#[bindgen_original_name("nsBaseHashtable::KeyType")]
2425
pub type nsBaseHashtable_KeyType = [u8; 0usize];
2526
#[bindgen_unused_template_param]
27+
#[bindgen_original_name("nsBaseHashtable::EntryType")]
2628
pub type nsBaseHashtable_EntryType = nsBaseHashtableET;
2729
#[repr(C)]
2830
#[derive(Debug, Copy, Clone)]

tests/expectations/tests/nsStyleAutoArray.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct nsStyleAutoArray<T> {
2424
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
2525
}
2626
#[repr(i32)]
27+
#[bindgen_original_name("nsStyleAutoArray::WithSingleInitialElement")]
2728
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2829
pub enum nsStyleAutoArray_WithSingleInitialElement {
2930
WITH_SINGLE_INITIAL_ELEMENT = 0,

tests/expectations/tests/replace_template_alias.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/// But the replacement type does use T!
99
///
1010
/// <div rustbindgen replaces="JS::detail::MaybeWrapped" />
11+
#[bindgen_original_name("MaybeWrapped")]
1112
pub type JS_detail_MaybeWrapped<T> = T;
1213
#[repr(C)]
1314
#[derive(Debug, Copy, Clone)]

tests/expectations/tests/replaces_double.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl<T> Default for Wrapper_Wrapped<T> {
1717
unsafe { ::std::mem::zeroed() }
1818
}
1919
}
20+
#[bindgen_original_name("Wrapper::Type")]
2021
pub type Wrapper_Type<T> = Wrapper_Wrapped<T>;
2122
#[repr(C)]
2223
#[derive(Debug, Copy, Clone)]
@@ -25,6 +26,7 @@ pub struct Rooted<T> {
2526
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
2627
}
2728
/// <div rustbindgen replaces="Rooted_MaybeWrapped"></div>
29+
#[bindgen_original_name("Rooted::Rooted_MaybeWrapped")]
2830
pub type Rooted_MaybeWrapped<T> = T;
2931
impl<T> Default for Rooted<T> {
3032
fn default() -> Self {

tests/expectations/tests/struct_with_typedef_template_arg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
pub struct Proxy {
1111
pub _address: u8,
1212
}
13+
#[bindgen_original_name("Proxy::foo")]
1314
pub type Proxy_foo<T> =
1415
::std::option::Option<unsafe extern "C" fn(bar: *mut T)>;

0 commit comments

Comments
 (0)