Commit a41a08e
authored
Translate socket functions in C mode (#141)
`getsockname`, and the rest of the socket functions that receive a
`struct sockaddr*` (`__SOCKADDR_ARG`) parameter, are defined differently
in C and C++:
```c
int getsockname (int fd, __SOCKADDR_ARG addr, socklen_t *len)
#if defined __cplusplus
#define __SOCKADDR_ARG struct sockaddr *__restrict
#else
typedef union {
struct sockaddr *__restrict __sockaddr__;
struct sockaddr_in *__restrict __sockaddr_in__;
} __SOCKADDR_ARG __attribute__ ((__transparent_union__));
#endif
```
`__transparent_union__` is an attribute that allows a function whose
parameter has that union type can be called with any of the union's arm
types directly, without a cast:
```c
struct sockaddr_in in;
// this would normally generate an error because &in has type
// sockaddr_in* but the argument is declared sockaddr*
getsockname(fd, &in, len);
struct sockaddr_in6 in6;
getsockname(fd, &in6, len); // same tension between sockaddr_in6* and sockaddr_in*
```
`__transparent_union` is only available in C. In C++, an explicit cast
is required.
In the Clang AST, the transparent union object is created through a
`CompoundLiteralExpression(InitListExpr(struct sockaddr_in *))`. We
convert the argument of CompoundLiteralExpression in order to get the
correct rust code.1 parent 2c67f24 commit a41a08e
5 files changed
Lines changed: 72 additions & 0 deletions
File tree
- cpp2rust/converter
- rules/socket
- tests/unit
- out/unsafe
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2621 | 2621 | | |
2622 | 2622 | | |
2623 | 2623 | | |
| 2624 | + | |
| 2625 | + | |
| 2626 | + | |
| 2627 | + | |
| 2628 | + | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
2624 | 2636 | | |
2625 | 2637 | | |
2626 | 2638 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
283 | 283 | | |
284 | 284 | | |
285 | 285 | | |
| 286 | + | |
| 287 | + | |
286 | 288 | | |
287 | 289 | | |
288 | 290 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
0 commit comments