Skip to content

Commit b043c6e

Browse files
committed
Add rules for fd_set macros
1 parent 943b97c commit b043c6e

5 files changed

Lines changed: 86 additions & 0 deletions

File tree

cpp2rust/compat/sys/select.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include_next <sys/select.h>
5+
6+
#undef FD_SET
7+
#undef FD_CLR
8+
#undef FD_ISSET
9+
#undef FD_ZERO
10+
11+
void cpp2rust_fd_set(int fd, fd_set *set);
12+
void cpp2rust_fd_clr(int fd, fd_set *set);
13+
int cpp2rust_fd_isset(int fd, const fd_set *set);
14+
void cpp2rust_fd_zero(fd_set *set);
15+
16+
#define FD_SET(fd, set) cpp2rust_fd_set(fd, set)
17+
#define FD_CLR(fd, set) cpp2rust_fd_clr(fd, set)
18+
#define FD_ISSET(fd, set) cpp2rust_fd_isset(fd, set)
19+
#define FD_ZERO(set) cpp2rust_fd_zero(set)

rules/select/src.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
#include <sys/select.h>
55

6+
using t1 = fd_set;
7+
68
int f1(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
79
struct timeval *timeout) {
810
return select(nfds, readfds, writefds, exceptfds, timeout);
911
}
12+
13+
void f2(int fd, fd_set *set) { return FD_SET(fd, set); }
14+
void f3(int fd, fd_set *set) { return FD_CLR(fd, set); }
15+
int f4(int fd, const fd_set *set) { return FD_ISSET(fd, set); }
16+
void f5(fd_set *set) { return FD_ZERO(set); }

rules/select/tgt_unsafe.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4+
unsafe fn types() {
5+
let t1: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>();
6+
}
7+
48
unsafe fn f1(
59
a0: i32,
610
a1: *mut ::libc::fd_set,
@@ -10,3 +14,16 @@ unsafe fn f1(
1014
) -> i32 {
1115
libc::select(a0, a1, a2, a3, a4)
1216
}
17+
18+
unsafe fn f2(a0: i32, a1: *mut ::libc::fd_set) {
19+
libc::FD_SET(a0, a1);
20+
}
21+
unsafe fn f3(a0: i32, a1: *mut ::libc::fd_set) {
22+
libc::FD_CLR(a0, a1);
23+
}
24+
unsafe fn f4(a0: i32, a1: *const ::libc::fd_set) -> i32 {
25+
libc::FD_ISSET(a0, a1) as i32
26+
}
27+
unsafe fn f5(a0: *mut ::libc::fd_set) {
28+
libc::FD_ZERO(a0);
29+
}

tests/unit/fd_set_macros.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// no-compile: refcount
2+
#include <assert.h>
3+
#include <sys/select.h>
4+
5+
int main() {
6+
fd_set set;
7+
FD_ZERO(&set);
8+
assert(!FD_ISSET(3, &set));
9+
FD_SET(3, &set);
10+
assert(FD_ISSET(3, &set));
11+
FD_CLR(3, &set);
12+
assert(!FD_ISSET(3, &set));
13+
return 0;
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut set: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>();
16+
libc::FD_ZERO((&mut set as *mut ::libc::fd_set));
17+
assert!(
18+
((!(libc::FD_ISSET(3, (&mut set as *mut ::libc::fd_set).cast_const()) as i32 != 0) as i32)
19+
!= 0)
20+
);
21+
libc::FD_SET(3, (&mut set as *mut ::libc::fd_set));
22+
assert!((libc::FD_ISSET(3, (&mut set as *mut ::libc::fd_set).cast_const()) as i32 != 0));
23+
libc::FD_CLR(3, (&mut set as *mut ::libc::fd_set));
24+
assert!(
25+
((!(libc::FD_ISSET(3, (&mut set as *mut ::libc::fd_set).cast_const()) as i32 != 0) as i32)
26+
!= 0)
27+
);
28+
return 0;
29+
}

0 commit comments

Comments
 (0)