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
5 changes: 3 additions & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,9 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {

if (decl->isFileVarDecl()) {
name = ReplaceAll(Mapper::ToString(decl), "::", "_");
if (decl->isThisDeclarationADefinition() ==
clang::VarDecl::DeclarationOnly ||
if ((decl->isThisDeclarationADefinition() ==
clang::VarDecl::DeclarationOnly &&
!decl->hasInit()) ||
!globals_.insert(name).second) {
return false;
}
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/out/refcount/static_var_in_class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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!(
static C_inner_const: Value<i32> = Rc::new(RefCell::new(1));
);
#[derive(Default)]
pub struct C {}
impl C {
pub fn get(&self) -> i32 {
return (*C_inner_const.with(Value::clone).borrow());
}
}
impl Clone for C {
fn clone(&self) -> Self {
let mut this = Self {};
this
}
}
impl ByteRepr for C {}
thread_local!(
pub static S_inner_const: Value<i32> = Rc::new(RefCell::new(2));
);
#[derive(Default)]
pub struct S {}
impl Clone for S {
fn clone(&self) -> Self {
let mut this = Self {};
this
}
}
impl ByteRepr for S {}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let c: Value<C> = Rc::new(RefCell::new(<C>::default()));
assert!((({ (*c.borrow()).get() }) == 1));
assert!(((*S_inner_const.with(Value::clone).borrow()) == 2));
return 0;
}
32 changes: 32 additions & 0 deletions tests/unit/out/unsafe/static_var_in_class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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;
static mut C_inner_const: i32 = 1;
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct C {}
impl C {
pub unsafe fn get(&mut self) -> i32 {
return C_inner_const;
}
}
pub static mut S_inner_const: i32 = 2;
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct S {}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut c: C = <C>::default();
assert!(((unsafe { c.get() }) == (1)));
assert!(((S_inner_const) == (2)));
return 0;
}
19 changes: 19 additions & 0 deletions tests/unit/static_var_in_class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <assert.h>

class C {
static const int inner_const = 1;

public:
int get() { return inner_const; }
};

struct S {
static const int inner_const = 2;
};

int main() {
C c;
assert(c.get() == 1);
assert(S::inner_const == 2);
return 0;
}
Loading