diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 67e8e705..bd1de66c 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -385,16 +385,11 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) { !globals_.insert(name).second) { return false; } - StrCat(AccessSpecifierAsString(decl->getAccess()), keyword::kStatic); - if (!qual_type.isConstQualified()) { - StrCat(keyword_mut_); - } + StrCat(AccessSpecifierAsString(decl->getAccess()), keyword::kStatic, + keyword_mut_); ENSURE(decl_ids_.insert(GetID(decl)).second); } else if (decl->isStaticLocal()) { - StrCat(keyword::kStatic); - if (!qual_type.isConstQualified()) { - StrCat(keyword_mut_); - } + StrCat(keyword::kStatic, keyword_mut_); } else if (decl->isLocalVarDecl()) { StrCat(keyword::kLet); } diff --git a/tests/unit/global_pointers.cpp b/tests/unit/global_pointers.cpp new file mode 100644 index 00000000..f787ecbc --- /dev/null +++ b/tests/unit/global_pointers.cpp @@ -0,0 +1,25 @@ +#include +#include + +struct Entry { + const char *name; + int *p; +}; + +static const Entry single_entry = {"alone", nullptr}; + +static const Entry entries[2] = { + {"first", nullptr}, + {"second", nullptr}, +}; + +char *arr_of_pointers[3] = {}; + +int main() { + assert(single_entry.p == nullptr); + for (int i = 0; i < 2; ++i) { + assert(entries[i].p == nullptr); + assert(arr_of_pointers[i] == nullptr); + } + return 0; +} diff --git a/tests/unit/out/refcount/global_pointers.rs b/tests/unit/out/refcount/global_pointers.rs new file mode 100644 index 00000000..9f6f3e50 --- /dev/null +++ b/tests/unit/out/refcount/global_pointers.rs @@ -0,0 +1,66 @@ +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}; +#[derive(Default)] +pub struct Entry { + pub name: Value>, + pub p: Value>, +} +impl Clone for Entry { + fn clone(&self) -> Self { + let mut this = Self { + name: Rc::new(RefCell::new((*self.name.borrow()).clone())), + p: Rc::new(RefCell::new((*self.p.borrow()).clone())), + }; + this + } +} +impl ByteRepr for Entry {} +thread_local!( + pub static single_entry: Value = Rc::new(RefCell::new(Entry { + name: Rc::new(RefCell::new(Ptr::from_string_literal("alone"))), + p: Rc::new(RefCell::new(Default::default())), + })); +); +thread_local!( + pub static entries: Value> = Rc::new(RefCell::new(Box::new([ + Entry { + name: Rc::new(RefCell::new(Ptr::from_string_literal("first"))), + p: Rc::new(RefCell::new(Default::default())), + }, + Entry { + name: Rc::new(RefCell::new(Ptr::from_string_literal("second"))), + p: Rc::new(RefCell::new(Default::default())), + }, + ]))); +); +thread_local!( + pub static arr_of_pointers: Value]>> = Rc::new(RefCell::new(Box::new([ + Ptr::::null(), + Ptr::::null(), + Ptr::::null(), + ]))); +); +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!((*(*single_entry.with(Value::clone).borrow()).p.borrow()).is_null()); + let i: Value = Rc::new(RefCell::new(0)); + 'loop_: while ((*i.borrow()) < 2) { + assert!( + (*(*entries.with(Value::clone).borrow())[(*i.borrow()) as usize] + .p + .borrow()) + .is_null() + ); + assert!(((*arr_of_pointers.with(Value::clone).borrow())[(*i.borrow()) as usize]).is_null()); + (*i.borrow_mut()).prefix_inc(); + } + return 0; +} diff --git a/tests/unit/out/unsafe/global_pointers.rs b/tests/unit/out/unsafe/global_pointers.rs new file mode 100644 index 00000000..e4d2d303 --- /dev/null +++ b/tests/unit/out/unsafe/global_pointers.rs @@ -0,0 +1,48 @@ +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; +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Entry { + pub name: *const u8, + pub p: *mut i32, +} +pub static mut single_entry: Entry = Entry { + name: b"alone\0".as_ptr(), + p: std::ptr::null_mut(), +}; +pub static mut entries: [Entry; 2] = [ + Entry { + name: b"first\0".as_ptr(), + p: std::ptr::null_mut(), + }, + Entry { + name: b"second\0".as_ptr(), + p: std::ptr::null_mut(), + }, +]; +pub static mut arr_of_pointers: [*mut u8; 3] = [ + std::ptr::null_mut(), + std::ptr::null_mut(), + std::ptr::null_mut(), +]; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!((single_entry.p).is_null()); + let mut i: i32 = 0; + 'loop_: while ((i) < (2)) { + assert!((entries[(i) as usize].p).is_null()); + assert!((arr_of_pointers[(i) as usize]).is_null()); + i.prefix_inc(); + } + return 0; +} diff --git a/tests/unit/out/unsafe/static_local.rs b/tests/unit/out/unsafe/static_local.rs index af318676..022ee582 100644 --- a/tests/unit/out/unsafe/static_local.rs +++ b/tests/unit/out/unsafe/static_local.rs @@ -11,7 +11,7 @@ pub unsafe fn foo_0() -> i32 { static mut static_f: f32 = 0.0_f32;; static mut static_b: bool = false;; static mut kX1: i32 = 1;; - static kX2: i32 = 2;; + static mut kX2: i32 = 2;; kX1 += 1; return (((kX1) + (kX2)) + (static_i)); } diff --git a/tests/unit/out/unsafe/string_escape.rs b/tests/unit/out/unsafe/string_escape.rs index 285f6dde..d04d2c0b 100644 --- a/tests/unit/out/unsafe/string_escape.rs +++ b/tests/unit/out/unsafe/string_escape.rs @@ -14,7 +14,7 @@ pub fn main() { unsafe fn main_0() -> i32 { let mut special: *const u8 = b"\x07\x08\t\n\x0b\x0c\r !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\0".as_ptr(); - static expected: [u8; 40] = [ + static mut expected: [u8; 40] = [ 7_u8, 8_u8, 9_u8, 10_u8, 11_u8, 12_u8, 13_u8, 32_u8, 33_u8, 34_u8, 35_u8, 36_u8, 37_u8, 38_u8, 39_u8, 40_u8, 41_u8, 42_u8, 43_u8, 44_u8, 45_u8, 46_u8, 47_u8, 58_u8, 59_u8, 60_u8, 61_u8, 62_u8, 63_u8, 64_u8, 91_u8, 92_u8, 93_u8, 94_u8, 95_u8, 96_u8, 123_u8, 124_u8,