Skip to content

ir: Ignore non-finite macro constants from macros. #583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,38 @@ pub mod ast_ty {
.build_lit(aster::AstBuilder::new().lit().byte_str(string))
}

pub fn float_expr(f: f64) -> P<ast::Expr> {
pub fn float_expr(ctx: &BindgenContext,
f: f64)
-> Result<P<ast::Expr>, ()> {
use aster::symbol::ToSymbol;
let mut string = f.to_string();

// So it gets properly recognised as a floating point constant.
if !string.contains('.') {
string.push('.');
if f.is_finite() {
let mut string = f.to_string();

// So it gets properly recognised as a floating point constant.
if !string.contains('.') {
string.push('.');
}

let kind = ast::LitKind::FloatUnsuffixed(string.as_str().to_symbol());
return Ok(aster::AstBuilder::new().expr().lit().build_lit(kind))
}

let prefix = ctx.trait_prefix();
if f.is_nan() {
return Ok(quote_expr!(ctx.ext_cx(), ::$prefix::f64::NAN));
}

if f.is_infinite() {
return Ok(if f.is_sign_positive() {
quote_expr!(ctx.ext_cx(), ::$prefix::f64::INFINITY)
} else {
quote_expr!(ctx.ext_cx(), ::$prefix::f64::NEG_INFINITY)
});
}

let kind = ast::LitKind::FloatUnsuffixed(string.as_str().to_symbol());
aster::AstBuilder::new().expr().lit().build_lit(kind)
warn!("Unknown non-finite float number: {:?}", f);
return Err(());
}

pub fn arguments_from_signature(signature: &FunctionSig,
Expand Down
10 changes: 7 additions & 3 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,12 @@ impl CodeGenerator for Var {
}
}
VarType::Float(f) => {
const_item.build(helpers::ast_ty::float_expr(f))
.build(ty)
match helpers::ast_ty::float_expr(ctx, f) {
Ok(expr) => {
const_item.build(expr).build(ty)
}
Err(..) => return,
}
}
VarType::Char(c) => {
const_item
Expand Down Expand Up @@ -2419,7 +2423,7 @@ impl ToRustTy for TemplateInstantiation {
.map(|(arg, _)| arg.to_rust_ty(ctx))
.collect::<Vec<_>>();

path.segments.last_mut().unwrap().parameters = if
path.segments.last_mut().unwrap().parameters = if
template_args.is_empty() {
None
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl ClangSubItemParser for Var {
let (type_kind, val) = match value {
EvalResult::Invalid => return Err(ParseError::Continue),
EvalResult::Float(f) => {
(TypeKind::Float(FloatKind::Float), VarType::Float(f))
(TypeKind::Float(FloatKind::Double), VarType::Float(f))
}
EvalResult::Char(c) => {
let c = match c {
Expand Down Expand Up @@ -178,9 +178,7 @@ impl ClangSubItemParser for Var {
} else {
IntKind::Int
}
} else if value >
u32::max_value() as
i64 {
} else if value > u32::max_value() as i64 {
IntKind::ULongLong
} else {
IntKind::UInt
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/tests/infinite-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


pub const INFINITY: f64 = ::std::f64::INFINITY;
pub const NAN: f64 = ::std::f64::NAN;
4 changes: 2 additions & 2 deletions tests/expectations/tests/macro_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
pub const foo: &'static [u8; 4usize] = b"bar\x00";
pub const CHAR: u8 = b'b';
pub const CHARR: u8 = b'\x00';
pub const FLOAT: f32 = 5.09;
pub const FLOAT_EXPR: f32 = 0.005;
pub const FLOAT: f64 = 5.09;
pub const FLOAT_EXPR: f64 = 0.005;
pub const INVALID_UTF8: [u8; 5usize] = [240, 40, 140, 40, 0];
2 changes: 2 additions & 0 deletions tests/headers/infinite-macro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define INFINITY (1.0f/0.0f)
#define NAN (0.0f/0.0f)