Skip to content

Commit 1e8a76a

Browse files
committed
Allow Destruct to call only the destructor without delete/free.
1 parent db81a49 commit 1e8a76a

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,10 @@ namespace Cpp {
262262
TCppObject_t Construct(TInterp_t interp, TCppScope_t scope,
263263
void *arena = nullptr);
264264

265-
/// Calls the destructor of object of type \c type.
266-
void Destruct(TInterp_t interp, TCppObject_t This, TCppScope_t type);
265+
/// Calls the destructor of object of type \c type. When withFree is true it
266+
/// calls operator delete/free.
267+
void Destruct(TInterp_t interp, TCppObject_t This, TCppScope_t type,
268+
bool withFree = true);
267269
} // end namespace Cpp
268270

269271
#endif // CPPINTEROP_CPPINTEROP_H

lib/Interpreter/CppInterOp.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,11 +2873,12 @@ namespace Cpp {
28732873
return nullptr;
28742874
}
28752875

2876-
void Destruct(TInterp_t interp, TCppObject_t This, TCppScope_t scope) {
2876+
void Destruct(TInterp_t interp, TCppObject_t This, TCppScope_t scope,
2877+
bool withFree /*=true*/) {
28772878
Decl* Class = (Decl*)scope;
28782879
auto *I = (compat::Interpreter *)interp;
28792880
if (CallFuncDtorWrapper_t wrapper = make_dtor_wrapper(I, Class)) {
2880-
(*wrapper)(This, /*nary=*/0, /*withFree=*/true);
2881+
(*wrapper)(This, /*nary=*/0, withFree);
28812882
return;
28822883
}
28832884
// FIXME: Diagnose.

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,4 +718,10 @@ TEST(FunctionReflectionTest, Destruct) {
718718
std::string output = testing::internal::GetCapturedStdout();
719719

720720
EXPECT_EQ(output, "Destructor Executed");
721+
722+
object = Cpp::Construct(Interp.get(), scope);
723+
// Make sure we do not call delete by adding an explicit Deallocate. If we
724+
// called delete the Deallocate will cause a double deletion error.
725+
Cpp::Destruct(Interp.get(), object, scope, /*withFree=*/false);
726+
Cpp::Deallocate(scope, object);
721727
}

0 commit comments

Comments
 (0)