Skip to content

Commit 75bff7f

Browse files
authored
Reduce libc usage in c2rust-transpile (#1222)
Mostly in the output, but also in its own code.
2 parents 7a0bb7c + 841993c commit 75bff7f

File tree

11 files changed

+30
-31
lines changed

11 files changed

+30
-31
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

c2rust-transpile/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fern = { version = "0.6", features = ["colored"] }
2323
handlebars = "4.2"
2424
indexmap = { version = "1.0.1", features = ["serde-1"] }
2525
itertools = "0.10"
26-
libc = "0.2"
2726
log = "0.4"
2827
log-reroute = "0.1"
2928
pathdiff = "0.2"

c2rust-transpile/src/convert_type.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl TypeConverter {
272272
// in the case of pointers.
273273
CTypeKind::Void => Ok(mk()
274274
.set_mutbl(mutbl)
275-
.ptr_ty(mk().path_ty(vec!["libc", "c_void"]))),
275+
.ptr_ty(mk().path_ty(vec!["std", "ffi", "c_void"]))),
276276

277277
CTypeKind::VariableArray(mut elt, _len) => {
278278
while let CTypeKind::VariableArray(elt_, _) = ctxt.resolve_type(elt).kind {
@@ -313,20 +313,20 @@ impl TypeConverter {
313313
match ctxt.index(ctype).kind {
314314
CTypeKind::Void => Ok(mk().tuple_ty(vec![])),
315315
CTypeKind::Bool => Ok(mk().path_ty(mk().path(vec!["bool"]))),
316-
CTypeKind::Short => Ok(mk().path_ty(mk().path(vec!["libc", "c_short"]))),
317-
CTypeKind::Int => Ok(mk().path_ty(mk().path(vec!["libc", "c_int"]))),
318-
CTypeKind::Long => Ok(mk().path_ty(mk().path(vec!["libc", "c_long"]))),
319-
CTypeKind::LongLong => Ok(mk().path_ty(mk().path(vec!["libc", "c_longlong"]))),
320-
CTypeKind::UShort => Ok(mk().path_ty(mk().path(vec!["libc", "c_ushort"]))),
321-
CTypeKind::UInt => Ok(mk().path_ty(mk().path(vec!["libc", "c_uint"]))),
322-
CTypeKind::ULong => Ok(mk().path_ty(mk().path(vec!["libc", "c_ulong"]))),
323-
CTypeKind::ULongLong => Ok(mk().path_ty(mk().path(vec!["libc", "c_ulonglong"]))),
324-
CTypeKind::SChar => Ok(mk().path_ty(mk().path(vec!["libc", "c_schar"]))),
325-
CTypeKind::UChar => Ok(mk().path_ty(mk().path(vec!["libc", "c_uchar"]))),
326-
CTypeKind::Char => Ok(mk().path_ty(mk().path(vec!["libc", "c_char"]))),
327-
CTypeKind::Double => Ok(mk().path_ty(mk().path(vec!["libc", "c_double"]))),
316+
CTypeKind::Short => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_short"]))),
317+
CTypeKind::Int => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_int"]))),
318+
CTypeKind::Long => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_long"]))),
319+
CTypeKind::LongLong => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_longlong"]))),
320+
CTypeKind::UShort => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_ushort"]))),
321+
CTypeKind::UInt => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_uint"]))),
322+
CTypeKind::ULong => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_ulong"]))),
323+
CTypeKind::ULongLong => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_ulonglong"]))),
324+
CTypeKind::SChar => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_schar"]))),
325+
CTypeKind::UChar => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_uchar"]))),
326+
CTypeKind::Char => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_char"]))),
327+
CTypeKind::Double => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_double"]))),
328328
CTypeKind::LongDouble => Ok(mk().path_ty(mk().path(vec!["f128", "f128"]))),
329-
CTypeKind::Float => Ok(mk().path_ty(mk().path(vec!["libc", "c_float"]))),
329+
CTypeKind::Float => Ok(mk().path_ty(mk().path(vec!["std", "ffi", "c_float"]))),
330330
CTypeKind::Int128 => Ok(mk().path_ty(mk().path(vec!["i128"]))),
331331
CTypeKind::UInt128 => Ok(mk().path_ty(mk().path(vec!["u128"]))),
332332
CTypeKind::BFloat16 => Ok(mk().path_ty(mk().path(vec!["bf16"]))),

c2rust-transpile/src/translator/builtins.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'c> Translation<'c> {
8383
Ok(val.map(|v| {
8484
let val = mk().method_call_expr(v, "is_sign_negative", vec![]);
8585

86-
mk().cast_expr(val, mk().path_ty(vec!["libc", "c_int"]))
86+
mk().cast_expr(val, mk().path_ty(vec!["std", "ffi", "c_int"]))
8787
}))
8888
}
8989
"__builtin_ffs" | "__builtin_ffsl" | "__builtin_ffsll" => {

c2rust-transpile/src/translator/main_function.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'c> Translation<'c> {
5858
Some(mk().path_ty(vec![mk().path_segment_with_args(
5959
"Vec",
6060
mk().angle_bracketed_args(vec![
61-
mk().mutbl().ptr_ty(mk().path_ty(vec!["libc", "c_char"])),
61+
mk().mutbl().ptr_ty(mk().path_ty(vec!["std", "ffi", "c_char"])),
6262
]),
6363
)])),
6464
Some(mk().call_expr(mk().path_expr(vec!["Vec", "new"]), vec![])),
@@ -126,7 +126,7 @@ impl<'c> Translation<'c> {
126126
Some(mk().path_ty(vec![mk().path_segment_with_args(
127127
"Vec",
128128
mk().angle_bracketed_args(vec![
129-
mk().mutbl().ptr_ty(mk().path_ty(vec!["libc", "c_char"])),
129+
mk().mutbl().ptr_ty(mk().path_ty(vec!["std", "ffi", "c_char"])),
130130
]),
131131
)])),
132132
Some(mk().call_expr(mk().path_expr(vec!["Vec", "new"]), vec![])),

c2rust-transpile/src/translator/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ fn arrange_header(t: &Translation, is_binary: bool) -> (Vec<syn::Attribute>, Vec
10911091

10921092
/// Convert a boolean expression to a c_int
10931093
fn bool_to_int(val: Box<Expr>) -> Box<Expr> {
1094-
mk().cast_expr(val, mk().path_ty(vec!["libc", "c_int"]))
1094+
mk().cast_expr(val, mk().path_ty(vec!["std", "ffi", "c_int"]))
10951095
}
10961096

10971097
/// Add a src_loc = "line:col" attribute to an item/foreign_item
@@ -3273,7 +3273,7 @@ impl<'c> Translation<'c> {
32733273
UnTypeOp::PreferredAlignOf => self.compute_align_of_type(arg_ty.ctype, true)?,
32743274
};
32753275

3276-
Ok(result.map(|x| mk().cast_expr(x, mk().path_ty(vec!["libc", "c_ulong"]))))
3276+
Ok(result.map(|x| mk().cast_expr(x, mk().path_ty(vec!["std", "ffi", "c_ulong"]))))
32773277
}
32783278

32793279
ConstantExpr(_ty, child, value) => {

c2rust-transpile/src/translator/operators.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ impl<'c> Translation<'c> {
935935

936936
c_ast::UnOp::Not => {
937937
let val = self.convert_condition(ctx, false, arg)?;
938-
Ok(val.map(|x| mk().cast_expr(x, mk().path_ty(vec!["libc", "c_int"]))))
938+
Ok(val.map(|x| mk().cast_expr(x, mk().path_ty(vec!["std", "ffi", "c_int"]))))
939939
}
940940
c_ast::UnOp::Extension => {
941941
let arg = self.convert_expr(ctx, arg)?;

c2rust-transpile/src/translator/structs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ impl<'a> Translation<'a> {
284284
/// #[derive(BitfieldStruct, Clone, Copy)]
285285
/// #[repr(C, align(2))]
286286
/// struct Foo {
287-
/// #[bitfield(name = "bf1", ty = "libc::c_char", bits = "0..=9")]
288-
/// #[bitfield(name = "bf2", ty = "libc::c_uchar",bits = "10..=15")]
287+
/// #[bitfield(name = "bf1", ty = "std::ffi::c_char", bits = "0..=9")]
288+
/// #[bitfield(name = "bf2", ty = "std::ffi::c_uchar",bits = "10..=15")]
289289
/// bf1_bf2: [u8; 2],
290290
/// non_bf: u64,
291291
/// _pad: [u8; 2],
@@ -392,8 +392,8 @@ impl<'a> Translation<'a> {
392392
/// # #[derive(BitfieldStruct, Clone, Copy)]
393393
/// # #[repr(C, align(2))]
394394
/// # struct Foo {
395-
/// # #[bitfield(name = "bf1", ty = "libc::c_char", bits = "0..=9")]
396-
/// # #[bitfield(name = "bf2", ty = "libc::c_uchar",bits = "10..=15")]
395+
/// # #[bitfield(name = "bf1", ty = "std::ffi::c_char", bits = "0..=9")]
396+
/// # #[bitfield(name = "bf2", ty = "std::ffi::c_uchar",bits = "10..=15")]
397397
/// # bf1_bf2: [u8; 2],
398398
/// # non_bf: u64,
399399
/// # _pad: [u8; 2],

c2rust-transpile/src/translator/variadic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ impl<'c> Translation<'c> {
193193
})
194194
{
195195
real_arg_ty = Some(arg_ty.clone());
196-
arg_ty = mk().mutbl().ptr_ty(mk().path_ty(vec!["libc", "c_void"]));
196+
arg_ty = mk()
197+
.mutbl()
198+
.ptr_ty(mk().path_ty(vec!["std", "ffi", "c_void"]));
197199
}
198200

199201
val.and_then(|val| {

scripts/test_translator.py

-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ def run(self) -> List[TestOutcome]:
392392

393393
rust_file_builder = RustFileBuilder()
394394
rust_file_builder.add_features([
395-
"libc",
396395
"extern_types",
397396
"simd_ffi",
398397
"stdsimd",

tests/statics/src/test_sections.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn test_sectioned_used_static() {
3838

3939
let pos = lines
4040
.iter()
41-
.position(|&x| x == "static mut rust_used_static4: libc::c_int = 1 as libc::c_int;")
41+
.position(|&x| x == "static mut rust_used_static4: std::ffi::c_int = 1 as std::ffi::c_int;")
4242
.expect("Did not find expected static string in source");
4343
// The ordering of these attributes is not stable between LLVM versions
4444
assert!(
@@ -47,7 +47,7 @@ pub fn test_sectioned_used_static() {
4747
);
4848

4949
// This static is pub, but we want to ensure it has attributes applied
50-
assert!(src.contains("#[link_section = \"fb\"]\npub static mut rust_initialized_extern: libc::c_int = 1 as libc::c_int;"));
51-
assert!(src.contains("extern \"C\" {\n #[link_name = \"no_attrs\"]\n static mut rust_aliased_static: libc::c_int;"))
50+
assert!(src.contains("#[link_section = \"fb\"]\npub static mut rust_initialized_extern: std::ffi::c_int = 1 as std::ffi::c_int;"));
51+
assert!(src.contains("extern \"C\" {\n #[link_name = \"no_attrs\"]\n static mut rust_aliased_static: std::ffi::c_int;"))
5252
}
5353
}

0 commit comments

Comments
 (0)