Skip to content

Commit b9e15e9

Browse files
author
bors-servo
authored
Auto merge of #273 - upsuper:const-bool, r=emilio
Generate bool value for bool constants This also includes an unrelated commit which ensures newly-added test would be included.
2 parents e74acce + 47cb4e3 commit b9e15e9

File tree

7 files changed

+56
-1
lines changed

7 files changed

+56
-1
lines changed

libbindgen/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod testgen {
2424
let out_dir = Path::new(env!("OUT_DIR"));
2525
let mut dst = File::create(Path::new(&out_dir).join("tests.rs")).unwrap();
2626

27+
println!("cargo:rerun-if-changed=tests/headers");
2728
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
2829
let headers_dir = manifest_dir.join("tests").join("headers");
2930

libbindgen/src/codegen/helpers.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ pub mod ast_ty {
133133
}
134134
}
135135

136+
pub fn bool_expr(val: bool) -> P<ast::Expr> {
137+
aster::AstBuilder::new().expr().bool(val)
138+
}
139+
136140
pub fn byte_array_expr(bytes: &[u8]) -> P<ast::Expr> {
137141
let mut vec = Vec::with_capacity(bytes.len() + 1);
138142
for byte in bytes {

libbindgen/src/codegen/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ impl CodeGenerator for Var {
323323
.const_(canonical_name)
324324
.expr();
325325
let item = match *val {
326+
VarType::Bool(val) => {
327+
const_item.build(helpers::ast_ty::bool_expr(val))
328+
.build(ty)
329+
}
326330
VarType::Int(val) => {
327331
const_item.build(helpers::ast_ty::int_expr(val))
328332
.build(ty)

libbindgen/src/ir/ty.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ impl Type {
131131
}
132132
}
133133

134+
/// Is this a boolean type?
135+
pub fn is_bool(&self) -> bool {
136+
match self.kind {
137+
TypeKind::Int(IntKind::Bool) => true,
138+
_ => false,
139+
}
140+
}
141+
134142
/// Is this an integer type?
135143
pub fn is_integer(&self) -> bool {
136144
match self.kind {

libbindgen/src/ir/var.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use super::ty::{FloatKind, TypeKind};
1313
/// The type for a constant variable.
1414
#[derive(Debug)]
1515
pub enum VarType {
16+
/// An boolean.
17+
Bool(bool),
1618
/// An integer.
1719
Int(i64),
1820
/// A floating point number.
@@ -194,14 +196,18 @@ impl ClangSubItemParser for Var {
194196
let canonical_ty = ctx.safe_resolve_type(ty)
195197
.and_then(|t| t.safe_canonical_type(ctx));
196198

199+
let is_bool = canonical_ty.map_or(false, |t| t.is_bool());
197200
let is_integer = canonical_ty.map_or(false, |t| t.is_integer());
198201
let is_float = canonical_ty.map_or(false, |t| t.is_float());
199202

200203
// TODO: We could handle `char` more gracefully.
201204
// TODO: Strings, though the lookup is a bit more hard (we need
202205
// to look at the canonical type of the pointee too, and check
203206
// is char, u8, or i8 I guess).
204-
let value = if is_integer {
207+
let value = if is_bool {
208+
cursor.evaluate().as_int()
209+
.map(|val| VarType::Bool(val != 0))
210+
} else if is_integer {
205211
cursor.evaluate()
206212
.as_int()
207213
.map(|val| val as i64)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
pub const k: bool = true;
8+
#[repr(C)]
9+
#[derive(Debug, Copy)]
10+
pub struct A {
11+
pub _address: u8,
12+
}
13+
pub const A_k: bool = false;
14+
#[test]
15+
fn bindgen_test_layout_A() {
16+
assert_eq!(::std::mem::size_of::<A>() , 1usize);
17+
assert_eq!(::std::mem::align_of::<A>() , 1usize);
18+
}
19+
impl Clone for A {
20+
fn clone(&self) -> Self { *self }
21+
}
22+
pub type foo = bool;
23+
pub const k2: foo = true;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// bindgen-unstable
2+
3+
const bool k = true;
4+
struct A {
5+
static const bool k = false;
6+
};
7+
8+
typedef bool foo;
9+
const foo k2 = true;

0 commit comments

Comments
 (0)