diff options
22 files changed, 352 insertions, 223 deletions
diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs index f15b92d1..6c8a36f7 100644 --- a/libbindgen/src/codegen/mod.rs +++ b/libbindgen/src/codegen/mod.rs @@ -228,7 +228,8 @@ impl CodeGenerator for Item { result: &mut CodegenResult, _extra: &()) { if self.is_hidden(ctx) || result.seen(self.id()) { - debug!("<Item as CodeGenerator>::codegen: Ignoring hidden or seen: self = {:?}", self); + debug!("<Item as CodeGenerator>::codegen: Ignoring hidden or seen: \ + self = {:?}", self); return; } @@ -328,8 +329,7 @@ impl CodeGenerator for Var { .build(ty) } VarType::Int(val) => { - const_item.build(helpers::ast_ty::int_expr(val)) - .build(ty) + const_item.build(helpers::ast_ty::int_expr(val)).build(ty) } VarType::String(ref bytes) => { // Account the trailing zero. @@ -596,7 +596,7 @@ impl<'a> Bitfield<'a> { let field_type = field_item.to_rust_ty(ctx); let int_type = BlobTyBuilder::new(field_ty_layout).build(); - let getter_name = ctx.ext_cx().ident_of(&field_name); + let getter_name = ctx.rust_ident(&field_name); let setter_name = ctx.ext_cx() .ident_of(&format!("set_{}", &field_name)); let mask = ((1usize << width) - 1) << offset; diff --git a/libbindgen/src/ir/int.rs b/libbindgen/src/ir/int.rs index 2d85db83..179ebb96 100644 --- a/libbindgen/src/ir/int.rs +++ b/libbindgen/src/ir/int.rs @@ -90,4 +90,9 @@ impl IntKind { Custom { is_signed, .. } => is_signed, } } + + /// Whether this type's signedness matches the value. + pub fn signedness_matches(&self, val: i64) -> bool { + val >= 0 || self.is_signed() + } } diff --git a/libbindgen/src/ir/item.rs b/libbindgen/src/ir/item.rs index 253db8c0..176eca14 100644 --- a/libbindgen/src/ir/item.rs +++ b/libbindgen/src/ir/item.rs @@ -2,8 +2,8 @@ use clang; use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult}; -use regex::Regex; use std::cell::{Cell, RefCell}; +use std::fmt::Write; use super::annotations::Annotations; use super::context::{BindgenContext, ItemId}; use super::function::Function; @@ -490,7 +490,8 @@ impl Item { } } - fn is_module(&self) -> bool { + /// Is this item a module? + pub fn is_module(&self) -> bool { match self.kind { ItemKind::Module(..) => true, _ => false, @@ -525,6 +526,108 @@ impl Item { self.as_type().map_or(false, |ty| ty.is_type_ref()) } + /// Get the target item id for name generation. + fn name_target(&self, ctx: &BindgenContext, for_name_checking: bool) -> ItemId { + let mut item = self; + loop { + match *item.kind() { + ItemKind::Type(ref ty) => { + match *ty.kind() { + // If we're a template specialization, our name is our + // parent's name. + TypeKind::Comp(ref ci) if ci.is_template_specialization() => { + item = ctx.resolve_item(ci.specialized_template().unwrap()); + }, + // Same as above. + TypeKind::ResolvedTypeRef(inner) | + TypeKind::TemplateRef(inner, _) => { + item = ctx.resolve_item(inner); + } + // Template aliases use their inner alias type's name if we + // are checking names for whitelisting/replacement/etc. + TypeKind::TemplateAlias(inner, _) if for_name_checking => { + item = ctx.resolve_item(inner); + assert_eq!(item.id(), item.name_target(ctx, for_name_checking)); + return item.id(); + } + _ => return item.id(), + } + }, + _ => return item.id(), + } + } + } + + /// Get this function item's name, or `None` if this item is not a function. + fn func_name(&self) -> Option<&str> { + match *self.kind() { + ItemKind::Function(ref func) => Some(func.name()), + _ => None, + } + } + + /// Get the overload index for this method. If this is not a method, return + /// `None`. + fn method_overload_index(&self, ctx: &BindgenContext) -> Option<usize> { + self.func_name().and_then(|func_name| { + let parent = ctx.resolve_item(self.parent_id()); + if let ItemKind::Type(ref ty) = *parent.kind() { + if let TypeKind::Comp(ref ci) = *ty.kind() { + return ci.methods() + .iter() + .filter(|method| { + let item = ctx.resolve_item(method.signature()); + let func = item.expect_function(); + func.name() == func_name + }) + .enumerate() + .find(|&(_, ref method)| method.signature() == self.id()) + .map(|(idx, _)| idx); + } + } + + None + }) + } + + /// Get this item's base name (aka non-namespaced name). + /// + /// The `for_name_checking` boolean parameter informs us whether we are + /// asking for the name in order to do a whitelisting/replacement/etc check + /// or if we are instead using it for code generation. + fn base_name(&self, ctx: &BindgenContext, for_name_checking: bool) -> String { + match *self.kind() { + ItemKind::Var(ref var) => var.name().to_owned(), + ItemKind::Module(ref module) => { + module.name() + .map(ToOwned::to_owned) + .unwrap_or_else(|| format!("_bindgen_mod_{}", self.exposed_id(ctx))) + }, + ItemKind::Type(ref ty) => { + let name = match *ty.kind() { + TypeKind::ResolvedTypeRef(..) => + panic!("should have resolved this in name_target()"), + TypeKind::TemplateAlias(..) if !for_name_checking => Some(""), + TypeKind::TemplateAlias(..) => None, + _ => ty.name(), + }; + name.map(ToOwned::to_owned) + .unwrap_or_else(|| format!("_bindgen_ty_{}", self.exposed_id(ctx))) + } + ItemKind::Function(ref fun) => { + let mut name = fun.name().to_owned(); + + if let Some(idx) = self.method_overload_index(ctx) { + if idx > 0 { + write!(&mut name, "{}", idx).unwrap(); + } + } + + name + }, + } + } + /// Get the canonical name without taking into account the replaces /// annotation. /// @@ -538,102 +641,38 @@ impl Item { /// type and the parent chain, since it should be consistent. pub fn real_canonical_name(&self, ctx: &BindgenContext, - count_namespaces: bool, + within_namespace: bool, for_name_checking: bool) -> String { - let base_name = match *self.kind() { - ItemKind::Type(ref ty) => { - match *ty.kind() { - // If we're a template specialization, our name is our - // parent's. - TypeKind::Comp(ref ci) - if ci.is_template_specialization() => { - return ci.specialized_template().unwrap() - .canonical_name(ctx); - }, - // Same as above - TypeKind::ResolvedTypeRef(inner) | - TypeKind::TemplateRef(inner, _) => { - return inner.canonical_name(ctx); - } - // If we're a named type, we don't need to mangle it, and we - // should be able to assert we're not top level. - TypeKind::Named(ref name, _) => { - return name.to_owned(); - } - // We call codegen on the inner type, but we do not want - // this alias's name to appear in the canonical name just - // because it is in the inner type's parent chain, so we use - // an empty base name. - // - // Note that this would be incorrect if this type could be - // referenced from, let's say, a member variable, but in - // that case the referenced type is the inner alias, so - // we're good there. If we wouldn't, a more complex solution - // would be needed. - TypeKind::TemplateAlias(inner, _) => { - if for_name_checking { - return ctx.resolve_item(inner) - .real_canonical_name(ctx, - count_namespaces, - false); - } - Some("") - } - // Else use the proper name, or fallback to a name with an - // id. - _ => { - ty.name() - } - }.map(ToOwned::to_owned) - } - ItemKind::Function(ref fun) => { - let mut base = fun.name().to_owned(); - - // We might need to deduplicate if we're a method. - let parent = ctx.resolve_item(self.parent_id()); - if let ItemKind::Type(ref ty) = *parent.kind() { - if let TypeKind::Comp(ref ci) = *ty.kind() { - let mut count = 0; - let mut found = false; - for method in ci.methods() { - if method.signature() == self.id() { - found = true; - break; - } - let fun = ctx.resolve_item(method.signature()) - .expect_function(); - if fun.name() == base { - count += 1; - } - } - - assert!(found, "Method not found?"); - if count != 0 { - base.push_str(&count.to_string()); - } - } - } - Some(base) - } - ItemKind::Var(ref var) => Some(var.name().to_owned()), - ItemKind::Module(ref module) => { - module.name().map(ToOwned::to_owned) - } - }; + let target = ctx.resolve_item(self.name_target(ctx, for_name_checking)); + let base_name = target.base_name(ctx, for_name_checking); - let parent = ctx.resolve_item(self.parent_id()); - let parent_is_namespace = parent.is_module(); + // Named template type arguments are never namespaced, and never + // mangled. + if target.as_type().map_or(false, |ty| ty.is_named()) { + return base_name; + } - if self.is_toplevel(ctx) || (parent_is_namespace && count_namespaces) { - let base_name = self.make_exposed_name(None, base_name, ctx); + if within_namespace { return ctx.rust_mangle(&base_name).into_owned(); } - // TODO: allow modification of the mangling functions, maybe even per - // item type? - let parent_name = parent.canonical_name(ctx); - self.make_exposed_name(Some(parent_name), base_name, ctx) + // Concatenate this item's ancestors' names together. + let mut names: Vec<_> = target.parent_id() + .ancestors(ctx) + .filter(|id| *id != ctx.root_module()) + .map(|id| { + let item = ctx.resolve_item(id); + let target = ctx.resolve_item(item.name_target(ctx, false)); + target.base_name(ctx, false) + }) + .filter(|name| !name.is_empty()) + .collect(); + names.reverse(); + names.push(base_name); + let name = names.join("_"); + + ctx.rust_mangle(&name).into_owned() } fn exposed_id(&self, ctx: &BindgenContext) -> String { @@ -654,45 +693,6 @@ impl Item { format!("id_{}", self.id().as_usize()) } - fn make_exposed_name(&self, - parent_name: Option<String>, - base_name: Option<String>, - ctx: &BindgenContext) - -> String { - lazy_static! { - static ref RE_ENDS_WITH_BINDGEN_TY: Regex = - Regex::new(r"_bindgen_ty(_\d+)+$").unwrap(); - - static ref RE_ENDS_WITH_BINDGEN_MOD: Regex = - Regex::new(r"_bindgen_mod(_\d+)+$").unwrap(); - } - - let (re, kind) = match *self.kind() { - ItemKind::Module(..) => (&*RE_ENDS_WITH_BINDGEN_MOD, "mod"), - _ => (&*RE_ENDS_WITH_BINDGEN_TY, "ty"), - }; - - let parent_name = - parent_name.and_then(|n| if n.is_empty() { None } else { Some(n) }); - match (parent_name, base_name) { - (Some(parent), Some(base)) => format!("{}_{}", parent, base), - (Some(parent), None) => { - if re.is_match(parent.as_str()) { - format!("{}_{}", parent, self.exposed_id(ctx)) - } else { - format!("{}__bindgen_{}_{}", - parent, - kind, - self.exposed_id(ctx)) - } - } - (None, Some(base)) => base, - (None, None) => { - format!("_bindgen_{}_{}", kind, self.exposed_id(ctx)) - } - } - } - /// Get a mutable reference to this item's `Module`, or `None` if this is /// not a `Module` item. pub fn as_module_mut(&mut self) -> Option<&mut Module> { @@ -1032,7 +1032,8 @@ impl ClangItemParser for Item { // It's harmless, but if we restrict that, then // tests/headers/nsStyleAutoArray.hpp crashes. if let Err(ParseError::Recurse) = result { - warn!("Unknown type, assuming named template type: id = {:?}; spelling = {}", + warn!("Unknown type, assuming named template type: \ + id = {:?}; spelling = {}", id, ty.spelling()); Ok(Self::named_type_with_id(id, diff --git a/libbindgen/src/ir/ty.rs b/libbindgen/src/ir/ty.rs index b87dd11b..60611b8d 100644 --- a/libbindgen/src/ir/ty.rs +++ b/libbindgen/src/ir/ty.rs @@ -878,7 +878,9 @@ impl TypeCollector for Type { } // FIXME: Pending types! ref other @ _ => { - debug!("<Type as TypeCollector>::collect_types: Ignoring: {:?}", other); + debug!("<Type as TypeCollector>::collect_types: Ignoring: \ + {:?}", + other); } } } diff --git a/libbindgen/src/ir/var.rs b/libbindgen/src/ir/var.rs index 98b94da3..bbaea939 100644 --- a/libbindgen/src/ir/var.rs +++ b/libbindgen/src/ir/var.rs @@ -13,7 +13,7 @@ use super::ty::{FloatKind, TypeKind}; /// The type for a constant variable. #[derive(Debug)] pub enum VarType { - /// An boolean. + /// A boolean. Bool(bool), /// An integer. Int(i64), @@ -196,7 +196,6 @@ impl ClangSubItemParser for Var { let canonical_ty = ctx.safe_resolve_type(ty) .and_then(|t| t.safe_canonical_type(ctx)); - let is_bool = canonical_ty.map_or(false, |t| t.is_bool()); let is_integer = canonical_ty.map_or(false, |t| t.is_integer()); let is_float = canonical_ty.map_or(false, |t| t.is_float()); @@ -204,18 +203,26 @@ impl ClangSubItemParser for Var { // TODO: Strings, though the lookup is a bit more hard (we need // to look at the canonical type of the pointee too, and check // is char, u8, or i8 I guess). - let value = if is_bool { - cursor.evaluate().as_int() - .map(|val| VarType::Bool(val != 0)) - } else if is_integer { - cursor.evaluate() - .as_int() - .map(|val| val as i64) - .or_else(|| { - let tu = ctx.translation_unit(); - get_integer_literal_from_cursor(&cursor, tu) - }) - .map(VarType::Int) + let value = if is_integer { + let kind = match *canonical_ty.unwrap().kind() { + TypeKind::Int(kind) => kind, + _ => unreachable!(), + }; + + let mut val = + cursor.evaluate().as_int().map(|val| val as i64); + if val.is_none() || !kind.signedness_matches(val.unwrap()) { + let tu = ctx.translation_unit(); + val = get_integer_literal_from_cursor(&cursor, tu); + } + + val.map(|val| { + if kind == IntKind::Bool { + VarType::Bool(val != 0) + } else { + VarType::Int(val) + } + }) } else if is_float { cursor.evaluate() .as_double() diff --git a/libbindgen/tests/expectations/tests/bitfield_method_mangling.rs b/libbindgen/tests/expectations/tests/bitfield_method_mangling.rs new file mode 100644 index 00000000..5aba8abb --- /dev/null +++ b/libbindgen/tests/expectations/tests/bitfield_method_mangling.rs @@ -0,0 +1,49 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy)] +pub struct _bindgen_ty_1 { + pub _bitfield_1: u32, +} +#[test] +fn bindgen_test_layout__bindgen_ty_1() { + assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize); + assert_eq!(::std::mem::align_of::<_bindgen_ty_1>() , 4usize); +} +impl Clone for _bindgen_ty_1 { + fn clone(&self) -> Self { *self } +} +impl _bindgen_ty_1 { + #[inline] + pub fn pad3(&self) -> ::std::os::raw::c_uint { + unsafe { + ::std::mem::transmute(((self._bitfield_1 & (16777215usize as u32)) + >> 0u32) as u32) + } + } + #[inline] + pub fn set_pad3(&mut self, val: ::std::os::raw::c_uint) { + self._bitfield_1 &= !(16777215usize as u32); + self._bitfield_1 |= + ((val as u32 as u32) << 0u32) & (16777215usize as u32); + } + #[inline] + pub fn type_(&self) -> ::std::os::raw::c_uint { + unsafe { + ::std::mem::transmute(((self._bitfield_1 & + (4278190080usize as u32)) >> 24u32) as + u32) + } + } + #[inline] + pub fn set_type(&mut self, val: ::std::os::raw::c_uint) { + self._bitfield_1 &= !(4278190080usize as u32); + self._bitfield_1 |= + ((val as u32 as u32) << 24u32) & (4278190080usize as u32); + } +} +pub type mach_msg_type_descriptor_t = _bindgen_ty_1; diff --git a/libbindgen/tests/expectations/tests/class_with_inner_struct.rs b/libbindgen/tests/expectations/tests/class_with_inner_struct.rs index ab51396d..a1bacbdb 100644 --- a/libbindgen/tests/expectations/tests/class_with_inner_struct.rs +++ b/libbindgen/tests/expectations/tests/class_with_inner_struct.rs @@ -129,38 +129,42 @@ pub struct C { #[repr(C)] #[derive(Debug, Copy)] pub struct C__bindgen_ty_1 { - pub mFunc: __BindgenUnionField<C__bindgen_ty_1_1>, - pub __bindgen_anon_1: __BindgenUnionField<C__bindgen_ty_1_2>, + pub mFunc: __BindgenUnionField<C__bindgen_ty_1__bindgen_ty_1>, + pub __bindgen_anon_1: __BindgenUnionField<C__bindgen_ty_1__bindgen_ty_2>, pub bindgen_union_field: [u32; 4usize], } #[repr(C)] #[derive(Debug, Copy)] -pub struct C__bindgen_ty_1_1 { +pub struct C__bindgen_ty_1__bindgen_ty_1 { pub mX1: f32, pub mY1: f32, pub mX2: f32, pub mY2: f32, } #[test] -fn bindgen_test_layout_C__bindgen_ty_1_1() { - assert_eq!(::std::mem::size_of::<C__bindgen_ty_1_1>() , 16usize); - assert_eq!(::std::mem::align_of::<C__bindgen_ty_1_1>() , 4usize); +fn bindgen_test_layout_C__bindgen_ty_1__bindgen_ty_1() { + assert_eq!(::std::mem::size_of::<C__bindgen_ty_1__bindgen_ty_1>() , + 16usize); + assert_eq!(::std::mem::align_of::<C__bindgen_ty_1__bindgen_ty_1>() , + 4usize); } -impl Clone for C__bindgen_ty_1_1 { +impl Clone for C__bindgen_ty_1__bindgen_ty_1 { fn clone(&self) -> Self { *self } } #[repr(C)] #[derive(Debug, Copy)] -pub struct C__bindgen_ty_1_2 { +pub struct C__bindgen_ty_1__bindgen_ty_2 { pub mStepSyntax: StepSyntax, pub mSteps: ::std::os::raw::c_uint, } #[test] -fn bindgen_test_layout_C__bindgen_ty_1_2() { - assert_eq!(::std::mem::size_of::<C__bindgen_ty_1_2>() , 8usize); - assert_eq!(::std::mem::align_of::<C__bindgen_ty_1_2>() , 4usize); +fn bindgen_test_layout_C__bindgen_ty_1__bindgen_ty_2() { + assert_eq!(::std::mem::size_of::<C__bindgen_ty_1__bindgen_ty_2>() , + 8usize); + assert_eq!(::std::mem::align_of::<C__bindgen_ty_1__bindgen_ty_2>() , + 4usize); } -impl Clone for C__bindgen_ty_1_2 { +impl Clone for C__bindgen_ty_1__bindgen_ty_2 { fn clone(&self) -> Self { *self } } #[test] diff --git a/libbindgen/tests/expectations/tests/constant-evaluate.rs b/libbindgen/tests/expectations/tests/constant-evaluate.rs index 27b8d829..6947be98 100644 --- a/libbindgen/tests/expectations/tests/constant-evaluate.rs +++ b/libbindgen/tests/expectations/tests/constant-evaluate.rs @@ -9,6 +9,9 @@ pub const bar: _bindgen_ty_1 = _bindgen_ty_1::bar; #[repr(u32)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum _bindgen_ty_1 { foo = 4, bar = 8, } +pub type EasyToOverflow = ::std::os::raw::c_ulonglong; +pub const k: EasyToOverflow = 2147483648; +pub const k_expr: EasyToOverflow = 0; pub const BAZ: ::std::os::raw::c_longlong = 24; pub const fuzz: f64 = 51.; pub const BAZZ: ::std::os::raw::c_char = 53; diff --git a/libbindgen/tests/expectations/tests/elaborated.rs b/libbindgen/tests/expectations/tests/elaborated.rs index db373d41..0e8f4ee5 100644 --- a/libbindgen/tests/expectations/tests/elaborated.rs +++ b/libbindgen/tests/expectations/tests/elaborated.rs @@ -4,8 +4,8 @@ #![allow(non_snake_case)] -pub type whatever_t = ::std::os::raw::c_int; +pub type whatever_whatever_t = ::std::os::raw::c_int; extern "C" { #[link_name = "_Z9somethingPKi"] - pub fn something(wat: *const whatever_t); + pub fn something(wat: *const whatever_whatever_t); } diff --git a/libbindgen/tests/expectations/tests/inherit-namespaced.rs b/libbindgen/tests/expectations/tests/inherit-namespaced.rs index a58058b0..c0d2e5a6 100644 --- a/libbindgen/tests/expectations/tests/inherit-namespaced.rs +++ b/libbindgen/tests/expectations/tests/inherit-namespaced.rs @@ -6,7 +6,7 @@ #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct RootedBase<T> { +pub struct js_RootedBase<T> { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData<T>, } diff --git a/libbindgen/tests/expectations/tests/jsval_layout_opaque.rs b/libbindgen/tests/expectations/tests/jsval_layout_opaque.rs index 91f898af..530fdb22 100644 --- a/libbindgen/tests/expectations/tests/jsval_layout_opaque.rs +++ b/libbindgen/tests/expectations/tests/jsval_layout_opaque.rs @@ -154,24 +154,24 @@ impl jsval_layout__bindgen_ty_1 { #[repr(C)] #[derive(Debug, Copy)] pub struct jsval_layout__bindgen_ty_2 { - pub payload: jsval_layout__bindgen_ty_2_1, + pub payload: jsval_layout__bindgen_ty_2__bindgen_ty_1, } #[repr(C)] #[derive(Debug, Copy)] -pub struct jsval_layout__bindgen_ty_2_1 { +pub struct jsval_layout__bindgen_ty_2__bindgen_ty_1 { pub i32: __BindgenUnionField<i32>, pub u32: __BindgenUnionField<u32>, pub why: __BindgenUnionField<JSWhyMagic>, pub bindgen_union_field: u32, } #[test] -fn bindgen_test_layout_jsval_layout__bindgen_ty_2_1() { - assert_eq!(::std::mem::size_of::<jsval_layout__bindgen_ty_2_1>() , - 4usize); - assert_eq!(::std::mem::align_of::<jsval_layout__bindgen_ty_2_1>() , - 4usize); +fn bindgen_test_layout_jsval_layout__bindgen_ty_2__bindgen_ty_1() { + assert_eq!(::std::mem::size_of::<jsval_layout__bindgen_ty_2__bindgen_ty_1>() + , 4usize); + assert_eq!(::std::mem::align_of::<jsval_layout__bindgen_ty_2__bindgen_ty_1>() + , 4usize); } -impl Clone for jsval_layout__bindgen_ty_2_1 { +impl Clone for jsval_layout__bindgen_ty_2__bindgen_ty_1 { fn clone(&self) -> Self { *self } } #[test] diff --git a/libbindgen/tests/expectations/tests/overloading.rs b/libbindgen/tests/expectations/tests/overloading.rs index 4e138e7d..71002e23 100644 --- a/libbindgen/tests/expectations/tests/overloading.rs +++ b/libbindgen/tests/expectations/tests/overloading.rs @@ -15,9 +15,9 @@ extern "C" { } extern "C" { #[link_name = "_ZN3foo10MyFunctionEv"] - pub fn MyFunction(); + pub fn foo_MyFunction(); } extern "C" { #[link_name = "_ZN3bar10MyFunctionEv"] - pub fn MyFunction1(); + pub fn bar_MyFunction(); } diff --git a/libbindgen/tests/expectations/tests/replace_template_alias.rs b/libbindgen/tests/expectations/tests/replace_template_alias.rs index 61a2fbcc..b9a0d0c9 100644 --- a/libbindgen/tests/expectations/tests/replace_template_alias.rs +++ b/libbindgen/tests/expectations/tests/replace_template_alias.rs @@ -6,10 +6,10 @@ #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct Rooted<T> { - pub ptr: MaybeWrapped<T>, +pub struct JS_Rooted<T> { + pub ptr: JS_detail_MaybeWrapped<T>, } /// But the replacement type does use T! /// -/// <div rustbindgen replaces="MaybeWrapped" /> -pub type MaybeWrapped<T> = T; +/// <div rustbindgen replaces="JS_detail_MaybeWrapped" /> +pub type JS_detail_MaybeWrapped<T> = T; diff --git a/libbindgen/tests/expectations/tests/same_struct_name_in_different_namespaces.rs b/libbindgen/tests/expectations/tests/same_struct_name_in_different_namespaces.rs new file mode 100644 index 00000000..8e7c177b --- /dev/null +++ b/libbindgen/tests/expectations/tests/same_struct_name_in_different_namespaces.rs @@ -0,0 +1,28 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy)] +pub struct JS_Zone { + pub _address: u8, +} +impl Clone for JS_Zone { + fn clone(&self) -> Self { *self } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct JS_shadow_Zone { + pub x: ::std::os::raw::c_int, + pub y: ::std::os::raw::c_int, +} +#[test] +fn bindgen_test_layout_JS_shadow_Zone() { + assert_eq!(::std::mem::size_of::<JS_shadow_Zone>() , 8usize); + assert_eq!(::std::mem::align_of::<JS_shadow_Zone>() , 4usize); +} +impl Clone for JS_shadow_Zone { + fn clone(&self) -> Self { *self } +} diff --git a/libbindgen/tests/expectations/tests/struct_with_nesting.rs b/libbindgen/tests/expectations/tests/struct_with_nesting.rs index 97a0949e..b3e0a5ca 100644 --- a/libbindgen/tests/expectations/tests/struct_with_nesting.rs +++ b/libbindgen/tests/expectations/tests/struct_with_nesting.rs @@ -38,38 +38,42 @@ pub struct foo { #[derive(Debug, Copy)] pub struct foo__bindgen_ty_1 { pub b: __BindgenUnionField<::std::os::raw::c_uint>, - pub __bindgen_anon_1: __BindgenUnionField<foo__bindgen_ty_1_1>, - pub __bindgen_anon_2: __BindgenUnionField<foo__bindgen_ty_1_2>, + pub __bindgen_anon_1: __BindgenUnionField<foo__bindgen_ty_1__bindgen_ty_1>, + pub __bindgen_anon_2: __BindgenUnionField<foo__bindgen_ty_1__bindgen_ty_2>, pub bindgen_union_field: u32, } #[repr(C)] #[derive(Debug, Copy)] -pub struct foo__bindgen_ty_1_1 { +pub struct foo__bindgen_ty_1__bindgen_ty_1 { pub c1: ::std::os::raw::c_ushort, pub c2: ::std::os::raw::c_ushort, } #[test] -fn bindgen_test_layout_foo__bindgen_ty_1_1() { - assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1_1>() , 4usize); - assert_eq!(::std::mem::align_of::<foo__bindgen_ty_1_1>() , 2usize); +fn bindgen_test_layout_foo__bindgen_ty_1__bindgen_ty_1() { + assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1__bindgen_ty_1>() , + 4usize); + assert_eq!(::std::mem::align_of::<foo__bindgen_ty_1__bindgen_ty_1>() , + 2usize); } -impl Clone for foo__bindgen_ty_1_1 { +impl Clone for foo__bindgen_ty_1__bindgen_ty_1 { fn clone(&self) -> Self { *self } } #[repr(C)] #[derive(Debug, Copy)] -pub struct foo__bindgen_ty_1_2 { +pub struct foo__bindgen_ty_1__bindgen_ty_2 { pub d1: ::std::os::raw::c_uchar, pub d2: ::std::os::raw::c_uchar, pub d3: ::std::os::raw::c_uchar, pub d4: ::std::os::raw::c_uchar, } #[test] -fn bindgen_test_layout_foo__bindgen_ty_1_2() { - assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1_2>() , 4usize); - assert_eq!(::std::mem::align_of::<foo__bindgen_ty_1_2>() , 1usize); +fn bindgen_test_layout_foo__bindgen_ty_1__bindgen_ty_2() { + assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1__bindgen_ty_2>() , + 4usize); + assert_eq!(::std::mem::align_of::<foo__bindgen_ty_1__bindgen_ty_2>() , + 1usize); } -impl Clone for foo__bindgen_ty_1_2 { +impl Clone for foo__bindgen_ty_1__bindgen_ty_2 { fn clone(&self) -> Self { *self } } #[test] diff --git a/libbindgen/tests/expectations/tests/template_alias.rs b/libbindgen/tests/expectations/tests/template_alias.rs index 6457381f..d301a11c 100644 --- a/libbindgen/tests/expectations/tests/template_alias.rs +++ b/libbindgen/tests/expectations/tests/template_alias.rs @@ -4,9 +4,9 @@ #![allow(non_snake_case)] -pub type Wrapped<T> = T; +pub type JS_detail_Wrapped<T> = T; #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct Rooted<T> { - pub ptr: Wrapped<T>, +pub struct JS_Rooted<T> { + pub ptr: JS_detail_Wrapped<T>, } diff --git a/libbindgen/tests/expectations/tests/typeref.rs b/libbindgen/tests/expectations/tests/typeref.rs index da944146..21217ced 100644 --- a/libbindgen/tests/expectations/tests/typeref.rs +++ b/libbindgen/tests/expectations/tests/typeref.rs @@ -31,7 +31,7 @@ impl <T> ::std::fmt::Debug for __BindgenUnionField<T> { #[repr(C)] #[derive(Debug, Copy)] pub struct nsFoo { - pub mBar: StyleShapeSource<::std::os::raw::c_int>, + pub mBar: mozilla_StyleShapeSource<::std::os::raw::c_int>, } #[test] fn bindgen_test_layout_nsFoo() { @@ -43,28 +43,28 @@ impl Clone for nsFoo { } #[repr(C)] #[derive(Debug, Copy)] -pub struct FragmentOrURL { +pub struct mozilla_FragmentOrURL { pub mIsLocalRef: bool, } #[test] -fn bindgen_test_layout_FragmentOrURL() { - assert_eq!(::std::mem::size_of::<FragmentOrURL>() , 1usize); - assert_eq!(::std::mem::align_of::<FragmentOrURL>() , 1usize); +fn bindgen_test_layout_mozilla_FragmentOrURL() { + assert_eq!(::std::mem::size_of::<mozilla_FragmentOrURL>() , 1usize); + assert_eq!(::std::mem::align_of::<mozilla_FragmentOrURL>() , 1usize); } -impl Clone for FragmentOrURL { +impl Clone for mozilla_FragmentOrURL { fn clone(&self) -> Self { *self } } #[repr(C)] #[derive(Debug, Copy)] -pub struct Position { +pub struct mozilla_Position { pub _address: u8, } #[test] -fn bindgen_test_layout_Position() { - assert_eq!(::std::mem::size_of::<Position>() , 1usize); - assert_eq!(::std::mem::align_of::<Position>() , 1usize); +fn bindgen_test_layout_mozilla_Position() { + assert_eq!(::std::mem::size_of::<mozilla_Position>() , 1usize); + assert_eq!(::std::mem::align_of::<mozilla_Position>() , 1usize); } -impl Clone for Position { +impl Clone for mozilla_Position { fn clone(&self) -> Self { *self } } #[repr(C)] @@ -82,15 +82,15 @@ impl Clone for Bar { } #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct StyleShapeSource<ReferenceBox> { - pub __bindgen_anon_1: StyleShapeSource__bindgen_ty_1<ReferenceBox>, +pub struct mozilla_StyleShapeSource<ReferenceBox> { + pub __bindgen_anon_1: mozilla_StyleShapeSource__bindgen_ty_1<ReferenceBox>, pub _phantom_0: ::std::marker::PhantomData<ReferenceBox>, } #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct StyleShapeSource__bindgen_ty_1<ReferenceBox> { - pub mPosition: __BindgenUnionField<*mut Position>, - pub mFragmentOrURL: __BindgenUnionField<*mut FragmentOrURL>, +pub struct mozilla_StyleShapeSource__bindgen_ty_1<ReferenceBox> { + pub mPosition: __BindgenUnionField<*mut mozilla_Position>, + pub mFragmentOrURL: __BindgenUnionField<*mut mozilla_FragmentOrURL>, pub bindgen_union_field: u64, pub _phantom_0: ::std::marker::PhantomData<ReferenceBox>, } diff --git a/libbindgen/tests/expectations/tests/union_with_nesting.rs b/libbindgen/tests/expectations/tests/union_with_nesting.rs index 61c08a2a..af9e442d 100644 --- a/libbindgen/tests/expectations/tests/union_with_nesting.rs +++ b/libbindgen/tests/expectations/tests/union_with_nesting.rs @@ -38,37 +38,41 @@ pub struct foo { #[repr(C)] #[derive(Debug, Copy)] pub struct foo__bindgen_ty_1 { - pub __bindgen_anon_1: foo__bindgen_ty_1_1, - pub __bindgen_anon_2: foo__bindgen_ty_1_2, + pub __bindgen_anon_1: foo__bindgen_ty_1__bindgen_ty_1, + pub __bindgen_anon_2: foo__bindgen_ty_1__bindgen_ty_2, } #[repr(C)] #[derive(Debug, Copy)] -pub struct foo__bindgen_ty_1_1 { +pub struct foo__bindgen_ty_1__bindgen_ty_1 { pub b1: __BindgenUnionField<::std::os::raw::c_ushort>, pub b2: __BindgenUnionField<::std::os::raw::c_ushort>, pub bindgen_union_field: u16, } #[test] -fn bindgen_test_layout_foo__bindgen_ty_1_1() { - assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1_1>() , 2usize); - assert_eq!(::std::mem::align_of::<foo__bindgen_ty_1_1>() , 2usize); +fn bindgen_test_layout_foo__bindgen_ty_1__bindgen_ty_1() { + assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1__bindgen_ty_1>() , + 2usize); + assert_eq!(::std::mem::align_of::<foo__bindgen_ty_1__bindgen_ty_1>() , + 2usize); } -impl Clone for foo__bindgen_ty_1_1 { +impl Clone for foo__bindgen_ty_1__bindgen_ty_1 { fn clone(&self) -> Self { *self } } #[repr(C)] #[derive(Debug, Copy)] -pub struct foo__bindgen_ty_1_2 { +pub struct foo__bindgen_ty_1__bindgen_ty_2 { pub c1: __BindgenUnionField<::std::os::raw::c_ushort>, pub c2: __BindgenUnionField<::std::os::raw::c_ushort>, pub bindgen_union_field: u16, } #[test] -fn bindgen_test_layout_foo__bindgen_ty_1_2() { - assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1_2>() , 2usize); - assert_eq!(::std::mem::align_of::<foo__bindgen_ty_1_2>() , 2usize); +fn bindgen_test_layout_foo__bindgen_ty_1__bindgen_ty_2() { + assert_eq!(::std::mem::size_of::<foo__bindgen_ty_1__bindgen_ty_2>() , + 2usize); + assert_eq!(::std::mem::align_of::<foo__bindgen_ty_1__bindgen_ty_2>() , + 2usize); } -impl Clone for foo__bindgen_ty_1_2 { +impl Clone for foo__bindgen_ty_1__bindgen_ty_2 { fn clone(&self) -> Self { *self } } #[test] diff --git a/libbindgen/tests/headers/bitfield_method_mangling.h b/libbindgen/tests/headers/bitfield_method_mangling.h new file mode 100644 index 00000000..257648ee --- /dev/null +++ b/libbindgen/tests/headers/bitfield_method_mangling.h @@ -0,0 +1,5 @@ +typedef struct +{ + unsigned int pad3: 24; + unsigned int type: 8; +} mach_msg_type_descriptor_t; diff --git a/libbindgen/tests/headers/constant-evaluate.h b/libbindgen/tests/headers/constant-evaluate.h index 2790d603..b6a0492f 100644 --- a/libbindgen/tests/headers/constant-evaluate.h +++ b/libbindgen/tests/headers/constant-evaluate.h @@ -5,6 +5,11 @@ enum { bar = 8, }; +typedef unsigned long long EasyToOverflow; +const EasyToOverflow k = 0x80000000; + +const EasyToOverflow k_expr = 1ULL << 60; + const long long BAZ = (1 << foo) | bar; const double fuzz = (1 + 50.0f); const char BAZZ = '5'; diff --git a/libbindgen/tests/headers/replace_template_alias.hpp b/libbindgen/tests/headers/replace_template_alias.hpp index 6ceae4e5..b0648994 100644 --- a/libbindgen/tests/headers/replace_template_alias.hpp +++ b/libbindgen/tests/headers/replace_template_alias.hpp @@ -18,6 +18,6 @@ class Rooted { /// But the replacement type does use T! /// -/// <div rustbindgen replaces="MaybeWrapped" /> +/// <div rustbindgen replaces="JS_detail_MaybeWrapped" /> template <typename T> using replaces_MaybeWrapped = T; diff --git a/libbindgen/tests/headers/same_struct_name_in_different_namespaces.hpp b/libbindgen/tests/headers/same_struct_name_in_different_namespaces.hpp new file mode 100644 index 00000000..fe685845 --- /dev/null +++ b/libbindgen/tests/headers/same_struct_name_in_different_namespaces.hpp @@ -0,0 +1,12 @@ +namespace JS { + + struct Zone; + + namespace shadow { + + struct Zone { + int x; + int y; + }; + } +} |