Skip to content

Commit

Permalink
Don't traverse through special-cased <stdint.h> types.
Browse files Browse the repository at this point in the history
  • Loading branch information
goffrie committed Sep 28, 2022
1 parent b5ec18e commit 70115a0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
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;
};

0 comments on commit 70115a0

Please sign in to comment.