Skip to content

Commit c0195b8

Browse files
authored
Translate globals without inits (#73)
1 parent 6a7b502 commit c0195b8

5 files changed

Lines changed: 89 additions & 4 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
381381

382382
if (decl->isFileVarDecl()) {
383383
name = ReplaceAll(Mapper::ToString(decl), "::", "_");
384-
if ((decl->isExternallyDeclarable() && !decl->hasInit()) ||
384+
if (decl->isThisDeclarationADefinition() ==
385+
clang::VarDecl::DeclarationOnly ||
385386
!globals_.insert(name).second) {
386387
return false;
387388
}

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,8 +1635,8 @@ std::string ConverterRefCount::GetDefaultAsString(clang::QualType qual_type) {
16351635
if (pointee_type->isVoidType()) {
16361636
ret = "AnyPtr::default()";
16371637
} else {
1638-
ret = std::format("Ptr::<{}>::null()",
1639-
GetUnsafeTypeAsString(pointee_type));
1638+
PushConversionKind push(*this, ConversionKind::Unboxed);
1639+
ret = std::format("Ptr::<{}>::null()", ConvertPointeeType(qual_type));
16401640
}
16411641
}
16421642
} else if (auto *array_type =
@@ -2240,14 +2240,14 @@ std::string ConverterRefCount::ConvertMappedMethodCall(
22402240
}
22412241

22422242
std::string ConverterRefCount::ConvertPointeeType(clang::QualType ptr_type) {
2243+
assert(!ptr_type.isNull() && ptr_type->isPointerType());
22432244
if (ptr_type->getPointeeType()->isIntegerType()) {
22442245
return ToString(ptr_type->getPointeeType());
22452246
}
22462247

22472248
// Pointee of a pointer to incomplete type is an incomplete type that does
22482249
// not have a translation rule. Hence ToString(ptr_type->getPointeeType()) is
22492250
// not enough
2250-
assert(ptr_type->isPointerType());
22512251
auto str = ToString(ptr_type);
22522252
Unwrap(str, "Ptr<", ">");
22532253
return str;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
4+
struct S {
5+
int a;
6+
};
7+
8+
S *s;
9+
FILE *file;
10+
size_t size;
11+
12+
int main() {
13+
assert(s == nullptr);
14+
assert(file == nullptr);
15+
assert(size == 0);
16+
return 0;
17+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#[derive(Default)]
10+
pub struct S {
11+
pub a: Value<i32>,
12+
}
13+
impl Clone for S {
14+
fn clone(&self) -> Self {
15+
let mut this = Self {
16+
a: Rc::new(RefCell::new((*self.a.borrow()))),
17+
};
18+
this
19+
}
20+
}
21+
impl ByteRepr for S {}
22+
thread_local!(
23+
pub static s: Value<Ptr<S>> = Rc::new(RefCell::new(Ptr::<S>::null()));
24+
);
25+
thread_local!(
26+
pub static file: Value<Ptr<::std::fs::File>> =
27+
Rc::new(RefCell::new(Ptr::<::std::fs::File>::null()));
28+
);
29+
thread_local!(
30+
pub static size: Value<u64> = <Value<u64>>::default();
31+
);
32+
pub fn main() {
33+
std::process::exit(main_0());
34+
}
35+
fn main_0() -> i32 {
36+
assert!((*s.with(Value::clone).borrow()).is_null());
37+
assert!((*file.with(Value::clone).borrow()).is_null());
38+
assert!(((*size.with(Value::clone).borrow()) == 0_u64));
39+
return 0;
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#[repr(C)]
10+
#[derive(Copy, Clone, Default)]
11+
pub struct S {
12+
pub a: i32,
13+
}
14+
pub static mut s: *mut S = std::ptr::null_mut();
15+
pub static mut file: *mut ::std::fs::File = std::ptr::null_mut();
16+
pub static mut size: u64 = 0_u64;
17+
pub fn main() {
18+
unsafe {
19+
std::process::exit(main_0() as i32);
20+
}
21+
}
22+
unsafe fn main_0() -> i32 {
23+
assert!((s).is_null());
24+
assert!((file).is_null());
25+
assert!(((size) == (0_u64)));
26+
return 0;
27+
}

0 commit comments

Comments
 (0)