Skip to content

Commit 6adf4ff

Browse files
author
Jethro Beekman
committed
Add flag to ignore type blocklist when computing derive information
Fixes #1454
1 parent 9bf85fb commit 6adf4ff

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

src/ir/analysis/derive.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ impl<'ctx> CannotDerive<'ctx> {
138138
}
139139

140140
fn constrain_type(&mut self, item: &Item, ty: &Type) -> CanDerive {
141-
if !self.ctx.allowlisted_items().contains(&item.id()) {
142-
trace!(
141+
if !self.ctx.options().derive_ignore_blocklist &&
142+
!self.ctx.allowlisted_items().contains(&item.id())
143+
{ trace!(
143144
" cannot derive {} for blocklisted type",
144145
self.derive_trait
145146
);

src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ impl Builder {
549549
output_vector.push("--respect-cxx-access-specs".into());
550550
}
551551

552+
if self.options.derive_ignore_blocklist {
553+
output_vector.push("--derive-ignore-blocklist".into());
554+
}
555+
552556
// Add clang arguments
553557

554558
output_vector.push("--".into());
@@ -1568,6 +1572,12 @@ impl Builder {
15681572
self.options.respect_cxx_access_specs = doit;
15691573
self
15701574
}
1575+
1576+
/// Ignore the type blocklist when computing type derive information
1577+
pub fn derive_ignore_blocklist(mut self, doit: bool) -> Self {
1578+
self.options.derive_ignore_blocklist = doit;
1579+
self
1580+
}
15711581
}
15721582

15731583
/// Configuration options for generated bindings.
@@ -1859,6 +1869,9 @@ struct BindgenOptions {
18591869
/// Only make generated bindings `pub` if the items would be publically accessible
18601870
/// by C++.
18611871
respect_cxx_access_specs: bool,
1872+
1873+
/// Ignore the type blocklist when computing type derive information
1874+
derive_ignore_blocklist: bool,
18621875
}
18631876

18641877
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -1996,6 +2009,7 @@ impl Default for BindgenOptions {
19962009
wasm_import_module_name: None,
19972010
dynamic_library_name: None,
19982011
respect_cxx_access_specs: false,
2012+
derive_ignore_blocklist: false,
19992013
}
20002014
}
20012015
}

src/options.rs

+7
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ where
503503
Arg::with_name("respect-cxx-access-specs")
504504
.long("respect-cxx-access-specs")
505505
.help("Makes generated bindings `pub` only for items if the items are publically accessible in C++."),
506+
Arg::with_name("derive-ignore-blocklist")
507+
.long("derive-ignore-blocklist")
508+
.help("Ignore the type blocklist when computing type derive information"),
506509
]) // .args()
507510
.get_matches_from(args);
508511

@@ -929,6 +932,10 @@ where
929932
builder = builder.respect_cxx_access_specs(true);
930933
}
931934

935+
if matches.is_present("derive-ignore-blocklist") {
936+
builder = builder.derive_ignore_blocklist(true);
937+
}
938+
932939
let verbose = matches.is_present("verbose");
933940

934941
Ok((builder, output, verbose))
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
#[repr(C)]
9+
#[derive(Debug, Default, Copy, Clone)]
10+
pub struct rlimit;
11+
12+
#[repr(C)]
13+
#[derive(Debug, Default, Copy, Clone)]
14+
pub struct my_rlimit_conf {
15+
pub core: rlimit,
16+
pub cpu: rlimit,
17+
pub data: rlimit,
18+
pub fsize: rlimit,
19+
}
20+
#[test]
21+
fn bindgen_test_layout_my_rlimit_conf() {
22+
assert_eq!(
23+
::std::mem::size_of::<my_rlimit_conf>(),
24+
0usize,
25+
concat!("Size of: ", stringify!(my_rlimit_conf))
26+
);
27+
assert_eq!(
28+
::std::mem::align_of::<my_rlimit_conf>(),
29+
1usize,
30+
concat!("Alignment of ", stringify!(my_rlimit_conf))
31+
);
32+
assert_eq!(
33+
unsafe {
34+
&(*(::std::ptr::null::<my_rlimit_conf>())).core as *const _ as usize
35+
},
36+
0usize,
37+
concat!(
38+
"Offset of field: ",
39+
stringify!(my_rlimit_conf),
40+
"::",
41+
stringify!(core)
42+
)
43+
);
44+
assert_eq!(
45+
unsafe {
46+
&(*(::std::ptr::null::<my_rlimit_conf>())).cpu as *const _ as usize
47+
},
48+
0usize,
49+
concat!(
50+
"Offset of field: ",
51+
stringify!(my_rlimit_conf),
52+
"::",
53+
stringify!(cpu)
54+
)
55+
);
56+
assert_eq!(
57+
unsafe {
58+
&(*(::std::ptr::null::<my_rlimit_conf>())).data as *const _ as usize
59+
},
60+
0usize,
61+
concat!(
62+
"Offset of field: ",
63+
stringify!(my_rlimit_conf),
64+
"::",
65+
stringify!(data)
66+
)
67+
);
68+
assert_eq!(
69+
unsafe {
70+
&(*(::std::ptr::null::<my_rlimit_conf>())).fsize as *const _
71+
as usize
72+
},
73+
0usize,
74+
concat!(
75+
"Offset of field: ",
76+
stringify!(my_rlimit_conf),
77+
"::",
78+
stringify!(fsize)
79+
)
80+
);
81+
}

tests/headers/issue-1454.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// bindgen-flags: --no-recursive-allowlist --allowlist-type "my_rlimit_conf" --derive-ignore-blocklist --raw-line "#[repr(C)] #[derive(Debug, Default, Copy, Clone)] pub struct rlimit;"
2+
3+
struct rlimit {};
4+
5+
typedef struct
6+
{
7+
struct rlimit core;
8+
struct rlimit cpu;
9+
struct rlimit data;
10+
struct rlimit fsize;
11+
}
12+
my_rlimit_conf;

0 commit comments

Comments
 (0)