Skip to content

Commit 08230b7

Browse files
author
bors-servo
authored
Auto merge of #583 - emilio:macro-inf, r=fitzgen
ir: Ignore non-finite macro constants from macros. Fixes #582.
2 parents 16bd8b7 + 22d5a8f commit 08230b7

File tree

6 files changed

+49
-16
lines changed

6 files changed

+49
-16
lines changed

src/codegen/helpers.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,38 @@ pub mod ast_ty {
158158
.build_lit(aster::AstBuilder::new().lit().byte_str(string))
159159
}
160160

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

165-
// So it gets properly recognised as a floating point constant.
166-
if !string.contains('.') {
167-
string.push('.');
166+
if f.is_finite() {
167+
let mut string = f.to_string();
168+
169+
// So it gets properly recognised as a floating point constant.
170+
if !string.contains('.') {
171+
string.push('.');
172+
}
173+
174+
let kind = ast::LitKind::FloatUnsuffixed(string.as_str().to_symbol());
175+
return Ok(aster::AstBuilder::new().expr().lit().build_lit(kind))
176+
}
177+
178+
let prefix = ctx.trait_prefix();
179+
if f.is_nan() {
180+
return Ok(quote_expr!(ctx.ext_cx(), ::$prefix::f64::NAN));
181+
}
182+
183+
if f.is_infinite() {
184+
return Ok(if f.is_sign_positive() {
185+
quote_expr!(ctx.ext_cx(), ::$prefix::f64::INFINITY)
186+
} else {
187+
quote_expr!(ctx.ext_cx(), ::$prefix::f64::NEG_INFINITY)
188+
});
168189
}
169190

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

174195
pub fn arguments_from_signature(signature: &FunctionSig,

src/codegen/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,12 @@ impl CodeGenerator for Var {
470470
}
471471
}
472472
VarType::Float(f) => {
473-
const_item.build(helpers::ast_ty::float_expr(f))
474-
.build(ty)
473+
match helpers::ast_ty::float_expr(ctx, f) {
474+
Ok(expr) => {
475+
const_item.build(expr).build(ty)
476+
}
477+
Err(..) => return,
478+
}
475479
}
476480
VarType::Char(c) => {
477481
const_item
@@ -2419,7 +2423,7 @@ impl ToRustTy for TemplateInstantiation {
24192423
.map(|(arg, _)| arg.to_rust_ty(ctx))
24202424
.collect::<Vec<_>>();
24212425

2422-
path.segments.last_mut().unwrap().parameters = if
2426+
path.segments.last_mut().unwrap().parameters = if
24232427
template_args.is_empty() {
24242428
None
24252429
} else {

src/ir/var.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl ClangSubItemParser for Var {
146146
let (type_kind, val) = match value {
147147
EvalResult::Invalid => return Err(ParseError::Continue),
148148
EvalResult::Float(f) => {
149-
(TypeKind::Float(FloatKind::Float), VarType::Float(f))
149+
(TypeKind::Float(FloatKind::Double), VarType::Float(f))
150150
}
151151
EvalResult::Char(c) => {
152152
let c = match c {
@@ -178,9 +178,7 @@ impl ClangSubItemParser for Var {
178178
} else {
179179
IntKind::Int
180180
}
181-
} else if value >
182-
u32::max_value() as
183-
i64 {
181+
} else if value > u32::max_value() as i64 {
184182
IntKind::ULongLong
185183
} else {
186184
IntKind::UInt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
pub const INFINITY: f64 = ::std::f64::INFINITY;
8+
pub const NAN: f64 = ::std::f64::NAN;

tests/expectations/tests/macro_const.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
pub const foo: &'static [u8; 4usize] = b"bar\x00";
88
pub const CHAR: u8 = b'b';
99
pub const CHARR: u8 = b'\x00';
10-
pub const FLOAT: f32 = 5.09;
11-
pub const FLOAT_EXPR: f32 = 0.005;
10+
pub const FLOAT: f64 = 5.09;
11+
pub const FLOAT_EXPR: f64 = 0.005;
1212
pub const INVALID_UTF8: [u8; 5usize] = [240, 40, 140, 40, 0];

tests/headers/infinite-macro.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define INFINITY (1.0f/0.0f)
2+
#define NAN (0.0f/0.0f)

0 commit comments

Comments
 (0)