From 20a044387f077343001834cbaaa75024b99d3b5f Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 11 May 2026 15:36:18 +0100 Subject: [PATCH 1/5] Add basic function-shared-using-header test --- .../multi-file/header_function/CMakeLists.txt | 3 ++ tests/multi-file/header_function/a.c | 7 ++++ tests/multi-file/header_function/b.c | 12 ++++++ tests/multi-file/header_function/header.h | 3 ++ .../out/refcount/header_function.rs | 40 ++++++++++++++++++ .../out/unsafe/header_function.rs | 41 +++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 tests/multi-file/header_function/CMakeLists.txt create mode 100644 tests/multi-file/header_function/a.c create mode 100644 tests/multi-file/header_function/b.c create mode 100644 tests/multi-file/header_function/header.h create mode 100644 tests/multi-file/header_function/out/refcount/header_function.rs create mode 100644 tests/multi-file/header_function/out/unsafe/header_function.rs diff --git a/tests/multi-file/header_function/CMakeLists.txt b/tests/multi-file/header_function/CMakeLists.txt new file mode 100644 index 00000000..d4ca928c --- /dev/null +++ b/tests/multi-file/header_function/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.16) +project(extern_functions LANGUAGES C) +add_executable(app a.c b.c) diff --git a/tests/multi-file/header_function/a.c b/tests/multi-file/header_function/a.c new file mode 100644 index 00000000..00cc1904 --- /dev/null +++ b/tests/multi-file/header_function/a.c @@ -0,0 +1,7 @@ +#include +#include "header.h" + +int main(void) { + assert(helper(42) == 43); + return 0; +} diff --git a/tests/multi-file/header_function/b.c b/tests/multi-file/header_function/b.c new file mode 100644 index 00000000..6be6dab8 --- /dev/null +++ b/tests/multi-file/header_function/b.c @@ -0,0 +1,12 @@ +#include "header.h" + +static int unrelated1(void) { return 1; } +static int unrelated2(void) { return 2; } +static int unrelated3(void) { return 3; } + +int helper(int x) { + (void)unrelated1(); + (void)unrelated2(); + (void)unrelated3(); + return x + 1; +} diff --git a/tests/multi-file/header_function/header.h b/tests/multi-file/header_function/header.h new file mode 100644 index 00000000..c0104c86 --- /dev/null +++ b/tests/multi-file/header_function/header.h @@ -0,0 +1,3 @@ +#pragma once + +int helper(int x); diff --git a/tests/multi-file/header_function/out/refcount/header_function.rs b/tests/multi-file/header_function/out/refcount/header_function.rs new file mode 100644 index 00000000..531f32ef --- /dev/null +++ b/tests/multi-file/header_function/out/refcount/header_function.rs @@ -0,0 +1,40 @@ +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}; + +// a.rs +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!( + (((({ + let _x: i32 = 42; + helper_0(_x) + }) == 43) as i32) + != 0) + ); + return 0; +} +// b.rs +pub fn unrelated1_1() -> i32 { + return 1; +} +pub fn unrelated2_2() -> i32 { + return 2; +} +pub fn unrelated3_3() -> i32 { + return 3; +} +pub fn helper_0(x: i32) -> i32 { + let x: Value = Rc::new(RefCell::new(x)); + ({ unrelated1_1() }); + ({ unrelated2_2() }); + ({ unrelated3_3() }); + return ((*x.borrow()) + 1); +} diff --git a/tests/multi-file/header_function/out/unsafe/header_function.rs b/tests/multi-file/header_function/out/unsafe/header_function.rs new file mode 100644 index 00000000..65b74641 --- /dev/null +++ b/tests/multi-file/header_function/out/unsafe/header_function.rs @@ -0,0 +1,41 @@ +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; + +// a.rs +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!( + ((((unsafe { + let _x: i32 = 42; + helper_0(_x) + }) == (43)) as i32) + != 0) + ); + return 0; +} +// b.rs +pub unsafe fn unrelated1_1() -> i32 { + return 1; +} +pub unsafe fn unrelated2_2() -> i32 { + return 2; +} +pub unsafe fn unrelated3_3() -> i32 { + return 3; +} +pub unsafe fn helper_0(mut x: i32) -> i32 { + (unsafe { unrelated1_1() }); + (unsafe { unrelated2_2() }); + (unsafe { unrelated3_3() }); + return ((x) + (1)); +} From d9a97850bc39762da72328c0827d6168145212ba Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 11 May 2026 15:37:16 +0100 Subject: [PATCH 2/5] Update multi-file test --- .../extern_functions/out/refcount/extern_functions.rs | 2 +- .../multi-file/extern_functions/out/unsafe/extern_functions.rs | 2 +- tests/multi-file/extern_functions/test.expectations | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) delete mode 100644 tests/multi-file/extern_functions/test.expectations diff --git a/tests/multi-file/extern_functions/out/refcount/extern_functions.rs b/tests/multi-file/extern_functions/out/refcount/extern_functions.rs index 0e8f5a77..531f32ef 100644 --- a/tests/multi-file/extern_functions/out/refcount/extern_functions.rs +++ b/tests/multi-file/extern_functions/out/refcount/extern_functions.rs @@ -31,7 +31,7 @@ pub fn unrelated2_2() -> i32 { pub fn unrelated3_3() -> i32 { return 3; } -pub fn helper_4(x: i32) -> i32 { +pub fn helper_0(x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); ({ unrelated1_1() }); ({ unrelated2_2() }); diff --git a/tests/multi-file/extern_functions/out/unsafe/extern_functions.rs b/tests/multi-file/extern_functions/out/unsafe/extern_functions.rs index 567f8b7c..65b74641 100644 --- a/tests/multi-file/extern_functions/out/unsafe/extern_functions.rs +++ b/tests/multi-file/extern_functions/out/unsafe/extern_functions.rs @@ -33,7 +33,7 @@ pub unsafe fn unrelated2_2() -> i32 { pub unsafe fn unrelated3_3() -> i32 { return 3; } -pub unsafe fn helper_4(mut x: i32) -> i32 { +pub unsafe fn helper_0(mut x: i32) -> i32 { (unsafe { unrelated1_1() }); (unsafe { unrelated2_2() }); (unsafe { unrelated3_3() }); diff --git a/tests/multi-file/extern_functions/test.expectations b/tests/multi-file/extern_functions/test.expectations deleted file mode 100644 index 791ea66f..00000000 --- a/tests/multi-file/extern_functions/test.expectations +++ /dev/null @@ -1 +0,0 @@ -no-compile From fcc0246c86c8303be7334cb81ede2a7cad2a5b29 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 11 May 2026 16:00:17 +0100 Subject: [PATCH 3/5] Keep stable name for external symbols --- cpp2rust/converter/converter_lib.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index c75cdb57..fabfe0fe 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -332,19 +332,24 @@ unsigned GetAnonIndex(const clang::NamedDecl *decl) { return 0; } -std::string GetID(const clang::Decl *decl) { - assert(decl); - const auto file_name = GetFileName(decl); - const auto line_num = GetLineNumber(decl); - const auto column_num = GetColumnNumber(decl); +static std::string GetLocationID(const clang::Decl *decl) { + return GetFileName(decl) + std::to_string(GetLineNumber(decl)) + + std::to_string(GetColumnNumber(decl)); +} + +static std::string GetParamSignature(const clang::Decl *decl) { std::string args; if (auto fdecl = clang::dyn_cast(decl)) { for (unsigned i = 0; i < fdecl->getNumParams(); ++i) { args += fdecl->getParamDecl(i)->getType().getAsString(); } } - return file_name + std::to_string(line_num) + std::to_string(column_num) + - args; + return args; +} + +std::string GetID(const clang::Decl *decl) { + assert(decl); + return GetLocationID(decl) + GetParamSignature(decl); } static std::unordered_map type_mapping; @@ -368,7 +373,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) { if (!clang::isa(fn)) { auto mangled = clang::ASTNameGenerator(decl->getASTContext()).getName(decl) + - GetID(decl); + GetParamSignature(decl); + if (fn->getFormalLinkage() == clang::Linkage::Internal) { + mangled += GetLocationID(decl); + } auto id = type_mapping.try_emplace(mangled, type_mapping.size()).first->second; name += '_'; From 436c9ff67d2b1e91287f641ad6f9df26d4e688cc Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 11 May 2026 16:00:29 +0100 Subject: [PATCH 4/5] clang-format --- tests/multi-file/header_function/a.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/multi-file/header_function/a.c b/tests/multi-file/header_function/a.c index 00cc1904..11eb9309 100644 --- a/tests/multi-file/header_function/a.c +++ b/tests/multi-file/header_function/a.c @@ -1,4 +1,5 @@ #include + #include "header.h" int main(void) { From 30a332de6d7ff044140b598d1855c0e85fcdcd64 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sun, 17 May 2026 19:12:35 +0100 Subject: [PATCH 5/5] Update tests --- .../extern_functions/out/unsafe/extern_functions.rs | 6 +++--- .../header_function/out/unsafe/header_function.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/multi-file/extern_functions/out/unsafe/extern_functions.rs b/tests/multi-file/extern_functions/out/unsafe/extern_functions.rs index 65b74641..4c6608cd 100644 --- a/tests/multi-file/extern_functions/out/unsafe/extern_functions.rs +++ b/tests/multi-file/extern_functions/out/unsafe/extern_functions.rs @@ -34,8 +34,8 @@ pub unsafe fn unrelated3_3() -> i32 { return 3; } pub unsafe fn helper_0(mut x: i32) -> i32 { - (unsafe { unrelated1_1() }); - (unsafe { unrelated2_2() }); - (unsafe { unrelated3_3() }); + &(unsafe { unrelated1_1() }); + &(unsafe { unrelated2_2() }); + &(unsafe { unrelated3_3() }); return ((x) + (1)); } diff --git a/tests/multi-file/header_function/out/unsafe/header_function.rs b/tests/multi-file/header_function/out/unsafe/header_function.rs index 65b74641..4c6608cd 100644 --- a/tests/multi-file/header_function/out/unsafe/header_function.rs +++ b/tests/multi-file/header_function/out/unsafe/header_function.rs @@ -34,8 +34,8 @@ pub unsafe fn unrelated3_3() -> i32 { return 3; } pub unsafe fn helper_0(mut x: i32) -> i32 { - (unsafe { unrelated1_1() }); - (unsafe { unrelated2_2() }); - (unsafe { unrelated3_3() }); + &(unsafe { unrelated1_1() }); + &(unsafe { unrelated2_2() }); + &(unsafe { unrelated3_3() }); return ((x) + (1)); }