From 276695159b3462a625cf5c42bc07aef19cfb0a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 23 Dec 2016 12:11:03 +0100 Subject: [PATCH 1/5] ir: Don't parse default template parameter types. It's a bunch of complexity we don't use nor need. --- libbindgen/src/ir/comp.rs | 6 ------ libbindgen/src/ir/item.rs | 11 ++++------- libbindgen/src/ir/ty.rs | 22 +++++++--------------- libbindgen/src/parse.rs | 2 -- 4 files changed, 11 insertions(+), 30 deletions(-) diff --git a/libbindgen/src/ir/comp.rs b/libbindgen/src/ir/comp.rs index d5d3426245..eaccda8efa 100644 --- a/libbindgen/src/ir/comp.rs +++ b/libbindgen/src/ir/comp.rs @@ -644,13 +644,7 @@ impl CompInfo { return CXChildVisit_Continue; } - let default_type = Item::from_ty(&cur.cur_type(), - Some(cur), - Some(potential_id), - ctx) - .ok(); let param = Item::named_type(cur.spelling(), - default_type, potential_id, ctx); ci.template_args.push(param); diff --git a/libbindgen/src/ir/item.rs b/libbindgen/src/ir/item.rs index 0d5e6ba2ad..932b06fc9f 100644 --- a/libbindgen/src/ir/item.rs +++ b/libbindgen/src/ir/item.rs @@ -499,8 +499,8 @@ impl Item { parent_template_args.iter().any(|parent_item| { let parent_ty = ctx.resolve_type(*parent_item); match (parent_ty.kind(), item_ty.kind()) { - (&TypeKind::Named(ref n, _), - &TypeKind::Named(ref i, _)) => n == i, + (&TypeKind::Named(ref n), + &TypeKind::Named(ref i)) => n == i, _ => false, } }) @@ -1163,7 +1163,6 @@ impl ClangItemParser for Item { ty.spelling()); Ok(Self::named_type_with_id(id, ty.spelling(), - None, relevant_parent_id, ctx)) } else { @@ -1189,7 +1188,6 @@ impl ClangItemParser for Item { /// available yet. fn named_type_with_id(id: ItemId, name: S, - default: Option, parent_id: ItemId, ctx: &mut BindgenContext) -> ItemId @@ -1203,7 +1201,7 @@ impl ClangItemParser for Item { None, None, parent_id, - ItemKind::Type(Type::named(name, default))), + ItemKind::Type(Type::named(name))), None, None); @@ -1211,14 +1209,13 @@ impl ClangItemParser for Item { } fn named_type(name: S, - default: Option, parent_id: ItemId, ctx: &mut BindgenContext) -> ItemId where S: Into, { let id = ctx.next_item_id(); - Self::named_type_with_id(id, name, default, parent_id, ctx) + Self::named_type_with_id(id, name, parent_id, ctx) } } diff --git a/libbindgen/src/ir/ty.rs b/libbindgen/src/ir/ty.rs index 60092d5449..b04afeb6af 100644 --- a/libbindgen/src/ir/ty.rs +++ b/libbindgen/src/ir/ty.rs @@ -140,10 +140,10 @@ impl Type { } /// Creates a new named type, with name `name`. - pub fn named(name: String, default: Option) -> Self { + pub fn named(name: String) -> Self { assert!(!name.is_empty()); // TODO: stop duplicating the name, it's stupid. - let kind = TypeKind::Named(name.clone(), default); + let kind = TypeKind::Named(name.clone()); Self::new(Some(name), None, kind, false) } @@ -318,12 +318,12 @@ impl Type { ty: &Type) -> bool { let name = match *ty.kind() { - TypeKind::Named(ref name, _) => name, + TypeKind::Named(ref name) => name, ref other @ _ => unreachable!("Not a named type: {:?}", other), }; match self.kind { - TypeKind::Named(ref this_name, _) => this_name == name, + TypeKind::Named(ref this_name) => this_name == name, TypeKind::ResolvedTypeRef(t) | TypeKind::Array(t, _) | TypeKind::Pointer(t) | @@ -478,9 +478,8 @@ pub enum TypeKind { /// replace one type with another. ResolvedTypeRef(ItemId), - /// A named type, that is, a template parameter, with an optional default - /// type. - Named(String, Option), + /// A named type, that is, a template parameter. + Named(String), } impl Type { @@ -675,15 +674,8 @@ impl Type { return CXChildVisit_Continue; } - let default_type = - Item::from_ty(&cur.cur_type(), - Some(cur), - Some(potential_id), - ctx) - .ok(); let param = Item::named_type(cur.spelling(), - default_type, potential_id, ctx); args.push(param); @@ -903,7 +895,6 @@ impl TypeCollector for Type { TypeKind::Reference(inner) | TypeKind::Array(inner, _) | TypeKind::Alias(_, inner) | - TypeKind::Named(_, Some(inner)) | TypeKind::ResolvedTypeRef(inner) => { types.insert(inner); } @@ -919,6 +910,7 @@ impl TypeCollector for Type { TypeKind::Function(ref sig) => { sig.collect_types(context, types, item) } + TypeKind::Named(_) => {}, // FIXME: Pending types! ref other @ _ => { debug!("::collect_types: Ignoring: \ diff --git a/libbindgen/src/parse.rs b/libbindgen/src/parse.rs index 28e6575945..0e4164f0b3 100644 --- a/libbindgen/src/parse.rs +++ b/libbindgen/src/parse.rs @@ -82,7 +82,6 @@ pub trait ClangItemParser: Sized { /// Create a named template type. fn named_type(name: S, - default: Option, parent: ItemId, context: &mut BindgenContext) -> ItemId @@ -92,7 +91,6 @@ pub trait ClangItemParser: Sized { /// `ItemId`. fn named_type_with_id(id: ItemId, name: S, - default: Option, parent: ItemId, context: &mut BindgenContext) -> ItemId From c681687ed045d0d8e73bae51fa2f22dcf2c6d517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 23 Dec 2016 12:28:51 +0100 Subject: [PATCH 2/5] ir: Don't assume that base classes are already resolved. Fixes #359 --- libbindgen/src/ir/comp.rs | 3 +-- .../tests/expectations/tests/anon_union.rs | 14 +++++----- .../bad-namespace-parenthood-inheritance.rs | 20 ++++++++++++++ libbindgen/tests/expectations/tests/crtp.rs | 24 ++++++++--------- .../forward-inherit-struct-with-fields.rs | 10 +++---- .../tests/forward-inherit-struct.rs | 4 +-- .../tests/vtable_recursive_sig.rs | 26 +++++++++---------- .../bad-namespace-parenthood-inheritance.hpp | 15 +++++++++++ 8 files changed, 75 insertions(+), 41 deletions(-) create mode 100644 libbindgen/tests/expectations/tests/bad-namespace-parenthood-inheritance.rs create mode 100644 libbindgen/tests/headers/bad-namespace-parenthood-inheritance.hpp diff --git a/libbindgen/src/ir/comp.rs b/libbindgen/src/ir/comp.rs index eaccda8efa..1c69e6189d 100644 --- a/libbindgen/src/ir/comp.rs +++ b/libbindgen/src/ir/comp.rs @@ -654,8 +654,7 @@ impl CompInfo { ci.has_vtable = cur.is_virtual_base(); } let type_id = - Item::from_ty(&cur.cur_type(), Some(cur), None, ctx) - .expect("BaseSpecifier"); + Item::from_ty_or_ref(cur.cur_type(), Some(cur), None, ctx); ci.base_members.push(type_id); } CXCursor_Constructor | diff --git a/libbindgen/tests/expectations/tests/anon_union.rs b/libbindgen/tests/expectations/tests/anon_union.rs index 8ae92b316a..f8559ca920 100644 --- a/libbindgen/tests/expectations/tests/anon_union.rs +++ b/libbindgen/tests/expectations/tests/anon_union.rs @@ -62,13 +62,6 @@ pub struct TErrorResult__bindgen_ty_1 { pub bindgen_union_field: u64, pub _phantom_0: ::std::marker::PhantomData, } -#[test] -fn __bindgen_test_layout_template_1() { - assert_eq!(::std::mem::size_of::>() , - 24usize); - assert_eq!(::std::mem::align_of::>() , - 8usize); -} #[repr(C)] #[derive(Debug, Copy)] pub struct ErrorResult { @@ -82,3 +75,10 @@ fn bindgen_test_layout_ErrorResult() { impl Clone for ErrorResult { fn clone(&self) -> Self { *self } } +#[test] +fn __bindgen_test_layout_template_1() { + assert_eq!(::std::mem::size_of::>() , + 24usize); + assert_eq!(::std::mem::align_of::>() , + 8usize); +} diff --git a/libbindgen/tests/expectations/tests/bad-namespace-parenthood-inheritance.rs b/libbindgen/tests/expectations/tests/bad-namespace-parenthood-inheritance.rs new file mode 100644 index 0000000000..553879b7a8 --- /dev/null +++ b/libbindgen/tests/expectations/tests/bad-namespace-parenthood-inheritance.rs @@ -0,0 +1,20 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct std_char_traits<_CharT> { + pub _address: u8, + pub _phantom_0: ::std::marker::PhantomData<_CharT>, +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct __gnu_cxx_char_traits { + pub _address: u8, +} +impl Clone for __gnu_cxx_char_traits { + fn clone(&self) -> Self { *self } +} diff --git a/libbindgen/tests/expectations/tests/crtp.rs b/libbindgen/tests/expectations/tests/crtp.rs index 109a768a62..cc488fd6c3 100644 --- a/libbindgen/tests/expectations/tests/crtp.rs +++ b/libbindgen/tests/expectations/tests/crtp.rs @@ -10,11 +10,6 @@ pub struct Base { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData, } -#[test] -fn __bindgen_test_layout_template_1() { - assert_eq!(::std::mem::size_of::>() , 1usize); - assert_eq!(::std::mem::align_of::>() , 1usize); -} #[repr(C)] #[derive(Debug, Copy)] pub struct Derived { @@ -34,13 +29,6 @@ pub struct BaseWithDestructor { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData, } -#[test] -fn __bindgen_test_layout_template_2() { - assert_eq!(::std::mem::size_of::>() - , 1usize); - assert_eq!(::std::mem::align_of::>() - , 1usize); -} #[repr(C)] #[derive(Debug)] pub struct DerivedFromBaseWithDestructor { @@ -53,3 +41,15 @@ fn bindgen_test_layout_DerivedFromBaseWithDestructor() { assert_eq!(::std::mem::align_of::() , 1usize); } +#[test] +fn __bindgen_test_layout_template_1() { + assert_eq!(::std::mem::size_of::>() , 1usize); + assert_eq!(::std::mem::align_of::>() , 1usize); +} +#[test] +fn __bindgen_test_layout_template_2() { + assert_eq!(::std::mem::size_of::>() + , 1usize); + assert_eq!(::std::mem::align_of::>() + , 1usize); +} diff --git a/libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs b/libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs index fc24e98921..e377b3ad2a 100644 --- a/libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs +++ b/libbindgen/tests/expectations/tests/forward-inherit-struct-with-fields.rs @@ -6,12 +6,12 @@ #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct RootedBase { - pub foo: *mut T, - pub next: *mut Rooted, +pub struct Rooted { + pub _base: js_RootedBase, } #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct Rooted { - pub _base: RootedBase, +pub struct js_RootedBase { + pub foo: *mut T, + pub next: *mut Rooted, } diff --git a/libbindgen/tests/expectations/tests/forward-inherit-struct.rs b/libbindgen/tests/expectations/tests/forward-inherit-struct.rs index a58058b002..5de70fa9fa 100644 --- a/libbindgen/tests/expectations/tests/forward-inherit-struct.rs +++ b/libbindgen/tests/expectations/tests/forward-inherit-struct.rs @@ -6,13 +6,13 @@ #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct RootedBase { +pub struct Rooted { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData, } #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct Rooted { +pub struct js_RootedBase { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData, } diff --git a/libbindgen/tests/expectations/tests/vtable_recursive_sig.rs b/libbindgen/tests/expectations/tests/vtable_recursive_sig.rs index 773123367f..ce62eeb0fc 100644 --- a/libbindgen/tests/expectations/tests/vtable_recursive_sig.rs +++ b/libbindgen/tests/expectations/tests/vtable_recursive_sig.rs @@ -4,6 +4,19 @@ #![allow(non_snake_case)] +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Derived { + pub _base: Base, +} +#[test] +fn bindgen_test_layout_Derived() { + assert_eq!(::std::mem::size_of::() , 8usize); + assert_eq!(::std::mem::align_of::() , 8usize); +} +impl Clone for Derived { + fn clone(&self) -> Self { *self } +} #[repr(C)] pub struct Base__bindgen_vtable { } @@ -20,16 +33,3 @@ fn bindgen_test_layout_Base() { impl Clone for Base { fn clone(&self) -> Self { *self } } -#[repr(C)] -#[derive(Debug, Copy)] -pub struct Derived { - pub _base: Base, -} -#[test] -fn bindgen_test_layout_Derived() { - assert_eq!(::std::mem::size_of::() , 8usize); - assert_eq!(::std::mem::align_of::() , 8usize); -} -impl Clone for Derived { - fn clone(&self) -> Self { *self } -} diff --git a/libbindgen/tests/headers/bad-namespace-parenthood-inheritance.hpp b/libbindgen/tests/headers/bad-namespace-parenthood-inheritance.hpp new file mode 100644 index 0000000000..ce21a40158 --- /dev/null +++ b/libbindgen/tests/headers/bad-namespace-parenthood-inheritance.hpp @@ -0,0 +1,15 @@ +namespace std +{ + template < typename > struct char_traits; +} +namespace __gnu_cxx +{ + template < typename > struct char_traits; +} +namespace std +{ + template < class _CharT > struct char_traits:__gnu_cxx::char_traits < + _CharT > + { + }; +} From e470b4475dcd7f79042a4905d38bf393da23df44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 23 Dec 2016 12:37:16 +0100 Subject: [PATCH 3/5] lib: Remove unused libc dependency. --- libbindgen/Cargo.toml | 1 - libbindgen/src/lib.rs | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libbindgen/Cargo.toml b/libbindgen/Cargo.toml index 408d9fc1ad..0f7b0e183d 100644 --- a/libbindgen/Cargo.toml +++ b/libbindgen/Cargo.toml @@ -28,7 +28,6 @@ cexpr = "0.2" cfg-if = "0.1.0" clang-sys = { version = "0.12", features = ["runtime", "clang_3_9"] } lazy_static = "0.2.1" -libc = "0.2" rustc-serialize = "0.3.19" syntex_syntax = "0.50" regex = "0.1" diff --git a/libbindgen/src/lib.rs b/libbindgen/src/lib.rs index a74c1799e1..5054cd13ef 100644 --- a/libbindgen/src/lib.rs +++ b/libbindgen/src/lib.rs @@ -7,6 +7,7 @@ #![deny(missing_docs)] #![deny(warnings)] +#![deny(unused_extern_crates)] // We internally use the deprecated BindgenOptions all over the place. Once we // remove its `pub` declaration, we can un-deprecate it and remove this pragma. @@ -17,13 +18,13 @@ #![allow(non_upper_case_globals)] #[macro_use] +#[allow(unused_extern_crates)] extern crate cfg_if; extern crate cexpr; extern crate syntex_syntax as syntax; extern crate aster; extern crate quasi; extern crate clang_sys; -extern crate libc; extern crate regex; #[macro_use] extern crate lazy_static; From ae84a039ce3790091c529ee8ecf767de919659a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 23 Dec 2016 13:31:12 +0100 Subject: [PATCH 4/5] libbindgen: Bump version for crates.io --- libbindgen/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libbindgen/Cargo.toml b/libbindgen/Cargo.toml index 0f7b0e183d..e6e9a5db29 100644 --- a/libbindgen/Cargo.toml +++ b/libbindgen/Cargo.toml @@ -12,7 +12,7 @@ license = "BSD-3-Clause" name = "libbindgen" readme = "README.md" repository = "https://github.com/servo/rust-bindgen" -version = "0.1.3" +version = "0.1.4" workspace = ".." [dev-dependencies] From b93faf9015e91303a9cfb14281c1ebe5ca877acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 23 Dec 2016 17:55:16 +0100 Subject: [PATCH 5/5] Add test from #358. --- .../tests/expectations/tests/issue-358.rs | 20 +++++++++++++++++++ libbindgen/tests/headers/issue-358.hpp | 8 ++++++++ 2 files changed, 28 insertions(+) create mode 100644 libbindgen/tests/expectations/tests/issue-358.rs create mode 100644 libbindgen/tests/headers/issue-358.hpp diff --git a/libbindgen/tests/expectations/tests/issue-358.rs b/libbindgen/tests/expectations/tests/issue-358.rs new file mode 100644 index 0000000000..1b933d3437 --- /dev/null +++ b/libbindgen/tests/expectations/tests/issue-358.rs @@ -0,0 +1,20 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct JS_PersistentRooted { + pub _base: a, + pub _phantom_0: ::std::marker::PhantomData, +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct a { + pub b: *mut a, +} +impl Clone for a { + fn clone(&self) -> Self { *self } +} diff --git a/libbindgen/tests/headers/issue-358.hpp b/libbindgen/tests/headers/issue-358.hpp new file mode 100644 index 0000000000..b14521b73b --- /dev/null +++ b/libbindgen/tests/headers/issue-358.hpp @@ -0,0 +1,8 @@ +// bindgen-flags: -- -std=c++11 +namespace JS { +template class PersistentRooted; +} +template class a { a *b; }; +namespace JS { +template class PersistentRooted : a> {}; +}