diff options
author | Emilio Cobos Álvarez <ecoal95@gmail.com> | 2016-11-24 21:06:42 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <ecoal95@gmail.com> | 2016-11-24 21:06:42 +0100 |
commit | 294c1b07e2ed10fb9d6afdd5a6aa234ebf0258d8 (patch) | |
tree | 21c35ca87733a28e2bd04c7eae07166c35242d89 | |
parent | 7d6cc9a96d9011f089feaac45264ae955215b7d9 (diff) |
Add a new disable-name-namespacing option.
-rw-r--r-- | libbindgen/src/ir/context.rs | 6 | ||||
-rw-r--r-- | libbindgen/src/ir/item.rs | 8 | ||||
-rw-r--r-- | libbindgen/src/lib.rs | 20 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/disable-namespacing.rs | 7 | ||||
-rw-r--r-- | libbindgen/tests/headers/disable-namespacing.hpp | 9 | ||||
-rw-r--r-- | src/options.rs | 7 |
6 files changed, 49 insertions, 8 deletions
diff --git a/libbindgen/src/ir/context.rs b/libbindgen/src/ir/context.rs index 67db2a59..a0e57610 100644 --- a/libbindgen/src/ir/context.rs +++ b/libbindgen/src/ir/context.rs @@ -367,10 +367,8 @@ impl<'ctx> BindgenContext<'ctx> { _ => continue, } - let name = item.real_canonical_name(self, - self.options() - .enable_cxx_namespaces, - true); + let in_namespace = self.options().enable_cxx_namespaces; + let name = item.real_canonical_name(self, in_namespace, true); let replacement = self.replacements.get(&name); if let Some(replacement) = replacement { diff --git a/libbindgen/src/ir/item.rs b/libbindgen/src/ir/item.rs index 1f15ff0f..c81eab8b 100644 --- a/libbindgen/src/ir/item.rs +++ b/libbindgen/src/ir/item.rs @@ -1152,11 +1152,11 @@ impl ItemCanonicalName for Item { debug_assert!(ctx.in_codegen_phase(), "You're not supposed to call this yet"); if self.canonical_name_cache.borrow().is_none() { + let in_namespace = ctx.options().enable_cxx_namespaces || + ctx.options().disable_name_namespacing; + *self.canonical_name_cache.borrow_mut() = - Some(self.real_canonical_name(ctx, - ctx.options() - .enable_cxx_namespaces, - false)); + Some(self.real_canonical_name(ctx, in_namespace, false)); } return self.canonical_name_cache.borrow().as_ref().unwrap().clone(); } diff --git a/libbindgen/src/lib.rs b/libbindgen/src/lib.rs index 10961e36..177e67cd 100644 --- a/libbindgen/src/lib.rs +++ b/libbindgen/src/lib.rs @@ -243,6 +243,22 @@ impl Builder { self } + /// Disable auto-namespacing of names if namespaces are disabled. + /// + /// By default, if namespaces are disabled, bindgen tries to mangle the + /// names to from `foo::bar::Baz` to look like `foo_bar_Baz`, instead of + /// just `Baz`. + /// + /// This option disables that behavior. + /// + /// Note that this intentionally doesn't change the names using for + /// whitelisting and blacklisting, that should still be mangled with the + /// namespaces. + pub fn disable_name_namespacing(mut self) -> Builder { + self.options.disable_name_namespacing = true; + self + } + /// Ignore functions. pub fn ignore_functions(mut self) -> Builder { self.options.ignore_functions = true; @@ -341,6 +357,9 @@ pub struct BindgenOptions { /// generated bindings. pub enable_cxx_namespaces: bool, + /// True if we should avoid mangling names with namespaces. + pub disable_name_namespacing: bool, + /// True if we shold derive Debug trait implementations for C/C++ structures /// and types. pub derive_debug: bool, @@ -400,6 +419,7 @@ impl Default for BindgenOptions { ignore_methods: false, derive_debug: true, enable_cxx_namespaces: false, + disable_name_namespacing: false, unstable_rust: true, use_core: false, ctypes_prefix: None, diff --git a/libbindgen/tests/expectations/tests/disable-namespacing.rs b/libbindgen/tests/expectations/tests/disable-namespacing.rs new file mode 100644 index 00000000..5c166946 --- /dev/null +++ b/libbindgen/tests/expectations/tests/disable-namespacing.rs @@ -0,0 +1,7 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +pub type Baz = ::std::os::raw::c_int; diff --git a/libbindgen/tests/headers/disable-namespacing.hpp b/libbindgen/tests/headers/disable-namespacing.hpp new file mode 100644 index 00000000..11191361 --- /dev/null +++ b/libbindgen/tests/headers/disable-namespacing.hpp @@ -0,0 +1,9 @@ +// bindgen-flags: --disable-name-namespacing + +namespace foo { +namespace bar { + +typedef int Baz; + +} +} diff --git a/src/options.rs b/src/options.rs index 2ea74a27..9632619f 100644 --- a/src/options.rs +++ b/src/options.rs @@ -58,6 +58,9 @@ pub fn builder_from_flags<I>(args: I) Arg::with_name("enable-cxx-namespaces") .long("enable-cxx-namespaces") .help("Enable support for C++ namespaces."), + Arg::with_name("disable-name-namespacing") + .long("disable-name-namespacing") + .help("Disable name namespacing if namespaces are disabled."), Arg::with_name("framework") .long("framework-link") .help("Link to framework.") @@ -194,6 +197,10 @@ pub fn builder_from_flags<I>(args: I) builder = builder.enable_cxx_namespaces(); } + if matches.is_present("disable-name-namespacing") { + builder = builder.disable_name_namespacing(); + } + if let Some(links) = matches.values_of("framework") { for framework in links { builder = builder.link_framework(framework); |