Skip to content

Commit 01d9f7a

Browse files
authored
Give compat helper declarations C linkage in C++ (#554)
1 parent 820eb22 commit 01d9f7a

6 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎cpp2rust/compat/arpa/inet.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
#undef htonl
99
#undef htons
1010

11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
1114
uint32_t cpp2rust_ntohl(uint32_t x);
1215
uint16_t cpp2rust_ntohs(uint16_t x);
1316
uint32_t cpp2rust_htonl(uint32_t x);
1417
uint16_t cpp2rust_htons(uint16_t x);
18+
#ifdef __cplusplus
19+
}
20+
#endif
1521

1622
#define ntohl(x) cpp2rust_ntohl(x)
1723
#define ntohs(x) cpp2rust_ntohs(x)

‎cpp2rust/compat/errno.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
#undef errno
77

8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
811
int *cpp2rust_errno(void);
12+
#ifdef __cplusplus
13+
}
14+
#endif
915

1016
#define errno (*cpp2rust_errno())

‎cpp2rust/compat/sys/select.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
#undef FD_ISSET
99
#undef FD_ZERO
1010

11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
1114
void cpp2rust_fd_set(int fd, fd_set *set);
1215
void cpp2rust_fd_clr(int fd, fd_set *set);
1316
int cpp2rust_fd_isset(int fd, const fd_set *set);
1417
void cpp2rust_fd_zero(fd_set *set);
18+
#ifdef __cplusplus
19+
}
20+
#endif
1521

1622
#define FD_SET(fd, set) cpp2rust_fd_set(fd, set)
1723
#define FD_CLR(fd, set) cpp2rust_fd_clr(fd, set)

‎tests/unit/out/refcount/size_t_ssize_t.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ fn main_0() -> i32 {
208208
((sizes.as_pointer() as Ptr<usize>).to_end()).reinterpret_cast::<usize>();
209209
assert!((((sizes_end).clone() - (sizes.as_pointer() as Ptr::<usize>)) as i64 == 4_i64));
210210
assert!((((sizes_end.offset(-((1) as isize))).read()) == 4_usize));
211+
let set: Value<CFdSet> = Rc::new(RefCell::new(Default::default()));
212+
(set.as_pointer()).with_mut(|__s| __s.zero());
213+
(set.as_pointer()).with_mut(|__s| __s.set(3));
214+
assert!(
215+
(if (set.as_pointer()).with(|__s| __s.isset(3)) {
216+
1
217+
} else {
218+
0
219+
} != 0)
220+
);
221+
libcc2rs::cpp2rust_errno().write(42);
222+
assert!(((libcc2rs::cpp2rust_errno().read()) == 42));
223+
assert!((u32::from_be(u32::to_be(7_u32)) == 7_u32));
211224
return 0;
212225
}
213226
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/size_t_ssize_t.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ unsafe fn main_0() -> i32 {
197197
== (4_i64))
198198
);
199199
assert!(((*(sizes_end.offset(-((1) as isize)))) == (4_usize)));
200+
let mut set: ::libc::fd_set = std::mem::zeroed::<::libc::fd_set>();
201+
libc::FD_ZERO((&mut set as *mut ::libc::fd_set));
202+
libc::FD_SET(3, (&mut set as *mut ::libc::fd_set));
203+
assert!((libc::FD_ISSET(3, (&mut set as *mut ::libc::fd_set).cast_const()) as i32 != 0));
204+
(*libcc2rs::cpp2rust_errno_unsafe()) = 42;
205+
assert!(((*libcc2rs::cpp2rust_errno_unsafe()) == (42)));
206+
assert!(((u32::from_be(u32::to_be(7_u32))) == (7_u32)));
200207
return 0;
201208
}
202209
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/size_t_ssize_t.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <algorithm>
2+
#include <arpa/inet.h>
23
#include <cassert>
34
#include <cstddef>
45
#include <cstdint>
6+
#include <errno.h>
57
#include <iterator>
8+
#include <sys/select.h>
69
#include <sys/types.h>
710
#include <utility>
811

@@ -113,5 +116,15 @@ int main() {
113116
const size_t *sizes_end = std::end(sizes);
114117
assert(sizes_end - sizes == 4);
115118
assert(*(sizes_end - 1) == 4);
119+
120+
fd_set set;
121+
FD_ZERO(&set);
122+
FD_SET(3, &set);
123+
assert(FD_ISSET(3, &set));
124+
125+
errno = 42;
126+
assert(errno == 42);
127+
128+
assert(ntohl(htonl(7)) == 7);
116129
return 0;
117130
}

0 commit comments

Comments
 (0)