Skip to content

Commit 9bddf21

Browse files
committed
Test suite changes, tests, pffft.
1 parent 0ef84e9 commit 9bddf21

13 files changed

+98
-33
lines changed

libbindgen/src/codegen/helpers.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,21 @@ pub mod ast_ty {
147147

148148
pub fn cstr_expr(mut string: String) -> P<ast::Expr> {
149149
string.push('\0');
150-
aster::AstBuilder::new().expr().build_lit(
151-
aster::AstBuilder::new().lit().byte_str(string))
150+
aster::AstBuilder::new()
151+
.expr()
152+
.build_lit(aster::AstBuilder::new().lit().byte_str(string))
152153
}
153154

154155
pub fn float_expr(f: f64) -> P<ast::Expr> {
155156
use aster::str::ToInternedString;
156-
let interned_str = f.to_string().as_str().to_interned_string();
157+
let mut string = f.to_string();
158+
159+
// So it gets properly recognised as a floating point constant.
160+
if !string.contains('.') {
161+
string.push('.');
162+
}
163+
164+
let interned_str = string.as_str().to_interned_string();
157165
let kind = ast::LitKind::FloatUnsuffixed(interned_str);
158166
aster::AstBuilder::new().expr().lit().build_lit(kind)
159167
}

libbindgen/src/codegen/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ impl CodeGenerator for Var {
318318
.expr();
319319
let item = match *val {
320320
VarType::Int(val) => {
321-
const_item
322-
.build(helpers::ast_ty::int_expr(val))
321+
const_item.build(helpers::ast_ty::int_expr(val))
323322
.build(ty)
324323
}
325324
VarType::String(ref bytes) => {
@@ -332,8 +331,7 @@ impl CodeGenerator for Var {
332331

333332
match String::from_utf8(bytes.clone()) {
334333
Ok(string) => {
335-
const_item
336-
.build(helpers::ast_ty::cstr_expr(string))
334+
const_item.build(helpers::ast_ty::cstr_expr(string))
337335
.build(quote_ty!(ctx.ext_cx(), &'static $ty))
338336
}
339337
Err(..) => {
@@ -344,8 +342,7 @@ impl CodeGenerator for Var {
344342
}
345343
}
346344
VarType::Float(f) => {
347-
const_item
348-
.build(helpers::ast_ty::float_expr(f))
345+
const_item.build(helpers::ast_ty::float_expr(f))
349346
.build(ty)
350347
}
351348
VarType::Char(c) => {

libbindgen/src/ir/item.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,7 @@ impl ClangItemParser for Item {
840840
ctx: &mut BindgenContext)
841841
-> ItemId {
842842
let id = ctx.next_item_id();
843-
Self::from_ty_or_ref_with_id(id,
844-
ty,
845-
location,
846-
parent_id,
847-
ctx)
843+
Self::from_ty_or_ref_with_id(id, ty, location, parent_id, ctx)
848844
}
849845

850846
/// Parse a C++ type. If we find a reference to a type that has not been

libbindgen/src/ir/var.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::context::{BindgenContext, ItemId};
88
use super::function::cursor_mangling;
99
use super::int::IntKind;
1010
use super::item::Item;
11-
use super::ty::{TypeKind, FloatKind};
11+
use super::ty::{FloatKind, TypeKind};
1212

1313
#[derive(Debug)]
1414
pub enum VarType {
@@ -135,7 +135,8 @@ impl ClangSubItemParser for Var {
135135
EvalResult::Str(val) => {
136136
let char_ty =
137137
Item::builtin_type(TypeKind::Int(IntKind::U8),
138-
true, ctx);
138+
true,
139+
ctx);
139140
(TypeKind::Pointer(char_ty), VarType::String(val))
140141
}
141142
EvalResult::Int(Wrapping(value)) => {
@@ -202,7 +203,8 @@ impl ClangSubItemParser for Var {
202203
.or_else(|| {
203204
let tu = ctx.translation_unit();
204205
get_integer_literal_from_cursor(&cursor, tu)
205-
}).map(VarType::Int)
206+
})
207+
.map(VarType::Int)
206208
} else if is_float {
207209
cursor.evaluate()
208210
.as_double()

tests/expectations/tests/constant-evaluate.rs renamed to libbindgen/tests/expectations/tests/constant-evaluate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ pub const bar: _bindgen_ty_1 = _bindgen_ty_1::bar;
1010
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1111
pub enum _bindgen_ty_1 { foo = 4, bar = 8, }
1212
pub const BAZ: ::std::os::raw::c_longlong = 24;
13-
pub const fuzz: f64 = 51;
13+
pub const fuzz: f64 = 51.;
1414
pub const BAZZ: ::std::os::raw::c_char = 53;
1515
pub const WAT: ::std::os::raw::c_char = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// bindgen-unstable
2+
3+
enum {
4+
foo = 4,
5+
bar = 8,
6+
};
7+
8+
const long long BAZ = (1 << foo) | bar;
9+
const double fuzz = (1 + 50.0f);
10+
const char BAZZ = '5';
11+
const char WAT = '\0';
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define foo "bar"
2+
#define CHAR 'b'
3+
#define CHARR '\0'
4+
#define FLOAT 5.09f
5+
#define FLOAT_EXPR (5 / 1000.0f)
6+
7+
#define INVALID_UTF8 "\xf0\x28\x8c\x28"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: -- -std=c++14
2+
template <typename A> using MaybeWrapped = A;
3+
4+
template<class T>
5+
class Rooted {
6+
MaybeWrapped<T> ptr;
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// bindgen-flags: --whitelist-type Rooted -- -std=c++14
2+
3+
template <typename a> using MaybeWrapped = a;
4+
class Rooted {
5+
MaybeWrapped<int> ptr;
6+
};
7+
8+
/// <div rustbindgen replaces="MaybeWrapped"></div>
9+
template <typename a> using replaces_MaybeWrapped = a;

libbindgen/tests/tests.rs

+43-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern crate shlex;
88

99
use std::env;
1010
use std::fs;
11-
use std::io::{BufRead, BufReader, Error, ErrorKind, Read};
11+
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Write};
1212
use std::path::{Path, PathBuf};
1313

1414
#[path="../../src/options.rs"]
@@ -36,11 +36,18 @@ fn compare_generated_header(header: &PathBuf,
3636
};
3737

3838
let mut buffer = String::new();
39-
let f = try!(fs::File::open(&expected));
40-
let _ = try!(BufReader::new(f).read_to_string(&mut buffer));
39+
{
40+
if let Ok(expected_file) = fs::File::open(&expected) {
41+
try!(BufReader::new(expected_file).read_to_string(&mut buffer));
42+
}
43+
}
4144

4245
if output == buffer {
43-
return Ok(());
46+
if !output.is_empty() {
47+
return Ok(());
48+
}
49+
return Err(Error::new(ErrorKind::Other,
50+
"Something's gone really wrong!"))
4451
}
4552

4653
println!("diff expected generated");
@@ -54,21 +61,34 @@ fn compare_generated_header(header: &PathBuf,
5461
diff::Result::Right(r) => println!("+{}", r),
5562
}
5663
}
64+
65+
// Override the diff.
66+
{
67+
let mut expected_file = try!(fs::File::create(&expected));
68+
try!(expected_file.write_all(output.as_bytes()));
69+
}
70+
5771
Err(Error::new(ErrorKind::Other, "Header and binding differ!"))
5872
}
5973

6074
fn create_bindgen_builder(header: &PathBuf)
61-
-> Result<libbindgen::Builder, Error> {
75+
-> Result<Option<libbindgen::Builder>, Error> {
6276
let source = try!(fs::File::open(header));
6377
let reader = BufReader::new(source);
6478

6579
// Scoop up bindgen-flags from test header
66-
let line: String = try!(reader.lines().take(1).collect());
67-
let flags: Vec<String> = if line.contains("bindgen-flags:") {
68-
line.split("bindgen-flags:").last().and_then(shlex::split)
69-
} else {
70-
None
71-
}.unwrap_or(Vec::with_capacity(2));
80+
let mut flags = Vec::with_capacity(2);
81+
82+
for line in reader.lines().take(2) {
83+
let line = try!(line);
84+
if line.contains("bindgen-flags: ") {
85+
let extra_flags = line.split("bindgen-flags: ")
86+
.last().and_then(shlex::split).unwrap();
87+
flags.extend(extra_flags.into_iter());
88+
} else if line.contains("bindgen-unstable") && !cfg!(feature = "llvm_stable") {
89+
return Ok(None)
90+
}
91+
}
7292

7393
// Fool builder_from_flags() into believing it has real env::args_os...
7494
// - add "bindgen" as executable name 0th element
@@ -90,7 +110,8 @@ fn create_bindgen_builder(header: &PathBuf)
90110
.map(ToString::to_string)
91111
.chain(flags.into_iter());
92112

93-
builder_from_flags(args).map(|(builder, _)| builder.no_unstable_rust())
113+
builder_from_flags(args)
114+
.map(|(builder, _)| Some(builder.no_unstable_rust()))
94115
}
95116

96117
#[test]
@@ -123,16 +144,23 @@ fn run_bindgen_tests() {
123144
let failures: Vec<_> = headers.iter()
124145
.filter_map(|header| {
125146
create_bindgen_builder(header)
126-
.and_then(|builder| compare_generated_header(header, builder))
147+
.and_then(|builder| {
148+
if let Some(builder) = builder {
149+
compare_generated_header(header, builder)
150+
} else {
151+
Ok(())
152+
}
153+
})
127154
.err()
128155
})
129156
.collect();
130157

131158
let num_failures = failures.len();
132159

133160
if num_failures > 0 {
134-
panic!("{} test{} failed!",
161+
panic!("{} test{} failed! {:?}",
135162
num_failures,
136-
if num_failures > 1 {"s"} else {""});
163+
if num_failures > 1 {"s"} else {""},
164+
failures);
137165
}
138166
}

0 commit comments

Comments
 (0)