diff --git a/libc-dep/Cargo.toml b/libc-dep/Cargo.toml index fefb8b74..06d6bd3f 100644 --- a/libc-dep/Cargo.toml +++ b/libc-dep/Cargo.toml @@ -6,3 +6,4 @@ edition = "2024" [dependencies] libc = "0.2" nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } +jiff = "0.2" diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index f9030457..2f475a95 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -46,7 +46,14 @@ fn build_rustc_args(crate_root: &Path) -> Vec { args.push("-L".to_string()); args.push(format!("dependency={}", deps.display())); - for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi", "nix"] { + for dep in &[ + "libcc2rs", + "libc", + "brotli_sys", + "rustls_ffi", + "nix", + "jiff", + ] { if let Some(rlib) = find_rlib(deps.as_path(), dep) { args.push("--extern".to_string()); args.push(format!("{}={}", dep, rlib.display())); diff --git a/rules/Cargo.toml b/rules/Cargo.toml index d9a5e1be..0c8d3841 100644 --- a/rules/Cargo.toml +++ b/rules/Cargo.toml @@ -14,3 +14,4 @@ libcc2rs = { version = "0.1.0", path = "../libcc2rs" } brotli-sys = "0.3" rustls-ffi = { version = "0.15.3", default-features = false } nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } +jiff = "0.2" diff --git a/rules/time/tgt_refcount.rs b/rules/time/tgt_refcount.rs index d5bd7893..1305e223 100644 --- a/rules/time/tgt_refcount.rs +++ b/rules/time/tgt_refcount.rs @@ -14,3 +14,151 @@ fn t2() -> libcc2rs::Timeval { fn t3() -> libcc2rs::Timespec { Default::default() } + +fn f1(a0: Ptr<::libc::time_t>) -> ::libc::time_t { + let __out = a0; + match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + let __s = __ts.tv_sec(); + if !__out.is_null() { + __out.write(__s); + } + __s + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f2(a0: ::libc::clockid_t, a1: Ptr) -> i32 { + match nix::time::clock_gettime(nix::time::ClockId::from_raw(a0)) { + Ok(__ts) => { + a1.with_mut(|__t| { + *__t.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__t.tv_nsec.borrow_mut() = __ts.tv_nsec() as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +fn f4(a0: Ptr<::libc::time_t>, a1: Ptr) -> Ptr { + let __res = a1; + match jiff::Timestamp::from_second(a0.read()) { + Ok(__ts) => { + let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC); + __res.with_mut(|__tm| { + *__tm.tm_sec.borrow_mut() = __dt.second() as i32; + *__tm.tm_min.borrow_mut() = __dt.minute() as i32; + *__tm.tm_hour.borrow_mut() = __dt.hour() as i32; + *__tm.tm_mday.borrow_mut() = __dt.day() as i32; + *__tm.tm_mon.borrow_mut() = __dt.month() as i32 - 1; + *__tm.tm_year.borrow_mut() = __dt.year() as i32 - 1900; + *__tm.tm_wday.borrow_mut() = __dt.weekday().to_sunday_zero_offset() as i32; + *__tm.tm_yday.borrow_mut() = __dt.day_of_year() as i32 - 1; + *__tm.tm_isdst.borrow_mut() = 0; + *__tm.tm_gmtoff.borrow_mut() = 0; + *__tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT"); + }); + __res + } + Err(_) => { + libcc2rs::cpp2rust_errno().write(::libc::EOVERFLOW); + Ptr::null() + } + } +} + +fn f6(a0: Ptr, a1: usize, a2: Ptr, a3: Ptr) -> usize { + let __dt = a3.with(|__tm| { + jiff::civil::DateTime::new( + (*__tm.tm_year.borrow() + 1900) as i16, + (*__tm.tm_mon.borrow() + 1) as i8, + *__tm.tm_mday.borrow() as i8, + *__tm.tm_hour.borrow() as i8, + *__tm.tm_min.borrow() as i8, + *__tm.tm_sec.borrow() as i8, + 0, + ) + }); + let __text = match __dt { + Ok(__d) => { + jiff::fmt::strtime::format(a2.to_rust_string().as_str(), __d).unwrap_or_default() + } + Err(_) => String::new(), + }; + if __text.is_empty() || __text.len() + 1 > a1 { + 0 + } else { + let mut __dst = a0.clone(); + for __b in __text.as_bytes() { + __dst.write(*__b); + __dst += 1; + } + __dst.write(0); + __text.len() + } +} + +fn f7(a0: Ptr, a1: Ptr) -> i32 { + let __times = a1; + let __at = __times.with(|__tv| { + nix::sys::time::TimeVal::new( + *__tv.tv_sec.borrow() as ::libc::time_t, + *__tv.tv_usec.borrow() as ::libc::suseconds_t, + ) + }); + let __mt = __times.offset(1).with(|__tv| { + nix::sys::time::TimeVal::new( + *__tv.tv_sec.borrow() as ::libc::time_t, + *__tv.tv_usec.borrow() as ::libc::suseconds_t, + ) + }); + match nix::sys::stat::utimes(a0.to_rust_string().as_str(), &__at, &__mt) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +#[cfg(target_os = "linux")] +fn f8(a0: Ptr, a1: Ptr<::libc::timezone>) -> i32 { + match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + a0.with_mut(|__tv| { + *__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} + +#[cfg(target_os = "macos")] +fn f8(a0: Ptr, a1: AnyPtr) -> i32 { + match nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME) { + Ok(__ts) => { + a0.with_mut(|__tv| { + *__tv.tv_sec.borrow_mut() = __ts.tv_sec() as i64; + *__tv.tv_usec.borrow_mut() = (__ts.tv_nsec() / 1000) as i64; + }); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +} diff --git a/tests/lit/lit/formats/Cpp2RustTest.py b/tests/lit/lit/formats/Cpp2RustTest.py index 89465616..006bc2a1 100644 --- a/tests/lit/lit/formats/Cpp2RustTest.py +++ b/tests/lit/lit/formats/Cpp2RustTest.py @@ -212,6 +212,10 @@ def build_rust(self): libc_dep_deps.glob("libnix-*.rlib"), key=lambda p: p.stat().st_mtime, ) + jiff_rlib = max( + libc_dep_deps.glob("libjiff-*.rlib"), + key=lambda p: p.stat().st_mtime, + ) cmd = [ "rustc", "+" + read_rust_version(), @@ -240,6 +244,8 @@ def build_rust(self): f"libc={libc_rlib}", "--extern", f"nix={nix_rlib}", + "--extern", + f"jiff={jiff_rlib}", ] _, err, returncode = lit.util.executeCommand(cmd, str(self.tmp_dir)) if exp.should_not_compile: