diff options
-rwxr-xr-x | src/bin/bindgen.rs | 5 | ||||
-rwxr-xr-x | src/codegen/mod.rs | 25 | ||||
-rwxr-xr-x | src/lib.rs | 10 | ||||
-rw-r--r-- | tests/expectations/tests/convert-floats.rs | 22 | ||||
-rw-r--r-- | tests/headers/convert-floats.h | 7 |
5 files changed, 63 insertions, 6 deletions
diff --git a/src/bin/bindgen.rs b/src/bin/bindgen.rs index 60632f43..2dbb1690 100755 --- a/src/bin/bindgen.rs +++ b/src/bin/bindgen.rs @@ -63,6 +63,8 @@ Options: --use-msvc-mangling Handle MSVC C++ ABI mangling; requires that target be set to (i686|x86_64)-pc-win32 + --no-convert-floats Don't convert floats automatically to f32/f64. + --raw-line=<raw> Add a raw line at the beginning of the output. --no-unstable-rust Avoid generating unstable rust. @@ -196,6 +198,9 @@ fn parse_args_or_exit(args: Vec<String>) -> (BindgenOptions, Box<io::Write>) { "--emit-clang-ast" => { options.emit_ast = true; } + "--no-convert-floats" => { + options.convert_floats = false; + } "--use-msvc-mangling" => { options.msvc_mangling = true; } diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 85ac13b5..bf6af82b 100755 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1465,12 +1465,25 @@ impl ToRustTy for Type { } TypeKind::Float(fk) => { use ir::ty::FloatKind; - // TODO: we probably should just take the type layout into - // account? - match fk { - FloatKind::Float => aster::ty::TyBuilder::new().f32(), - FloatKind::Double | FloatKind::LongDouble => { - aster::ty::TyBuilder::new().f64() + if ctx.options().convert_floats { + // TODO: we probably should just take the type layout into + // account? + // + // Also, maybe this one shouldn't be the default? + match fk { + FloatKind::Float => aster::ty::TyBuilder::new().f32(), + FloatKind::Double | FloatKind::LongDouble => { + aster::ty::TyBuilder::new().f64() + } + } + } else { + // FIXME: `c_longdouble` doesn't seem to be defined in some + // systems, so we use `c_double` directly. + match fk { + FloatKind::Float => raw!(c_float), + FloatKind::Double | FloatKind::LongDouble => { + raw!(c_double) + } } } } @@ -204,6 +204,12 @@ impl Builder { self } + /// Avoid converting floats to f32/f64 by default. + pub fn no_convert_floats(mut self) -> Self { + self.options.convert_floats = false; + self + } + /// Avoid generating any unstable Rust in the generated bindings. pub fn no_unstable_rust(mut self) -> Builder { self.options.unstable_rust = false; @@ -298,6 +304,9 @@ pub struct BindgenOptions { /// True if we should use MSVC name mangling rules. pub msvc_mangling: bool, + /// Whether we should convert float types to f32/f64 types. + pub convert_floats: bool, + /// The set of raw lines to prepend to the generated Rust code. pub raw_lines: Vec<String>, @@ -332,6 +341,7 @@ impl Default for BindgenOptions { ctypes_prefix: None, namespaced_constants: true, msvc_mangling: false, + convert_floats: true, raw_lines: vec![], clang_args: vec![], input_header: None, diff --git a/tests/expectations/tests/convert-floats.rs b/tests/expectations/tests/convert-floats.rs new file mode 100644 index 00000000..ed5bcdcd --- /dev/null +++ b/tests/expectations/tests/convert-floats.rs @@ -0,0 +1,22 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy)] +pub struct foo { + pub bar: ::std::os::raw::c_float, + pub baz: ::std::os::raw::c_float, + pub bazz: ::std::os::raw::c_double, + pub bazzz: *mut ::std::os::raw::c_double, +} +#[test] +fn bindgen_test_layout_foo() { + assert_eq!(::std::mem::size_of::<foo>() , 24usize); + assert_eq!(::std::mem::align_of::<foo>() , 8usize); +} +impl Clone for foo { + fn clone(&self) -> Self { *self } +} diff --git a/tests/headers/convert-floats.h b/tests/headers/convert-floats.h new file mode 100644 index 00000000..0e0ebd33 --- /dev/null +++ b/tests/headers/convert-floats.h @@ -0,0 +1,7 @@ +// bindgen-flags: --no-convert-floats + +struct foo { + float bar, baz; + double bazz; + long double* bazzz; +}; |