Translate socket functions in C mode#141
Merged
Merged
Conversation
lucic71
added a commit
to lucic71/cpp2rust
that referenced
this pull request
May 23, 2026
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
getsockname, and the rest of the socket functions that receive astruct sockaddr*(__SOCKADDR_ARG) parameter, are defined differently in C and C++:__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:__transparent_unionis 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.