Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<clang::FunctionDecl>(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<std::string, size_t> type_mapping;
Expand All @@ -368,7 +373,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
if (!clang::isa<clang::CXXMethodDecl>(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 += '_';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32> = Rc::new(RefCell::new(x));
({ unrelated1_1() });
({ unrelated2_2() });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ pub unsafe fn unrelated2_2() -> i32 {
pub unsafe fn unrelated3_3() -> i32 {
return 3;
}
pub unsafe fn helper_4(mut x: i32) -> i32 {
(unsafe { unrelated1_1() });
(unsafe { unrelated2_2() });
(unsafe { unrelated3_3() });
pub unsafe fn helper_0(mut x: i32) -> i32 {
&(unsafe { unrelated1_1() });
&(unsafe { unrelated2_2() });
&(unsafe { unrelated3_3() });
return ((x) + (1));
}
1 change: 0 additions & 1 deletion tests/multi-file/extern_functions/test.expectations

This file was deleted.

3 changes: 3 additions & 0 deletions tests/multi-file/header_function/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.16)
project(extern_functions LANGUAGES C)
add_executable(app a.c b.c)
8 changes: 8 additions & 0 deletions tests/multi-file/header_function/a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <assert.h>

#include "header.h"

int main(void) {
assert(helper(42) == 43);
return 0;
}
12 changes: 12 additions & 0 deletions tests/multi-file/header_function/b.c
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions tests/multi-file/header_function/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

int helper(int x);
40 changes: 40 additions & 0 deletions tests/multi-file/header_function/out/refcount/header_function.rs
Original file line number Diff line number Diff line change
@@ -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<i32> = Rc::new(RefCell::new(x));
({ unrelated1_1() });
({ unrelated2_2() });
({ unrelated3_3() });
return ((*x.borrow()) + 1);
}
41 changes: 41 additions & 0 deletions tests/multi-file/header_function/out/unsafe/header_function.rs
Original file line number Diff line number Diff line change
@@ -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));
}
Loading