From a1c6802bee7b687ae64e90acc4dc8d4b03ffe3ab Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Thu, 5 Oct 2017 11:02:26 +0300 Subject: Use bitfield getter_name in impl_debug. Also make impl_partialeq test to also cover impl_debug case. --- src/codegen/impl_debug.rs | 10 +- .../tests/derive-bitfield-method-same-name.rs | 136 +++++++++++++++++++++ .../tests/derive-partialeq-bitfield-same-name.rs | 117 ------------------ tests/headers/derive-bitfield-method-same-name.hpp | 13 ++ .../derive-partialeq-bitfield-same-name.hpp | 9 -- 5 files changed, 154 insertions(+), 131 deletions(-) create mode 100644 tests/expectations/tests/derive-bitfield-method-same-name.rs delete mode 100644 tests/expectations/tests/derive-partialeq-bitfield-same-name.rs create mode 100644 tests/headers/derive-bitfield-method-same-name.hpp delete mode 100644 tests/headers/derive-partialeq-bitfield-same-name.hpp diff --git a/src/codegen/impl_debug.rs b/src/codegen/impl_debug.rs index 2ebcaf1a..e0204f4d 100644 --- a/src/codegen/impl_debug.rs +++ b/src/codegen/impl_debug.rs @@ -28,7 +28,6 @@ pub fn gen_debug_impl( &Field::Bitfields(ref bu) => bu.impl_debug(ctx, ()), }); - for (i, (fstring, toks)) in processed_fields.enumerate() { if i > 0 { format_string.push_str(", "); @@ -91,14 +90,15 @@ impl<'a> ImplDebug<'a> for BitfieldUnit { ) -> Option<(String, Vec)> { let mut format_string = String::new(); let mut tokens = vec![]; - for (i, bu) in self.bitfields().iter().enumerate() { + for (i, bitfield) in self.bitfields().iter().enumerate() { if i > 0 { format_string.push_str(", "); } - if let Some(name) = bu.name() { - format_string.push_str(&format!("{} : {{:?}}", name)); - let name_ident = ctx.rust_ident_raw(name); + if let Some(bitfield_name) = bitfield.name() { + format_string.push_str(&format!("{} : {{:?}}", bitfield_name)); + let getter_name = bitfield.getter_name(); + let name_ident = ctx.rust_ident_raw(getter_name); tokens.push(quote! { self.#name_ident () }); diff --git a/tests/expectations/tests/derive-bitfield-method-same-name.rs b/tests/expectations/tests/derive-bitfield-method-same-name.rs new file mode 100644 index 00000000..1873d0d6 --- /dev/null +++ b/tests/expectations/tests/derive-bitfield-method-same-name.rs @@ -0,0 +1,136 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + + + +/// Because this struct have array larger than 32 items +/// and --with-derive-partialeq --impl-partialeq --impl-debug is provided, +/// this struct should manually implement `Debug` and `PartialEq`. +#[repr(C)] +#[derive(Copy)] +pub struct Foo { + pub large: [::std::os::raw::c_int; 33usize], + pub _bitfield_1: [u8; 2usize], + pub __bindgen_padding_0: u16, +} +#[test] +fn bindgen_test_layout_Foo() { + assert_eq!( + ::std::mem::size_of::(), + 136usize, + concat!("Size of: ", stringify!(Foo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(Foo)) + ); + assert_eq!( + unsafe { &(*(0 as *const Foo)).large as *const _ as usize }, + 0usize, + concat!( + "Alignment of field: ", + stringify!(Foo), + "::", + stringify!(large) + ) + ); +} +extern "C" { + #[link_name = "_ZN3Foo4typeEv"] + pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char; +} +extern "C" { + #[link_name = "_ZN3Foo9set_type_Ec"] + pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char); +} +extern "C" { + #[link_name = "_ZN3Foo8set_typeEc"] + pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char); +} +impl Clone for Foo { + fn clone(&self) -> Self { + *self + } +} +impl Default for Foo { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} +impl ::std::fmt::Debug for Foo { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + write!( + f, + "Foo {{ large: [{}], type_ : {:?}, }}", + self.large + .iter() + .enumerate() + .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) + .collect::(), + self.type__bindgen_bitfield() + ) + } +} +impl ::std::cmp::PartialEq for Foo { + fn eq(&self, other: &Foo) -> bool { + &self.large[..] == &other.large[..] + && self.type__bindgen_bitfield() == other.type__bindgen_bitfield() + } +} +impl Foo { + #[inline] + pub fn type__bindgen_bitfield(&self) -> ::std::os::raw::c_char { + let mut unit_field_val: u16 = unsafe { ::std::mem::uninitialized() }; + unsafe { + ::std::ptr::copy_nonoverlapping( + &self._bitfield_1 as *const _ as *const u8, + &mut unit_field_val as *mut u16 as *mut u8, + ::std::mem::size_of::(), + ) + }; + let mask = 7u64 as u16; + let val = (unit_field_val & mask) >> 0usize; + unsafe { ::std::mem::transmute(val as u8) } + } + #[inline] + pub fn set_type__bindgen_bitfield(&mut self, val: ::std::os::raw::c_char) { + let mask = 7u64 as u16; + let val = val as u8 as u16; + let mut unit_field_val: u16 = unsafe { ::std::mem::uninitialized() }; + unsafe { + ::std::ptr::copy_nonoverlapping( + &self._bitfield_1 as *const _ as *const u8, + &mut unit_field_val as *mut u16 as *mut u8, + ::std::mem::size_of::(), + ) + }; + unit_field_val &= !mask; + unit_field_val |= (val << 0usize) & mask; + unsafe { + ::std::ptr::copy_nonoverlapping( + &unit_field_val as *const _ as *const u8, + &mut self._bitfield_1 as *mut _ as *mut u8, + ::std::mem::size_of::(), + ); + } + } + #[inline] + pub fn new_bitfield_1(type__bindgen_bitfield: ::std::os::raw::c_char) -> u16 { + (0 | ((type__bindgen_bitfield as u8 as u16) << 0usize) & (7u64 as u16)) + } + #[inline] + pub unsafe fn type_(&mut self) -> ::std::os::raw::c_char { + Foo_type(self) + } + #[inline] + pub unsafe fn set_type_(&mut self, c: ::std::os::raw::c_char) { + Foo_set_type_(self, c) + } + #[inline] + pub unsafe fn set_type(&mut self, c: ::std::os::raw::c_char) { + Foo_set_type(self, c) + } +} diff --git a/tests/expectations/tests/derive-partialeq-bitfield-same-name.rs b/tests/expectations/tests/derive-partialeq-bitfield-same-name.rs deleted file mode 100644 index 9ee2db42..00000000 --- a/tests/expectations/tests/derive-partialeq-bitfield-same-name.rs +++ /dev/null @@ -1,117 +0,0 @@ -/* automatically generated by rust-bindgen */ - - -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - - -#[repr(C)] -#[derive(Copy)] -pub struct Foo { - pub big_array: [::std::os::raw::c_char; 33usize], - pub _bitfield_1: u8, -} -#[test] -fn bindgen_test_layout_Foo() { - assert_eq!( - ::std::mem::size_of::(), - 34usize, - concat!("Size of: ", stringify!(Foo)) - ); - assert_eq!( - ::std::mem::align_of::(), - 1usize, - concat!("Alignment of ", stringify!(Foo)) - ); - assert_eq!( - unsafe { &(*(0 as *const Foo)).big_array as *const _ as usize }, - 0usize, - concat!( - "Alignment of field: ", - stringify!(Foo), - "::", - stringify!(big_array) - ) - ); -} -extern "C" { - #[link_name = "_ZN3Foo4typeEv"] - pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char; -} -extern "C" { - #[link_name = "_ZN3Foo9set_type_Ec"] - pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char); -} -extern "C" { - #[link_name = "_ZN3Foo8set_typeEc"] - pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char); -} -impl Clone for Foo { - fn clone(&self) -> Self { - *self - } -} -impl Default for Foo { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} -impl ::std::cmp::PartialEq for Foo { - fn eq(&self, other: &Foo) -> bool { - &self.big_array[..] == &other.big_array[..] - && self.type__bindgen_bitfield() == other.type__bindgen_bitfield() - } -} -impl Foo { - #[inline] - pub fn type__bindgen_bitfield(&self) -> ::std::os::raw::c_char { - let mut unit_field_val: u8 = unsafe { ::std::mem::uninitialized() }; - unsafe { - ::std::ptr::copy_nonoverlapping( - &self._bitfield_1 as *const _ as *const u8, - &mut unit_field_val as *mut u8 as *mut u8, - ::std::mem::size_of::(), - ) - }; - let mask = 7u64 as u8; - let val = (unit_field_val & mask) >> 0usize; - unsafe { ::std::mem::transmute(val as u8) } - } - #[inline] - pub fn set_type__bindgen_bitfield(&mut self, val: ::std::os::raw::c_char) { - let mask = 7u64 as u8; - let val = val as u8 as u8; - let mut unit_field_val: u8 = unsafe { ::std::mem::uninitialized() }; - unsafe { - ::std::ptr::copy_nonoverlapping( - &self._bitfield_1 as *const _ as *const u8, - &mut unit_field_val as *mut u8 as *mut u8, - ::std::mem::size_of::(), - ) - }; - unit_field_val &= !mask; - unit_field_val |= (val << 0usize) & mask; - unsafe { - ::std::ptr::copy_nonoverlapping( - &unit_field_val as *const _ as *const u8, - &mut self._bitfield_1 as *mut _ as *mut u8, - ::std::mem::size_of::(), - ); - } - } - #[inline] - pub fn new_bitfield_1(type__bindgen_bitfield: ::std::os::raw::c_char) -> u8 { - (0 | ((type__bindgen_bitfield as u8 as u8) << 0usize) & (7u64 as u8)) - } - #[inline] - pub unsafe fn type_(&mut self) -> ::std::os::raw::c_char { - Foo_type(self) - } - #[inline] - pub unsafe fn set_type_(&mut self, c: ::std::os::raw::c_char) { - Foo_set_type_(self, c) - } - #[inline] - pub unsafe fn set_type(&mut self, c: ::std::os::raw::c_char) { - Foo_set_type(self, c) - } -} diff --git a/tests/headers/derive-bitfield-method-same-name.hpp b/tests/headers/derive-bitfield-method-same-name.hpp new file mode 100644 index 00000000..4b7b21e9 --- /dev/null +++ b/tests/headers/derive-bitfield-method-same-name.hpp @@ -0,0 +1,13 @@ +// bindgen-flags: --with-derive-partialeq --impl-partialeq --impl-debug + +/// Because this struct have array larger than 32 items +/// and --with-derive-partialeq --impl-partialeq --impl-debug is provided, +/// this struct should manually implement `Debug` and `PartialEq`. +struct Foo { + int large[33]; + char type_ : 3; + unsigned : 8; + char type(); + void set_type_(char c); + void set_type(char c); +}; diff --git a/tests/headers/derive-partialeq-bitfield-same-name.hpp b/tests/headers/derive-partialeq-bitfield-same-name.hpp deleted file mode 100644 index a85016c7..00000000 --- a/tests/headers/derive-partialeq-bitfield-same-name.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// bindgen-flags: --with-derive-hash --with-derive-partialeq --with-derive-eq --impl-partialeq - -struct Foo { - char big_array[33]; - char type_ : 3; - char type(); - void set_type_(char c); - void set_type(char c); -}; -- cgit v1.2.3