Skip to content

[cxx-interop] ensure destructors referenced only by the exception cle… #74722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2024
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
13 changes: 12 additions & 1 deletion lib/IRGen/GenClangDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,19 @@ class ClangDeclFinder
bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *CXXCD) {
callback(CXXCD);
for (clang::CXXCtorInitializer *CXXCI : CXXCD->inits()) {
if (clang::FieldDecl *FD = CXXCI->getMember())
if (clang::FieldDecl *FD = CXXCI->getMember()) {
callback(FD);
// A throwing constructor might throw after the field is initialized,
// emitting additional cleanup code that destroys the field. Make sure
// we record the destructor of the field in that case as it might need
// to be potentially emitted.
if (auto *recordType = FD->getType()->getAsCXXRecordDecl()) {
if (auto *destructor = recordType->getDestructor()) {
if (!destructor->isDeleted())
callback(destructor);
}
}
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: %target-swiftxx-frontend -emit-ir -I %t/Inputs -validate-tbd-against-ir=none %t/test.swift | %FileCheck %s

//--- Inputs/module.modulemap
module ThrowingConstructorDestructorCleanupRef {
header "test.h"
requires cplusplus
}
//--- Inputs/test.h

void testFunc(int x);

template<class T>
class HasDestructor {
T x = 0;
public:

HasDestructor() { }
HasDestructor(const HasDestructor &other) : x(other.x) {}
inline ~HasDestructor() { testFunc(42); }
};

template<class T>
class HasThrowingConstructor {
HasDestructor<T> m;
public:
HasThrowingConstructor();
~HasThrowingConstructor();
inline HasThrowingConstructor(const HasThrowingConstructor &f) : m(f.m) {
doSomethingThatMightThrow();
}

void doSomethingThatMightThrow();
};

inline void test33(const HasThrowingConstructor<int> x) {

}

using HasThrowingConstructorInt = HasThrowingConstructor<int>;

//--- test.swift

import ThrowingConstructorDestructorCleanupRef

public func test() {
let x = HasThrowingConstructorInt()
test33(x)
}

// Make sure we reach the destructor of 'HasDestructor'
// CHECK: define linkonce_odr {{.*}} @{{_ZN13HasDestructorIiED2Ev|"\?\?1\?\$HasDestructor@H@@QEAA@XZ"}}