Skip to content

Commit dfc1141

Browse files
committed
Test suite changes, tests, pffft.
1 parent 15b43ff commit dfc1141

13 files changed

+100
-33
lines changed

libbindgen/src/codegen/helpers.rs

Lines changed: 11 additions & 3 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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;
Lines changed: 11 additions & 0 deletions
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';
Lines changed: 7 additions & 0 deletions
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"
Lines changed: 7 additions & 0 deletions
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+
};
Lines changed: 9 additions & 0 deletions
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

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate log;
77
extern crate shlex;
88

99
use std::fs;
10-
use std::io::{BufRead, BufReader, Error, ErrorKind, Read};
10+
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Write};
1111
use std::path::PathBuf;
1212

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

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

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

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

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

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

7292
// Fool builder_from_flags() into believing it has real env::args_os...
7393
// - add "bindgen" as executable name 0th element
@@ -89,17 +109,27 @@ fn create_bindgen_builder(header: &PathBuf)
89109
.map(ToString::to_string)
90110
.chain(flags.into_iter());
91111

92-
builder_from_flags(args).map(|(builder, _)| builder.no_unstable_rust())
112+
builder_from_flags(args)
113+
.map(|(builder, _)| Some(builder.no_unstable_rust()))
93114
}
94115

95116
macro_rules! test_header {
96117
($function:ident, $header:expr) => (
97118
#[test]
98119
fn $function() {
99120
let header = PathBuf::from($header);
100-
let _ = create_bindgen_builder(&header)
101-
.and_then(|builder| compare_generated_header(&header, builder))
102-
.map_err(|err| panic!(format!("{}", err)) );
121+
let result = create_bindgen_builder(&header)
122+
.and_then(|builder| {
123+
if let Some(builder) = builder {
124+
compare_generated_header(&header, builder)
125+
} else {
126+
Ok(())
127+
}
128+
});
129+
130+
if let Err(err) = result {
131+
panic!("{}", err);
132+
}
103133
}
104134
)
105135
}

0 commit comments

Comments
 (0)