Skip to content

Commit e8f6cd3

Browse files
authored
Merge pull request rust-lang#4 from martinboehme/original-name-anonymous-struct
Don't emit `bindgen_original_name` attribute for anonymous structs
2 parents c66a558 + dbc8c2c commit e8f6cd3

Some content is hidden

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

45 files changed

+106
-15
lines changed

src/codegen/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,9 +2008,10 @@ impl CodeGenerator for CompInfo {
20082008
attributes.push(attributes::derives(&derives))
20092009
}
20102010

2011-
let original_name = item.original_name(ctx);
2012-
if canonical_name != original_name {
2013-
attributes.push(attributes::original_name(&original_name));
2011+
if let Some(original_name) = item.original_name(ctx) {
2012+
if canonical_name != original_name {
2013+
attributes.push(attributes::original_name(&original_name));
2014+
}
20142015
}
20152016

20162017
let mut tokens = if is_union && struct_layout.is_rust_union() {

src/ir/item.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -811,17 +811,23 @@ impl Item {
811811
/// Get this item's original C++ name, including any containing types, but without
812812
/// the namespace name. For nested types, the C++ name differs from the Rust name, e.g.
813813
/// a nested C++ type `A::B` would correspond to the Rust type `A_B`.
814-
pub fn original_name(
815-
&self,
816-
ctx: &BindgenContext,
817-
) -> String {
814+
/// If the item or any of its containing types is anonymous, returns None.
815+
pub fn original_name(&self, ctx: &BindgenContext) -> Option<String> {
818816
let target = ctx.resolve_item(self.name_target(ctx));
819817

820-
// Concatenate name of item and all ancestors until we reach a namespace.
821-
let mut names: Vec<_> = target
818+
// Get item and all ancestors until the first enclosing namespace.
819+
let ancestors: Vec<_> = target
822820
.ancestors(ctx)
823821
.map(|id| ctx.resolve_item(id))
824822
.take_while(|item| !item.is_module())
823+
.collect();
824+
825+
if ancestors.iter().any(|item| item.is_anon()) {
826+
return None;
827+
}
828+
829+
let mut names: Vec<_> = ancestors
830+
.iter()
825831
.map(|item| {
826832
let target = ctx.resolve_item(item.name_target(ctx));
827833
target.base_name(ctx)
@@ -831,7 +837,7 @@ impl Item {
831837

832838
names.reverse();
833839

834-
names.join("::")
840+
Some(names.join("::"))
835841
}
836842

837843
/// Get the canonical name without taking into account the replaces

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

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

88
#[repr(C)]
99
#[derive(Debug, Default, Copy, Clone)]
10+
#[bindgen_original_name("allocator_traits")]
1011
pub struct std_allocator_traits {
1112
pub _address: u8,
1213
}

tests/expectations/tests/allowlist_basic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct AllowlistMe<T> {
1414
}
1515
#[repr(C)]
1616
#[derive(Debug, Copy, Clone)]
17+
#[bindgen_original_name("AllowlistMe::Inner")]
1718
pub struct AllowlistMe_Inner<T> {
1819
pub bar: T,
1920
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,

tests/expectations/tests/anon_union.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ pub enum TErrorResult_UnionState {
2323
}
2424
#[repr(C)]
2525
#[derive(Debug, Copy, Clone)]
26+
#[bindgen_original_name("TErrorResult::Message")]
2627
pub struct TErrorResult_Message {
2728
_unused: [u8; 0],
2829
}
2930
#[repr(C)]
3031
#[derive(Debug, Copy, Clone)]
32+
#[bindgen_original_name("TErrorResult::DOMExceptionInfo")]
3133
pub struct TErrorResult_DOMExceptionInfo {
3234
_unused: [u8; 0],
3335
}

tests/expectations/tests/anon_union_1_0.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ pub enum TErrorResult_UnionState {
6565
}
6666
#[repr(C)]
6767
#[derive(Debug, Copy, Clone)]
68+
#[bindgen_original_name("TErrorResult::Message")]
6869
pub struct TErrorResult_Message {
6970
_unused: [u8; 0],
7071
}
7172
#[repr(C)]
7273
#[derive(Debug, Copy, Clone)]
74+
#[bindgen_original_name("TErrorResult::DOMExceptionInfo")]
7375
pub struct TErrorResult_DOMExceptionInfo {
7476
_unused: [u8; 0],
7577
}

tests/expectations/tests/bad-namespace-parenthood-inheritance.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#[repr(C)]
99
#[derive(Debug, Copy, Clone)]
10+
#[bindgen_original_name("char_traits")]
1011
pub struct std_char_traits {
1112
pub _address: u8,
1213
}
@@ -17,6 +18,7 @@ impl Default for std_char_traits {
1718
}
1819
#[repr(C)]
1920
#[derive(Debug, Default, Copy, Clone)]
21+
#[bindgen_original_name("char_traits")]
2022
pub struct __gnu_cxx_char_traits {
2123
pub _address: u8,
2224
}

tests/expectations/tests/class_with_inner_struct.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct A {
1414
}
1515
#[repr(C)]
1616
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
17+
#[bindgen_original_name("A::Segment")]
1718
pub struct A_Segment {
1819
pub begin: ::std::os::raw::c_int,
1920
pub end: ::std::os::raw::c_int,
@@ -167,6 +168,7 @@ pub struct B {
167168
}
168169
#[repr(C)]
169170
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
171+
#[bindgen_original_name("B::Segment")]
170172
pub struct B_Segment {
171173
pub begin: ::std::os::raw::c_int,
172174
pub end: ::std::os::raw::c_int,
@@ -402,6 +404,7 @@ impl Default for C__bindgen_ty_1 {
402404
}
403405
#[repr(C)]
404406
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
407+
#[bindgen_original_name("C::Segment")]
405408
pub struct C_Segment {
406409
pub begin: ::std::os::raw::c_int,
407410
pub end: ::std::os::raw::c_int,

tests/expectations/tests/class_with_inner_struct_1_0.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub struct A {
5757
}
5858
#[repr(C)]
5959
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
60+
#[bindgen_original_name("A::Segment")]
6061
pub struct A_Segment {
6162
pub begin: ::std::os::raw::c_int,
6263
pub end: ::std::os::raw::c_int,
@@ -217,6 +218,7 @@ pub struct B {
217218
}
218219
#[repr(C)]
219220
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
221+
#[bindgen_original_name("B::Segment")]
220222
pub struct B_Segment {
221223
pub begin: ::std::os::raw::c_int,
222224
pub end: ::std::os::raw::c_int,
@@ -473,6 +475,7 @@ impl Clone for C__bindgen_ty_1 {
473475
}
474476
#[repr(C)]
475477
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
478+
#[bindgen_original_name("C::Segment")]
476479
pub struct C_Segment {
477480
pub begin: ::std::os::raw::c_int,
478481
pub end: ::std::os::raw::c_int,

tests/expectations/tests/comment-indent.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod root {
2222
/// This class is not so interesting, but worth a bit of docs too!
2323
#[repr(C)]
2424
#[derive(Debug, Default, Copy, Clone)]
25+
#[bindgen_original_name("Foo::Bar")]
2526
pub struct Foo_Bar {
2627
pub _address: u8,
2728
}

tests/expectations/tests/constant-non-specialized-tp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct Outer {
1717
}
1818
#[repr(C)]
1919
#[derive(Debug, Default, Copy, Clone)]
20+
#[bindgen_original_name("Outer::Inner")]
2021
pub struct Outer_Inner {
2122
pub _address: u8,
2223
}

tests/expectations/tests/disable-nested-struct-naming.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,18 @@ pub struct bar1 {
1919
}
2020
#[repr(C)]
2121
#[derive(Debug, Default, Copy, Clone)]
22-
#[bindgen_original_name("foo::bar1::_bindgen_ty_1")]
2322
pub struct bar1__bindgen_ty_1 {
2423
pub x2: ::std::os::raw::c_int,
2524
pub b3: bar1__bindgen_ty_1__bindgen_ty_1,
2625
}
2726
#[repr(C)]
2827
#[derive(Debug, Default, Copy, Clone)]
29-
#[bindgen_original_name("foo::bar1::_bindgen_ty_1::_bindgen_ty_1")]
3028
pub struct bar1__bindgen_ty_1__bindgen_ty_1 {
3129
pub x3: ::std::os::raw::c_int,
3230
pub b4: bar4,
3331
}
3432
#[repr(C)]
3533
#[derive(Debug, Default, Copy, Clone)]
36-
#[bindgen_original_name("foo::bar1::_bindgen_ty_1::_bindgen_ty_1::bar4")]
3734
pub struct bar4 {
3835
pub x4: ::std::os::raw::c_int,
3936
}
@@ -184,13 +181,11 @@ pub struct _bindgen_ty_1 {
184181
}
185182
#[repr(C)]
186183
#[derive(Debug, Default, Copy, Clone)]
187-
#[bindgen_original_name("_bindgen_ty_1::_bindgen_ty_1")]
188184
pub struct _bindgen_ty_1__bindgen_ty_1 {
189185
pub b: baz,
190186
}
191187
#[repr(C)]
192188
#[derive(Debug, Default, Copy, Clone)]
193-
#[bindgen_original_name("_bindgen_ty_1::_bindgen_ty_1::baz")]
194189
pub struct baz {
195190
pub x: ::std::os::raw::c_int,
196191
}

tests/expectations/tests/enum_in_template_with_typedef.rs

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

88
#[repr(C)]
99
#[derive(Debug, Default, Copy, Clone)]
10+
#[bindgen_original_name("fbstring_core")]
1011
pub struct std_fbstring_core {
1112
pub _address: u8,
1213
}

tests/expectations/tests/forward-inherit-struct-with-fields.rs

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

88
#[repr(C)]
99
#[derive(Debug, Copy, Clone)]
10+
#[bindgen_original_name("RootedBase")]
1011
pub struct js_RootedBase<T> {
1112
pub foo: *mut T,
1213
pub next: *mut Rooted<T>,

tests/expectations/tests/forward-inherit-struct.rs

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

88
#[repr(C)]
99
#[derive(Debug, Default, Copy, Clone)]
10+
#[bindgen_original_name("RootedBase")]
1011
pub struct js_RootedBase {
1112
pub _address: u8,
1213
}

tests/expectations/tests/in_class_typedef.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub type Foo_elem_type<T> = T;
1414
pub type Foo_ptr_type<T> = *mut T;
1515
#[repr(C)]
1616
#[derive(Debug, Default, Copy, Clone)]
17+
#[bindgen_original_name("Foo::Bar")]
1718
pub struct Foo_Bar {
1819
pub x: ::std::os::raw::c_int,
1920
pub y: ::std::os::raw::c_int,

tests/expectations/tests/inherit-namespaced.rs

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

88
#[repr(C)]
99
#[derive(Debug, Default, Copy, Clone)]
10+
#[bindgen_original_name("RootedBase")]
1011
pub struct js_RootedBase {
1112
pub _address: u8,
1213
}

tests/expectations/tests/inline_namespace_no_ns_enabled.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#[repr(C)]
99
#[derive(Debug)]
10+
#[bindgen_original_name("basic_string")]
1011
pub struct std_basic_string<CharT> {
1112
pub hider: std_basic_string_Alloc_hider,
1213
pub length: ::std::os::raw::c_ulong,
@@ -15,6 +16,7 @@ pub struct std_basic_string<CharT> {
1516
}
1617
#[repr(C)]
1718
#[derive(Debug, Copy, Clone)]
19+
#[bindgen_original_name("basic_string::Alloc_hider")]
1820
pub struct std_basic_string_Alloc_hider {
1921
pub storage: *mut ::std::os::raw::c_void,
2022
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct nsBaseHashtable {
2626
pub type nsBaseHashtable_EntryType<K, V> = Entry<K, V>;
2727
#[repr(C)]
2828
#[derive(Debug, Copy, Clone)]
29+
#[bindgen_original_name("nsBaseHashtable::EntryPtr")]
2930
pub struct nsBaseHashtable_EntryPtr<K, V> {
3031
pub mEntry: *mut nsBaseHashtable_EntryType<K, V>,
3132
pub mExistingEntry: bool,

tests/expectations/tests/issue-1514.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct Thing {
1212
}
1313
#[repr(C)]
1414
#[derive(Debug, Copy, Clone)]
15+
#[bindgen_original_name("Thing::Inner")]
1516
pub struct Thing_Inner<T> {
1617
pub ptr: *mut T,
1718
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
@@ -23,6 +24,7 @@ impl<T> Default for Thing_Inner<T> {
2324
}
2425
#[repr(C)]
2526
#[derive(Debug, Copy, Clone)]
27+
#[bindgen_original_name("Thing::AnotherInner")]
2628
pub struct Thing_AnotherInner<T> {
2729
pub _base: Thing_Inner<T>,
2830
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,

tests/expectations/tests/issue-358.rs

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

88
#[repr(C)]
99
#[derive(Debug, Copy, Clone)]
10+
#[bindgen_original_name("PersistentRooted")]
1011
pub struct JS_PersistentRooted {
1112
pub _base: a,
1213
}

tests/expectations/tests/issue-493.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub type basic_string_value_type = ::std::os::raw::c_char;
5858
pub type basic_string_pointer = *mut basic_string_value_type;
5959
#[repr(C)]
6060
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
61+
#[bindgen_original_name("basic_string::__long")]
6162
pub struct basic_string___long {
6263
pub __cap_: basic_string_size_type,
6364
pub __size_: basic_string_size_type,
@@ -76,6 +77,7 @@ pub enum basic_string__bindgen_ty_1 {
7677
__min_cap = 0,
7778
}
7879
#[repr(C)]
80+
#[bindgen_original_name("basic_string::__short")]
7981
pub struct basic_string___short {
8082
pub __bindgen_anon_1: basic_string___short__bindgen_ty_1,
8183
pub __data_: *mut basic_string_value_type,
@@ -97,6 +99,7 @@ impl Default for basic_string___short {
9799
}
98100
#[repr(C)]
99101
#[repr(align(1))]
102+
#[bindgen_original_name("basic_string::__ulx")]
100103
pub struct basic_string___ulx {
101104
pub __lx: __BindgenUnionField<basic_string___long>,
102105
pub __lxx: __BindgenUnionField<basic_string___short>,
@@ -116,6 +119,7 @@ pub enum basic_string__bindgen_ty_2 {
116119
}
117120
#[repr(C)]
118121
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
122+
#[bindgen_original_name("basic_string::__raw")]
119123
pub struct basic_string___raw {
120124
pub __words: *mut basic_string_size_type,
121125
}
@@ -125,6 +129,7 @@ impl Default for basic_string___raw {
125129
}
126130
}
127131
#[repr(C)]
132+
#[bindgen_original_name("basic_string::__rep")]
128133
pub struct basic_string___rep {
129134
pub __bindgen_anon_1: basic_string___rep__bindgen_ty_1,
130135
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub type basic_string_value_type = ::std::os::raw::c_char;
5858
pub type basic_string_pointer = *mut basic_string_value_type;
5959
#[repr(C)]
6060
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
61+
#[bindgen_original_name("basic_string::__long")]
6162
pub struct basic_string___long {
6263
pub __cap_: basic_string_size_type,
6364
pub __size_: basic_string_size_type,
@@ -77,6 +78,7 @@ pub enum basic_string__bindgen_ty_1 {
7778
}
7879
#[repr(C)]
7980
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
81+
#[bindgen_original_name("basic_string::__short")]
8082
pub struct basic_string___short {
8183
pub __bindgen_anon_1: basic_string___short__bindgen_ty_1,
8284
pub __data_: *mut basic_string_value_type,
@@ -95,6 +97,7 @@ impl Default for basic_string___short {
9597
}
9698
#[repr(C)]
9799
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
100+
#[bindgen_original_name("basic_string::__ulx")]
98101
pub struct basic_string___ulx {
99102
pub __lx: __BindgenUnionField<basic_string___long>,
100103
pub __lxx: __BindgenUnionField<basic_string___short>,
@@ -109,6 +112,7 @@ pub enum basic_string__bindgen_ty_2 {
109112
}
110113
#[repr(C)]
111114
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
115+
#[bindgen_original_name("basic_string::__raw")]
112116
pub struct basic_string___raw {
113117
pub __words: *mut basic_string_size_type,
114118
}
@@ -119,6 +123,7 @@ impl Default for basic_string___raw {
119123
}
120124
#[repr(C)]
121125
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
126+
#[bindgen_original_name("basic_string::__rep")]
122127
pub struct basic_string___rep {
123128
pub __bindgen_anon_1: basic_string___rep__bindgen_ty_1,
124129
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum _bindgen_ty_1 {
1616
pub type JS_Alias = u8;
1717
#[repr(C)]
1818
#[derive(Debug, Copy, Clone)]
19+
#[bindgen_original_name("Base")]
1920
pub struct JS_Base {
2021
pub f: JS_Alias,
2122
}
@@ -26,6 +27,7 @@ impl Default for JS_Base {
2627
}
2728
#[repr(C)]
2829
#[derive(Debug, Copy, Clone)]
30+
#[bindgen_original_name("AutoIdVector")]
2931
pub struct JS_AutoIdVector {
3032
pub _base: JS_Base,
3133
}

0 commit comments

Comments
 (0)