Skip to content

Commit dfeff89

Browse files
varphoneemilio
authored andcommitted
Add --anon-fields-prefix option
Allow to use the given prefix for the anon fields instead of `__bindgen_anon_`.
1 parent 87b2bc0 commit dfeff89

File tree

5 files changed

+207
-3
lines changed

5 files changed

+207
-3
lines changed

src/ir/comp.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,15 @@ impl CompFields {
825825
}
826826

827827
anon_field_counter += 1;
828-
let generated_name =
829-
format!("__bindgen_anon_{}", anon_field_counter);
830-
*name = Some(generated_name);
828+
if let Some(ref prefix) = ctx.options().anon_fields_prefix {
829+
*name =
830+
Some(format!("{}{}", prefix, anon_field_counter));
831+
} else {
832+
*name = Some(format!(
833+
"__bindgen_anon_{}",
834+
anon_field_counter
835+
));
836+
}
831837
}
832838
Field::Bitfields(ref mut bu) => {
833839
for bitfield in &mut bu.bitfields {

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ impl Builder {
385385
output_vector.push(prefix.clone());
386386
}
387387

388+
if let Some(ref prefix) = self.options.anon_fields_prefix {
389+
output_vector.push("--anon-fields-prefix".into());
390+
output_vector.push(prefix.clone());
391+
}
392+
388393
if self.options.emit_ast {
389394
output_vector.push("--emit-clang-ast".into());
390395
}
@@ -1212,6 +1217,12 @@ impl Builder {
12121217
self
12131218
}
12141219

1220+
/// Use the given prefix for the anon fields instead of `__bindgen_anon_`.
1221+
pub fn anon_fields_prefix<T: Into<String>>(mut self, prefix: T) -> Builder {
1222+
self.options.anon_fields_prefix = Some(prefix.into());
1223+
self
1224+
}
1225+
12151226
/// Allows configuring types in different situations, see the
12161227
/// [`ParseCallbacks`](./callbacks/trait.ParseCallbacks.html) documentation.
12171228
pub fn parse_callbacks(
@@ -1590,6 +1601,9 @@ struct BindgenOptions {
15901601
/// An optional prefix for the "raw" types, like `c_int`, `c_void`...
15911602
ctypes_prefix: Option<String>,
15921603

1604+
/// An optional prefix for the anon fields instead of `__bindgen_anon_`.
1605+
anon_fields_prefix: Option<String>,
1606+
15931607
/// Whether to time the bindgen phases.
15941608
time_phases: bool,
15951609

@@ -1809,6 +1823,7 @@ impl Default for BindgenOptions {
18091823
disable_header_comment: false,
18101824
use_core: false,
18111825
ctypes_prefix: None,
1826+
anon_fields_prefix: None,
18121827
namespaced_constants: true,
18131828
msvc_mangling: false,
18141829
convert_floats: true,

src/options.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ where
234234
)
235235
.value_name("prefix")
236236
.takes_value(true),
237+
Arg::with_name("anon-fields-prefix")
238+
.long("anon-fields-prefix")
239+
.help(
240+
"Use the given prefix for the anon fields instead of \
241+
__bindgen_anon_.",
242+
)
243+
.value_name("prefix")
244+
.takes_value(true),
237245
Arg::with_name("time-phases")
238246
.long("time-phases")
239247
.help("Time the different bindgen phases and print to stderr"),
@@ -634,6 +642,10 @@ where
634642
builder = builder.ctypes_prefix(prefix);
635643
}
636644

645+
if let Some(prefix) = matches.value_of("anon-fields-prefix") {
646+
builder = builder.anon_fields_prefix(prefix);
647+
}
648+
637649
if let Some(what_to_generate) = matches.value_of("generate") {
638650
let mut config = CodegenConfig::empty();
639651
for what in what_to_generate.split(",") {
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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(Copy, Clone)]
10+
pub union color {
11+
pub u1: color__bindgen_ty_1,
12+
pub u2: color__bindgen_ty_2,
13+
pub v3: [::std::os::raw::c_uchar; 3usize],
14+
_bindgen_union_align: [u8; 3usize],
15+
}
16+
#[repr(C)]
17+
#[derive(Debug, Default, Copy, Clone)]
18+
pub struct color__bindgen_ty_1 {
19+
pub r: ::std::os::raw::c_uchar,
20+
pub g: ::std::os::raw::c_uchar,
21+
pub b: ::std::os::raw::c_uchar,
22+
}
23+
#[test]
24+
fn bindgen_test_layout_color__bindgen_ty_1() {
25+
assert_eq!(
26+
::std::mem::size_of::<color__bindgen_ty_1>(),
27+
3usize,
28+
concat!("Size of: ", stringify!(color__bindgen_ty_1))
29+
);
30+
assert_eq!(
31+
::std::mem::align_of::<color__bindgen_ty_1>(),
32+
1usize,
33+
concat!("Alignment of ", stringify!(color__bindgen_ty_1))
34+
);
35+
assert_eq!(
36+
unsafe {
37+
&(*(::std::ptr::null::<color__bindgen_ty_1>())).r as *const _
38+
as usize
39+
},
40+
0usize,
41+
concat!(
42+
"Offset of field: ",
43+
stringify!(color__bindgen_ty_1),
44+
"::",
45+
stringify!(r)
46+
)
47+
);
48+
assert_eq!(
49+
unsafe {
50+
&(*(::std::ptr::null::<color__bindgen_ty_1>())).g as *const _
51+
as usize
52+
},
53+
1usize,
54+
concat!(
55+
"Offset of field: ",
56+
stringify!(color__bindgen_ty_1),
57+
"::",
58+
stringify!(g)
59+
)
60+
);
61+
assert_eq!(
62+
unsafe {
63+
&(*(::std::ptr::null::<color__bindgen_ty_1>())).b as *const _
64+
as usize
65+
},
66+
2usize,
67+
concat!(
68+
"Offset of field: ",
69+
stringify!(color__bindgen_ty_1),
70+
"::",
71+
stringify!(b)
72+
)
73+
);
74+
}
75+
#[repr(C)]
76+
#[derive(Debug, Default, Copy, Clone)]
77+
pub struct color__bindgen_ty_2 {
78+
pub y: ::std::os::raw::c_uchar,
79+
pub u: ::std::os::raw::c_uchar,
80+
pub v: ::std::os::raw::c_uchar,
81+
}
82+
#[test]
83+
fn bindgen_test_layout_color__bindgen_ty_2() {
84+
assert_eq!(
85+
::std::mem::size_of::<color__bindgen_ty_2>(),
86+
3usize,
87+
concat!("Size of: ", stringify!(color__bindgen_ty_2))
88+
);
89+
assert_eq!(
90+
::std::mem::align_of::<color__bindgen_ty_2>(),
91+
1usize,
92+
concat!("Alignment of ", stringify!(color__bindgen_ty_2))
93+
);
94+
assert_eq!(
95+
unsafe {
96+
&(*(::std::ptr::null::<color__bindgen_ty_2>())).y as *const _
97+
as usize
98+
},
99+
0usize,
100+
concat!(
101+
"Offset of field: ",
102+
stringify!(color__bindgen_ty_2),
103+
"::",
104+
stringify!(y)
105+
)
106+
);
107+
assert_eq!(
108+
unsafe {
109+
&(*(::std::ptr::null::<color__bindgen_ty_2>())).u as *const _
110+
as usize
111+
},
112+
1usize,
113+
concat!(
114+
"Offset of field: ",
115+
stringify!(color__bindgen_ty_2),
116+
"::",
117+
stringify!(u)
118+
)
119+
);
120+
assert_eq!(
121+
unsafe {
122+
&(*(::std::ptr::null::<color__bindgen_ty_2>())).v as *const _
123+
as usize
124+
},
125+
2usize,
126+
concat!(
127+
"Offset of field: ",
128+
stringify!(color__bindgen_ty_2),
129+
"::",
130+
stringify!(v)
131+
)
132+
);
133+
}
134+
#[test]
135+
fn bindgen_test_layout_color() {
136+
assert_eq!(
137+
::std::mem::size_of::<color>(),
138+
3usize,
139+
concat!("Size of: ", stringify!(color))
140+
);
141+
assert_eq!(
142+
::std::mem::align_of::<color>(),
143+
1usize,
144+
concat!("Alignment of ", stringify!(color))
145+
);
146+
assert_eq!(
147+
unsafe { &(*(::std::ptr::null::<color>())).v3 as *const _ as usize },
148+
0usize,
149+
concat!("Offset of field: ", stringify!(color), "::", stringify!(v3))
150+
);
151+
}
152+
impl Default for color {
153+
fn default() -> Self {
154+
unsafe { ::std::mem::zeroed() }
155+
}
156+
}

tests/headers/anon-fields-prefix.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// bindgen-flags: --anon-fields-prefix "u"
2+
3+
union color {
4+
struct {
5+
unsigned char r;
6+
unsigned char g;
7+
unsigned char b;
8+
};
9+
struct {
10+
unsigned char y;
11+
unsigned char u;
12+
unsigned char v;
13+
};
14+
unsigned char v3[3];
15+
};

0 commit comments

Comments
 (0)