Skip to content

Commit 16ffaa4

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 cf2f0b5 commit 16ffaa4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,52 @@ 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 path = self.typed_context.files[node.loc.fileid as usize]
1959+
.path
1960+
.as_ref();
1961+
if let Some(path) = path {
1962+
if let Some(filename) = path.file_name() {
1963+
if filename == "stdint.h"
1964+
|| filename == "types.h"
1965+
|| filename
1966+
.to_str()
1967+
.map(|s| s.starts_with("__stddef_"))
1968+
.unwrap_or(false)
1969+
{
1970+
typ = id_for_name(&*name).unwrap_or(typ);
1971+
}
1972+
}
1973+
}
1974+
19291975
let typdef_decl = CDeclKind::Typedef {
19301976
name,
19311977
typ,

0 commit comments

Comments
 (0)