diff options
author | Vladimir Vukicevic <vladimir@pobox.com> | 2016-05-31 10:29:28 -0400 |
---|---|---|
committer | Vladimir Vukicevic <vladimir@pobox.com> | 2016-07-18 17:32:02 -0400 |
commit | 38f7561c8e28ce84cc651369dce7952352f5b82e (patch) | |
tree | f1240ea1019bfc2b7db66a136dfaf8da3c686e4e | |
parent | 644589c7fe75c2f1aa1568d80d60c30687b698be (diff) |
Add explicit option for selecting MSVC mangling
There's no easy way to get the current target triple from the clang-c
bindings. Make this simple and require an option to be passed when
generating bindings for msvc, using the x86_64-pc-win32 target (or i686)
-rw-r--r-- | src/bin/bindgen.rs | 6 | ||||
-rw-r--r-- | src/clang.rs | 8 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/parser.rs | 19 |
4 files changed, 27 insertions, 10 deletions
diff --git a/src/bin/bindgen.rs b/src/bin/bindgen.rs index 7ad4d07f..443a0ff0 100644 --- a/src/bin/bindgen.rs +++ b/src/bin/bindgen.rs @@ -166,6 +166,10 @@ fn parse_args(args: &[String]) -> ParseResult { options.raw_lines.push(args[ix + 1].clone()); ix += 2; } + "-use-msvc-mangling" => { + options.msvc_mangling = true; + ix += 1; + } _ => { options.clang_args.push(args[ix].clone()); ix += 1; @@ -201,6 +205,8 @@ Options: -allow-unknown-types Don't fail if we encounter types we do not support, instead treat them as void -emit-clang-ast Output the ast (for debugging purposes) + -use-msvc-mangling Handle MSVC C++ ABI mangling; requires that --target + be set to (i686|x86_64)-pc-win32 -override-enum-type <type> Override enum type, type name could be uchar schar diff --git a/src/clang.rs b/src/clang.rs index 2f6e6223..c230460d 100644 --- a/src/clang.rs +++ b/src/clang.rs @@ -32,15 +32,9 @@ impl Cursor { } pub fn mangling(&self) -> String { - let mut mangling = unsafe { + unsafe { String_ { x: clang_Cursor_getMangling(self.x) }.to_string() - }; - - // Try to undo backend mangling - if cfg!(target_os = "macos") || cfg!(all(target_os = "windows", target_env = "gnu")) { - mangling.remove(0); } - mangling } pub fn lexical_parent(&self) -> Cursor { @@ -170,6 +170,8 @@ pub struct BindgenOptions { pub class_constants: bool, /// Wether to generate names that are **directly** under namespaces. pub namespaced_constants: bool, + // whether to use msvc mangling rules + pub msvc_mangling: bool, pub override_enum_ty: String, pub raw_lines: Vec<String>, /// Attributes for a type with destructor @@ -197,6 +199,7 @@ impl Default for BindgenOptions { unstable_rust: true, class_constants: true, namespaced_constants: true, + msvc_mangling: false, raw_lines: vec![], dtor_attrs: vec![], clang_args: vec![], @@ -325,6 +328,7 @@ fn parse_headers(options: &BindgenOptions, logger: &Logger) -> Result<ModuleMap, clang_args: options.clang_args.clone(), opaque_types: options.opaque_types.clone(), blacklist_type: options.blacklist_type.clone(), + msvc_mangling: options.msvc_mangling, }; parser::parse(clang_opts, logger) diff --git a/src/parser.rs b/src/parser.rs index 71a6dff9..08c3bace 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -31,6 +31,7 @@ pub struct ClangParserOptions { pub clang_args: Vec<String>, pub opaque_types: Vec<String>, pub blacklist_type: Vec<String>, + pub msvc_mangling: bool, } struct ClangParserCtx<'a> { @@ -66,6 +67,18 @@ impl<'a> ClangParserCtx<'a> { } } +fn cursor_link_name(ctx: &mut ClangParserCtx, cursor: &Cursor) -> String { + let mut mangling = cursor.mangling(); + + // Try to undo backend linkage munging (prepended _, generally) + if cfg!(target_os = "macos") || + (cfg!(target_os = "windows") && !ctx.options.msvc_mangling) + { + mangling.remove(0); + } + mangling +} + fn match_pattern(ctx: &mut ClangParserCtx, cursor: &Cursor) -> bool { let (file, _, _, _) = cursor.location().location(); @@ -195,7 +208,7 @@ fn decl_name(ctx: &mut ClangParserCtx, cursor: &Cursor) -> Global { GType(ti) } CXCursor_VarDecl => { - let mangled = cursor.mangling(); + let mangled = cursor_link_name(ctx, &cursor); let is_const = ty.is_const(); let ty = conv_ty_resolving_typedefs(ctx, &ty, &cursor, true); let mut vi = VarInfo::new(spelling, mangled, comment, ty); @@ -211,7 +224,7 @@ fn decl_name(ctx: &mut ClangParserCtx, cursor: &Cursor) -> Global { GVar(vi) } CXCursor_FunctionDecl => { - let mangled = cursor.mangling(); + let mangled = cursor_link_name(ctx, &cursor); let vi = Rc::new(RefCell::new(VarInfo::new(spelling, mangled, comment, TVoid))); GFunc(vi) } @@ -971,7 +984,7 @@ fn visit_composite(cursor: &Cursor, parent: &Cursor, return CXChildVisit_Continue; } - let mut vi = VarInfo::new(spelling, cursor.mangling(), cursor.raw_comment(), sig); + let mut vi = VarInfo::new(spelling, cursor_link_name(ctx, &cursor), cursor.raw_comment(), sig); vi.is_static = cursor.method_is_static(); vi.is_const = cursor.cur_type().is_const(); |