diff options
-rw-r--r-- | src/codegen/mod.rs | 10 | ||||
-rw-r--r-- | src/lib.rs | 10 | ||||
-rw-r--r-- | src/options.rs | 7 | ||||
-rw-r--r-- | tests/expectations/tests/prepend_enum_name.rs | 9 | ||||
-rw-r--r-- | tests/headers/prepend_enum_name.hpp | 6 |
5 files changed, 39 insertions, 3 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 46b0a3e7..120acefa 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1993,10 +1993,14 @@ impl CodeGenerator for Enum { Some(item.parent_id().canonical_name(ctx)) }; - let constant_mangling_prefix = if enum_ty.name().is_none() { - parent_canonical_name.as_ref().map(|n| &*n) + let constant_mangling_prefix = if ctx.options().prepend_enum_name { + if enum_ty.name().is_none() { + parent_canonical_name.as_ref().map(|n| &*n) + } else { + Some(&name) + } } else { - Some(&name) + None }; // NB: We defer the creation of constified variants, in case we find @@ -458,6 +458,12 @@ impl Builder { self } + /// Prepend the enum name to constant or bitfield variants. + pub fn prepend_enum_name(mut self, doit: bool) -> Self { + self.options.prepend_enum_name = doit; + self + } + /// Generate the Rust bindings using the options built up thus far. pub fn generate<'ctx>(self) -> Result<Bindings<'ctx>, ()> { Bindings::generate(self.options, None) @@ -593,6 +599,9 @@ pub struct BindgenOptions { /// /// [1]: https://github.com/servo/rust-bindgen/issues/528 pub enable_mangling: bool, + + /// Whether to prepend the enum name to bitfield or constant variants. + pub prepend_enum_name: bool, } /// TODO(emilio): This is sort of a lie (see the error message that results from @@ -648,6 +657,7 @@ impl Default for BindgenOptions { whitelist_recursively: true, objc_extern_crate: false, enable_mangling: true, + prepend_enum_name: true, } } } diff --git a/src/options.rs b/src/options.rs index a62aa73d..78a0e30b 100644 --- a/src/options.rs +++ b/src/options.rs @@ -127,6 +127,9 @@ pub fn builder_from_flags<I> Arg::with_name("no-convert-floats") .long("no-convert-floats") .help("Don't automatically convert floats to f32/f64."), + Arg::with_name("no-prepend-enum-name") + .long("no-prepend-enum-name") + .help("Do not prepend the enum name to bitfield or constant variants"), Arg::with_name("no-unstable-rust") .long("no-unstable-rust") .help("Do not generate unstable Rust code.") @@ -239,6 +242,10 @@ pub fn builder_from_flags<I> builder = builder.derive_default(false); } + if matches.is_present("no-prepend-enum-name") { + builder = builder.prepend_enum_name(false); + } + if let Some(prefix) = matches.value_of("ctypes-prefix") { builder = builder.ctypes_prefix(prefix); } diff --git a/tests/expectations/tests/prepend_enum_name.rs b/tests/expectations/tests/prepend_enum_name.rs new file mode 100644 index 00000000..d95bd1b4 --- /dev/null +++ b/tests/expectations/tests/prepend_enum_name.rs @@ -0,0 +1,9 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +pub const FOO_BAR: foo = 0; +pub const FOO_BAZ: foo = 1; +pub type foo = ::std::os::raw::c_uint; diff --git a/tests/headers/prepend_enum_name.hpp b/tests/headers/prepend_enum_name.hpp new file mode 100644 index 00000000..df4ecf1f --- /dev/null +++ b/tests/headers/prepend_enum_name.hpp @@ -0,0 +1,6 @@ +// bindgen-flags: --constified-enum foo --no-prepend-enum-name + +enum foo { + FOO_BAR, + FOO_BAZ, +}; |