From 9de95ad2e3ae61f60a448012e79df83e81bffc2b Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 12 May 2026 16:05:52 +0100 Subject: [PATCH 1/7] Add rules for socket_type enum values --- rules/socket/ir_unsafe.json | 90 +++++++++++++++++++++++++++++++++++++ rules/socket/src.cpp | 36 +++++++++++++++ rules/socket/tgt_unsafe.rs | 36 +++++++++++++++ 3 files changed, 162 insertions(+) diff --git a/rules/socket/ir_unsafe.json b/rules/socket/ir_unsafe.json index 68f4a473..5db600df 100644 --- a/rules/socket/ir_unsafe.json +++ b/rules/socket/ir_unsafe.json @@ -8,5 +8,95 @@ "return_type": { "type": "i32" } + }, + "f10": { + "body": [ + { + "text": "libc::SOCK_NONBLOCK" + } + ], + "return_type": { + "type": "i32" + } + }, + "f2": { + "body": [ + { + "text": "libc::SOCK_STREAM" + } + ], + "return_type": { + "type": "i32" + } + }, + "f3": { + "body": [ + { + "text": "libc::SOCK_DGRAM" + } + ], + "return_type": { + "type": "i32" + } + }, + "f4": { + "body": [ + { + "text": "libc::SOCK_RAW" + } + ], + "return_type": { + "type": "i32" + } + }, + "f5": { + "body": [ + { + "text": "libc::SOCK_RDM" + } + ], + "return_type": { + "type": "i32" + } + }, + "f6": { + "body": [ + { + "text": "libc::SOCK_SEQPACKET" + } + ], + "return_type": { + "type": "i32" + } + }, + "f7": { + "body": [ + { + "text": "libc::SOCK_DCCP" + } + ], + "return_type": { + "type": "i32" + } + }, + "f8": { + "body": [ + { + "text": "libc::SOCK_PACKET" + } + ], + "return_type": { + "type": "i32" + } + }, + "f9": { + "body": [ + { + "text": "libc::SOCK_CLOEXEC" + } + ], + "return_type": { + "type": "i32" + } } } diff --git a/rules/socket/src.cpp b/rules/socket/src.cpp index 7b246ec3..24621668 100644 --- a/rules/socket/src.cpp +++ b/rules/socket/src.cpp @@ -4,3 +4,39 @@ int f1() { return MSG_NOSIGNAL; } + +int f2() { + return SOCK_STREAM; +} + +int f3() { + return SOCK_DGRAM; +} + +int f4() { + return SOCK_RAW; +} + +int f5() { + return SOCK_RDM; +} + +int f6() { + return SOCK_SEQPACKET; +} + +int f7() { + return SOCK_DCCP; +} + +int f8() { + return SOCK_PACKET; +} + +int f9() { + return SOCK_CLOEXEC; +} + +int f10() { + return SOCK_NONBLOCK; +} diff --git a/rules/socket/tgt_unsafe.rs b/rules/socket/tgt_unsafe.rs index de6e21d3..a60cebe0 100644 --- a/rules/socket/tgt_unsafe.rs +++ b/rules/socket/tgt_unsafe.rs @@ -1,3 +1,39 @@ unsafe fn f1() -> i32 { libc::MSG_NOSIGNAL } + +unsafe fn f2() -> i32 { + libc::SOCK_STREAM +} + +unsafe fn f3() -> i32 { + libc::SOCK_DGRAM +} + +unsafe fn f4() -> i32 { + libc::SOCK_RAW +} + +unsafe fn f5() -> i32 { + libc::SOCK_RDM +} + +unsafe fn f6() -> i32 { + libc::SOCK_SEQPACKET +} + +unsafe fn f7() -> i32 { + libc::SOCK_DCCP +} + +unsafe fn f8() -> i32 { + libc::SOCK_PACKET +} + +unsafe fn f9() -> i32 { + libc::SOCK_CLOEXEC +} + +unsafe fn f10() -> i32 { + libc::SOCK_NONBLOCK +} From e6e287b33aedaa7b8f967f7d85a6bf2f71f6a5a0 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 12 May 2026 16:06:27 +0100 Subject: [PATCH 2/7] Add socket_type_constants unit test --- .../out/refcount/socket_type_constants.rs | 27 +++++++++++++++++ .../unit/out/unsafe/socket_type_constants.rs | 29 +++++++++++++++++++ tests/unit/socket_type_constants.c | 23 +++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/unit/out/refcount/socket_type_constants.rs create mode 100644 tests/unit/out/unsafe/socket_type_constants.rs create mode 100644 tests/unit/socket_type_constants.c diff --git a/tests/unit/out/refcount/socket_type_constants.rs b/tests/unit/out/refcount/socket_type_constants.rs new file mode 100644 index 00000000..0effb739 --- /dev/null +++ b/tests/unit/out/refcount/socket_type_constants.rs @@ -0,0 +1,27 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!((((libc::SOCK_STREAM == 1) as i32) != 0)); + assert!((((libc::SOCK_DGRAM == 2) as i32) != 0)); + assert!((((libc::SOCK_RAW == 3) as i32) != 0)); + assert!((((libc::SOCK_RDM == 4) as i32) != 0)); + assert!((((libc::SOCK_SEQPACKET == 5) as i32) != 0)); + assert!((((libc::SOCK_DCCP == 6) as i32) != 0)); + assert!((((libc::SOCK_PACKET == 10) as i32) != 0)); + let x: Value = Rc::new(RefCell::new((libc::SOCK_STREAM | libc::SOCK_CLOEXEC))); + assert!((((((*x.borrow()) & libc::SOCK_STREAM) == libc::SOCK_STREAM) as i32) != 0)); + assert!((((((*x.borrow()) & libc::SOCK_CLOEXEC) == libc::SOCK_CLOEXEC) as i32) != 0)); + let y: Value = Rc::new(RefCell::new((libc::SOCK_DGRAM | libc::SOCK_NONBLOCK))); + assert!((((((*y.borrow()) & libc::SOCK_DGRAM) == libc::SOCK_DGRAM) as i32) != 0)); + assert!((((((*y.borrow()) & libc::SOCK_NONBLOCK) == libc::SOCK_NONBLOCK) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/socket_type_constants.rs b/tests/unit/out/unsafe/socket_type_constants.rs new file mode 100644 index 00000000..1c291c12 --- /dev/null +++ b/tests/unit/out/unsafe/socket_type_constants.rs @@ -0,0 +1,29 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!(((((libc::SOCK_STREAM) == (1)) as i32) != 0)); + assert!(((((libc::SOCK_DGRAM) == (2)) as i32) != 0)); + assert!(((((libc::SOCK_RAW) == (3)) as i32) != 0)); + assert!(((((libc::SOCK_RDM) == (4)) as i32) != 0)); + assert!(((((libc::SOCK_SEQPACKET) == (5)) as i32) != 0)); + assert!(((((libc::SOCK_DCCP) == (6)) as i32) != 0)); + assert!(((((libc::SOCK_PACKET) == (10)) as i32) != 0)); + let mut x: i32 = ((libc::SOCK_STREAM) | (libc::SOCK_CLOEXEC)); + assert!((((((x) & (libc::SOCK_STREAM)) == (libc::SOCK_STREAM)) as i32) != 0)); + assert!((((((x) & (libc::SOCK_CLOEXEC)) == (libc::SOCK_CLOEXEC)) as i32) != 0)); + let mut y: i32 = ((libc::SOCK_DGRAM) | (libc::SOCK_NONBLOCK)); + assert!((((((y) & (libc::SOCK_DGRAM)) == (libc::SOCK_DGRAM)) as i32) != 0)); + assert!((((((y) & (libc::SOCK_NONBLOCK)) == (libc::SOCK_NONBLOCK)) as i32) != 0)); + return 0; +} diff --git a/tests/unit/socket_type_constants.c b/tests/unit/socket_type_constants.c new file mode 100644 index 00000000..90894daa --- /dev/null +++ b/tests/unit/socket_type_constants.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int main() { + assert(SOCK_STREAM == 1); + assert(SOCK_DGRAM == 2); + assert(SOCK_RAW == 3); + assert(SOCK_RDM == 4); + assert(SOCK_SEQPACKET == 5); + assert(SOCK_DCCP == 6); + assert(SOCK_PACKET == 10); + + int x = SOCK_STREAM | SOCK_CLOEXEC; + assert((x & SOCK_STREAM) == SOCK_STREAM); + assert((x & SOCK_CLOEXEC) == SOCK_CLOEXEC); + + int y = SOCK_DGRAM | SOCK_NONBLOCK; + assert((y & SOCK_DGRAM) == SOCK_DGRAM); + assert((y & SOCK_NONBLOCK) == SOCK_NONBLOCK); + + return 0; +} From 3b456f19feaea6b452cf8ff1455d2871ff9dccbf Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 13 May 2026 10:19:02 +0100 Subject: [PATCH 3/7] Only keep socket_type values shared between Linux and macOS --- rules/socket/ir_unsafe.json | 54 +------------------ rules/socket/src.cpp | 22 +------- rules/socket/tgt_unsafe.rs | 22 +------- .../out/refcount/socket_type_constants.rs | 5 -- .../unit/out/unsafe/socket_type_constants.rs | 5 -- tests/unit/socket_type_constants.c | 5 -- 6 files changed, 4 insertions(+), 109 deletions(-) diff --git a/rules/socket/ir_unsafe.json b/rules/socket/ir_unsafe.json index 5db600df..dabe9d13 100644 --- a/rules/socket/ir_unsafe.json +++ b/rules/socket/ir_unsafe.json @@ -9,16 +9,6 @@ "type": "i32" } }, - "f10": { - "body": [ - { - "text": "libc::SOCK_NONBLOCK" - } - ], - "return_type": { - "type": "i32" - } - }, "f2": { "body": [ { @@ -42,7 +32,7 @@ "f4": { "body": [ { - "text": "libc::SOCK_RAW" + "text": "libc::SOCK_CLOEXEC" } ], "return_type": { @@ -52,47 +42,7 @@ "f5": { "body": [ { - "text": "libc::SOCK_RDM" - } - ], - "return_type": { - "type": "i32" - } - }, - "f6": { - "body": [ - { - "text": "libc::SOCK_SEQPACKET" - } - ], - "return_type": { - "type": "i32" - } - }, - "f7": { - "body": [ - { - "text": "libc::SOCK_DCCP" - } - ], - "return_type": { - "type": "i32" - } - }, - "f8": { - "body": [ - { - "text": "libc::SOCK_PACKET" - } - ], - "return_type": { - "type": "i32" - } - }, - "f9": { - "body": [ - { - "text": "libc::SOCK_CLOEXEC" + "text": "libc::SOCK_NONBLOCK" } ], "return_type": { diff --git a/rules/socket/src.cpp b/rules/socket/src.cpp index 24621668..9f68541e 100644 --- a/rules/socket/src.cpp +++ b/rules/socket/src.cpp @@ -14,29 +14,9 @@ int f3() { } int f4() { - return SOCK_RAW; -} - -int f5() { - return SOCK_RDM; -} - -int f6() { - return SOCK_SEQPACKET; -} - -int f7() { - return SOCK_DCCP; -} - -int f8() { - return SOCK_PACKET; -} - -int f9() { return SOCK_CLOEXEC; } -int f10() { +int f5() { return SOCK_NONBLOCK; } diff --git a/rules/socket/tgt_unsafe.rs b/rules/socket/tgt_unsafe.rs index a60cebe0..eb490adf 100644 --- a/rules/socket/tgt_unsafe.rs +++ b/rules/socket/tgt_unsafe.rs @@ -11,29 +11,9 @@ unsafe fn f3() -> i32 { } unsafe fn f4() -> i32 { - libc::SOCK_RAW -} - -unsafe fn f5() -> i32 { - libc::SOCK_RDM -} - -unsafe fn f6() -> i32 { - libc::SOCK_SEQPACKET -} - -unsafe fn f7() -> i32 { - libc::SOCK_DCCP -} - -unsafe fn f8() -> i32 { - libc::SOCK_PACKET -} - -unsafe fn f9() -> i32 { libc::SOCK_CLOEXEC } -unsafe fn f10() -> i32 { +unsafe fn f5() -> i32 { libc::SOCK_NONBLOCK } diff --git a/tests/unit/out/refcount/socket_type_constants.rs b/tests/unit/out/refcount/socket_type_constants.rs index 0effb739..a80c66a3 100644 --- a/tests/unit/out/refcount/socket_type_constants.rs +++ b/tests/unit/out/refcount/socket_type_constants.rs @@ -12,11 +12,6 @@ pub fn main() { fn main_0() -> i32 { assert!((((libc::SOCK_STREAM == 1) as i32) != 0)); assert!((((libc::SOCK_DGRAM == 2) as i32) != 0)); - assert!((((libc::SOCK_RAW == 3) as i32) != 0)); - assert!((((libc::SOCK_RDM == 4) as i32) != 0)); - assert!((((libc::SOCK_SEQPACKET == 5) as i32) != 0)); - assert!((((libc::SOCK_DCCP == 6) as i32) != 0)); - assert!((((libc::SOCK_PACKET == 10) as i32) != 0)); let x: Value = Rc::new(RefCell::new((libc::SOCK_STREAM | libc::SOCK_CLOEXEC))); assert!((((((*x.borrow()) & libc::SOCK_STREAM) == libc::SOCK_STREAM) as i32) != 0)); assert!((((((*x.borrow()) & libc::SOCK_CLOEXEC) == libc::SOCK_CLOEXEC) as i32) != 0)); diff --git a/tests/unit/out/unsafe/socket_type_constants.rs b/tests/unit/out/unsafe/socket_type_constants.rs index 1c291c12..b590f194 100644 --- a/tests/unit/out/unsafe/socket_type_constants.rs +++ b/tests/unit/out/unsafe/socket_type_constants.rs @@ -14,11 +14,6 @@ pub fn main() { unsafe fn main_0() -> i32 { assert!(((((libc::SOCK_STREAM) == (1)) as i32) != 0)); assert!(((((libc::SOCK_DGRAM) == (2)) as i32) != 0)); - assert!(((((libc::SOCK_RAW) == (3)) as i32) != 0)); - assert!(((((libc::SOCK_RDM) == (4)) as i32) != 0)); - assert!(((((libc::SOCK_SEQPACKET) == (5)) as i32) != 0)); - assert!(((((libc::SOCK_DCCP) == (6)) as i32) != 0)); - assert!(((((libc::SOCK_PACKET) == (10)) as i32) != 0)); let mut x: i32 = ((libc::SOCK_STREAM) | (libc::SOCK_CLOEXEC)); assert!((((((x) & (libc::SOCK_STREAM)) == (libc::SOCK_STREAM)) as i32) != 0)); assert!((((((x) & (libc::SOCK_CLOEXEC)) == (libc::SOCK_CLOEXEC)) as i32) != 0)); diff --git a/tests/unit/socket_type_constants.c b/tests/unit/socket_type_constants.c index 90894daa..1fa9adb4 100644 --- a/tests/unit/socket_type_constants.c +++ b/tests/unit/socket_type_constants.c @@ -5,11 +5,6 @@ int main() { assert(SOCK_STREAM == 1); assert(SOCK_DGRAM == 2); - assert(SOCK_RAW == 3); - assert(SOCK_RDM == 4); - assert(SOCK_SEQPACKET == 5); - assert(SOCK_DCCP == 6); - assert(SOCK_PACKET == 10); int x = SOCK_STREAM | SOCK_CLOEXEC; assert((x & SOCK_STREAM) == SOCK_STREAM); From 7f188eb1e8e2a4497247ce849e719d409496e142 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 13 May 2026 11:21:43 +0100 Subject: [PATCH 4/7] Add support for platform specific rules --- cpp2rust/converter/translation_rule.cpp | 14 +++++++++++++ rule-preprocessor/Cargo.toml | 1 + rule-preprocessor/src/ir.rs | 3 ++- rule-preprocessor/src/syntactic.rs | 26 +++++++++++++++++++++++++ rules/socket/ir_unsafe.json | 2 ++ rules/socket/src.cpp | 2 ++ rules/socket/tgt_unsafe.rs | 2 ++ tests/unit/socket_type_constants.c | 2 ++ 8 files changed, 51 insertions(+), 1 deletion(-) diff --git a/cpp2rust/converter/translation_rule.cpp b/cpp2rust/converter/translation_rule.cpp index 1d6a757a..9cab9713 100644 --- a/cpp2rust/converter/translation_rule.cpp +++ b/cpp2rust/converter/translation_rule.cpp @@ -130,6 +130,16 @@ TypeRule ParseTypeRuleJSON(const llvm::json::Object &obj) { return rule; } +bool CfgMatchesHost(llvm::StringRef cfg) { +#if defined(__linux__) + return cfg == "linux"; +#elif defined(__APPLE__) + return cfg == "macos"; +#else + return false; +#endif +} + void LoadTgtFromIR(ExprRules &exprs, TypeRules &types, const std::filesystem::path &json_path) { auto buf = llvm::MemoryBuffer::getFile(json_path.string()); @@ -153,6 +163,10 @@ void LoadTgtFromIR(ExprRules &exprs, TypeRules &types, if (!obj) continue; + if (auto cfg = obj->getString("cfg"); cfg && !CfgMatchesHost(*cfg)) { + continue; + } + auto name = entry_name.str(); if (name[0] == 'f') { exprs[std::move(name)] = ParseExprRuleJSON(*obj); diff --git a/rule-preprocessor/Cargo.toml b/rule-preprocessor/Cargo.toml index 691fc896..497c57ac 100644 --- a/rule-preprocessor/Cargo.toml +++ b/rule-preprocessor/Cargo.toml @@ -9,3 +9,4 @@ rules = { path = "../rules" } ra_ap_syntax = "0.0.266" serde = { version = "1", features = ["derive"] } serde_json = "1" +syn = "2" diff --git a/rule-preprocessor/src/ir.rs b/rule-preprocessor/src/ir.rs index 03be4230..ec855313 100644 --- a/rule-preprocessor/src/ir.rs +++ b/rule-preprocessor/src/ir.rs @@ -48,9 +48,10 @@ pub struct TypeInfo { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct FnIr { - // Fields ordered alphabetically to match the old serde_json::Map output pub body: Vec, #[serde(skip_serializing_if = "Option::is_none")] + pub cfg: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub generics: Option>>, #[serde(skip_serializing_if = "Option::is_none")] pub multi_statement: Option, diff --git a/rule-preprocessor/src/syntactic.rs b/rule-preprocessor/src/syntactic.rs index a535c42a..3c746b57 100644 --- a/rule-preprocessor/src/syntactic.rs +++ b/rule-preprocessor/src/syntactic.rs @@ -258,6 +258,31 @@ impl<'a> FnIrBuilder<'a> { Self { fn_item } } + fn cfg_target_os(&self) -> Option { + use ast::HasAttrs; + for attr in self.fn_item.attrs() { + let meta_text = attr.meta()?.syntax().text().to_string(); + let syn::Meta::List(list) = syn::parse_str(&meta_text).ok()? else { + continue; + }; + if !list.path.is_ident("cfg") { + continue; + } + let mut found = None; + let _ = list.parse_nested_meta(|nested| { + if nested.path.is_ident("target_os") { + let lit: syn::LitStr = nested.value()?.parse()?; + found = Some(lit.value()); + } + Ok(()) + }); + if found.is_some() { + return found; + } + } + None + } + fn params(&self) -> Vec { let mut params = Vec::new(); let Some(param_list) = self.fn_item.param_list() else { @@ -499,6 +524,7 @@ impl<'a> FnIrBuilder<'a> { }, multi_statement, body, + cfg: self.cfg_target_os(), }; ir.validate(&format!("{}:{}", path.display(), fn_name)); ir diff --git a/rules/socket/ir_unsafe.json b/rules/socket/ir_unsafe.json index dabe9d13..3543abb5 100644 --- a/rules/socket/ir_unsafe.json +++ b/rules/socket/ir_unsafe.json @@ -35,6 +35,7 @@ "text": "libc::SOCK_CLOEXEC" } ], + "cfg": "linux", "return_type": { "type": "i32" } @@ -45,6 +46,7 @@ "text": "libc::SOCK_NONBLOCK" } ], + "cfg": "linux", "return_type": { "type": "i32" } diff --git a/rules/socket/src.cpp b/rules/socket/src.cpp index 9f68541e..b49b50d4 100644 --- a/rules/socket/src.cpp +++ b/rules/socket/src.cpp @@ -13,6 +13,7 @@ int f3() { return SOCK_DGRAM; } +#ifdef __linux__ int f4() { return SOCK_CLOEXEC; } @@ -20,3 +21,4 @@ int f4() { int f5() { return SOCK_NONBLOCK; } +#endif diff --git a/rules/socket/tgt_unsafe.rs b/rules/socket/tgt_unsafe.rs index eb490adf..f39b4c40 100644 --- a/rules/socket/tgt_unsafe.rs +++ b/rules/socket/tgt_unsafe.rs @@ -10,10 +10,12 @@ unsafe fn f3() -> i32 { libc::SOCK_DGRAM } +#[cfg(target_os = "linux")] unsafe fn f4() -> i32 { libc::SOCK_CLOEXEC } +#[cfg(target_os = "linux")] unsafe fn f5() -> i32 { libc::SOCK_NONBLOCK } diff --git a/tests/unit/socket_type_constants.c b/tests/unit/socket_type_constants.c index 1fa9adb4..2fc645ce 100644 --- a/tests/unit/socket_type_constants.c +++ b/tests/unit/socket_type_constants.c @@ -6,6 +6,7 @@ int main() { assert(SOCK_STREAM == 1); assert(SOCK_DGRAM == 2); +#ifdef __linux__ int x = SOCK_STREAM | SOCK_CLOEXEC; assert((x & SOCK_STREAM) == SOCK_STREAM); assert((x & SOCK_CLOEXEC) == SOCK_CLOEXEC); @@ -13,6 +14,7 @@ int main() { int y = SOCK_DGRAM | SOCK_NONBLOCK; assert((y & SOCK_DGRAM) == SOCK_DGRAM); assert((y & SOCK_NONBLOCK) == SOCK_NONBLOCK); +#endif return 0; } From 1b82f10d3ff1f59c7a368faeae04c21eccae6efd Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 13 May 2026 11:27:24 +0100 Subject: [PATCH 5/7] Delete linux specific test code --- tests/unit/out/refcount/socket_type_constants.rs | 6 ------ tests/unit/out/unsafe/socket_type_constants.rs | 6 ------ tests/unit/socket_type_constants.c | 11 ----------- 3 files changed, 23 deletions(-) diff --git a/tests/unit/out/refcount/socket_type_constants.rs b/tests/unit/out/refcount/socket_type_constants.rs index a80c66a3..605f0ca0 100644 --- a/tests/unit/out/refcount/socket_type_constants.rs +++ b/tests/unit/out/refcount/socket_type_constants.rs @@ -12,11 +12,5 @@ pub fn main() { fn main_0() -> i32 { assert!((((libc::SOCK_STREAM == 1) as i32) != 0)); assert!((((libc::SOCK_DGRAM == 2) as i32) != 0)); - let x: Value = Rc::new(RefCell::new((libc::SOCK_STREAM | libc::SOCK_CLOEXEC))); - assert!((((((*x.borrow()) & libc::SOCK_STREAM) == libc::SOCK_STREAM) as i32) != 0)); - assert!((((((*x.borrow()) & libc::SOCK_CLOEXEC) == libc::SOCK_CLOEXEC) as i32) != 0)); - let y: Value = Rc::new(RefCell::new((libc::SOCK_DGRAM | libc::SOCK_NONBLOCK))); - assert!((((((*y.borrow()) & libc::SOCK_DGRAM) == libc::SOCK_DGRAM) as i32) != 0)); - assert!((((((*y.borrow()) & libc::SOCK_NONBLOCK) == libc::SOCK_NONBLOCK) as i32) != 0)); return 0; } diff --git a/tests/unit/out/unsafe/socket_type_constants.rs b/tests/unit/out/unsafe/socket_type_constants.rs index b590f194..c7af392e 100644 --- a/tests/unit/out/unsafe/socket_type_constants.rs +++ b/tests/unit/out/unsafe/socket_type_constants.rs @@ -14,11 +14,5 @@ pub fn main() { unsafe fn main_0() -> i32 { assert!(((((libc::SOCK_STREAM) == (1)) as i32) != 0)); assert!(((((libc::SOCK_DGRAM) == (2)) as i32) != 0)); - let mut x: i32 = ((libc::SOCK_STREAM) | (libc::SOCK_CLOEXEC)); - assert!((((((x) & (libc::SOCK_STREAM)) == (libc::SOCK_STREAM)) as i32) != 0)); - assert!((((((x) & (libc::SOCK_CLOEXEC)) == (libc::SOCK_CLOEXEC)) as i32) != 0)); - let mut y: i32 = ((libc::SOCK_DGRAM) | (libc::SOCK_NONBLOCK)); - assert!((((((y) & (libc::SOCK_DGRAM)) == (libc::SOCK_DGRAM)) as i32) != 0)); - assert!((((((y) & (libc::SOCK_NONBLOCK)) == (libc::SOCK_NONBLOCK)) as i32) != 0)); return 0; } diff --git a/tests/unit/socket_type_constants.c b/tests/unit/socket_type_constants.c index 2fc645ce..1e5036f5 100644 --- a/tests/unit/socket_type_constants.c +++ b/tests/unit/socket_type_constants.c @@ -5,16 +5,5 @@ int main() { assert(SOCK_STREAM == 1); assert(SOCK_DGRAM == 2); - -#ifdef __linux__ - int x = SOCK_STREAM | SOCK_CLOEXEC; - assert((x & SOCK_STREAM) == SOCK_STREAM); - assert((x & SOCK_CLOEXEC) == SOCK_CLOEXEC); - - int y = SOCK_DGRAM | SOCK_NONBLOCK; - assert((y & SOCK_DGRAM) == SOCK_DGRAM); - assert((y & SOCK_NONBLOCK) == SOCK_NONBLOCK); -#endif - return 0; } From 46f4c9e48f722448e3249bd55a395d01ed4b2aa6 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 13 May 2026 11:37:37 +0100 Subject: [PATCH 6/7] Rename cfg to target_os --- cpp2rust/converter/translation_rule.cpp | 9 +++++---- rule-preprocessor/src/ir.rs | 2 +- rule-preprocessor/src/syntactic.rs | 4 ++-- rules/socket/ir_unsafe.json | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cpp2rust/converter/translation_rule.cpp b/cpp2rust/converter/translation_rule.cpp index 9cab9713..06f21921 100644 --- a/cpp2rust/converter/translation_rule.cpp +++ b/cpp2rust/converter/translation_rule.cpp @@ -130,11 +130,11 @@ TypeRule ParseTypeRuleJSON(const llvm::json::Object &obj) { return rule; } -bool CfgMatchesHost(llvm::StringRef cfg) { +bool TargetOSMatchesHost(llvm::StringRef target_os) { #if defined(__linux__) - return cfg == "linux"; + return target_os == "linux"; #elif defined(__APPLE__) - return cfg == "macos"; + return target_os == "macos"; #else return false; #endif @@ -163,7 +163,8 @@ void LoadTgtFromIR(ExprRules &exprs, TypeRules &types, if (!obj) continue; - if (auto cfg = obj->getString("cfg"); cfg && !CfgMatchesHost(*cfg)) { + if (auto target_os = obj->getString("target_os"); + target_os && !TargetOSMatchesHost(*target_os)) { continue; } diff --git a/rule-preprocessor/src/ir.rs b/rule-preprocessor/src/ir.rs index ec855313..324935c6 100644 --- a/rule-preprocessor/src/ir.rs +++ b/rule-preprocessor/src/ir.rs @@ -50,7 +50,7 @@ pub struct TypeInfo { pub struct FnIr { pub body: Vec, #[serde(skip_serializing_if = "Option::is_none")] - pub cfg: Option, + pub target_os: Option, #[serde(skip_serializing_if = "Option::is_none")] pub generics: Option>>, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/rule-preprocessor/src/syntactic.rs b/rule-preprocessor/src/syntactic.rs index 3c746b57..6f3ba7d6 100644 --- a/rule-preprocessor/src/syntactic.rs +++ b/rule-preprocessor/src/syntactic.rs @@ -258,7 +258,7 @@ impl<'a> FnIrBuilder<'a> { Self { fn_item } } - fn cfg_target_os(&self) -> Option { + fn get_target_os(&self) -> Option { use ast::HasAttrs; for attr in self.fn_item.attrs() { let meta_text = attr.meta()?.syntax().text().to_string(); @@ -524,7 +524,7 @@ impl<'a> FnIrBuilder<'a> { }, multi_statement, body, - cfg: self.cfg_target_os(), + target_os: self.get_target_os(), }; ir.validate(&format!("{}:{}", path.display(), fn_name)); ir diff --git a/rules/socket/ir_unsafe.json b/rules/socket/ir_unsafe.json index 3543abb5..bbf8730e 100644 --- a/rules/socket/ir_unsafe.json +++ b/rules/socket/ir_unsafe.json @@ -35,7 +35,7 @@ "text": "libc::SOCK_CLOEXEC" } ], - "cfg": "linux", + "target_os": "linux", "return_type": { "type": "i32" } @@ -46,7 +46,7 @@ "text": "libc::SOCK_NONBLOCK" } ], - "cfg": "linux", + "target_os": "linux", "return_type": { "type": "i32" } From 696a358a1b9289f29fab982977673390d59af24a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 13 May 2026 11:41:34 +0100 Subject: [PATCH 7/7] Keep FnIr fields in alphabetical order --- rule-preprocessor/src/ir.rs | 4 ++-- rules/socket/ir_unsafe.json | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rule-preprocessor/src/ir.rs b/rule-preprocessor/src/ir.rs index 324935c6..9a0c5f58 100644 --- a/rule-preprocessor/src/ir.rs +++ b/rule-preprocessor/src/ir.rs @@ -50,8 +50,6 @@ pub struct TypeInfo { pub struct FnIr { pub body: Vec, #[serde(skip_serializing_if = "Option::is_none")] - pub target_os: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub generics: Option>>, #[serde(skip_serializing_if = "Option::is_none")] pub multi_statement: Option, @@ -59,6 +57,8 @@ pub struct FnIr { pub params: Option>, #[serde(skip_serializing_if = "Option::is_none")] pub return_type: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub target_os: Option, } impl FnIr { diff --git a/rules/socket/ir_unsafe.json b/rules/socket/ir_unsafe.json index bbf8730e..c37b5314 100644 --- a/rules/socket/ir_unsafe.json +++ b/rules/socket/ir_unsafe.json @@ -35,10 +35,10 @@ "text": "libc::SOCK_CLOEXEC" } ], - "target_os": "linux", "return_type": { "type": "i32" - } + }, + "target_os": "linux" }, "f5": { "body": [ @@ -46,9 +46,9 @@ "text": "libc::SOCK_NONBLOCK" } ], - "target_os": "linux", "return_type": { "type": "i32" - } + }, + "target_os": "linux" } }