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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
29 changes: 26 additions & 3 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,22 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
}

void Converter::ConvertVarDeclInitializer(clang::VarDecl *decl) {
bool registers_destructor = NeedsStaticDestructor(decl);
if (registers_destructor) {
StrCat("{ let __static = ");
}
if (decl->hasInit()) {
ScopedValue constant(constant_evaluated_,
HasConstantEvaluatedInit(ctx_, decl));
ConvertVarInit(decl->getType(), decl->getInit());
} else if (!clang::isa<clang::ParmVarDecl>(decl)) {
StrCat(ConvertVarDefaultInit(decl));
}
if (registers_destructor) {
StrCat(std::format(
"; libcc2rs::register_static_destructor(|| {}); __static }}",
StaticDestructorCall(decl)));
}
}

void Converter::EmitHoistedInArmAssignment(clang::VarDecl *decl) {
Expand Down Expand Up @@ -701,7 +710,14 @@ bool Converter::NeedsScopedDestructor(const clang::VarDecl *decl) const {
if (in_function_formals_ || !decl->isLocalVarDecl() || IsGlobalVar(decl)) {
return false;
}
auto type = decl->getType();
return VarTypeNeedsDestructor(decl->getType());
}

bool Converter::NeedsStaticDestructor(const clang::VarDecl *decl) const {
return IsGlobalVar(decl) && VarTypeNeedsDestructor(decl->getType());
}

bool Converter::VarTypeNeedsDestructor(clang::QualType type) const {
if (auto lambda = AsLambdaClass(type);
lambda && LambdaNeedsDestruction(lambda)) {
return true;
Expand All @@ -710,6 +726,13 @@ bool Converter::NeedsScopedDestructor(const clang::VarDecl *decl) const {
TypeNeedsDestruction(type);
}

std::string Converter::StaticDestructorCall(const clang::VarDecl *decl) {
auto destroy = AsLambdaClass(decl->getType()) ? "destroy" : kDestructorName;
return std::format(
"(*std::cell::LazyCell::force_mut(&mut *&raw mut {})).{}()",
GetNamedDeclAsString(decl), destroy);
}

void Converter::EmitScopedDestructor(const clang::VarDecl *decl) {
if (!NeedsScopedDestructor(decl)) {
return;
Expand Down Expand Up @@ -4825,13 +4848,13 @@ pub fn main() {{
argv.push(::std::ptr::null_mut());
unsafe {{
__cpp2rust_init_globals();
::std::process::exit(main_0((argv.len() - 1) as i32, argv.as_mut_ptr()) as i32)
libcc2rs::cpp2rust_exit(main_0((argv.len() - 1) as i32, argv.as_mut_ptr()) as i32)
}}
}})",
main_function_name));
} else {
StrCat(std::format("pub fn main() {{ unsafe {{ __cpp2rust_init_globals(); "
"std::process::exit({}() as i32); }} }}",
"libcc2rs::cpp2rust_exit({}() as i32); }} }}",
main_function_name));
}
}
Expand Down
6 changes: 6 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

bool NeedsScopedDestructor(const clang::VarDecl *decl) const;

bool NeedsStaticDestructor(const clang::VarDecl *decl) const;

bool VarTypeNeedsDestructor(clang::QualType type) const;

virtual std::string StaticDestructorCall(const clang::VarDecl *decl);

virtual void EmitScopedDestructor(const clang::VarDecl *decl);

void EmitDeallocation(clang::CXXDeleteExpr *expr,
Expand Down
13 changes: 11 additions & 2 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,15 @@ void ConverterRefCount::EmitScopedDestructor(const clang::VarDecl *decl) {
GetNamedDeclAsString(decl), destroy));
}

std::string
ConverterRefCount::StaticDestructorCall(const clang::VarDecl *decl) {
auto destroy = AsLambdaClass(decl->getType())
? std::string("with(|__f| __f.destroy())")
: std::format("{}()", kDestructorName);
return std::format("{}.with(Value::clone).as_pointer().{}",
GetNamedDeclAsString(decl), destroy);
}

bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
auto *sub_expr = expr->getSubExpr();
const char *method = GetIncDecMethod(expr->getOpcode());
Expand Down Expand Up @@ -2929,13 +2938,13 @@ pub fn main() {{
.collect(),
));
__cpp2rust_init_globals();
::std::process::exit({}(::std::env::args().len() as i32,
libcc2rs::cpp2rust_exit({}(::std::env::args().len() as i32,
argv.decay()));
}})",
main_function_name));
} else {
StrCat(std::format("pub fn main() {{ __cpp2rust_init_globals(); "
"std::process::exit({}()); }}",
"libcc2rs::cpp2rust_exit({}()); }}",
main_function_name));
}
}
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/models/converter_refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ class ConverterRefCount final : public Converter {

void EmitScopedDestructor(const clang::VarDecl *decl) override;

std::string StaticDestructorCall(const clang::VarDecl *decl) override;

std::pair<std::string, std::string>
MaterializeTemp(const std::string &binding_name, clang::QualType param_type,
clang::Expr *expr,
Expand Down
3 changes: 3 additions & 0 deletions libcc2rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ pub use va_args::*;
mod fd;
pub use fd::*;

mod static_storage;
pub use static_storage::*;

mod format;
pub use format::*;

Expand Down
25 changes: 25 additions & 0 deletions libcc2rs/src/static_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use std::sync::Mutex;

static STATIC_DESTRUCTORS: Mutex<Vec<fn()>> = Mutex::new(Vec::new());

pub fn register_static_destructor(destructor: fn()) {
STATIC_DESTRUCTORS.lock().unwrap().push(destructor);
}

pub fn run_static_destructors() {
loop {
let destructor = STATIC_DESTRUCTORS.lock().unwrap().pop();
match destructor {
Some(destructor) => destructor(),
None => return,
}
}
}

pub fn cpp2rust_exit(code: i32) -> ! {
run_static_destructors();
std::process::exit(code)
}
2 changes: 2 additions & 0 deletions rules/cstdlib/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ void f9(void *base, size_t nmemb, size_t size,
char *f10(const char *path, char *resolved_path) {
return realpath(path, resolved_path);
}

void f11(int a0) { return exit(a0); }
4 changes: 4 additions & 0 deletions rules/cstdlib/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ unsafe fn f9(
unsafe fn f10(a0: *const libc::c_char, a1: *mut libc::c_char) -> *mut libc::c_char {
libc::realpath(a0, a1)
}

unsafe fn f11(a0: i32) {
libcc2rs::cpp2rust_exit(a0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn a_value_1() -> i32 {
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
assert!((({ a_value_1() }) == 7));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub unsafe fn a_value_1() -> i32 {
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn a_value_0() -> i32 {
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
assert!((({ a_value_0() }) == 11));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub unsafe fn a_value_0() -> i32 {
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn sum_0(s: Ptr<S>) -> i32 {
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
let s: Value<S> = Rc::new(RefCell::new(S::new({ 2 })));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub unsafe fn sum_0(s: *const S) -> i32 {
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
assert!((({ helper_0(42,) }) == 43));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::rc::Rc;
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct S {
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
let s1: Value<S> = Rc::new(RefCell::new(S { a: 1, b: 2 }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct S {
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
assert!((({ helper_0(42,) }) == 43));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::rc::Rc;
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct container {
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
let c: Value<container> = Rc::new(RefCell::new(container {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct container {
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn pair_diff_0(mut p: Ptr<Pair>) -> i32 {
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
let s: Value<S> = Rc::new(RefCell::new(S::new({ 1 })));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub unsafe fn pair_diff_0(mut p: *const Pair) -> i32 {
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn a_bar_3() -> i32 {
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
assert!((({ a_foo_2() }) == 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub unsafe fn a_bar_3() -> i32 {
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Default for S_int_ {
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
let p: Value<S_int_> = Rc::new(RefCell::new(<S_int_>::default()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Default for S_int_ {
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,22 @@ pub struct AssertsDtorCountAtExit {
pub expected: i32,
}
thread_local!(
pub static check_4: Value<AssertsDtorCountAtExit> =
Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 2 }));
pub static check_4: Value<AssertsDtorCountAtExit> = {
let __static = Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 2 }));
libcc2rs::register_static_destructor(|| {
check_4.with(Value::clone).as_pointer().destructor()
});
__static
};
);
thread_local!(
pub static shared_5: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
pub static shared_5: Value<LifetimeTracked> = {
let __static = Rc::new(RefCell::new(LifetimeTracked::new()));
libcc2rs::register_static_destructor(|| {
shared_5.with(Value::clone).as_pointer().destructor()
});
__static
};
);
#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
Expand All @@ -89,11 +100,17 @@ impl HasInlineMembers {
}
}
thread_local!(
static local_6: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked::new()));
static local_6: Value<LifetimeTracked> = {
let __static = Rc::new(RefCell::new(LifetimeTracked::new()));
libcc2rs::register_static_destructor(|| {
local_6.with(Value::clone).as_pointer().destructor()
});
__static
};
);
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
({ LifetimeTrackedImpl::use_(&shared_5.with(|v| v.as_pointer())) });
Expand Down
Loading
Loading