Skip to content

Commit

Permalink
Auto merge of #543 - th0rex:master, r=emilio
Browse files Browse the repository at this point in the history
Add option to not add enum name to bitfield or constant variants

Many C libraries prefix the variants of their enums already, because C enums are not scoped. That means if bindgen prefixes them again that can lead to ugly names like `cs_arch_CS_ARCH_ARM`. The `cs_arch_` part comes from the name of the enum itself and `CS_ARCH_ARM` is the name of the actual variant.

This pull request introduces a variable for changing this behaviour, alongside command line flags and a test case. If `prepend_enum_names` is set to false the resulting variant name will be `CS_ARCH_ARM` instead of `cs_arch_CS_ARCH_ARM` as it is now.

If there is anything that could be improved, or this toggle already exists (I have not found anything like that), please let me know.
  • Loading branch information
bors-servo authored Feb 27, 2017
2 parents b666c73 + 49fa299 commit 4e4f092
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -648,6 +657,7 @@ impl Default for BindgenOptions {
whitelist_recursively: true,
objc_extern_crate: false,
enable_mangling: true,
prepend_enum_name: true,
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/expectations/tests/prepend_enum_name.rs
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions tests/headers/prepend_enum_name.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// bindgen-flags: --constified-enum foo --no-prepend-enum-name

enum foo {
FOO_BAR,
FOO_BAZ,
};

0 comments on commit 4e4f092

Please sign in to comment.