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
31 changes: 23 additions & 8 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,25 +437,40 @@ bool Converter::ConvertLambdaVarDecl(clang::VarDecl *decl) {
return false;
}

void Converter::ConvertVarDeclInitializer(clang::VarDecl *decl) {
if (decl->hasInit()) {
ConvertVarInit(decl->getType(), decl->getInit());
} else if (!clang::isa<clang::ParmVarDecl>(decl)) {
StrCat(ConvertVarDefaultInit(decl->getType()));
}
}

void Converter::ConvertVarDecl(clang::VarDecl *decl) {
if (!ConvertVarDeclSkipInit(decl)) {
// Skip global variables declared extern
return;
}
auto qual_type = decl->getType();
PushConstInitializer static_init(*this, decl->isFileVarDecl() ||
decl->isStaticLocal());
if (decl->hasInit()) {
StrCat(token::kAssign);
ConvertVarInit(qual_type, decl->getInit());
} else if (!clang::isa<clang::ParmVarDecl>(decl)) {
StrCat(token::kAssign, ConvertVarDefaultInit(qual_type));
}
StrCat(token::kAssign);
ConvertVarDeclInitializer(decl);
StrCat(token::kSemiColon);
}

void Converter::ConvertGlobalVarDecl(clang::VarDecl *decl) {
ConvertVarDecl(decl);
if (!ConvertVarDeclSkipInit(decl)) {
// Skip global variables declared extern
return;
}
PushConstInitializer static_init(*this, decl->isFileVarDecl() ||
decl->isStaticLocal());
StrCat(token::kAssign);
StrCat(keyword_unsafe_);
{
PushBrace push(*this);
ConvertVarDeclInitializer(decl);
}
StrCat(token::kSemiColon);
}

bool Converter::VisitVarDecl(clang::VarDecl *decl) {
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

void ConvertVarDecl(clang::VarDecl *decl);

void ConvertVarDeclInitializer(clang::VarDecl *decl);

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

virtual void ConvertVaListVarDecl(clang::VarDecl *decl);
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/global-initialization-using-global.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <assert.h>

int first;
int second = first + 1;

int main() {
assert(first == 0);
assert(second == first + 1);
return 0;
}
23 changes: 23 additions & 0 deletions tests/unit/out/refcount/global-initialization-using-global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 first: Value<i32> = <Value<i32>>::default();
);
thread_local!(
pub static second: Value<i32> =
Rc::new(RefCell::new(((*first.with(Value::clone).borrow()) + 1)));
);
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!(((*first.with(Value::clone).borrow()) == 0));
assert!(((*second.with(Value::clone).borrow()) == ((*first.with(Value::clone).borrow()) + 1)));
return 0;
}
32 changes: 19 additions & 13 deletions tests/unit/out/unsafe/addr_of_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ pub struct Inner {
pub struct Outer {
pub p: *mut Inner,
}
pub static mut alpha: Inner = Inner { value: 1 };
pub static mut beta: Inner = Inner { value: 2 };
pub static mut shared: Inner = Inner { value: 42 };
pub static mut items: [*mut Inner; 2] = [
(&raw mut alpha as *mut Inner),
(&raw mut beta as *mut Inner),
];
pub static mut obj: Outer = Outer {
p: (&raw mut shared as *mut Inner),
pub static mut alpha: Inner = unsafe { Inner { value: 1 } };
pub static mut beta: Inner = unsafe { Inner { value: 2 } };
pub static mut shared: Inner = unsafe { Inner { value: 42 } };
pub static mut items: [*mut Inner; 2] = unsafe {
[
(&raw mut alpha as *mut Inner),
(&raw mut beta as *mut Inner),
]
};
pub static mut obj: Outer = unsafe {
Outer {
p: (&raw mut shared as *mut Inner),
}
};
pub fn main() {
unsafe {
Expand All @@ -35,10 +39,12 @@ unsafe fn main_0() -> i32 {
assert!((((*items[(0) as usize]).value) == (1)));
assert!((((*items[(1) as usize]).value) == (2)));
assert!((((*obj.p).value) == (42)));
static mut cache: [*mut Inner; 2] = [
(&raw mut alpha as *mut Inner),
(&raw mut beta as *mut Inner),
];;
static mut cache: [*mut Inner; 2] = unsafe {
[
(&raw mut alpha as *mut Inner),
(&raw mut beta as *mut Inner),
]
};;
assert!((((*cache[(0) as usize]).value) == (1)));
assert!((((*cache[(1) as usize]).value) == (2)));
return 0;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/bool_condition_logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl From<i32> for Code {
}
}
}
pub static mut side_effect: i32 = 0;
pub static mut side_effect: i32 = unsafe { 0 };
pub unsafe fn observe_0(mut v: i32) -> i32 {
side_effect.prefix_inc();
return v;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/bool_condition_logical_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl From<i32> for Code {
}
}
}
pub static mut side_effect: i32 = 0;
pub static mut side_effect: i32 = unsafe { 0 };
pub unsafe fn observe_0(mut v: i32) -> i32 {
side_effect.prefix_inc();
return v;
Expand Down
100 changes: 55 additions & 45 deletions tests/unit/out/unsafe/default_in_statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,49 +58,9 @@ impl Default for Foo {
}
}
}
pub static mut static_fn: Option<unsafe fn(i32) -> i32> = None;
pub static mut static_outer: Outer = Outer {
p1: std::ptr::null_mut(),
p2: std::ptr::null(),
arr: [std::ptr::null_mut(); 3],
cp: std::ptr::null(),
pp: std::ptr::null_mut(),
inner: Inner {
v: 0_i32,
name: std::ptr::null(),
},
x: 0_i32,
fn_: None,
};
pub static mut static_inner_array: [Inner; 2] = [Inner {
v: 0_i32,
name: std::ptr::null(),
}; 2];
pub static mut static_foo: Foo = Foo {
s1: b"hello\0".as_ptr(),
s2: std::ptr::null(),
fn1: None,
fn2: None,
n: 42,
};
pub static mut static_foo_array: [Foo; 2] = [
Foo {
s1: b"first\0".as_ptr(),
s2: std::ptr::null(),
fn1: None,
fn2: None,
n: 1,
},
Foo {
s1: b"second\0".as_ptr(),
s2: std::ptr::null(),
fn1: None,
fn2: None,
n: 2,
},
];
pub unsafe fn check_local_static_0() {
static mut local_outer: Outer = Outer {
pub static mut static_fn: Option<unsafe fn(i32) -> i32> = unsafe { None };
pub static mut static_outer: Outer = unsafe {
Outer {
p1: std::ptr::null_mut(),
p2: std::ptr::null(),
arr: [std::ptr::null_mut(); 3],
Expand All @@ -112,9 +72,59 @@ pub unsafe fn check_local_static_0() {
},
x: 0_i32,
fn_: None,
}
};
pub static mut static_inner_array: [Inner; 2] = unsafe {
[Inner {
v: 0_i32,
name: std::ptr::null(),
}; 2]
};
pub static mut static_foo: Foo = unsafe {
Foo {
s1: b"hello\0".as_ptr(),
s2: std::ptr::null(),
fn1: None,
fn2: None,
n: 42,
}
};
pub static mut static_foo_array: [Foo; 2] = unsafe {
[
Foo {
s1: b"first\0".as_ptr(),
s2: std::ptr::null(),
fn1: None,
fn2: None,
n: 1,
},
Foo {
s1: b"second\0".as_ptr(),
s2: std::ptr::null(),
fn1: None,
fn2: None,
n: 2,
},
]
};
pub unsafe fn check_local_static_0() {
static mut local_outer: Outer = unsafe {
Outer {
p1: std::ptr::null_mut(),
p2: std::ptr::null(),
arr: [std::ptr::null_mut(); 3],
cp: std::ptr::null(),
pp: std::ptr::null_mut(),
inner: Inner {
v: 0_i32,
name: std::ptr::null(),
},
x: 0_i32,
fn_: None,
}
};;
static mut local_fn: Option<unsafe fn(i32) -> i32> = None;;
static mut local_p: *mut i32 = std::ptr::null_mut();;
static mut local_fn: Option<unsafe fn(i32) -> i32> = unsafe { None };;
static mut local_p: *mut i32 = unsafe { std::ptr::null_mut() };;
assert!((local_outer.p1).is_null());
assert!((local_outer.fn_).is_none());
assert!((local_fn).is_none());
Expand Down
42 changes: 22 additions & 20 deletions tests/unit/out/unsafe/enum_int_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,28 @@ pub struct Entry {
pub color: Color,
pub opt: Option,
}
pub static mut global_color: Color = Color::GREEN;
pub static mut global_opt: Option = Option::OPT_B;
pub static mut global_tag: Tag = Tag::TAG_TWO;
pub static mut entries: [Entry; 3] = [
Entry {
name: b"first\0".as_ptr(),
color: Color::RED,
opt: Option::OPT_NONE,
},
Entry {
name: b"second\0".as_ptr(),
color: Color::GREEN,
opt: Option::OPT_A,
},
Entry {
name: b"third\0".as_ptr(),
color: Color::BLUE,
opt: Option::OPT_C,
},
];
pub static mut global_color: Color = unsafe { Color::GREEN };
pub static mut global_opt: Option = unsafe { Option::OPT_B };
pub static mut global_tag: Tag = unsafe { Tag::TAG_TWO };
pub static mut entries: [Entry; 3] = unsafe {
[
Entry {
name: b"first\0".as_ptr(),
color: Color::RED,
opt: Option::OPT_NONE,
},
Entry {
name: b"second\0".as_ptr(),
color: Color::GREEN,
opt: Option::OPT_A,
},
Entry {
name: b"third\0".as_ptr(),
color: Color::BLUE,
opt: Option::OPT_C,
},
]
};
pub unsafe fn as_int_0(mut c: Color) -> i32 {
return (c as i32);
}
Expand Down
42 changes: 22 additions & 20 deletions tests/unit/out/unsafe/enum_int_interop_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,28 @@ pub struct Entry {
pub color: Color,
pub opt: Option,
}
pub static mut global_color: Color = Color::GREEN;
pub static mut global_opt: Option = Option::OPT_B;
pub static mut global_tag: Tag = Tag::TAG_TWO;
pub static mut entries: [Entry; 3] = [
Entry {
name: b"first\0".as_ptr().cast_mut().cast_const(),
color: Color::RED,
opt: Option::OPT_NONE,
},
Entry {
name: b"second\0".as_ptr().cast_mut().cast_const(),
color: Color::GREEN,
opt: Option::OPT_A,
},
Entry {
name: b"third\0".as_ptr().cast_mut().cast_const(),
color: Color::BLUE,
opt: Option::OPT_C,
},
];
pub static mut global_color: Color = unsafe { Color::GREEN };
pub static mut global_opt: Option = unsafe { Option::OPT_B };
pub static mut global_tag: Tag = unsafe { Tag::TAG_TWO };
pub static mut entries: [Entry; 3] = unsafe {
[
Entry {
name: b"first\0".as_ptr().cast_mut().cast_const(),
color: Color::RED,
opt: Option::OPT_NONE,
},
Entry {
name: b"second\0".as_ptr().cast_mut().cast_const(),
color: Color::GREEN,
opt: Option::OPT_A,
},
Entry {
name: b"third\0".as_ptr().cast_mut().cast_const(),
color: Color::BLUE,
opt: Option::OPT_C,
},
]
};
pub unsafe fn as_int_0(mut c: Color) -> i32 {
return (c as i32);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/fn_ptr_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub unsafe fn double_it_0(mut x: i32) -> i32 {
pub unsafe fn triple_it_1(mut x: i32) -> i32 {
return ((x) * (3));
}
pub static mut g_op: Option<unsafe fn(i32) -> i32> = None;
pub static mut g_op: Option<unsafe fn(i32) -> i32> = unsafe { None };
pub unsafe fn set_op_2(mut fn_: Option<unsafe fn(i32) -> i32>) {
g_op = fn_;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/fn_ptr_vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Default for Vtable {
}
}
}
pub static mut storage: i32 = 0_i32;
pub static mut storage: i32 = unsafe { 0_i32 };
pub unsafe fn int_create_0(mut val: i32) -> *mut ::libc::c_void {
storage = val;
return ((&raw mut storage as *mut i32) as *mut i32 as *mut ::libc::c_void);
Expand Down
Loading
Loading