diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-02-18 19:30:00 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-02-19 10:54:04 +0100 |
commit | 18a7d3e014fc99bdd086feaf1498e8ad67b00c87 (patch) | |
tree | ec167c9f1089cec29323dbca4ced97f705986264 | |
parent | 99974add51bc247a11dffb14887cf2bf71816328 (diff) |
Add an option to distrust clang mangling.
-rw-r--r-- | src/ir/function.rs | 10 | ||||
-rw-r--r-- | src/ir/var.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 22 | ||||
-rw-r--r-- | src/options.rs | 3 |
4 files changed, 34 insertions, 3 deletions
diff --git a/src/ir/function.rs b/src/ir/function.rs index 22b9c9b0..afd2adec 100644 --- a/src/ir/function.rs +++ b/src/ir/function.rs @@ -91,7 +91,13 @@ fn get_abi(cc: CXCallingConv) -> Option<abi::Abi> { } /// Get the mangled name for the cursor's referent. -pub fn cursor_mangling(cursor: &clang::Cursor) -> Option<String> { +pub fn cursor_mangling(ctx: &BindgenContext, + cursor: &clang::Cursor) + -> Option<String> { + if !ctx.options().enable_mangling { + return None; + } + // We early return here because libclang may crash in some case // if we pass in a variable inside a partial specialized template. // See servo/rust-bindgen#67, and servo/rust-bindgen#462. @@ -304,7 +310,7 @@ impl ClangSubItemParser for Function { let name = cursor.spelling(); assert!(!name.is_empty(), "Empty function name?"); - let mut mangled_name = cursor_mangling(&cursor); + let mut mangled_name = cursor_mangling(context, &cursor); if mangled_name.as_ref() == Some(&name) { mangled_name = None; } diff --git a/src/ir/var.rs b/src/ir/var.rs index 6cfcdae7..930e07ee 100644 --- a/src/ir/var.rs +++ b/src/ir/var.rs @@ -238,7 +238,7 @@ impl ClangSubItemParser for Var { .map(VarType::String) }; - let mangling = cursor_mangling(&cursor); + let mangling = cursor_mangling(ctx, &cursor); let var = Var::new(name, mangling, ty, value, is_const); Ok(ParseResult::New(var, Some(cursor))) @@ -212,6 +212,18 @@ impl Builder { self } + /// Whether to use the clang-provided name mangling. This is true and + /// probably needed for C++ features. + /// + /// However, some old libclang versions seem to return incorrect results in + /// some cases for non-mangled functions, see [1], so we allow disabling it. + /// + /// [1]: https://github.com/servo/rust-bindgen/issues/528 + pub fn trust_clang_mangling(mut self, doit: bool) -> Self { + self.options.enable_mangling = doit; + self + } + /// Generate a C/C++ file that includes the header and has dummy uses of /// every type defined in the header. pub fn dummy_uses<T: Into<String>>(mut self, dummy_uses: T) -> Builder { @@ -572,6 +584,15 @@ pub struct BindgenOptions { /// Intead of emitting 'use objc;' to files generated from objective c files, /// generate '#[macro_use] extern crate objc;' pub objc_extern_crate: bool, + + /// Whether to use the clang-provided name mangling. This is true and + /// probably needed for C++ features. + /// + /// However, some old libclang versions seem to return incorrect results in + /// some cases for non-mangled functions, see [1], so we allow disabling it. + /// + /// [1]: https://github.com/servo/rust-bindgen/issues/528 + pub enable_mangling: bool, } /// TODO(emilio): This is sort of a lie (see the error message that results from @@ -626,6 +647,7 @@ impl Default for BindgenOptions { generate_comments: true, whitelist_recursively: true, objc_extern_crate: false, + enable_mangling: true, } } } diff --git a/src/options.rs b/src/options.rs index e54ee012..a62aa73d 100644 --- a/src/options.rs +++ b/src/options.rs @@ -60,6 +60,9 @@ pub fn builder_from_flags<I> Arg::with_name("objc-extern-crate") .long("objc-extern-crate") .help("Use extern crate instead of use for objc"), + Arg::with_name("distrust-clang-mangling") + .long("distrust-clang-mangling") + .help("Do not trust the libclang-provided mangling"), Arg::with_name("builtins") .long("builtins") .help("Output bindings for builtin definitions, e.g. \ |