Skip to content

Canonicalize types before looking for their definitions. #2084

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 2 commits into from
Jul 31, 2021
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
33 changes: 19 additions & 14 deletions src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,21 +1408,26 @@ impl CompInfo {
let inner = Item::parse(cur, Some(potential_id), ctx)
.expect("Inner ClassDecl");

let inner = inner.expect_type_id(ctx);

ci.inner_types.push(inner);

// A declaration of an union or a struct without name could
// also be an unnamed field, unfortunately.
if cur.spelling().is_empty() &&
cur.kind() != CXCursor_EnumDecl
{
let ty = cur.cur_type();
let public = cur.public_accessible();
let offset = cur.offset_of_field().ok();
// If we avoided recursion parsing this type (in
// `Item::from_ty_with_id()`), then this might not be a
// valid type ID, so check and gracefully handle this.
if ctx.resolve_item_fallible(inner).is_some() {
let inner = inner.expect_type_id(ctx);

ci.inner_types.push(inner);

// A declaration of an union or a struct without name
// could also be an unnamed field, unfortunately.
if cur.spelling().is_empty() &&
cur.kind() != CXCursor_EnumDecl
{
let ty = cur.cur_type();
let public = cur.public_accessible();
let offset = cur.offset_of_field().ok();

maybe_anonymous_struct_field =
Some((inner, ty, public, offset));
maybe_anonymous_struct_field =
Some((inner, ty, public, offset));
}
}
}
CXCursor_PackedAttr => {
Expand Down
4 changes: 2 additions & 2 deletions src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,8 @@ impl ClangItemParser for Item {
}

let decl = {
let decl = ty.declaration();
decl.definition().unwrap_or(decl)
let canonical_def = ty.canonical_type().declaration().definition();
canonical_def.unwrap_or_else(|| ty.declaration())
};

let comment = decl.raw_comment().or_else(|| location.raw_comment());
Expand Down
278 changes: 278 additions & 0 deletions tests/expectations/tests/canonical-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct ClassA {
pub _address: u8,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassA_ClassAInner<T> {
pub x: *mut T,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl<T> Default for ClassA_ClassAInner<T> {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct ClassB {
pub _address: u8,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct ClassC {
pub _address: u8,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassC_ClassCInnerB {
pub cache: *mut ClassC_ClassCInnerA,
}
impl Default for ClassC_ClassCInnerB {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassC_ClassCInnerA {
pub member: *mut ClassC_ClassCInnerB,
}
impl Default for ClassC_ClassCInnerA {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassC_ClassCInnerCRTP {
pub _address: u8,
}
impl Default for ClassC_ClassCInnerCRTP {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassD {
pub _address: u8,
}
#[test]
fn bindgen_test_layout_ClassD() {
assert_eq!(
::std::mem::size_of::<ClassD>(),
1usize,
concat!("Size of: ", stringify!(ClassD))
);
assert_eq!(
::std::mem::align_of::<ClassD>(),
1usize,
concat!("Alignment of ", stringify!(ClassD))
);
}
impl Default for ClassD {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[test]
fn __bindgen_test_layout_ClassB_open0_ClassD_ClassCInnerCRTP_close0_instantiation(
) {
assert_eq!(
::std::mem::size_of::<ClassB>(),
1usize,
concat!("Size of template specialization: ", stringify!(ClassB))
);
assert_eq!(
::std::mem::align_of::<ClassB>(),
1usize,
concat!("Alignment of template specialization: ", stringify!(ClassB))
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassCInnerCRTP {
pub _address: u8,
}
#[test]
fn bindgen_test_layout_ClassCInnerCRTP() {
assert_eq!(
::std::mem::size_of::<ClassCInnerCRTP>(),
1usize,
concat!("Size of: ", stringify!(ClassCInnerCRTP))
);
assert_eq!(
::std::mem::align_of::<ClassCInnerCRTP>(),
1usize,
concat!("Alignment of ", stringify!(ClassCInnerCRTP))
);
}
impl Default for ClassCInnerCRTP {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[test]
fn __bindgen_test_layout_ClassB_open0_ClassCInnerCRTP_ClassAInner_close0_instantiation(
) {
assert_eq!(
::std::mem::size_of::<ClassB>(),
1usize,
concat!("Size of template specialization: ", stringify!(ClassB))
);
assert_eq!(
::std::mem::align_of::<ClassB>(),
1usize,
concat!("Alignment of template specialization: ", stringify!(ClassB))
);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassAInner {
pub x: *mut ClassCInnerA,
}
#[test]
fn bindgen_test_layout_ClassAInner() {
assert_eq!(
::std::mem::size_of::<ClassAInner>(),
8usize,
concat!("Size of: ", stringify!(ClassAInner))
);
assert_eq!(
::std::mem::align_of::<ClassAInner>(),
8usize,
concat!("Alignment of ", stringify!(ClassAInner))
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<ClassAInner>())).x as *const _ as usize
},
0usize,
concat!(
"Offset of field: ",
stringify!(ClassAInner),
"::",
stringify!(x)
)
);
}
impl Default for ClassAInner {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassCInnerA {
pub member: *mut ClassCInnerB,
}
#[test]
fn bindgen_test_layout_ClassCInnerA() {
assert_eq!(
::std::mem::size_of::<ClassCInnerA>(),
8usize,
concat!("Size of: ", stringify!(ClassCInnerA))
);
assert_eq!(
::std::mem::align_of::<ClassCInnerA>(),
8usize,
concat!("Alignment of ", stringify!(ClassCInnerA))
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<ClassCInnerA>())).member as *const _ as usize
},
0usize,
concat!(
"Offset of field: ",
stringify!(ClassCInnerA),
"::",
stringify!(member)
)
);
}
impl Default for ClassCInnerA {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ClassCInnerB {
pub cache: *mut ClassCInnerA,
}
#[test]
fn bindgen_test_layout_ClassCInnerB() {
assert_eq!(
::std::mem::size_of::<ClassCInnerB>(),
8usize,
concat!("Size of: ", stringify!(ClassCInnerB))
);
assert_eq!(
::std::mem::align_of::<ClassCInnerB>(),
8usize,
concat!("Alignment of ", stringify!(ClassCInnerB))
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<ClassCInnerB>())).cache as *const _ as usize
},
0usize,
concat!(
"Offset of field: ",
stringify!(ClassCInnerB),
"::",
stringify!(cache)
)
);
}
impl Default for ClassCInnerB {
fn default() -> Self {
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
unsafe {
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
s.assume_init()
}
}
}
2 changes: 2 additions & 0 deletions tests/expectations/tests/layout_cmdline_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ impl Default for cmdline_token_hdr {
}
}
}
/// Stores a pointer to the ops struct, and the offset: the place to
/// write the parsed result in the destination structure.
pub type cmdline_parse_token_hdr_t = cmdline_token_hdr;
/// A token is defined by this structure.
///
Expand Down
17 changes: 17 additions & 0 deletions tests/expectations/tests/nested-template-typedef.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Foo {
pub _address: u8,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Foo_Bar {
pub _address: u8,
}
Loading