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
3 changes: 2 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,8 @@ bool Converter::VisitCompoundStmt(clang::CompoundStmt *stmt) {

bool Converter::VisitDeclStmt(clang::DeclStmt *stmt) {
for (auto *decl : stmt->decls()) {
if (clang::isa<clang::TagDecl>(decl)) {
auto *var = clang::dyn_cast<clang::VarDecl>(decl);
if (clang::isa<clang::TagDecl>(decl) || (var && var->isStaticLocal())) {
Buffer buf(*this);
Convert(decl);
hoisted_records_ += std::move(buf).str();
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/out/refcount/addr_of_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ fn main_0() -> i32 {
== 2)
);
assert!(({ (*obj_4.with(Value::clone).borrow()).p.clone() }.with(|__s| __s.value) == 42));
thread_local!(
static cache_5: Value<Box<[Ptr<Inner>]>> = Rc::new(RefCell::new(Box::new([
(alpha_0.with(|v| v.as_pointer())),
(beta_1.with(|v| v.as_pointer())),
])));
);
assert!(
(({
let __idx = (0) as usize;
Expand All @@ -85,6 +79,12 @@ fn main_0() -> i32 {
);
return 0;
}
thread_local!(
static cache_5: Value<Box<[Ptr<Inner>]>> = Rc::new(RefCell::new(Box::new([
(alpha_0.with(|v| v.as_pointer())),
(beta_1.with(|v| v.as_pointer())),
])));
);
pub fn __cpp2rust_init_globals() {
let _ = alpha_0.with(|_| ());
let _ = beta_1.with(|_| ());
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/out/refcount/default_in_statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ thread_local!(
])));
);
pub fn check_local_static_5() {
thread_local!(
static local_outer_6: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
);
thread_local!(
static local_fn_7: Value<FnPtr<fn(i32) -> i32>> =
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null()));
);
thread_local!(
static local_p_8: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
);
assert!(({ (*local_outer_6.with(Value::clone).borrow()).p1.clone() }).is_null());
assert!(({ (*local_outer_6.with(Value::clone).borrow()).fn_.clone() }).is_null());
assert!((*local_fn_7.with(Value::clone).borrow()).is_null());
assert!((*local_p_8.with(Value::clone).borrow()).is_null());
}
thread_local!(
static local_outer_6: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
);
thread_local!(
static local_fn_7: Value<FnPtr<fn(i32) -> i32>> =
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::null()));
);
thread_local!(
static local_p_8: Value<Ptr<i32>> = Rc::new(RefCell::new(Ptr::<i32>::null()));
);
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down
24 changes: 12 additions & 12 deletions tests/unit/out/refcount/global_non_const_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn next_0() -> i32 {
thread_local!(
static counter_1: Value<i32> = Rc::new(RefCell::new(0));
);
return (*counter_1.with(Value::clone).borrow_mut()).prefix_inc();
}
thread_local!(
static counter_1: Value<i32> = Rc::new(RefCell::new(0));
);
pub fn marker_2(mut tag: u8) -> u8 {
return ((((tag as i32) << 3) | 2) as u8);
}
Expand Down Expand Up @@ -74,14 +74,14 @@ thread_local!(
pub static member_10: Value<i32> = Rc::new(RefCell::new(({ next_0() })));
);
pub fn local_static_12() -> i32 {
thread_local!(
static once_13: Value<i32> = Rc::new(RefCell::new(({ next_0() })));
);
thread_local!(
static local_ctor_14: Value<Ctor> = Rc::new(RefCell::new(Ctor::new_2({ 3 })));
);
return (once_13.with(|rc| *rc.borrow()) + { (*local_ctor_14.with(Value::clone).borrow()).v });
}
thread_local!(
static once_13: Value<i32> = Rc::new(RefCell::new(({ next_0() })));
);
thread_local!(
static local_ctor_14: Value<Ctor> = Rc::new(RefCell::new(Ctor::new_2({ 3 })));
);
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
#[byte_size(4)]
pub struct Singleton {
Expand All @@ -93,9 +93,6 @@ impl Singleton {
Self { hits: 0 }
}
pub fn instance() -> Ptr<Singleton> {
thread_local!(
static s_15: Value<Singleton> = Rc::new(RefCell::new(Singleton::new()));
);
return s_15.with(|v| v.as_pointer());
}
}
Expand All @@ -104,6 +101,9 @@ impl Default for Singleton {
{ Singleton::new() }
}
}
thread_local!(
static s_15: Value<Singleton> = Rc::new(RefCell::new(Singleton::new()));
);
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/out/refcount/static_function_vars_same_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn a_0() -> i32 {
thread_local!(
static i_1: Value<i32> = Rc::new(RefCell::new(1));
);
return i_1.with(|rc| *rc.borrow());
}
thread_local!(
static i_1: Value<i32> = Rc::new(RefCell::new(1));
);
pub fn b_2() -> i32 {
thread_local!(
static i_3: Value<i32> = Rc::new(RefCell::new(2));
);
return i_3.with(|rc| *rc.borrow());
}
thread_local!(
static i_3: Value<i32> = Rc::new(RefCell::new(2));
);
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down
50 changes: 35 additions & 15 deletions tests/unit/out/refcount/static_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,51 @@ use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn foo_0() -> i32 {
thread_local!(
static static_i_1: Value<i32> = Rc::new(RefCell::new(0_i32));
);
thread_local!(
static static_f_2: Value<f32> = Rc::new(RefCell::new(0_f32));
);
thread_local!(
static static_b_3: Value<bool> = Rc::new(RefCell::new(false));
);
thread_local!(
static kX1_4: Value<i32> = Rc::new(RefCell::new(1));
);
thread_local!(
static kX2_5: Value<i32> = Rc::new(RefCell::new(2));
);
kX1_4.with(|rc| *rc.borrow_mut() += 1);
return ((kX1_4.with(|rc| *rc.borrow()) + kX2_5.with(|rc| *rc.borrow()))
+ static_i_1.with(|rc| *rc.borrow()));
}
thread_local!(
static static_i_1: Value<i32> = Rc::new(RefCell::new(0_i32));
);
thread_local!(
static static_f_2: Value<f32> = Rc::new(RefCell::new(0_f32));
);
thread_local!(
static static_b_3: Value<bool> = Rc::new(RefCell::new(false));
);
thread_local!(
static kX1_4: Value<i32> = Rc::new(RefCell::new(1));
);
thread_local!(
static kX2_5: Value<i32> = Rc::new(RefCell::new(2));
);
pub fn from_local_class_6() -> i32 {
return (({ S_8Impl::get(&Rc::new(RefCell::new(<S_8>::default())).as_pointer()) })
+ ({ S_8Impl::get(&Rc::new(RefCell::new(<S_8>::default())).as_pointer()) }));
}
thread_local!(
static x_7: Value<i32> = Rc::new(RefCell::new(3));
);
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct S_8 {}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!((((({ foo_0() }) + ({ foo_0() })) + ({ foo_0() })) == 15));
assert!((({ from_local_class_6() }) == 9));
assert!((({ from_local_class_6() }) == 13));
return 0;
}
pub trait S_8Impl {
fn get(&self) -> i32;
}
impl S_8Impl for Ptr<S_8> {
fn get(&self) -> i32 {
return (*x_7.with(Value::clone).borrow_mut()).prefix_inc();
}
}
pub fn __cpp2rust_init_globals() {}
90 changes: 45 additions & 45 deletions tests/unit/out/refcount/string_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,6 @@ fn main_0() -> i32 {
let mut special: Ptr<i8> = Ptr::<i8>::from_string_literal(
b"\x07\x08\t\n\x0b\x0c\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\xff",
);
thread_local!(
static expected_0: Value<Box<[i8]>> = Rc::new(RefCell::new(Box::new([
7_i8,
8_i8,
9_i8,
10_i8,
11_i8,
12_i8,
13_i8,
32_i8,
33_i8,
34_i8,
35_i8,
36_i8,
37_i8,
38_i8,
39_i8,
40_i8,
41_i8,
42_i8,
43_i8,
44_i8,
45_i8,
46_i8,
47_i8,
58_i8,
59_i8,
60_i8,
61_i8,
62_i8,
63_i8,
64_i8,
91_i8,
92_i8,
93_i8,
94_i8,
95_i8,
96_i8,
123_i8,
124_i8,
125_i8,
126_i8,
(b'\xff' as i8),
])));
);
let mut i: i32 = 0;
'loop_: while (i
< (((::std::mem::size_of::<[i8; 41]>() as usize)
Expand All @@ -76,4 +31,49 @@ fn main_0() -> i32 {
}
return 0;
}
thread_local!(
static expected_0: Value<Box<[i8]>> = Rc::new(RefCell::new(Box::new([
7_i8,
8_i8,
9_i8,
10_i8,
11_i8,
12_i8,
13_i8,
32_i8,
33_i8,
34_i8,
35_i8,
36_i8,
37_i8,
38_i8,
39_i8,
40_i8,
41_i8,
42_i8,
43_i8,
44_i8,
45_i8,
46_i8,
47_i8,
58_i8,
59_i8,
60_i8,
61_i8,
62_i8,
63_i8,
64_i8,
91_i8,
92_i8,
93_i8,
94_i8,
95_i8,
96_i8,
123_i8,
124_i8,
125_i8,
126_i8,
(b'\xff' as i8),
])));
);
pub fn __cpp2rust_init_globals() {}
14 changes: 7 additions & 7 deletions tests/unit/out/refcount/union_tagged_struct_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
thread_local!(
static items_4: Value<Box<[Ptr<i8>]>> = Rc::new(RefCell::new(Box::new([
Ptr::<i8>::from_string_literal(b"a"),
Ptr::<i8>::from_string_literal(b"b"),
Ptr::<i8>::from_string_literal(b"c"),
])));
);
let p_list: Value<Branch> = <Value<Branch>>::default();
(*p_list.borrow_mut()).choice = Choice_enum_C_LIST;
(*p_list.borrow_mut()).index = 0;
Expand Down Expand Up @@ -165,4 +158,11 @@ fn main_0() -> i32 {
);
return 0;
}
thread_local!(
static items_4: Value<Box<[Ptr<i8>]>> = Rc::new(RefCell::new(Box::new([
Ptr::<i8>::from_string_literal(b"a"),
Ptr::<i8>::from_string_literal(b"b"),
Ptr::<i8>::from_string_literal(b"c"),
])));
);
pub fn __cpp2rust_init_globals() {}
13 changes: 6 additions & 7 deletions tests/unit/out/unsafe/addr_of_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ unsafe fn main_0() -> i32 {
(((*(*std::cell::LazyCell::force_mut(&mut *&raw mut items_3))[(1) as usize]).value) == (2))
);
assert!((((*(*std::cell::LazyCell::force_mut(&mut *&raw mut obj_4)).p).value) == (42)));
static mut cache_5: std::cell::LazyCell<[*mut Inner; 2]> =
std::cell::LazyCell::new(|| unsafe {
[
(&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut alpha_0)) as *mut Inner),
(&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut beta_1)) as *mut Inner),
]
});;
assert!(
(((*(*std::cell::LazyCell::force_mut(&mut *&raw mut cache_5))[(0) as usize]).value) == (1))
);
Expand All @@ -63,6 +56,12 @@ unsafe fn main_0() -> i32 {
);
return 0;
}
static mut cache_5: std::cell::LazyCell<[*mut Inner; 2]> = std::cell::LazyCell::new(|| unsafe {
[
(&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut alpha_0)) as *mut Inner),
(&raw mut (*std::cell::LazyCell::force_mut(&mut *&raw mut beta_1)) as *mut Inner),
]
});
pub unsafe fn __cpp2rust_init_globals() {
std::cell::LazyCell::force(&*&raw const alpha_0);
std::cell::LazyCell::force(&*&raw const beta_1);
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/out/unsafe/default_in_statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ pub static mut static_foo_array_4: std::cell::LazyCell<[Foo; 2]> =
]
});
pub unsafe fn check_local_static_5() {
static mut local_outer_6: std::cell::LazyCell<Outer> =
std::cell::LazyCell::new(|| unsafe { <Outer>::default() });;
static mut local_fn_7: std::cell::LazyCell<Option<unsafe fn(i32) -> i32>> =
std::cell::LazyCell::new(|| unsafe { None });;
static mut local_p_8: std::cell::LazyCell<*mut i32> =
std::cell::LazyCell::new(|| unsafe { std::ptr::null_mut() });;
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut local_outer_6)).p1).is_null());
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut local_outer_6)).fn_).is_none());
assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut local_fn_7)).is_none());
assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut local_p_8)).is_null());
}
static mut local_outer_6: std::cell::LazyCell<Outer> =
std::cell::LazyCell::new(|| unsafe { <Outer>::default() });
static mut local_fn_7: std::cell::LazyCell<Option<unsafe fn(i32) -> i32>> =
std::cell::LazyCell::new(|| unsafe { None });
static mut local_p_8: std::cell::LazyCell<*mut i32> =
std::cell::LazyCell::new(|| unsafe { std::ptr::null_mut() });
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand Down
Loading
Loading