Skip to content

Commit a14d133

Browse files
authored
Call destructor of global variable (#653)
Fixes #379 Same solution will be needed to translate std::atexit
1 parent bf6072e commit a14d133

1,307 files changed

Lines changed: 2299 additions & 1350 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,13 +596,22 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
596596
}
597597

598598
void Converter::ConvertVarDeclInitializer(clang::VarDecl *decl) {
599+
bool registers_destructor = NeedsStaticDestructor(decl);
600+
if (registers_destructor) {
601+
StrCat("{ let __static = ");
602+
}
599603
if (decl->hasInit()) {
600604
ScopedValue constant(constant_evaluated_,
601605
HasConstantEvaluatedInit(ctx_, decl));
602606
ConvertVarInit(decl->getType(), decl->getInit());
603607
} else if (!clang::isa<clang::ParmVarDecl>(decl)) {
604608
StrCat(ConvertVarDefaultInit(decl));
605609
}
610+
if (registers_destructor) {
611+
StrCat(std::format(
612+
"; libcc2rs::register_static_destructor(|| {}); __static }}",
613+
StaticDestructorCall(decl)));
614+
}
606615
}
607616

608617
void Converter::EmitHoistedInArmAssignment(clang::VarDecl *decl) {
@@ -701,7 +710,14 @@ bool Converter::NeedsScopedDestructor(const clang::VarDecl *decl) const {
701710
if (in_function_formals_ || !decl->isLocalVarDecl() || IsGlobalVar(decl)) {
702711
return false;
703712
}
704-
auto type = decl->getType();
713+
return VarTypeNeedsDestructor(decl->getType());
714+
}
715+
716+
bool Converter::NeedsStaticDestructor(const clang::VarDecl *decl) const {
717+
return IsGlobalVar(decl) && VarTypeNeedsDestructor(decl->getType());
718+
}
719+
720+
bool Converter::VarTypeNeedsDestructor(clang::QualType type) const {
705721
if (auto lambda = AsLambdaClass(type);
706722
lambda && LambdaNeedsDestruction(lambda)) {
707723
return true;
@@ -710,6 +726,13 @@ bool Converter::NeedsScopedDestructor(const clang::VarDecl *decl) const {
710726
TypeNeedsDestruction(type);
711727
}
712728

729+
std::string Converter::StaticDestructorCall(const clang::VarDecl *decl) {
730+
auto destroy = AsLambdaClass(decl->getType()) ? "destroy" : kDestructorName;
731+
return std::format(
732+
"(*std::cell::LazyCell::force_mut(&mut *&raw mut {})).{}()",
733+
GetNamedDeclAsString(decl), destroy);
734+
}
735+
713736
void Converter::EmitScopedDestructor(const clang::VarDecl *decl) {
714737
if (!NeedsScopedDestructor(decl)) {
715738
return;
@@ -4829,13 +4852,13 @@ pub fn main() {{
48294852
argv.push(::std::ptr::null_mut());
48304853
unsafe {{
48314854
__cpp2rust_init_globals();
4832-
::std::process::exit(main_0((argv.len() - 1) as i32, argv.as_mut_ptr()) as i32)
4855+
libcc2rs::cpp2rust_exit(main_0((argv.len() - 1) as i32, argv.as_mut_ptr()) as i32)
48334856
}}
48344857
}})",
48354858
main_function_name));
48364859
} else {
48374860
StrCat(std::format("pub fn main() {{ unsafe {{ __cpp2rust_init_globals(); "
4838-
"std::process::exit({}() as i32); }} }}",
4861+
"libcc2rs::cpp2rust_exit({}() as i32); }} }}",
48394862
main_function_name));
48404863
}
48414864
}

‎cpp2rust/converter/converter.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
213213

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

216+
bool NeedsStaticDestructor(const clang::VarDecl *decl) const;
217+
218+
bool VarTypeNeedsDestructor(clang::QualType type) const;
219+
220+
virtual std::string StaticDestructorCall(const clang::VarDecl *decl);
221+
216222
virtual void EmitScopedDestructor(const clang::VarDecl *decl);
217223

218224
void EmitDeallocation(clang::CXXDeleteExpr *expr,

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,15 @@ void ConverterRefCount::EmitScopedDestructor(const clang::VarDecl *decl) {
908908
GetNamedDeclAsString(decl), destroy));
909909
}
910910

911+
std::string
912+
ConverterRefCount::StaticDestructorCall(const clang::VarDecl *decl) {
913+
auto destroy = AsLambdaClass(decl->getType())
914+
? std::string("with(|__f| __f.destroy())")
915+
: std::format("{}()", kDestructorName);
916+
return std::format("{}.with(Value::clone).as_pointer().{}",
917+
GetNamedDeclAsString(decl), destroy);
918+
}
919+
911920
bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
912921
auto *sub_expr = expr->getSubExpr();
913922
const char *method = GetIncDecMethod(expr->getOpcode());
@@ -2929,13 +2938,13 @@ pub fn main() {{
29292938
.collect(),
29302939
));
29312940
__cpp2rust_init_globals();
2932-
::std::process::exit({}(::std::env::args().len() as i32,
2941+
libcc2rs::cpp2rust_exit({}(::std::env::args().len() as i32,
29332942
argv.decay()));
29342943
}})",
29352944
main_function_name));
29362945
} else {
29372946
StrCat(std::format("pub fn main() {{ __cpp2rust_init_globals(); "
2938-
"std::process::exit({}()); }}",
2947+
"libcc2rs::cpp2rust_exit({}()); }}",
29392948
main_function_name));
29402949
}
29412950
}

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ class ConverterRefCount final : public Converter {
376376

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

379+
std::string StaticDestructorCall(const clang::VarDecl *decl) override;
380+
379381
std::pair<std::string, std::string>
380382
MaterializeTemp(const std::string &binding_name, clang::QualType param_type,
381383
clang::Expr *expr,

‎libcc2rs/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ pub use va_args::*;
100100
mod fd;
101101
pub use fd::*;
102102

103+
mod static_storage;
104+
pub use static_storage::*;
105+
103106
mod format;
104107
pub use format::*;
105108

‎libcc2rs/src/static_storage.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use std::sync::Mutex;
5+
6+
static STATIC_DESTRUCTORS: Mutex<Vec<fn()>> = Mutex::new(Vec::new());
7+
8+
pub fn register_static_destructor(destructor: fn()) {
9+
STATIC_DESTRUCTORS.lock().unwrap().push(destructor);
10+
}
11+
12+
pub fn run_static_destructors() {
13+
loop {
14+
let destructor = STATIC_DESTRUCTORS.lock().unwrap().pop();
15+
match destructor {
16+
Some(destructor) => destructor(),
17+
None => return,
18+
}
19+
}
20+
}
21+
22+
pub fn cpp2rust_exit(code: i32) -> ! {
23+
run_static_destructors();
24+
std::process::exit(code)
25+
}

‎rules/cstdlib/src.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ void f9(void *base, size_t nmemb, size_t size,
3232
char *f10(const char *path, char *resolved_path) {
3333
return realpath(path, resolved_path);
3434
}
35+
36+
void f11(int a0) { return exit(a0); }

‎rules/cstdlib/tgt_unsafe.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ unsafe fn f9(
6868
unsafe fn f10(a0: *const libc::c_char, a1: *mut libc::c_char) -> *mut libc::c_char {
6969
libc::realpath(a0, a1)
7070
}
71+
72+
unsafe fn f11(a0: i32) {
73+
libcc2rs::cpp2rust_exit(a0);
74+
}

‎tests/multi-file/cross_tu_anon_enum_collision/out/refcount/cross_tu_anon_enum_collision.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn a_value_1() -> i32 {
1515
}
1616
pub fn main() {
1717
__cpp2rust_init_globals();
18-
std::process::exit(main_0());
18+
libcc2rs::cpp2rust_exit(main_0());
1919
}
2020
fn main_0() -> i32 {
2121
assert!((({ a_value_1() }) == 7));

‎tests/multi-file/cross_tu_anon_enum_collision/out/unsafe/cross_tu_anon_enum_collision.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub unsafe fn a_value_1() -> i32 {
1616
pub fn main() {
1717
unsafe {
1818
__cpp2rust_init_globals();
19-
std::process::exit(main_0() as i32);
19+
libcc2rs::cpp2rust_exit(main_0() as i32);
2020
}
2121
}
2222
unsafe fn main_0() -> i32 {

0 commit comments

Comments
 (0)