Skip to content

Commit d91a650

Browse files
committed
transpile: keep portable types for typedefs in stdint.h, sys/types.h, and __stddef_*
sadly it seems like we don't have many options here other than predicating on header+typedef name
1 parent 255cea8 commit d91a650

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,54 @@ impl ConversionContext {
19261926
})
19271927
.unwrap_or(typ);
19281928

1929+
// Other fixed-size types are defined without special compiler involvement, by
1930+
// standard headers and their transitive includes. In these contexts, we
1931+
// recognize these typedefs by the name of the typedef type.
1932+
let id_for_name = |name| -> Option<_> {
1933+
let kind = match name {
1934+
"intmax_t" => CTypeKind::IntMax,
1935+
"uintmax_t" => CTypeKind::UIntMax,
1936+
"intptr_t" => CTypeKind::IntPtr,
1937+
"uintptr_t" => CTypeKind::UIntPtr,
1938+
"__uint8_t" => CTypeKind::UInt8,
1939+
"__uint16_t" => CTypeKind::UInt16,
1940+
"__uint32_t" => CTypeKind::UInt32,
1941+
"__uint64_t" => CTypeKind::UInt64,
1942+
"__uint128_t" => CTypeKind::UInt128,
1943+
"__int8_t" => CTypeKind::Int8,
1944+
"__int16_t" => CTypeKind::Int16,
1945+
"__int32_t" => CTypeKind::Int32,
1946+
"__int64_t" => CTypeKind::Int64,
1947+
"__int128_t" => CTypeKind::Int128,
1948+
_ => {
1949+
log::debug!("Unknown fixed-size type typedef {name}!");
1950+
return None;
1951+
}
1952+
};
1953+
log::trace!("Selected kind {kind} for typedef {name}");
1954+
Some(CQualTypeId::new(
1955+
self.typed_context.type_for_kind(kind).unwrap(),
1956+
))
1957+
};
1958+
let file = self
1959+
.typed_context
1960+
.files
1961+
.get(self.typed_context.file_map[node.loc.fileid as usize]);
1962+
let file = file.unwrap();
1963+
if let Some(path) = file.path.as_ref() {
1964+
if let Some(filename) = path.file_name() {
1965+
if filename == "stdint.h"
1966+
|| filename == "types.h"
1967+
|| filename
1968+
.to_str()
1969+
.map(|s| s.starts_with("__stddef_"))
1970+
.unwrap_or(false)
1971+
{
1972+
typ = id_for_name(&*name).unwrap_or(typ);
1973+
}
1974+
}
1975+
}
1976+
19291977
let typdef_decl = CDeclKind::Typedef {
19301978
name,
19311979
typ,

0 commit comments

Comments
 (0)