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
11 changes: 3 additions & 8 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/global_pointers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <cassert>
#include <cstddef>

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;
}
66 changes: 66 additions & 0 deletions tests/unit/out/refcount/global_pointers.rs
Original file line number Diff line number Diff line change
@@ -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<Ptr<u8>>,
pub p: Value<Ptr<i32>>,
}
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<Entry> = 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<Box<[Entry]>> = 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<Box<[Ptr<u8>]>> = Rc::new(RefCell::new(Box::new([
Ptr::<u8>::null(),
Ptr::<u8>::null(),
Ptr::<u8>::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<i32> = 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;
}
48 changes: 48 additions & 0 deletions tests/unit/out/unsafe/global_pointers.rs
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/static_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/string_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading