Add --anon-fields-prefix option

Allow to use the given prefix for the anon fields instead of `__bindgen_anon_`.
This commit is contained in:
Varphone Wong 2019-10-31 23:32:45 +08:00 committed by Emilio Cobos Álvarez
parent 87b2bc033f
commit dfeff8992e
5 changed files with 207 additions and 3 deletions

View File

@ -825,9 +825,15 @@ impl CompFields {
}
anon_field_counter += 1;
let generated_name =
format!("__bindgen_anon_{}", anon_field_counter);
*name = Some(generated_name);
if let Some(ref prefix) = ctx.options().anon_fields_prefix {
*name =
Some(format!("{}{}", prefix, anon_field_counter));
} else {
*name = Some(format!(
"__bindgen_anon_{}",
anon_field_counter
));
}
}
Field::Bitfields(ref mut bu) => {
for bitfield in &mut bu.bitfields {

View File

@ -385,6 +385,11 @@ impl Builder {
output_vector.push(prefix.clone());
}
if let Some(ref prefix) = self.options.anon_fields_prefix {
output_vector.push("--anon-fields-prefix".into());
output_vector.push(prefix.clone());
}
if self.options.emit_ast {
output_vector.push("--emit-clang-ast".into());
}
@ -1212,6 +1217,12 @@ impl Builder {
self
}
/// Use the given prefix for the anon fields instead of `__bindgen_anon_`.
pub fn anon_fields_prefix<T: Into<String>>(mut self, prefix: T) -> Builder {
self.options.anon_fields_prefix = Some(prefix.into());
self
}
/// Allows configuring types in different situations, see the
/// [`ParseCallbacks`](./callbacks/trait.ParseCallbacks.html) documentation.
pub fn parse_callbacks(
@ -1590,6 +1601,9 @@ struct BindgenOptions {
/// An optional prefix for the "raw" types, like `c_int`, `c_void`...
ctypes_prefix: Option<String>,
/// An optional prefix for the anon fields instead of `__bindgen_anon_`.
anon_fields_prefix: Option<String>,
/// Whether to time the bindgen phases.
time_phases: bool,
@ -1809,6 +1823,7 @@ impl Default for BindgenOptions {
disable_header_comment: false,
use_core: false,
ctypes_prefix: None,
anon_fields_prefix: None,
namespaced_constants: true,
msvc_mangling: false,
convert_floats: true,

View File

@ -234,6 +234,14 @@ where
)
.value_name("prefix")
.takes_value(true),
Arg::with_name("anon-fields-prefix")
.long("anon-fields-prefix")
.help(
"Use the given prefix for the anon fields instead of \
__bindgen_anon_.",
)
.value_name("prefix")
.takes_value(true),
Arg::with_name("time-phases")
.long("time-phases")
.help("Time the different bindgen phases and print to stderr"),
@ -634,6 +642,10 @@ where
builder = builder.ctypes_prefix(prefix);
}
if let Some(prefix) = matches.value_of("anon-fields-prefix") {
builder = builder.anon_fields_prefix(prefix);
}
if let Some(what_to_generate) = matches.value_of("generate") {
let mut config = CodegenConfig::empty();
for what in what_to_generate.split(",") {

View File

@ -0,0 +1,156 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union color {
pub u1: color__bindgen_ty_1,
pub u2: color__bindgen_ty_2,
pub v3: [::std::os::raw::c_uchar; 3usize],
_bindgen_union_align: [u8; 3usize],
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct color__bindgen_ty_1 {
pub r: ::std::os::raw::c_uchar,
pub g: ::std::os::raw::c_uchar,
pub b: ::std::os::raw::c_uchar,
}
#[test]
fn bindgen_test_layout_color__bindgen_ty_1() {
assert_eq!(
::std::mem::size_of::<color__bindgen_ty_1>(),
3usize,
concat!("Size of: ", stringify!(color__bindgen_ty_1))
);
assert_eq!(
::std::mem::align_of::<color__bindgen_ty_1>(),
1usize,
concat!("Alignment of ", stringify!(color__bindgen_ty_1))
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<color__bindgen_ty_1>())).r as *const _
as usize
},
0usize,
concat!(
"Offset of field: ",
stringify!(color__bindgen_ty_1),
"::",
stringify!(r)
)
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<color__bindgen_ty_1>())).g as *const _
as usize
},
1usize,
concat!(
"Offset of field: ",
stringify!(color__bindgen_ty_1),
"::",
stringify!(g)
)
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<color__bindgen_ty_1>())).b as *const _
as usize
},
2usize,
concat!(
"Offset of field: ",
stringify!(color__bindgen_ty_1),
"::",
stringify!(b)
)
);
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct color__bindgen_ty_2 {
pub y: ::std::os::raw::c_uchar,
pub u: ::std::os::raw::c_uchar,
pub v: ::std::os::raw::c_uchar,
}
#[test]
fn bindgen_test_layout_color__bindgen_ty_2() {
assert_eq!(
::std::mem::size_of::<color__bindgen_ty_2>(),
3usize,
concat!("Size of: ", stringify!(color__bindgen_ty_2))
);
assert_eq!(
::std::mem::align_of::<color__bindgen_ty_2>(),
1usize,
concat!("Alignment of ", stringify!(color__bindgen_ty_2))
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<color__bindgen_ty_2>())).y as *const _
as usize
},
0usize,
concat!(
"Offset of field: ",
stringify!(color__bindgen_ty_2),
"::",
stringify!(y)
)
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<color__bindgen_ty_2>())).u as *const _
as usize
},
1usize,
concat!(
"Offset of field: ",
stringify!(color__bindgen_ty_2),
"::",
stringify!(u)
)
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<color__bindgen_ty_2>())).v as *const _
as usize
},
2usize,
concat!(
"Offset of field: ",
stringify!(color__bindgen_ty_2),
"::",
stringify!(v)
)
);
}
#[test]
fn bindgen_test_layout_color() {
assert_eq!(
::std::mem::size_of::<color>(),
3usize,
concat!("Size of: ", stringify!(color))
);
assert_eq!(
::std::mem::align_of::<color>(),
1usize,
concat!("Alignment of ", stringify!(color))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<color>())).v3 as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(color), "::", stringify!(v3))
);
}
impl Default for color {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}

View File

@ -0,0 +1,15 @@
// bindgen-flags: --anon-fields-prefix "u"
union color {
struct {
unsigned char r;
unsigned char g;
unsigned char b;
};
struct {
unsigned char y;
unsigned char u;
unsigned char v;
};
unsigned char v3[3];
};