Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cpp2rust/compat/sys/select.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include_next <sys/select.h>

#undef FD_SET
#undef FD_CLR
#undef FD_ISSET
#undef FD_ZERO

void cpp2rust_fd_set(int fd, fd_set *set);
void cpp2rust_fd_clr(int fd, fd_set *set);
int cpp2rust_fd_isset(int fd, const fd_set *set);
void cpp2rust_fd_zero(fd_set *set);

#define FD_SET(fd, set) cpp2rust_fd_set(fd, set)
#define FD_CLR(fd, set) cpp2rust_fd_clr(fd, set)
#define FD_ISSET(fd, set) cpp2rust_fd_isset(fd, set)
#define FD_ZERO(set) cpp2rust_fd_zero(set)
7 changes: 7 additions & 0 deletions rules/select/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

#include <sys/select.h>

using t1 = fd_set;

int f1(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout) {
return select(nfds, readfds, writefds, exceptfds, timeout);
}

void f2(int fd, fd_set *set) { return FD_SET(fd, set); }
void f3(int fd, fd_set *set) { return FD_CLR(fd, set); }
int f4(int fd, const fd_set *set) { return FD_ISSET(fd, set); }
void f5(fd_set *set) { return FD_ZERO(set); }
17 changes: 17 additions & 0 deletions rules/select/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

unsafe fn types() {
let t1: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>();
}

unsafe fn f1(
a0: i32,
a1: *mut ::libc::fd_set,
Expand All @@ -10,3 +14,16 @@ unsafe fn f1(
) -> i32 {
libc::select(a0, a1, a2, a3, a4)
}

unsafe fn f2(a0: i32, a1: *mut ::libc::fd_set) {
libc::FD_SET(a0, a1);
}
unsafe fn f3(a0: i32, a1: *mut ::libc::fd_set) {
libc::FD_CLR(a0, a1);
}
unsafe fn f4(a0: i32, a1: *const ::libc::fd_set) -> i32 {
libc::FD_ISSET(a0, a1) as i32
}
unsafe fn f5(a0: *mut ::libc::fd_set) {
libc::FD_ZERO(a0);
}
14 changes: 14 additions & 0 deletions tests/unit/fd_set_macros.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// no-compile: refcount
#include <assert.h>
#include <sys/select.h>

int main() {
fd_set set;
FD_ZERO(&set);
assert(!FD_ISSET(3, &set));
FD_SET(3, &set);
assert(FD_ISSET(3, &set));
FD_CLR(3, &set);
assert(!FD_ISSET(3, &set));
return 0;
}
29 changes: 29 additions & 0 deletions tests/unit/out/unsafe/fd_set_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut set: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>();
libc::FD_ZERO((&mut set as *mut ::libc::fd_set));
assert!(
((!(libc::FD_ISSET(3, (&mut set as *mut ::libc::fd_set).cast_const()) as i32 != 0) as i32)
!= 0)
);
libc::FD_SET(3, (&mut set as *mut ::libc::fd_set));
assert!((libc::FD_ISSET(3, (&mut set as *mut ::libc::fd_set).cast_const()) as i32 != 0));
libc::FD_CLR(3, (&mut set as *mut ::libc::fd_set));
assert!(
((!(libc::FD_ISSET(3, (&mut set as *mut ::libc::fd_set).cast_const()) as i32 != 0) as i32)
!= 0)
);
return 0;
}
Loading