Skip to content

Commit 0b358ce

Browse files
committed
Improve Cpp::Destruct to indicate success/failure, adapt C-API and tests
1 parent 9c94230 commit 0b358ce

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,8 @@ CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
865865
/// destructor
866866
/// \param[in] count indicate the number of objects to destruct, if \c This
867867
/// points to an array of objects
868-
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
868+
/// \returns true if wrapper generation and invocation succeeded.
869+
CPPINTEROP_API bool Destruct(TCppObject_t This, TCppScope_t type,
869870
bool withFree = true, TCppIndex_t count = 0UL);
870871

871872
/// @name Stream Redirection

include/clang-c/CXCppInterOp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,10 @@ CINDEX_LINKAGE void clang_invoke(CXScope func, void* result, void** args,
361361
* \param type The type of the object.
362362
*
363363
* \param withFree Whether to call operator delete/free or not.
364+
*
365+
* \returns true if wrapper generation and invocation succeeded.
364366
*/
365-
CINDEX_LINKAGE void clang_destruct(CXObject This, CXScope S,
367+
CINDEX_LINKAGE bool clang_destruct(CXObject This, CXScope S,
366368
bool withFree = true, size_t nary = 0UL);
367369

368370
/**

lib/CppInterOp/CXCppInterOp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,10 @@ void clang_invoke(CXScope func, void* result, void** args, size_t n,
613613
}
614614

615615
namespace Cpp {
616-
void Destruct(compat::Interpreter& interp, TCppObject_t This,
616+
bool Destruct(compat::Interpreter& interp, TCppObject_t This,
617617
clang::Decl* Class, bool withFree, size_t nary);
618618
} // namespace Cpp
619619

620-
void clang_destruct(CXObject This, CXScope S, bool withFree, size_t nary) {
621-
Cpp::Destruct(*getInterpreter(S), This, getDecl(S), withFree, nary);
620+
bool clang_destruct(CXObject This, CXScope S, bool withFree, size_t nary) {
621+
return Cpp::Destruct(*getInterpreter(S), This, getDecl(S), withFree, nary);
622622
}

lib/CppInterOp/CppInterOp.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,19 +3812,20 @@ TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
38123812
return Construct(getInterp(), scope, arena, count);
38133813
}
38143814

3815-
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3815+
bool Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
38163816
bool withFree, TCppIndex_t nary) {
38173817
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
38183818
(*wrapper)(This, nary, withFree);
3819-
return;
3819+
return true;
38203820
}
3821-
// FIXME: Diagnose.
3821+
return false;
3822+
// FIXME: Enable stronger diagnostics
38223823
}
38233824

3824-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
3825+
bool Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
38253826
TCppIndex_t count /*=0UL*/) {
38263827
auto* Class = static_cast<Decl*>(scope);
3827-
Destruct(getInterp(), This, Class, withFree, count);
3828+
return Destruct(getInterp(), This, Class, withFree, count);
38283829
}
38293830

38303831
class StreamCaptureInfo {

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ TEST(FunctionReflectionTest, JitCallAdvanced) {
14771477
Ctor.Invoke(&object);
14781478
EXPECT_TRUE(object) << "Failed to call the ctor.";
14791479
// Building a wrapper with a typedef decl must be possible.
1480-
Cpp::Destruct(object, Decls[1]);
1480+
EXPECT_TRUE(Cpp::Destruct(object, Decls[1]));
14811481

14821482
// C API
14831483
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
@@ -2342,7 +2342,7 @@ TEST(FunctionReflectionTest, ConstructArray) {
23422342
obj = reinterpret_cast<int*>(reinterpret_cast<char*>(where) +
23432343
(Cpp::SizeOf(scope) * 4));
23442344
EXPECT_TRUE(*obj == 42);
2345-
Cpp::Destruct(where, scope, /*withFree=*/false, 5);
2345+
EXPECT_TRUE(Cpp::Destruct(where, scope, /*withFree=*/false, 5));
23462346
Cpp::Deallocate(scope, where, 5);
23472347
output = testing::internal::GetCapturedStdout();
23482348
EXPECT_EQ(output,
@@ -2379,7 +2379,7 @@ TEST(FunctionReflectionTest, Destruct) {
23792379
testing::internal::CaptureStdout();
23802380
Cpp::TCppScope_t scope = Cpp::GetNamed("C");
23812381
Cpp::TCppObject_t object = Cpp::Construct(scope);
2382-
Cpp::Destruct(object, scope);
2382+
EXPECT_TRUE(Cpp::Destruct(object, scope));
23832383
std::string output = testing::internal::GetCapturedStdout();
23842384

23852385
EXPECT_EQ(output, "Destructor Executed");
@@ -2389,7 +2389,7 @@ TEST(FunctionReflectionTest, Destruct) {
23892389
object = Cpp::Construct(scope);
23902390
// Make sure we do not call delete by adding an explicit Deallocate. If we
23912391
// called delete the Deallocate will cause a double deletion error.
2392-
Cpp::Destruct(object, scope, /*withFree=*/false);
2392+
EXPECT_TRUE(Cpp::Destruct(object, scope, /*withFree=*/false));
23932393
Cpp::Deallocate(scope, object);
23942394
output = testing::internal::GetCapturedStdout();
23952395
EXPECT_EQ(output, "Destructor Executed");
@@ -2454,7 +2454,7 @@ TEST(FunctionReflectionTest, DestructArray) {
24542454

24552455
testing::internal::CaptureStdout();
24562456
// destruct 3 out of 5 objects
2457-
Cpp::Destruct(where, scope, false, 3);
2457+
EXPECT_TRUE(Cpp::Destruct(where, scope, false, 3));
24582458
output = testing::internal::GetCapturedStdout();
24592459

24602460
EXPECT_EQ(
@@ -2466,7 +2466,7 @@ TEST(FunctionReflectionTest, DestructArray) {
24662466
// destruct the rest
24672467
auto *new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
24682468
(Cpp::SizeOf(scope) * 3));
2469-
Cpp::Destruct(new_head, scope, false, 2);
2469+
EXPECT_TRUE(Cpp::Destruct(new_head, scope, false, 2));
24702470

24712471
output = testing::internal::GetCapturedStdout();
24722472
EXPECT_EQ(output, "\nDestructor Executed\n\nDestructor Executed\n");
@@ -2481,7 +2481,7 @@ TEST(FunctionReflectionTest, DestructArray) {
24812481
testing::internal::CaptureStdout();
24822482
// FIXME : This should work with the array of objects as well
24832483
// Cpp::Destruct(where, scope, true, 5);
2484-
Cpp::Destruct(where, scope, true);
2484+
EXPECT_TRUE(Cpp::Destruct(where, scope, true));
24852485
output = testing::internal::GetCapturedStdout();
24862486
EXPECT_EQ(output, "\nDestructor Executed\n");
24872487
output.clear();

0 commit comments

Comments
 (0)