Skip to content

ir: Handle properly template alias instantiations in clang >3.9 #696

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

Merged
merged 1 commit into from
May 10, 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
4 changes: 3 additions & 1 deletion src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ impl<'ctx> BindgenContext<'ctx> {
let id = item.id();
let is_type = item.kind().is_type();
let is_unnamed = is_type && item.expect_type().name().is_none();
let is_template_instantiation =
is_type && item.expect_type().is_template_instantiation();

// Be sure to track all the generated children under namespace, even
// those generated after resolving typerefs, etc.
Expand All @@ -317,7 +319,7 @@ impl<'ctx> BindgenContext<'ctx> {
// Unnamed items can have an USR, but they can't be referenced from
// other sites explicitly and the USR can match if the unnamed items are
// nested, so don't bother tracking them.
if is_type && declaration.is_some() {
if is_type && !is_template_instantiation && declaration.is_some() {
let mut declaration = declaration.unwrap();
if !declaration.is_valid() {
if let Some(location) = location {
Expand Down
39 changes: 22 additions & 17 deletions src/ir/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,30 @@ impl TemplateInstantiation {
.collect()
});

let definition = ty.declaration()
.specialized()
.or_else(|| {
let mut template_ref = None;
ty.declaration().visit(|child| {
if child.kind() == CXCursor_TemplateRef {
template_ref = Some(child);
return CXVisit_Break;
}
let declaration = ty.declaration();
let definition = if declaration.kind() == CXCursor_TypeAliasTemplateDecl {
Some(declaration)
} else {
declaration
.specialized()
.or_else(|| {
let mut template_ref = None;
ty.declaration().visit(|child| {
if child.kind() == CXCursor_TemplateRef {
template_ref = Some(child);
return CXVisit_Break;
}

// Instantiations of template aliases might have the
// TemplateRef to the template alias definition arbitrarily
// deep, so we need to recurse here and not only visit
// direct children.
CXChildVisit_Recurse
});
// Instantiations of template aliases might have the
// TemplateRef to the template alias definition arbitrarily
// deep, so we need to recurse here and not only visit
// direct children.
CXChildVisit_Recurse
});

template_ref.and_then(|cur| cur.referenced())
});
template_ref.and_then(|cur| cur.referenced())
})
};

let definition = match definition {
Some(def) => def,
Expand Down
8 changes: 8 additions & 0 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ impl Type {
}
}

/// Is this a template instantiation type?
pub fn is_template_instantiation(&self) -> bool {
match self.kind {
TypeKind::TemplateInstantiation(..) => true,
_ => false,
}
}

/// Is this a template alias type?
pub fn is_template_alias(&self) -> bool {
match self.kind {
Expand Down
15 changes: 13 additions & 2 deletions tests/expectations/tests/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,18 @@ fn __bindgen_test_layout_Foo_instantiation_95() {
Foo<::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_Rooted_instantiation_106() {
fn __bindgen_test_layout_Foo_instantiation_101() {
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int>>() , 24usize ,
concat ! (
"Size of template specialization: " , stringify ! (
Foo<::std::os::raw::c_int> ) ));
assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int>>() , 8usize ,
concat ! (
"Alignment of template specialization: " , stringify ! (
Foo<::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_Rooted_instantiation_111() {
assert_eq!(::std::mem::size_of::<Rooted<*mut ::std::os::raw::c_void>>() ,
24usize , concat ! (
"Size of template specialization: " , stringify ! (
Expand All @@ -267,7 +278,7 @@ fn __bindgen_test_layout_Rooted_instantiation_106() {
Rooted<*mut ::std::os::raw::c_void> ) ));
}
#[test]
fn __bindgen_test_layout_WithDtor_instantiation_114() {
fn __bindgen_test_layout_WithDtor_instantiation_123() {
assert_eq!(::std::mem::size_of::<WithDtor<::std::os::raw::c_int>>() ,
4usize , concat ! (
"Size of template specialization: " , stringify ! (
Expand Down