Skip to content

Commit 01d3f76

Browse files
authored
Fix usage of extern functions accross multiple translation units (#71)
1 parent 2414c36 commit 01d3f76

10 files changed

Lines changed: 128 additions & 14 deletions

File tree

cpp2rust/converter/converter_lib.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,19 +332,24 @@ unsigned GetAnonIndex(const clang::NamedDecl *decl) {
332332
return 0;
333333
}
334334

335-
std::string GetID(const clang::Decl *decl) {
336-
assert(decl);
337-
const auto file_name = GetFileName(decl);
338-
const auto line_num = GetLineNumber(decl);
339-
const auto column_num = GetColumnNumber(decl);
335+
static std::string GetLocationID(const clang::Decl *decl) {
336+
return GetFileName(decl) + std::to_string(GetLineNumber(decl)) +
337+
std::to_string(GetColumnNumber(decl));
338+
}
339+
340+
static std::string GetParamSignature(const clang::Decl *decl) {
340341
std::string args;
341342
if (auto fdecl = clang::dyn_cast<clang::FunctionDecl>(decl)) {
342343
for (unsigned i = 0; i < fdecl->getNumParams(); ++i) {
343344
args += fdecl->getParamDecl(i)->getType().getAsString();
344345
}
345346
}
346-
return file_name + std::to_string(line_num) + std::to_string(column_num) +
347-
args;
347+
return args;
348+
}
349+
350+
std::string GetID(const clang::Decl *decl) {
351+
assert(decl);
352+
return GetLocationID(decl) + GetParamSignature(decl);
348353
}
349354

350355
static std::unordered_map<std::string, size_t> type_mapping;
@@ -368,7 +373,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
368373
if (!clang::isa<clang::CXXMethodDecl>(fn)) {
369374
auto mangled =
370375
clang::ASTNameGenerator(decl->getASTContext()).getName(decl) +
371-
GetID(decl);
376+
GetParamSignature(decl);
377+
if (fn->getFormalLinkage() == clang::Linkage::Internal) {
378+
mangled += GetLocationID(decl);
379+
}
372380
auto id =
373381
type_mapping.try_emplace(mangled, type_mapping.size()).first->second;
374382
name += '_';

tests/multi-file/extern_functions/out/refcount/extern_functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn unrelated2_2() -> i32 {
3131
pub fn unrelated3_3() -> i32 {
3232
return 3;
3333
}
34-
pub fn helper_4(x: i32) -> i32 {
34+
pub fn helper_0(x: i32) -> i32 {
3535
let x: Value<i32> = Rc::new(RefCell::new(x));
3636
({ unrelated1_1() });
3737
({ unrelated2_2() });

tests/multi-file/extern_functions/out/unsafe/extern_functions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ pub unsafe fn unrelated2_2() -> i32 {
3333
pub unsafe fn unrelated3_3() -> i32 {
3434
return 3;
3535
}
36-
pub unsafe fn helper_4(mut x: i32) -> i32 {
37-
(unsafe { unrelated1_1() });
38-
(unsafe { unrelated2_2() });
39-
(unsafe { unrelated3_3() });
36+
pub unsafe fn helper_0(mut x: i32) -> i32 {
37+
&(unsafe { unrelated1_1() });
38+
&(unsafe { unrelated2_2() });
39+
&(unsafe { unrelated3_3() });
4040
return ((x) + (1));
4141
}

tests/multi-file/extern_functions/test.expectations

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(extern_functions LANGUAGES C)
3+
add_executable(app a.c b.c)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <assert.h>
2+
3+
#include "header.h"
4+
5+
int main(void) {
6+
assert(helper(42) == 43);
7+
return 0;
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "header.h"
2+
3+
static int unrelated1(void) { return 1; }
4+
static int unrelated2(void) { return 2; }
5+
static int unrelated3(void) { return 3; }
6+
7+
int helper(int x) {
8+
(void)unrelated1();
9+
(void)unrelated2();
10+
(void)unrelated3();
11+
return x + 1;
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
int helper(int x);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
10+
// a.rs
11+
pub fn main() {
12+
std::process::exit(main_0());
13+
}
14+
fn main_0() -> i32 {
15+
assert!(
16+
(((({
17+
let _x: i32 = 42;
18+
helper_0(_x)
19+
}) == 43) as i32)
20+
!= 0)
21+
);
22+
return 0;
23+
}
24+
// b.rs
25+
pub fn unrelated1_1() -> i32 {
26+
return 1;
27+
}
28+
pub fn unrelated2_2() -> i32 {
29+
return 2;
30+
}
31+
pub fn unrelated3_3() -> i32 {
32+
return 3;
33+
}
34+
pub fn helper_0(x: i32) -> i32 {
35+
let x: Value<i32> = Rc::new(RefCell::new(x));
36+
({ unrelated1_1() });
37+
({ unrelated2_2() });
38+
({ unrelated3_3() });
39+
return ((*x.borrow()) + 1);
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
10+
// a.rs
11+
pub fn main() {
12+
unsafe {
13+
std::process::exit(main_0() as i32);
14+
}
15+
}
16+
unsafe fn main_0() -> i32 {
17+
assert!(
18+
((((unsafe {
19+
let _x: i32 = 42;
20+
helper_0(_x)
21+
}) == (43)) as i32)
22+
!= 0)
23+
);
24+
return 0;
25+
}
26+
// b.rs
27+
pub unsafe fn unrelated1_1() -> i32 {
28+
return 1;
29+
}
30+
pub unsafe fn unrelated2_2() -> i32 {
31+
return 2;
32+
}
33+
pub unsafe fn unrelated3_3() -> i32 {
34+
return 3;
35+
}
36+
pub unsafe fn helper_0(mut x: i32) -> i32 {
37+
&(unsafe { unrelated1_1() });
38+
&(unsafe { unrelated2_2() });
39+
&(unsafe { unrelated3_3() });
40+
return ((x) + (1));
41+
}

0 commit comments

Comments
 (0)