Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a workaround for #528, and fix #527 #532

Merged
merged 4 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,13 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
&specialized);
}
}

if let Some(parent) = c.fallible_semantic_parent() {
println!("");
print_cursor(depth,
String::from(prefix) + "semantic-parent.",
&parent);
}
}

fn print_type<S: AsRef<str>>(depth: isize, prefix: S, ty: &Type) {
Expand Down
13 changes: 12 additions & 1 deletion src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,19 @@ impl CompInfo {
// StructDecl to note incomplete structs that hasn't been
// forward-declared before, see:
//
// Also, clang seems to scope struct definitions inside
// unions to the whole translation unit. Since those are
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh

// anonymous, let's just assume that if the cursor we've
// found is a definition it's a valid inner type.
//
// Note that doing this could be always ok, but let's just
// keep the union check for now.
//
// https://github.com/servo/rust-bindgen/issues/482
if cur.semantic_parent() != cursor {
let is_inner_struct = cur.semantic_parent() == cursor ||
(kind == CompKind::Union &&
cur.is_definition());
if !is_inner_struct {
return CXChildVisit_Continue;
}

Expand Down
10 changes: 8 additions & 2 deletions src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: when rendered as markdown, I think this link is going to disappear, since the "see [1]" isn't a proper markdown link. Instead it should be something like

... [old libclang versions seem to return incorrect results][1] ...

above.

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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -626,6 +647,7 @@ impl Default for BindgenOptions {
generate_comments: true,
whitelist_recursively: true,
objc_extern_crate: false,
enable_mangling: true,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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. \
Expand Down
92 changes: 92 additions & 0 deletions tests/expectations/tests/anon_struct_in_union.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


#[repr(C)]
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
impl <T> __BindgenUnionField<T> {
#[inline]
pub fn new() -> Self { __BindgenUnionField(::std::marker::PhantomData) }
#[inline]
pub unsafe fn as_ref(&self) -> &T { ::std::mem::transmute(self) }
#[inline]
pub unsafe fn as_mut(&mut self) -> &mut T { ::std::mem::transmute(self) }
}
impl <T> ::std::default::Default for __BindgenUnionField<T> {
#[inline]
fn default() -> Self { Self::new() }
}
impl <T> ::std::clone::Clone for __BindgenUnionField<T> {
#[inline]
fn clone(&self) -> Self { Self::new() }
}
impl <T> ::std::marker::Copy for __BindgenUnionField<T> { }
impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fmt.write_str("__BindgenUnionField")
}
}
#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct s {
pub u: s__bindgen_ty_1,
}
#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct s__bindgen_ty_1 {
pub field: __BindgenUnionField<s__bindgen_ty_1_inner>,
pub bindgen_union_field: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct s__bindgen_ty_1_inner {
pub b: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_s__bindgen_ty_1_inner() {
assert_eq!(::std::mem::size_of::<s__bindgen_ty_1_inner>() , 4usize ,
concat ! ( "Size of: " , stringify ! ( s__bindgen_ty_1_inner )
));
assert_eq! (::std::mem::align_of::<s__bindgen_ty_1_inner>() , 4usize ,
concat ! (
"Alignment of " , stringify ! ( s__bindgen_ty_1_inner ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const s__bindgen_ty_1_inner ) ) . b as * const
_ as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( s__bindgen_ty_1_inner )
, "::" , stringify ! ( b ) ));
}
impl Clone for s__bindgen_ty_1_inner {
fn clone(&self) -> Self { *self }
}
#[test]
fn bindgen_test_layout_s__bindgen_ty_1() {
assert_eq!(::std::mem::size_of::<s__bindgen_ty_1>() , 4usize , concat ! (
"Size of: " , stringify ! ( s__bindgen_ty_1 ) ));
assert_eq! (::std::mem::align_of::<s__bindgen_ty_1>() , 4usize , concat !
( "Alignment of " , stringify ! ( s__bindgen_ty_1 ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const s__bindgen_ty_1 ) ) . field as * const _
as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( s__bindgen_ty_1 ) ,
"::" , stringify ! ( field ) ));
}
impl Clone for s__bindgen_ty_1 {
fn clone(&self) -> Self { *self }
}
#[test]
fn bindgen_test_layout_s() {
assert_eq!(::std::mem::size_of::<s>() , 4usize , concat ! (
"Size of: " , stringify ! ( s ) ));
assert_eq! (::std::mem::align_of::<s>() , 4usize , concat ! (
"Alignment of " , stringify ! ( s ) ));
assert_eq! (unsafe { & ( * ( 0 as * const s ) ) . u as * const _ as usize
} , 0usize , concat ! (
"Alignment of field: " , stringify ! ( s ) , "::" , stringify
! ( u ) ));
}
impl Clone for s {
fn clone(&self) -> Self { *self }
}
7 changes: 7 additions & 0 deletions tests/headers/anon_struct_in_union.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct s {
union {
struct inner {
int b;
} field;
} u;
};