Skip to content
Open
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
17 changes: 13 additions & 4 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,17 @@ void Converter::ConvertVaListVarDecl(clang::VarDecl *decl) {
StrCat(keyword_mut_, GetNamedDeclAsString(decl), token::kColon, "VaList");
}

bool Converter::IsConvertableGlobalVarDecl(clang::VarDecl *decl) {
if (!decl->isFileVarDecl()) {
return true;
}
if (decl->isThisDeclarationADefinition() == clang::VarDecl::DeclarationOnly &&
!decl->hasInit()) {
return false;
}
return !globals_.contains(GetNamedDeclAsString(decl));
}

bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
auto qual_type = decl->getType();
auto name = GetNamedDeclAsString(decl);
Expand All @@ -467,12 +478,10 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
}

if (decl->isFileVarDecl()) {
if ((decl->isThisDeclarationADefinition() ==
clang::VarDecl::DeclarationOnly &&
!decl->hasInit()) ||
!globals_.insert(name).second) {
if (!IsConvertableGlobalVarDecl(decl)) {
return false;
}
globals_.insert(name);
StrCat(AccessSpecifierAsString(decl->getAccess()), keyword::kStatic,
keyword_mut_);
ENSURE(decl_ids_.insert(GetID(decl)).second);
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual void ConvertGlobalVarDecl(clang::VarDecl *decl);

bool IsConvertableGlobalVarDecl(clang::VarDecl *decl);

virtual void ConvertVaListVarDecl(clang::VarDecl *decl);

virtual bool ConvertVarDeclSkipInit(clang::VarDecl *decl);
Expand Down
3 changes: 3 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,9 @@ void ConverterRefCount::EmitHoistedInArmAssignment(clang::VarDecl *decl) {
}

void ConverterRefCount::ConvertGlobalVarDecl(clang::VarDecl *decl) {
if (!IsConvertableGlobalVarDecl(decl)) {
return;
}
StrCat("thread_local!");
{
PushParen paren(*this);
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/extern_global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extern int x;
int x;

int main(void) { return x; }
17 changes: 17 additions & 0 deletions tests/unit/out/refcount/extern_global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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};
thread_local!(
pub static x_0: Value<i32> = <Value<i32>>::default();
);
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
return (*x_0.with(Value::clone).borrow());
}
17 changes: 17 additions & 0 deletions tests/unit/out/unsafe/extern_global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 static mut x_0: i32 = unsafe { 0_i32 };
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
return x_0;
}
Loading