Don't traverse through special-cased <stdint.h> types.

This commit is contained in:
Geoffry Song 2022-09-27 21:17:41 +00:00
parent b5ec18e374
commit 70115a0863
4 changed files with 78 additions and 14 deletions

View File

@ -2250,24 +2250,27 @@ If you encounter an error missing from this list, please file an issue or a PR!"
// Sized integer types from <stdint.h> get mapped to Rust primitive
// types regardless of whether they are blocklisted, so ensure that
// standard traits are considered derivable for them too.
None => match name {
"int8_t" | "uint8_t" | "int16_t" | "uint16_t" |
"int32_t" | "uint32_t" | "int64_t" |
"uint64_t" | "uintptr_t" | "intptr_t" |
"ptrdiff_t" => Some(CanDerive::Yes),
"size_t" if self.options.size_t_is_usize => {
Some(CanDerive::Yes)
}
"ssize_t" if self.options.size_t_is_usize => {
Some(CanDerive::Yes)
}
_ => Some(CanDerive::No),
},
None => Some(if self.is_stdint_type(name) {
CanDerive::Yes
} else {
CanDerive::No
}),
})
.unwrap_or(CanDerive::No)
})
}
/// Is the given type a type from <stdint.h> that corresponds to a Rust primitive type?
pub fn is_stdint_type(&self, name: &str) -> bool {
match name {
"int8_t" | "uint8_t" | "int16_t" | "uint16_t" | "int32_t" |
"uint32_t" | "int64_t" | "uint64_t" | "uintptr_t" |
"intptr_t" | "ptrdiff_t" => true,
"size_t" | "ssize_t" => self.options.size_t_is_usize,
_ => false,
}
}
/// Get a reference to the set of items we should generate.
pub fn codegen_items(&self) -> &ItemSet {
assert!(self.in_codegen_phase());
@ -2355,7 +2358,10 @@ If you encounter an error missing from this list, please file an issue or a PR!"
TypeKind::Opaque |
TypeKind::TypeParam => return true,
_ => {}
};
}
if self.is_stdint_type(&name) {
return true;
}
}
// Unnamed top-level enums are special and we

View File

@ -1206,6 +1206,13 @@ impl Trace for Type {
where
T: Tracer,
{
if self
.name()
.map_or(false, |name| context.is_stdint_type(name))
{
// These types are special-cased in codegen and don't need to be traversed.
return;
}
match *self.kind() {
TypeKind::Pointer(inner) |
TypeKind::Reference(inner) |

View File

@ -0,0 +1,41 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]
extern "C" {
pub fn fun() -> u64;
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Struct {
pub field: u64,
}
#[test]
fn bindgen_test_layout_Struct() {
const UNINIT: ::std::mem::MaybeUninit<Struct> =
::std::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::std::mem::size_of::<Struct>(),
8usize,
concat!("Size of: ", stringify!(Struct))
);
assert_eq!(
::std::mem::align_of::<Struct>(),
8usize,
concat!("Alignment of ", stringify!(Struct))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).field) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(Struct),
"::",
stringify!(field)
)
);
}

View File

@ -0,0 +1,10 @@
// bindgen-flags: --allowlist-type="Struct" --allowlist-function="fun"
// no typedef should be emitted for `__uint64_t`
typedef unsigned long long __uint64_t;
typedef __uint64_t uint64_t;
uint64_t fun();
struct Struct {
uint64_t field;
};