Skip to content

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

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
Sep 29, 2022
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
34 changes: 20 additions & 14 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
41 changes: 41 additions & 0 deletions tests/expectations/tests/stdint_typedef.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/headers/stdint_typedef.h
Original file line number Diff line number Diff line change
@@ -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;
};