Skip to content

Commit 6c0d065

Browse files
author
bors-servo
authored
Auto merge of #221 - emilio:dont-override-float, r=fitzgen
Add an option to avoid converting to f32/f64 automatically float types. This implements another feature that we need for parity with upstream bindgen. r? @fitzgen
2 parents 8c737ec + 1091a42 commit 6c0d065

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

src/bin/bindgen.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Options:
6363
--use-msvc-mangling Handle MSVC C++ ABI mangling; requires that
6464
target be set to (i686|x86_64)-pc-win32
6565
66+
--no-convert-floats Don't convert floats automatically to f32/f64.
67+
6668
--raw-line=<raw> Add a raw line at the beginning of the output.
6769
6870
--no-unstable-rust Avoid generating unstable rust.
@@ -196,6 +198,9 @@ fn parse_args_or_exit(args: Vec<String>) -> (BindgenOptions, Box<io::Write>) {
196198
"--emit-clang-ast" => {
197199
options.emit_ast = true;
198200
}
201+
"--no-convert-floats" => {
202+
options.convert_floats = false;
203+
}
199204
"--use-msvc-mangling" => {
200205
options.msvc_mangling = true;
201206
}

src/codegen/mod.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,12 +1465,25 @@ impl ToRustTy for Type {
14651465
}
14661466
TypeKind::Float(fk) => {
14671467
use ir::ty::FloatKind;
1468-
// TODO: we probably should just take the type layout into
1469-
// account?
1470-
match fk {
1471-
FloatKind::Float => aster::ty::TyBuilder::new().f32(),
1472-
FloatKind::Double | FloatKind::LongDouble => {
1473-
aster::ty::TyBuilder::new().f64()
1468+
if ctx.options().convert_floats {
1469+
// TODO: we probably should just take the type layout into
1470+
// account?
1471+
//
1472+
// Also, maybe this one shouldn't be the default?
1473+
match fk {
1474+
FloatKind::Float => aster::ty::TyBuilder::new().f32(),
1475+
FloatKind::Double | FloatKind::LongDouble => {
1476+
aster::ty::TyBuilder::new().f64()
1477+
}
1478+
}
1479+
} else {
1480+
// FIXME: `c_longdouble` doesn't seem to be defined in some
1481+
// systems, so we use `c_double` directly.
1482+
match fk {
1483+
FloatKind::Float => raw!(c_float),
1484+
FloatKind::Double | FloatKind::LongDouble => {
1485+
raw!(c_double)
1486+
}
14741487
}
14751488
}
14761489
}

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ impl Builder {
204204
self
205205
}
206206

207+
/// Avoid converting floats to f32/f64 by default.
208+
pub fn no_convert_floats(mut self) -> Self {
209+
self.options.convert_floats = false;
210+
self
211+
}
212+
207213
/// Avoid generating any unstable Rust in the generated bindings.
208214
pub fn no_unstable_rust(mut self) -> Builder {
209215
self.options.unstable_rust = false;
@@ -298,6 +304,9 @@ pub struct BindgenOptions {
298304
/// True if we should use MSVC name mangling rules.
299305
pub msvc_mangling: bool,
300306

307+
/// Whether we should convert float types to f32/f64 types.
308+
pub convert_floats: bool,
309+
301310
/// The set of raw lines to prepend to the generated Rust code.
302311
pub raw_lines: Vec<String>,
303312

@@ -332,6 +341,7 @@ impl Default for BindgenOptions {
332341
ctypes_prefix: None,
333342
namespaced_constants: true,
334343
msvc_mangling: false,
344+
convert_floats: true,
335345
raw_lines: vec![],
336346
clang_args: vec![],
337347
input_header: None,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
#[repr(C)]
8+
#[derive(Debug, Copy)]
9+
pub struct foo {
10+
pub bar: ::std::os::raw::c_float,
11+
pub baz: ::std::os::raw::c_float,
12+
pub bazz: ::std::os::raw::c_double,
13+
pub bazzz: *mut ::std::os::raw::c_double,
14+
}
15+
#[test]
16+
fn bindgen_test_layout_foo() {
17+
assert_eq!(::std::mem::size_of::<foo>() , 24usize);
18+
assert_eq!(::std::mem::align_of::<foo>() , 8usize);
19+
}
20+
impl Clone for foo {
21+
fn clone(&self) -> Self { *self }
22+
}

tests/headers/convert-floats.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --no-convert-floats
2+
3+
struct foo {
4+
float bar, baz;
5+
double bazz;
6+
long double* bazzz;
7+
};

0 commit comments

Comments
 (0)