diff options
-rw-r--r-- | CONTRIBUTING.md | 104 | ||||
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | src/codegen/mod.rs | 3 | ||||
-rw-r--r-- | src/ir/comp.rs | 4 | ||||
-rw-r--r-- | src/ir/ty.rs | 7 | ||||
-rwxr-xr-x | src/lib.rs | 15 | ||||
-rw-r--r-- | tests/expectations/class.rs | 49 | ||||
-rw-r--r-- | tests/expectations/complex.rs | 84 | ||||
-rw-r--r-- | tests/expectations/const_tparam.rs | 3 | ||||
-rw-r--r-- | tests/headers/class.hpp | 9 | ||||
-rw-r--r-- | tests/headers/complex.h | 13 | ||||
-rw-r--r-- | tests/headers/const_tparam.hpp | 1 |
12 files changed, 273 insertions, 25 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..3011a853 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,104 @@ +# Contributing to `servo/rust-bindgen` + +Hi! We'd love to have your contributions! If you want help or mentorship, reach +out to us in a GitHub issue, or stop by #servo on irc.mozilla.org and introduce +yourself. + +* [Code of Conduct](#coc) +* [Filing an Issue](#issue) +* [Building](#building) +* [Testing](#tests) + * [Overview](#tests-overview) + * [Running All Tests](#tests-all) + * [Running a Single, Specific Test](#tests-one) + * [Authoring New Tests](#tests-new) +* [Debug Logging](#logs) + +## Code of Conduct <span id="coc"/> + +We abide by the [Rust Code of Conduct][coc] and ask that you do as well. + +[coc]: https://www.rust-lang.org/en-US/conduct.html + +## Filing an Issue <span id="issue"/> + +Think you've found a bug? File an issue! To help us understand and reproduce the +issue, provide us with: + +* A (preferrably reduced) C/C++ header file that reproduces the issue +* The `bindgen` flags used to reproduce the issue with the header file +* The expected `bindgen` output +* The actual `bindgen` output +* The [debugging logs](#logs) generated when running `bindgen` on this testcase + +## Building <span id="building"/> + +Build instructions are in the [README](./README.md). + +## Testing <span id="tests"/> + +### Overview <span id="tests-overview"/> + +Input C/C++ test headers reside in the `tests/headers` directory. The expected +output rust bindings live in `tests/expectations`; for example, +`tests/headers/my_header.h`'s expected generated rust bindings would be +`tests/expectations/my_header.rs`. + +The `tests/tools/run-bindgen.py` script runs `bindgen` on the test headers and +compares the results to the expectations. + +### Running All Tests <span id="tests-all"/> + +``` +$ cargo test [--features llvm_stable] +``` + +This spawns a `tests/tools/run-bindgen.py` subprocess for each test header. + +### Running a Single, Specific Test <span id="tests-one"/> + +``` +$ ./tests/tools/run-bindgen.py ./target/debug/bindgen ./tests/headers/some_header.h +``` + +To learn more about the options available with `run-bindgen.py`: + +``` +$ ./tests/tools/run-bindgen.py --help +``` + +### Authoring New Tests <span id="tests-new"/> + +To add a new test header to the suite, simply put it in the `tests/headers` +directory. Next, run the `run-bindgen.py` script with the new test header, which +will generate the initial expected output rust bindings. + +If your new test requires certain flags to be passed to `bindgen`, you can +specify them at the top of the test header, with a comment like this: + +```c +// bindgen-flags: --enable-cxx-namespaces -- -std=c++14 +``` + +If your new test requires `bindgen` to be built with certain features, you can +specify the required features at the top of the test header in a similar manner: + +```c +// bingden-features: llvm_stable +``` + +## Debug Logging <span id="logs"/> + +To help debug what `bindgen` is doing, you can define the environment variable +`RUST_LOG=bindgen` to get a bunch of debugging log spew. + +``` +$ RUST_LOG=bindgen ./target/debug/bindgen [flags...] ~/path/to/some/header.h +``` + +This logging can also be used when debugging failing tests under +`run-bindgen.py`: + +``` +$ RUST_LOG=bindgen ./tests/tools/run-bindgen.py ./target/debug/bindgen tests/headers/whatever.h +``` @@ -76,12 +76,6 @@ $ LIBCLANG_PATH=path/to/clang-3.9/build/lib \ cargo build ``` -## Running Tests - -``` -$ make test -``` - # Command Line Usage There are a few options documented when running `./bindgen --help`. Other diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index a88b3b60..65900739 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1444,7 +1444,8 @@ impl ToRustTy for Type { if inner_ty.canonical_type(ctx).is_function() { ty } else { - ty.to_ptr(inner.expect_type().is_const(), ctx.span()) + let is_const = self.is_const() || inner.expect_type().is_const(); + ty.to_ptr(is_const, ctx.span()) } } TypeKind::Named(..) => { diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 1f8a0292..9d1a6366 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -547,8 +547,8 @@ impl CompInfo { 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); + let param = Item::named_type(cur.spelling(), default_type, + potential_id, ctx); ci.template_args.push(param); } CXCursor_CXXBaseSpecifier => { diff --git a/src/ir/ty.rs b/src/ir/ty.rs index d74b1a0e..bf45b9fc 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -631,6 +631,13 @@ impl Type { .expect("Not able to resolve array element?"); TypeKind::Array(inner, ty.num_elements()) } + // A complex number is always a real and an imaginary part, so + // represent that as a two-item array. + CXType_Complex => { + let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx) + .expect("Not able to resolve array element?"); + TypeKind::Array(inner, 2) + } #[cfg(not(feature="llvm_stable"))] CXType_Elaborated => { return Self::from_clang_ty(potential_id, &ty.named(), @@ -59,11 +59,6 @@ impl Builder { self.clang_arg(header) } - pub fn match_pat<T: Into<String>>(mut self, arg: T) -> Builder { - self.options.match_pat.insert(arg.into()); - self - } - pub fn hide_type<T: Into<String>>(mut self, arg: T) -> Builder { self.options.hidden_types.insert(arg.into()); self @@ -114,11 +109,6 @@ impl Builder { self } - pub fn dtor_attr<T: Into<String>>(mut self, attr: T) -> Builder { - self.options.dtor_attrs.push(attr.into()); - self - } - pub fn forbid_unknown_types(mut self) -> Builder { self.options.fail_on_unknown_type = true; self @@ -161,7 +151,6 @@ impl Builder { /// Deprecated - use a `Builder` instead #[derive(Debug)] pub struct BindgenOptions { - pub match_pat: HashSet<String>, pub hidden_types: HashSet<String>, pub opaque_types: HashSet<String>, pub whitelisted_types: RegexSet, @@ -188,15 +177,12 @@ pub struct BindgenOptions { pub msvc_mangling: bool, pub override_enum_ty: String, pub raw_lines: Vec<String>, - /// Attributes for a type with destructor - pub dtor_attrs: Vec<String>, pub clang_args: Vec<String>, } impl Default for BindgenOptions { fn default() -> BindgenOptions { BindgenOptions { - match_pat: Default::default(), hidden_types: Default::default(), opaque_types: Default::default(), whitelisted_types: Default::default(), @@ -219,7 +205,6 @@ impl Default for BindgenOptions { namespaced_constants: true, msvc_mangling: false, raw_lines: vec![], - dtor_attrs: vec![], clang_args: vec![], } } diff --git a/tests/expectations/class.rs b/tests/expectations/class.rs index 5951e0e6..4f736342 100644 --- a/tests/expectations/class.rs +++ b/tests/expectations/class.rs @@ -72,3 +72,52 @@ fn bindgen_test_layout_WithUnion() { impl Clone for WithUnion { fn clone(&self) -> Self { *self } } +#[repr(C)] +#[derive(Debug, Copy)] +pub struct RealAbstractionWithTonsOfMethods { + pub _address: u8, +} +#[test] +fn bindgen_test_layout_RealAbstractionWithTonsOfMethods() { + assert_eq!(::std::mem::size_of::<RealAbstractionWithTonsOfMethods>() , + 1usize); + assert_eq!(::std::mem::align_of::<RealAbstractionWithTonsOfMethods>() , + 1usize); +} +extern "C" { + #[link_name = "_ZNK32RealAbstractionWithTonsOfMethods3barEv"] + pub fn RealAbstractionWithTonsOfMethods_bar(this: + *const RealAbstractionWithTonsOfMethods); +} +extern "C" { + #[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEv"] + pub fn RealAbstractionWithTonsOfMethods_bar1(this: + *mut RealAbstractionWithTonsOfMethods); +} +extern "C" { + #[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEi"] + pub fn RealAbstractionWithTonsOfMethods_bar2(this: + *mut RealAbstractionWithTonsOfMethods, + foo: ::std::os::raw::c_int); +} +extern "C" { + #[link_name = "_ZN32RealAbstractionWithTonsOfMethods3staEv"] + pub fn RealAbstractionWithTonsOfMethods_sta(); +} +impl Clone for RealAbstractionWithTonsOfMethods { + fn clone(&self) -> Self { *self } +} +impl RealAbstractionWithTonsOfMethods { + #[inline] + pub unsafe fn bar(&self) { RealAbstractionWithTonsOfMethods_bar(&*self) } + #[inline] + pub unsafe fn bar1(&mut self) { + RealAbstractionWithTonsOfMethods_bar1(&mut *self) + } + #[inline] + pub unsafe fn bar2(&mut self, foo: ::std::os::raw::c_int) { + RealAbstractionWithTonsOfMethods_bar2(&mut *self, foo) + } + #[inline] + pub unsafe fn sta() { RealAbstractionWithTonsOfMethods_sta() } +} diff --git a/tests/expectations/complex.rs b/tests/expectations/complex.rs new file mode 100644 index 00000000..e11a049e --- /dev/null +++ b/tests/expectations/complex.rs @@ -0,0 +1,84 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Testdouble { + pub mMember: [f64; 2usize], +} +#[test] +fn bindgen_test_layout_Testdouble() { + assert_eq!(::std::mem::size_of::<Testdouble>() , 16usize); + assert_eq!(::std::mem::align_of::<Testdouble>() , 8usize); +} +impl Clone for Testdouble { + fn clone(&self) -> Self { *self } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct TestdoublePtr { + pub mMember: *mut [f64; 2usize], +} +#[test] +fn bindgen_test_layout_TestdoublePtr() { + assert_eq!(::std::mem::size_of::<TestdoublePtr>() , 8usize); + assert_eq!(::std::mem::align_of::<TestdoublePtr>() , 8usize); +} +impl Clone for TestdoublePtr { + fn clone(&self) -> Self { *self } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Testfloat { + pub mMember: [f32; 2usize], +} +#[test] +fn bindgen_test_layout_Testfloat() { + assert_eq!(::std::mem::size_of::<Testfloat>() , 8usize); + assert_eq!(::std::mem::align_of::<Testfloat>() , 4usize); +} +impl Clone for Testfloat { + fn clone(&self) -> Self { *self } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct TestfloatPtr { + pub mMember: *mut [f32; 2usize], +} +#[test] +fn bindgen_test_layout_TestfloatPtr() { + assert_eq!(::std::mem::size_of::<TestfloatPtr>() , 8usize); + assert_eq!(::std::mem::align_of::<TestfloatPtr>() , 8usize); +} +impl Clone for TestfloatPtr { + fn clone(&self) -> Self { *self } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Testint { + pub mMember: [::std::os::raw::c_int; 2usize], +} +#[test] +fn bindgen_test_layout_Testint() { + assert_eq!(::std::mem::size_of::<Testint>() , 8usize); + assert_eq!(::std::mem::align_of::<Testint>() , 4usize); +} +impl Clone for Testint { + fn clone(&self) -> Self { *self } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct TestintPtr { + pub mMember: *mut [::std::os::raw::c_int; 2usize], +} +#[test] +fn bindgen_test_layout_TestintPtr() { + assert_eq!(::std::mem::size_of::<TestintPtr>() , 8usize); + assert_eq!(::std::mem::align_of::<TestintPtr>() , 8usize); +} +impl Clone for TestintPtr { + fn clone(&self) -> Self { *self } +} diff --git a/tests/expectations/const_tparam.rs b/tests/expectations/const_tparam.rs index 59649626..3ed10d28 100644 --- a/tests/expectations/const_tparam.rs +++ b/tests/expectations/const_tparam.rs @@ -7,5 +7,6 @@ #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct C<T> { - pub foo: *mut T, + pub foo: *const T, + pub bar: *mut T, } diff --git a/tests/headers/class.hpp b/tests/headers/class.hpp index a34d4d8e..e753f186 100644 --- a/tests/headers/class.hpp +++ b/tests/headers/class.hpp @@ -18,3 +18,12 @@ union Union { class WithUnion { Union data; }; + +class RealAbstractionWithTonsOfMethods { + void foo(); +public: + void bar() const; + void bar(); + void bar(int foo); + static void sta(); +}; diff --git a/tests/headers/complex.h b/tests/headers/complex.h new file mode 100644 index 00000000..ad7af59f --- /dev/null +++ b/tests/headers/complex.h @@ -0,0 +1,13 @@ + +#define COMPLEX_TEST(ty_) \ + struct Test##ty_ { \ + ty_ _Complex mMember; \ + \ + }; \ + struct Test##ty_##Ptr { \ + ty_ _Complex* mMember; \ + }; + +COMPLEX_TEST(double) +COMPLEX_TEST(float) +COMPLEX_TEST(int) diff --git a/tests/headers/const_tparam.hpp b/tests/headers/const_tparam.hpp index a2db574c..05f26e4a 100644 --- a/tests/headers/const_tparam.hpp +++ b/tests/headers/const_tparam.hpp @@ -1,4 +1,5 @@ template<typename T> class C { const T* const foo; + const T* bar; }; |