Skip to content

Commit a1006fd

Browse files
committed
libc: port freebsd to use ctest-next
1 parent 8380bad commit a1006fd

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed

ctest-next/src/tests.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ fn test_translation_type_bare_fn() {
100100

101101
#[test]
102102
fn test_translation_type_array() {
103-
assert_r2cdecl("[&u8; 2 + 2]", "const uint8_t *foo[2 + 2]");
103+
assert_r2cdecl(
104+
"[&u8; crate::ERRNO as usize + 2]",
105+
"const uint8_t *foo[(size_t)ERRNO + 2]",
106+
);
104107
}
105108

106109
#[test]

ctest-next/src/translator.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ pub(crate) fn translate_expr(expr: &syn::Expr) -> String {
369369
let index = translate_expr(&i.index);
370370
format!("{base}[{index}]")
371371
}
372-
// This is done to deal with things like 3usize.
373372
syn::Expr::Lit(l) => match &l.lit {
374373
syn::Lit::Int(i) => {
375374
let suffix = translate_primitive_type(i.suffix());
@@ -383,7 +382,17 @@ pub(crate) fn translate_expr(expr: &syn::Expr) -> String {
383382
_ => l.to_token_stream().to_string(),
384383
},
385384
syn::Expr::Path(p) => p.path.segments.last().unwrap().ident.to_string(),
386-
syn::Expr::Cast(c) => translate_expr(c.expr.deref()),
385+
syn::Expr::Cast(c) => {
386+
let val = translate_expr(&c.expr);
387+
let ty = translate_primitive_type(&c.ty.to_token_stream().to_string());
388+
format!("({ty}){val}")
389+
}
390+
syn::Expr::Binary(b) => {
391+
let left = translate_expr(&b.left);
392+
let op = b.op.to_token_stream().to_string();
393+
let right = translate_expr(&b.right);
394+
format!("{left} {op} {right}")
395+
}
387396
expr => expr.to_token_stream().to_string(),
388397
}
389398
}

libc-test/build.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ fn ctest_cfg() -> ctest::TestGenerator {
7878
}
7979

8080
fn ctest_next_cfg() -> ctest_next::TestGenerator {
81-
ctest_next::TestGenerator::new()
81+
let mut cfg = ctest_next::TestGenerator::new();
82+
cfg.skip_private(true);
83+
cfg
8284
}
8385

8486
fn do_semver() {
@@ -2286,7 +2288,7 @@ fn test_android(target: &str) {
22862288

22872289
fn test_freebsd(target: &str) {
22882290
assert!(target.contains("freebsd"));
2289-
let mut cfg = ctest_cfg();
2291+
let mut cfg = ctest_next_cfg();
22902292

22912293
let freebsd_ver = which_freebsd();
22922294

@@ -2428,7 +2430,13 @@ fn test_freebsd(target: &str) {
24282430
"wchar.h",
24292431
}
24302432

2431-
cfg.type_name(move |ty, is_struct, is_union| {
2433+
cfg.rename_type(|ty| match ty {
2434+
// FIXME(freebsd): https://github.com/rust-lang/libc/issues/1273
2435+
"sighandler_t" => Some("sig_t".to_string()),
2436+
_ => None,
2437+
});
2438+
2439+
cfg.rename_struct_ty(|ty| {
24322440
match ty {
24332441
// Just pass all these through, no need for a "struct" prefix
24342442
"FILE"
@@ -2443,24 +2451,16 @@ fn test_freebsd(target: &str) {
24432451
| "devstat_support_flags"
24442452
| "devstat_type_flags"
24452453
| "devstat_match_flags"
2446-
| "devstat_priority" => ty.to_string(),
2447-
2448-
// FIXME(freebsd): https://github.com/rust-lang/libc/issues/1273
2449-
"sighandler_t" => "sig_t".to_string(),
2450-
2451-
t if is_union => format!("union {t}"),
2454+
| "devstat_priority" => Some(ty.to_string()),
24522455

2453-
t if t.ends_with("_t") => t.to_string(),
2454-
2455-
// put `struct` in front of all structs:.
2456-
t if is_struct => format!("struct {t}"),
2457-
2458-
t => t.to_string(),
2456+
t if t.ends_with("_t") => Some(t.to_string()),
2457+
_ => None,
24592458
}
24602459
});
24612460

2462-
cfg.field_name(move |struct_, field| {
2463-
match field {
2461+
cfg.rename_struct_field(|struct_, field_| {
2462+
let struct_ = struct_.ident();
2463+
let replacement = match field_.ident() {
24642464
// Our stat *_nsec fields normally don't actually exist but are part
24652465
// of a timeval struct
24662466
s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
@@ -2474,12 +2474,13 @@ fn test_freebsd(target: &str) {
24742474
"type_" if struct_ == "input_event" => "type".to_string(),
24752475
// Field is named `gennum` in Rust because `gen` is a keyword
24762476
"gennum" if struct_ == "xktls_session_onedir" => "gen".to_string(),
2477-
s => s.to_string(),
2478-
}
2477+
_ => return None,
2478+
};
2479+
Some(replacement)
24792480
});
24802481

2481-
cfg.skip_const(move |name| {
2482-
match name {
2482+
cfg.skip_const(move |constant| {
2483+
match constant.ident() {
24832484
// These constants were introduced in FreeBSD 13:
24842485
"F_ADD_SEALS" | "F_GET_SEALS" | "F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW"
24852486
| "F_SEAL_WRITE"
@@ -2745,8 +2746,8 @@ fn test_freebsd(target: &str) {
27452746
}
27462747
});
27472748

2748-
cfg.skip_type(move |ty| {
2749-
match ty {
2749+
cfg.skip_alias(move |ty| {
2750+
match ty.ident() {
27502751
// the struct "__kvm" is quite tricky to bind so since we only use a pointer to it
27512752
// for now, it doesn't matter too much...
27522753
"kvm_t" => true,
@@ -2757,7 +2758,9 @@ fn test_freebsd(target: &str) {
27572758
}
27582759
});
27592760

2760-
cfg.skip_struct(move |ty| {
2761+
cfg.skip_union(|u| u.ident().starts_with("__c_anonymous_"));
2762+
cfg.skip_struct(move |struct_| {
2763+
let ty = struct_.ident();
27612764
if ty.starts_with("__c_anonymous_") {
27622765
return true;
27632766
}
@@ -2802,9 +2805,9 @@ fn test_freebsd(target: &str) {
28022805
}
28032806
});
28042807

2805-
cfg.skip_fn(move |name| {
2808+
cfg.skip_fn(move |func| {
28062809
// skip those that are manually verified
2807-
match name {
2810+
match func.ident() {
28082811
// This is introduced in FreeBSD 14.1
28092812
"execvpe" => true,
28102813

@@ -2864,18 +2867,12 @@ fn test_freebsd(target: &str) {
28642867
}
28652868
});
28662869

2867-
cfg.volatile_item(|i| {
2868-
use ctest::VolatileItemKind::*;
2869-
match i {
2870-
// aio_buf is a volatile void* but since we cannot express that in
2871-
// Rust types, we have to explicitly tell the checker about it here:
2872-
StructField(ref n, ref f) if n == "aiocb" && f == "aio_buf" => true,
2873-
_ => false,
2874-
}
2875-
});
2870+
// aio_buf is a volatile void* but since we cannot express that in
2871+
// Rust types, we have to explicitly tell the checker about it here:
2872+
cfg.volatile_struct_field(|s, f| s.ident() == "aiocb" && f.ident() == "aio_buf");
28762873

2877-
cfg.skip_field(move |struct_, field| {
2878-
match (struct_, field) {
2874+
cfg.skip_struct_field(move |struct_, field| {
2875+
match (struct_.ident(), field.ident()) {
28792876
// FIXME(freebsd): `sa_sigaction` has type `sighandler_t` but that type is
28802877
// incorrect, see: https://github.com/rust-lang/libc/issues/1359
28812878
("sigaction", "sa_sigaction") => true,
@@ -2947,7 +2944,10 @@ fn test_freebsd(target: &str) {
29472944
});
29482945
}
29492946

2950-
cfg.generate(src_hotfix_dir().join("lib.rs"), "ctest_output.rs");
2947+
// FIXME(ctest): The original ctest bypassed this requirement somehow.
2948+
cfg.rename_type(move |ty| (ty == "dot3Vendors").then_some(format!("enum {ty}")));
2949+
2950+
ctest_next::generate_test(&mut cfg, "../src/lib.rs", "ctest_output.rs").unwrap();
29512951
}
29522952

29532953
fn test_emscripten(target: &str) {

0 commit comments

Comments
 (0)