Skip to content

Commit 451ba0d

Browse files
committed
Option to avoid generating layout tests
1 parent 3f5975e commit 451ba0d

File tree

3 files changed

+141
-116
lines changed

3 files changed

+141
-116
lines changed

src/codegen/mod.rs

Lines changed: 94 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -987,37 +987,39 @@ impl CodeGenerator for TemplateInstantiation {
987987
// just converted to rust types in fields, vars, etc, we take this
988988
// opportunity to generate tests for their layout here.
989989

990-
let layout = item.kind().expect_type().layout(ctx);
990+
if ctx.options().layout_tests {
991+
let layout = item.kind().expect_type().layout(ctx);
991992

992-
if let Some(layout) = layout {
993-
let size = layout.size;
994-
let align = layout.align;
993+
if let Some(layout) = layout {
994+
let size = layout.size;
995+
let align = layout.align;
995996

996-
let name = item.canonical_name(ctx);
997-
let fn_name = format!("__bindgen_test_layout_{}_instantiation_{}",
998-
name,
999-
item.id().as_usize());
1000-
let fn_name = ctx.rust_ident_raw(&fn_name);
997+
let name = item.canonical_name(ctx);
998+
let fn_name = format!("__bindgen_test_layout_{}_instantiation_{}",
999+
name,
1000+
item.id().as_usize());
1001+
let fn_name = ctx.rust_ident_raw(&fn_name);
10011002

1002-
let prefix = ctx.trait_prefix();
1003-
let ident = item.to_rust_ty_or_opaque(ctx, &());
1004-
let size_of_expr = quote_expr!(ctx.ext_cx(),
1005-
::$prefix::mem::size_of::<$ident>());
1006-
let align_of_expr = quote_expr!(ctx.ext_cx(),
1007-
::$prefix::mem::align_of::<$ident>());
1008-
1009-
let item = quote_item!(
1010-
ctx.ext_cx(),
1011-
#[test]
1012-
fn $fn_name() {
1013-
assert_eq!($size_of_expr, $size,
1014-
concat!("Size of template specialization: ", stringify!($ident)));
1015-
assert_eq!($align_of_expr, $align,
1016-
concat!("Alignment of template specialization: ", stringify!($ident)));
1017-
})
1018-
.unwrap();
1003+
let prefix = ctx.trait_prefix();
1004+
let ident = item.to_rust_ty_or_opaque(ctx, &());
1005+
let size_of_expr = quote_expr!(ctx.ext_cx(),
1006+
::$prefix::mem::size_of::<$ident>());
1007+
let align_of_expr = quote_expr!(ctx.ext_cx(),
1008+
::$prefix::mem::align_of::<$ident>());
10191009

1020-
result.push(item);
1010+
let item = quote_item!(
1011+
ctx.ext_cx(),
1012+
#[test]
1013+
fn $fn_name() {
1014+
assert_eq!($size_of_expr, $size,
1015+
concat!("Size of template specialization: ", stringify!($ident)));
1016+
assert_eq!($align_of_expr, $align,
1017+
concat!("Alignment of template specialization: ", stringify!($ident)));
1018+
})
1019+
.unwrap();
1020+
1021+
result.push(item);
1022+
}
10211023
}
10221024
}
10231025
}
@@ -1498,76 +1500,78 @@ impl CodeGenerator for CompInfo {
14981500
.codegen(ctx, result, whitelisted_items, &());
14991501
}
15001502

1501-
if let Some(layout) = layout {
1502-
let fn_name = format!("bindgen_test_layout_{}", canonical_name);
1503-
let fn_name = ctx.rust_ident_raw(&fn_name);
1504-
let type_name = ctx.rust_ident_raw(&canonical_name);
1505-
let prefix = ctx.trait_prefix();
1506-
let size_of_expr = quote_expr!(ctx.ext_cx(),
1507-
::$prefix::mem::size_of::<$type_name>());
1508-
let align_of_expr = quote_expr!(ctx.ext_cx(),
1509-
::$prefix::mem::align_of::<$type_name>());
1510-
let size = layout.size;
1511-
let align = layout.align;
1512-
1513-
let check_struct_align = if align > mem::size_of::<*mut ()>() {
1514-
// FIXME when [RFC 1358](https://github.com/rust-lang/rust/issues/33626) ready
1515-
None
1516-
} else {
1517-
quote_item!(ctx.ext_cx(),
1518-
assert_eq!($align_of_expr,
1519-
$align,
1520-
concat!("Alignment of ", stringify!($type_name)));
1521-
)
1522-
};
1503+
if ctx.options().layout_tests {
1504+
if let Some(layout) = layout {
1505+
let fn_name = format!("bindgen_test_layout_{}", canonical_name);
1506+
let fn_name = ctx.rust_ident_raw(&fn_name);
1507+
let type_name = ctx.rust_ident_raw(&canonical_name);
1508+
let prefix = ctx.trait_prefix();
1509+
let size_of_expr = quote_expr!(ctx.ext_cx(),
1510+
::$prefix::mem::size_of::<$type_name>());
1511+
let align_of_expr = quote_expr!(ctx.ext_cx(),
1512+
::$prefix::mem::align_of::<$type_name>());
1513+
let size = layout.size;
1514+
let align = layout.align;
1515+
1516+
let check_struct_align = if align > mem::size_of::<*mut ()>() {
1517+
// FIXME when [RFC 1358](https://github.com/rust-lang/rust/issues/33626) ready
1518+
None
1519+
} else {
1520+
quote_item!(ctx.ext_cx(),
1521+
assert_eq!($align_of_expr,
1522+
$align,
1523+
concat!("Alignment of ", stringify!($type_name)));
1524+
)
1525+
};
15231526

1524-
// FIXME when [issue #465](https://github.com/servo/rust-bindgen/issues/465) ready
1525-
let too_many_base_vtables = self.base_members()
1526-
.iter()
1527-
.filter(|base| {
1528-
ctx.resolve_type(base.ty).has_vtable(ctx)
1529-
})
1530-
.count() > 1;
1527+
// FIXME when [issue #465](https://github.com/servo/rust-bindgen/issues/465) ready
1528+
let too_many_base_vtables = self.base_members()
1529+
.iter()
1530+
.filter(|base| {
1531+
ctx.resolve_type(base.ty).has_vtable(ctx)
1532+
})
1533+
.count() > 1;
15311534

1532-
let should_skip_field_offset_checks = item.is_opaque(ctx) ||
1533-
too_many_base_vtables;
1535+
let should_skip_field_offset_checks = item.is_opaque(ctx) ||
1536+
too_many_base_vtables;
15341537

1535-
let check_field_offset = if should_skip_field_offset_checks {
1536-
None
1537-
} else {
1538-
let asserts = self.fields()
1539-
.iter()
1540-
.filter(|field| field.bitfield().is_none())
1541-
.flat_map(|field| {
1542-
field.name().and_then(|name| {
1543-
field.offset().and_then(|offset| {
1544-
let field_offset = offset / 8;
1545-
let field_name = ctx.rust_ident(name);
1546-
1547-
quote_item!(ctx.ext_cx(),
1548-
assert_eq!(unsafe { &(*(0 as *const $type_name)).$field_name as *const _ as usize },
1549-
$field_offset,
1550-
concat!("Alignment of field: ", stringify!($type_name), "::", stringify!($field_name)));
1551-
)
1538+
let check_field_offset = if should_skip_field_offset_checks {
1539+
None
1540+
} else {
1541+
let asserts = self.fields()
1542+
.iter()
1543+
.filter(|field| field.bitfield().is_none())
1544+
.flat_map(|field| {
1545+
field.name().and_then(|name| {
1546+
field.offset().and_then(|offset| {
1547+
let field_offset = offset / 8;
1548+
let field_name = ctx.rust_ident(name);
1549+
1550+
quote_item!(ctx.ext_cx(),
1551+
assert_eq!(unsafe { &(*(0 as *const $type_name)).$field_name as *const _ as usize },
1552+
$field_offset,
1553+
concat!("Alignment of field: ", stringify!($type_name), "::", stringify!($field_name)));
1554+
)
1555+
})
15521556
})
1553-
})
1554-
}).collect::<Vec<P<ast::Item>>>();
1557+
}).collect::<Vec<P<ast::Item>>>();
15551558

1556-
Some(asserts)
1557-
};
1559+
Some(asserts)
1560+
};
15581561

1559-
let item = quote_item!(ctx.ext_cx(),
1560-
#[test]
1561-
fn $fn_name() {
1562-
assert_eq!($size_of_expr,
1563-
$size,
1564-
concat!("Size of: ", stringify!($type_name)));
1562+
let item = quote_item!(ctx.ext_cx(),
1563+
#[test]
1564+
fn $fn_name() {
1565+
assert_eq!($size_of_expr,
1566+
$size,
1567+
concat!("Size of: ", stringify!($type_name)));
15651568

1566-
$check_struct_align
1567-
$check_field_offset
1568-
})
1569-
.unwrap();
1570-
result.push(item);
1569+
$check_struct_align
1570+
$check_field_offset
1571+
})
1572+
.unwrap();
1573+
result.push(item);
1574+
}
15711575
}
15721576

15731577
let mut method_names = Default::default();

src/lib.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -214,29 +214,33 @@ impl Builder {
214214
})
215215
.count();
216216

217-
if self.options.derive_debug == false {
217+
if !self.options.layout_tests {
218+
output_vector.push("--no-layout-tests".into());
219+
}
220+
221+
if !self.options.derive_debug {
218222
output_vector.push("--no-derive-debug".into());
219223
}
220224

221-
if self.options.derive_default == false {
225+
if !self.options.derive_default {
222226
output_vector.push("--no-derive-default".into());
223227
} else {
224228
output_vector.push("--with-derive-default".into());
225229
}
226230

227-
if self.options.generate_comments == false {
231+
if !self.options.generate_comments {
228232
output_vector.push("--no-doc-comments".into());
229233
}
230234

231-
if self.options.whitelist_recursively == false {
235+
if !self.options.whitelist_recursively {
232236
output_vector.push("--no-recursive-whitelist".into());
233237
}
234238

235-
if self.options.objc_extern_crate == true {
239+
if self.options.objc_extern_crate {
236240
output_vector.push("--objc-extern-crate".into());
237241
}
238242

239-
if self.options.builtins == true {
243+
if self.options.builtins {
240244
output_vector.push("--builtins".into());
241245
}
242246

@@ -259,21 +263,21 @@ impl Builder {
259263
output_vector.push(dummy.clone());
260264
}
261265

262-
if self.options.emit_ast == true {
266+
if self.options.emit_ast {
263267
output_vector.push("--emit-clang-ast".into());
264268
}
265269

266-
if self.options.emit_ir == true {
270+
if self.options.emit_ir {
267271
output_vector.push("--emit-ir".into());
268272
}
269273
if let Some(ref graph) = self.options.emit_ir_graphviz {
270274
output_vector.push("--emit-ir-graphviz".into());
271275
output_vector.push(graph.clone())
272276
}
273-
if self.options.enable_cxx_namespaces == true {
277+
if self.options.enable_cxx_namespaces {
274278
output_vector.push("--enable-cxx-namespaces".into());
275279
}
276-
if self.options.disable_name_namespacing == true {
280+
if self.options.disable_name_namespacing {
277281
output_vector.push("--disable-name-namespacing".into());
278282
}
279283

@@ -286,36 +290,36 @@ impl Builder {
286290
})
287291
.count();
288292

289-
if self.options.codegen_config.functions == false {
293+
if !self.options.codegen_config.functions {
290294
output_vector.push("--ignore-functions".into());
291295
}
292296

293297
output_vector.push("--generate".into());
294298

295299
//Temporary placeholder for below 4 options
296300
let mut options:Vec<String> = Vec::new();
297-
if self.options.codegen_config.functions == true {
301+
if self.options.codegen_config.functions {
298302
options.push("function".into());
299303
}
300-
if self.options.codegen_config.types == true {
304+
if self.options.codegen_config.types {
301305
options.push("types".into());
302306
}
303-
if self.options.codegen_config.vars == true {
307+
if self.options.codegen_config.vars {
304308
options.push("vars".into());
305309
}
306-
if self.options.codegen_config.methods == true {
310+
if self.options.codegen_config.methods {
307311
options.push("methods".into());
308312
}
309-
if self.options.codegen_config.constructors == true {
313+
if self.options.codegen_config.constructors {
310314
options.push("constructors".into());
311315
}
312-
if self.options.codegen_config.destructors == true {
316+
if self.options.codegen_config.destructors {
313317
options.push("destructors".into());
314318
}
315319

316320
output_vector.push(options.join(","));
317321

318-
if self.options.codegen_config.methods == false{
322+
if !self.options.codegen_config.methods {
319323
output_vector.push("--ignore-methods".into());
320324
}
321325

@@ -328,15 +332,15 @@ impl Builder {
328332
})
329333
.count();
330334

331-
if self.options.convert_floats == false {
335+
if !self.options.convert_floats {
332336
output_vector.push("--no-convert-floats".into());
333337
}
334338

335-
if self.options.prepend_enum_name == false {
339+
if !self.options.prepend_enum_name {
336340
output_vector.push("--no-prepend-enum-name".into());
337341
}
338342

339-
if self.options.unstable_rust == false {
343+
if !self.options.unstable_rust {
340344
output_vector.push("--no-unstable-rust".into());
341345
}
342346

@@ -368,11 +372,11 @@ impl Builder {
368372
})
369373
.count();
370374

371-
if self.options.use_core == true {
375+
if self.options.use_core {
372376
output_vector.push("--use-core".into());
373377
}
374378

375-
if self.options.conservative_inline_namespaces == true {
379+
if self.options.conservative_inline_namespaces {
376380
output_vector.push("--conservative-inline-namespaces".into());
377381
}
378382

@@ -582,6 +586,12 @@ impl Builder {
582586
self
583587
}
584588

589+
/// Set whether layout tests should be generated.
590+
pub fn layout_tests(mut self, doit: bool) -> Self {
591+
self.options.layout_tests = doit;
592+
self
593+
}
594+
585595
/// Set whether `Debug` should be derived by default.
586596
pub fn derive_debug(mut self, doit: bool) -> Self {
587597
self.options.derive_debug = doit;
@@ -785,11 +795,14 @@ pub struct BindgenOptions {
785795
/// True if we should avoid mangling names with namespaces.
786796
pub disable_name_namespacing: bool,
787797

788-
/// True if we shold derive Debug trait implementations for C/C++ structures
798+
/// True if we should generate layout tests for generated structures.
799+
pub layout_tests: bool,
800+
801+
/// True if we should derive Debug trait implementations for C/C++ structures
789802
/// and types.
790803
pub derive_debug: bool,
791804

792-
/// True if we shold derive Default trait implementations for C/C++ structures
805+
/// True if we should derive Default trait implementations for C/C++ structures
793806
/// and types.
794807
pub derive_default: bool,
795808

@@ -901,6 +914,7 @@ impl Default for BindgenOptions {
901914
emit_ast: false,
902915
emit_ir: false,
903916
emit_ir_graphviz: None,
917+
layout_tests: true,
904918
derive_debug: true,
905919
derive_default: false,
906920
enable_cxx_namespaces: false,
@@ -1237,4 +1251,4 @@ fn commandline_flag_unit_test_function() {
12371251

12381252
assert!(test_cases.iter().all(|ref x| command_line_flags.contains(x)) );
12391253

1240-
}
1254+
}

0 commit comments

Comments
 (0)