-
Notifications
You must be signed in to change notification settings - Fork 749
typedef struct {} name #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4d628c9
06268eb
fd70398
4a14681
70c61e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,19 +311,21 @@ impl Type { | |
match self.kind { | ||
TypeKind::Named => { | ||
let name = self.name().expect("Unnamed named type?"); | ||
let mut chars = name.chars(); | ||
let first = chars.next().unwrap(); | ||
let mut remaining = chars; | ||
|
||
let valid = (first.is_alphabetic() || first == '_') && | ||
remaining.all(|c| c.is_alphanumeric() || c == '_'); | ||
|
||
!valid | ||
!Self::is_valid_identifier(&name) | ||
} | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Checks whether the name looks like an identifier, | ||
/// i.e. is alphanumeric (including '_') and does not start with a digit. | ||
pub fn is_valid_identifier(name: &str) -> bool { | ||
let mut chars = name.chars(); | ||
let first_valid = chars.next().map(|c| c.is_alphabetic() || c == '_').unwrap_or(false); | ||
|
||
first_valid && chars.all(|c| c.is_alphanumeric() || c == '_') | ||
} | ||
|
||
/// See safe_canonical_type. | ||
pub fn canonical_type<'tr>(&'tr self, | ||
ctx: &'tr BindgenContext) | ||
|
@@ -454,7 +456,6 @@ fn is_invalid_named_type_unnamed() { | |
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn is_invalid_named_type_empty_name() { | ||
let ty = Type::new(Some("".into()), None, TypeKind::Named, false); | ||
assert!(ty.is_invalid_named_type()) | ||
|
@@ -1074,12 +1075,30 @@ impl Type { | |
} | ||
CXType_Enum => { | ||
let enum_ = Enum::from_ty(ty, ctx).expect("Not an enum?"); | ||
|
||
if name.is_empty() { | ||
let pretty_name = ty.spelling(); | ||
if Self::is_valid_identifier(&pretty_name) { | ||
name = pretty_name; | ||
} | ||
} | ||
|
||
TypeKind::Enum(enum_) | ||
} | ||
CXType_Record => { | ||
let complex = | ||
CompInfo::from_ty(potential_id, ty, location, ctx) | ||
.expect("Not a complex type?"); | ||
|
||
if name.is_empty() { | ||
// The pretty-printed name may contain typedefed name, | ||
// but may also be "struct (anonymous at .h:1)" | ||
let pretty_name = ty.spelling(); | ||
if Self::is_valid_identifier(&pretty_name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels somewhat hacky... But I guess it's ok. This kind of overlaps with similar code in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
name = pretty_name; | ||
} | ||
} | ||
|
||
TypeKind::Comp(complex) | ||
} | ||
// FIXME: We stub vectors as arrays since in 99% of the cases the | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
|
||
#![allow(non_snake_case)] | ||
|
||
|
||
#[repr(C)] | ||
#[derive(Debug, Default, Copy)] | ||
pub struct typedef_named_struct { | ||
pub has_name: bool, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_typedef_named_struct() { | ||
assert_eq!(::std::mem::size_of::<typedef_named_struct>() , 1usize , concat | ||
! ( "Size of: " , stringify ! ( typedef_named_struct ) )); | ||
assert_eq! (::std::mem::align_of::<typedef_named_struct>() , 1usize , | ||
concat ! ( | ||
"Alignment of " , stringify ! ( typedef_named_struct ) )); | ||
assert_eq! (unsafe { | ||
& ( * ( 0 as * const typedef_named_struct ) ) . has_name as * | ||
const _ as usize } , 0usize , concat ! ( | ||
"Alignment of field: " , stringify ! ( typedef_named_struct ) | ||
, "::" , stringify ! ( has_name ) )); | ||
} | ||
impl Clone for typedef_named_struct { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct _bindgen_ty_1 { | ||
pub no_name: *mut ::std::os::raw::c_void, | ||
} | ||
#[test] | ||
fn bindgen_test_layout__bindgen_ty_1() { | ||
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 8usize , concat ! ( | ||
"Size of: " , stringify ! ( _bindgen_ty_1 ) )); | ||
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 8usize , concat ! ( | ||
"Alignment of " , stringify ! ( _bindgen_ty_1 ) )); | ||
assert_eq! (unsafe { | ||
& ( * ( 0 as * const _bindgen_ty_1 ) ) . no_name as * const _ | ||
as usize } , 0usize , concat ! ( | ||
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" | ||
, stringify ! ( no_name ) )); | ||
} | ||
impl Clone for _bindgen_ty_1 { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
impl Default for _bindgen_ty_1 { | ||
fn default() -> Self { unsafe { ::std::mem::zeroed() } } | ||
} | ||
pub type struct_ptr_t = *mut _bindgen_ty_1; | ||
pub type struct_ptr_ptr_t = *mut *mut _bindgen_ty_1; | ||
#[repr(u32)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub enum typedef_named_enum { ENUM_HAS_NAME = 1, } | ||
pub const ENUM_IS_ANON: _bindgen_ty_2 = _bindgen_ty_2::ENUM_IS_ANON; | ||
#[repr(u32)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub enum _bindgen_ty_2 { ENUM_IS_ANON = 0, } | ||
pub type enum_ptr_t = *mut _bindgen_ty_2; | ||
pub type enum_ptr_ptr_t = *mut *mut _bindgen_ty_2; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
|
||
#![allow(non_snake_case)] | ||
|
||
|
||
pub mod root { | ||
#[allow(unused_imports)] | ||
use self::super::root; | ||
pub mod whatever { | ||
#[allow(unused_imports)] | ||
use self::super::super::root; | ||
#[repr(C)] | ||
#[derive(Debug, Default, Copy)] | ||
pub struct _bindgen_ty_1 { | ||
pub foo: ::std::os::raw::c_int, | ||
} | ||
#[test] | ||
fn bindgen_test_layout__bindgen_ty_1() { | ||
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize , | ||
concat ! ( "Size of: " , stringify ! ( _bindgen_ty_1 ) | ||
)); | ||
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize , | ||
concat ! ( | ||
"Alignment of " , stringify ! ( _bindgen_ty_1 ) )); | ||
assert_eq! (unsafe { | ||
& ( * ( 0 as * const _bindgen_ty_1 ) ) . foo as * | ||
const _ as usize } , 0usize , concat ! ( | ||
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) | ||
, "::" , stringify ! ( foo ) )); | ||
} | ||
impl Clone for _bindgen_ty_1 { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
pub type typedef_struct = root::whatever::_bindgen_ty_1; | ||
pub const whatever_BAR: root::whatever::_bindgen_ty_2 = | ||
_bindgen_ty_2::BAR; | ||
#[repr(u32)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub enum _bindgen_ty_2 { BAR = 1, } | ||
pub use self::super::super::root::whatever::_bindgen_ty_2 as | ||
typedef_enum; | ||
} | ||
pub mod _bindgen_mod_id_12 { | ||
#[allow(unused_imports)] | ||
use self::super::super::root; | ||
#[repr(C)] | ||
#[derive(Debug, Default, Copy)] | ||
pub struct _bindgen_ty_1 { | ||
pub foo: ::std::os::raw::c_int, | ||
} | ||
#[test] | ||
fn bindgen_test_layout__bindgen_ty_1() { | ||
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize , | ||
concat ! ( "Size of: " , stringify ! ( _bindgen_ty_1 ) | ||
)); | ||
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize , | ||
concat ! ( | ||
"Alignment of " , stringify ! ( _bindgen_ty_1 ) )); | ||
assert_eq! (unsafe { | ||
& ( * ( 0 as * const _bindgen_ty_1 ) ) . foo as * | ||
const _ as usize } , 0usize , concat ! ( | ||
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) | ||
, "::" , stringify ! ( foo ) )); | ||
} | ||
impl Clone for _bindgen_ty_1 { | ||
fn clone(&self) -> Self { *self } | ||
} | ||
pub type typedef_struct = root::_bindgen_mod_id_12::_bindgen_ty_1; | ||
pub const _bindgen_mod_id_12_BAR: | ||
root::_bindgen_mod_id_12::_bindgen_ty_2 = | ||
_bindgen_ty_2::BAR; | ||
#[repr(u32)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub enum _bindgen_ty_2 { BAR = 1, } | ||
pub use self::super::super::root::_bindgen_mod_id_12::_bindgen_ty_2 as | ||
typedef_enum; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, good find with this, I thought this was going to be a lot trickier and that we were going to need to look back and forth for the correct typedef :)