From 1bf453cdc5fb278a0c840986a6723ad9c01f5ef3 Mon Sep 17 00:00:00 2001 From: th0rex Date: Mon, 27 Feb 2017 18:17:28 +0100 Subject: Add toggle for prepending enum name to variant Currently the name of a enum is always prepended to the beginning of a const or bitfield like variant. This can result in some quite unintuivite naming, if the library already prefixes its variants. For example hundreds of variants are named like this `cs_arch_CS_ARCH_ARM` when binding to the capstone library. This commit introduces a toggle for prepending the `cs_arch_` part. By default it is enabled to preserve current behaviour. It can be toggled with the `prepend_enum_name` function on the Builder. --- src/codegen/mod.rs | 10 +++++++--- src/lib.rs | 10 ++++++++++ 2 files changed, 17 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 diff --git a/src/lib.rs b/src/lib.rs index 94a13c61..5e14d81e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::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, } } } -- cgit v1.2.3