Skip to content

Add rules for socket_type enum values#86

Merged
nunoplopes merged 7 commits into
Cpp2Rust:masterfrom
lucic71:socket-type
May 13, 2026
Merged

Add rules for socket_type enum values#86
nunoplopes merged 7 commits into
Cpp2Rust:masterfrom
lucic71:socket-type

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 12, 2026

This adds support for translating socket_types values: SOCK_STREAM, SOCK_DGRAM, SOCK_CLOEXEC (Linux specific), SOCK_NONBLOCK (Linux specific).

The rules for CLOEXEC and NONBLOCK are only visible on Linux using the new IR field called target_os. On the C++ side, the rules are guarded with a macro:

#ifdef __linux__
int f4() {
  return SOCK_CLOEXEC;
}

int f5() {
  return SOCK_NONBLOCK;
}
#endif

On the Rust side, they are guarded using the target_os attribute:

#[cfg(target_os = "linux")]
unsafe fn f4() -> i32 {
    libc::SOCK_CLOEXEC
}

#[cfg(target_os = "linux")]
unsafe fn f5() -> i32 {
    libc::SOCK_NONBLOCK
}

rule-preprocessor parses the attributes of a function and adds target_os = linux in the IR. translation_rule.cpp skips the rule if the current OS is not the same as the OS declared by target_os.

@nunoplopes nunoplopes merged commit 7e9a423 into Cpp2Rust:master May 13, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants