summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <ecoal95@gmail.com>2016-11-06 20:52:01 +0100
committerEmilio Cobos Álvarez <ecoal95@gmail.com>2016-11-07 23:11:48 +0100
commit1091a4274eb5c91b6279905688759105ab95bfb4 (patch)
tree24bf02de9bb5524163c7f9b4b905c8c1597bea8d
parentc314a2f1b3b70a1322a23f785995124941ed632a (diff)
Add an option to avoid converting to f32/f64 automatically float types.
-rwxr-xr-xsrc/bin/bindgen.rs5
-rwxr-xr-xsrc/codegen/mod.rs25
-rwxr-xr-xsrc/lib.rs10
-rw-r--r--tests/expectations/tests/convert-floats.rs22
-rw-r--r--tests/headers/convert-floats.h7
5 files changed, 63 insertions, 6 deletions
diff --git a/src/bin/bindgen.rs b/src/bin/bindgen.rs
index c906efee..0c3d97cf 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.
@@ -183,6 +185,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 e7fa62ee..7d470be4 100755
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -1458,12 +1458,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)
+ }
}
}
}
diff --git a/src/lib.rs b/src/lib.rs
index a0eba4c0..8d2ff7ad 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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;
@@ -280,6 +286,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>,
@@ -312,6 +321,7 @@ impl Default for BindgenOptions {
unstable_rust: true,
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;
+};