Skip to content

Commit 3d497fc

Browse files
authored
Emit static variables declared inside record (#108)
1 parent 91fc4be commit 3d497fc

4 files changed

Lines changed: 100 additions & 2 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,9 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
382382

383383
if (decl->isFileVarDecl()) {
384384
name = ReplaceAll(Mapper::ToString(decl), "::", "_");
385-
if (decl->isThisDeclarationADefinition() ==
386-
clang::VarDecl::DeclarationOnly ||
385+
if ((decl->isThisDeclarationADefinition() ==
386+
clang::VarDecl::DeclarationOnly &&
387+
!decl->hasInit()) ||
387388
!globals_.insert(name).second) {
388389
return false;
389390
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
thread_local!(
10+
static C_inner_const: Value<i32> = Rc::new(RefCell::new(1));
11+
);
12+
#[derive(Default)]
13+
pub struct C {}
14+
impl C {
15+
pub fn get(&self) -> i32 {
16+
return (*C_inner_const.with(Value::clone).borrow());
17+
}
18+
}
19+
impl Clone for C {
20+
fn clone(&self) -> Self {
21+
let mut this = Self {};
22+
this
23+
}
24+
}
25+
impl ByteRepr for C {}
26+
thread_local!(
27+
pub static S_inner_const: Value<i32> = Rc::new(RefCell::new(2));
28+
);
29+
#[derive(Default)]
30+
pub struct S {}
31+
impl Clone for S {
32+
fn clone(&self) -> Self {
33+
let mut this = Self {};
34+
this
35+
}
36+
}
37+
impl ByteRepr for S {}
38+
pub fn main() {
39+
std::process::exit(main_0());
40+
}
41+
fn main_0() -> i32 {
42+
let c: Value<C> = Rc::new(RefCell::new(<C>::default()));
43+
assert!((({ (*c.borrow()).get() }) == 1));
44+
assert!(((*S_inner_const.with(Value::clone).borrow()) == 2));
45+
return 0;
46+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
static mut C_inner_const: i32 = 1;
10+
#[repr(C)]
11+
#[derive(Copy, Clone, Default)]
12+
pub struct C {}
13+
impl C {
14+
pub unsafe fn get(&mut self) -> i32 {
15+
return C_inner_const;
16+
}
17+
}
18+
pub static mut S_inner_const: i32 = 2;
19+
#[repr(C)]
20+
#[derive(Copy, Clone, Default)]
21+
pub struct S {}
22+
pub fn main() {
23+
unsafe {
24+
std::process::exit(main_0() as i32);
25+
}
26+
}
27+
unsafe fn main_0() -> i32 {
28+
let mut c: C = <C>::default();
29+
assert!(((unsafe { c.get() }) == (1)));
30+
assert!(((S_inner_const) == (2)));
31+
return 0;
32+
}

tests/unit/static_var_in_class.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <assert.h>
2+
3+
class C {
4+
static const int inner_const = 1;
5+
6+
public:
7+
int get() { return inner_const; }
8+
};
9+
10+
struct S {
11+
static const int inner_const = 2;
12+
};
13+
14+
int main() {
15+
C c;
16+
assert(c.get() == 1);
17+
assert(S::inner_const == 2);
18+
return 0;
19+
}

0 commit comments

Comments
 (0)