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 @@ -381,7 +381,8 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {

if (decl->isFileVarDecl()) {
name = ReplaceAll(Mapper::ToString(decl), "::", "_");
if ((decl->isExternallyDeclarable() && !decl->hasInit()) ||
if (decl->isThisDeclarationADefinition() ==
clang::VarDecl::DeclarationOnly ||
!globals_.insert(name).second) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1635,8 +1635,8 @@ std::string ConverterRefCount::GetDefaultAsString(clang::QualType qual_type) {
if (pointee_type->isVoidType()) {
ret = "AnyPtr::default()";
} else {
ret = std::format("Ptr::<{}>::null()",
GetUnsafeTypeAsString(pointee_type));
PushConversionKind push(*this, ConversionKind::Unboxed);
ret = std::format("Ptr::<{}>::null()", ConvertPointeeType(qual_type));
}
}
} else if (auto *array_type =
Expand Down Expand Up @@ -2240,14 +2240,14 @@ std::string ConverterRefCount::ConvertMappedMethodCall(
}

std::string ConverterRefCount::ConvertPointeeType(clang::QualType ptr_type) {
assert(!ptr_type.isNull() && ptr_type->isPointerType());
if (ptr_type->getPointeeType()->isIntegerType()) {
return ToString(ptr_type->getPointeeType());
}

// Pointee of a pointer to incomplete type is an incomplete type that does
// not have a translation rule. Hence ToString(ptr_type->getPointeeType()) is
// not enough
assert(ptr_type->isPointerType());
auto str = ToString(ptr_type);
Unwrap(str, "Ptr<", ">");
return str;
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/global_without_initializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>
#include <stdio.h>

struct S {
int a;
};

S *s;
FILE *file;
size_t size;

int main() {
assert(s == nullptr);
assert(file == nullptr);
assert(size == 0);
return 0;
};
40 changes: 40 additions & 0 deletions tests/unit/out/refcount/global_without_initializer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 S {
pub a: Value<i32>,
}
impl Clone for S {
fn clone(&self) -> Self {
let mut this = Self {
a: Rc::new(RefCell::new((*self.a.borrow()))),
};
this
}
}
impl ByteRepr for S {}
thread_local!(
pub static s: Value<Ptr<S>> = Rc::new(RefCell::new(Ptr::<S>::null()));
);
thread_local!(
pub static file: Value<Ptr<::std::fs::File>> =
Rc::new(RefCell::new(Ptr::<::std::fs::File>::null()));
);
thread_local!(
pub static size: Value<u64> = <Value<u64>>::default();
);
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
assert!((*s.with(Value::clone).borrow()).is_null());
assert!((*file.with(Value::clone).borrow()).is_null());
assert!(((*size.with(Value::clone).borrow()) == 0_u64));
return 0;
}
27 changes: 27 additions & 0 deletions tests/unit/out/unsafe/global_without_initializer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 S {
pub a: i32,
}
pub static mut s: *mut S = std::ptr::null_mut();
pub static mut file: *mut ::std::fs::File = std::ptr::null_mut();
pub static mut size: u64 = 0_u64;
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
assert!((s).is_null());
assert!((file).is_null());
assert!(((size) == (0_u64)));
return 0;
}
Loading