Skip to content

Commit 12ce997

Browse files
authored
Merge branch 'main' into codecov-x86
2 parents 048c272 + 98e07e7 commit 12ce997

File tree

6 files changed

+46
-25
lines changed

6 files changed

+46
-25
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
namespace Cpp {
3636
using TCppIndex_t = size_t;
3737
using TCppScope_t = void*;
38+
using TCppConstScope_t = const void*;
3839
using TCppType_t = void*;
3940
using TCppFunction_t = void*;
4041
using TCppConstFunction_t = const void*;
@@ -838,14 +839,14 @@ enum : long int {
838839
CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type);
839840

840841
/// Allocates memory required by an object of a given class
841-
/// \c scope Given class for which to allocate memory for
842-
/// \c count is used to indicate the number of objects to allocate for.
842+
/// \param[in] scope Given class for which to allocate memory for
843+
/// \param[in] count is used to indicate the number of objects to allocate for.
843844
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope,
844845
TCppIndex_t count = 1UL);
845846

846847
/// Deallocates memory for a given class.
847-
/// \c scope Class to indicate size of memory to deallocate
848-
/// \c count is used to indicate the number of objects to dallocate for
848+
/// \param[in] scope Class to indicate size of memory to deallocate
849+
/// \param[in] count is used to indicate the number of objects to dallocate for
849850
CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
850851
TCppIndex_t count = 1UL);
851852

@@ -855,6 +856,8 @@ CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address,
855856
/// \param[in] arena If set, this API uses placement new to construct at this
856857
/// address.
857858
/// \param[in] is used to indicate the number of objects to construct.
859+
/// \returns a pointer to the constructed object, which is arena if placement
860+
/// new is used.
858861
CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
859862
TCppIndex_t count = 1UL);
860863

@@ -865,7 +868,8 @@ CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope, void* arena = nullptr,
865868
/// destructor
866869
/// \param[in] count indicate the number of objects to destruct, if \c This
867870
/// points to an array of objects
868-
CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
871+
/// \returns true if wrapper generation and invocation succeeded.
872+
CPPINTEROP_API bool Destruct(TCppObject_t This, TCppConstScope_t type,
869873
bool withFree = true, TCppIndex_t count = 0UL);
870874

871875
/// @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: 4 additions & 4 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,
617-
clang::Decl* Class, bool withFree, size_t nary);
616+
bool Destruct(compat::Interpreter& interp, TCppObject_t This,
617+
const 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: 8 additions & 7 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, const 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-
TCppIndex_t count /*=0UL*/) {
3826-
auto* Class = static_cast<Decl*>(scope);
3827-
Destruct(getInterp(), This, Class, withFree, count);
3825+
bool Destruct(TCppObject_t This, TCppConstScope_t scope,
3826+
bool withFree /*=true*/, TCppIndex_t count /*=0UL*/) {
3827+
const auto* Class = static_cast<const Decl*>(scope);
3828+
return Destruct(getInterp(), This, Class, withFree, count);
38283829
}
38293830

38303831
class StreamCaptureInfo {

unittests/CppInterOp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ target_link_libraries(DynamicLibraryManagerTests
9797
)
9898

9999
if(EMSCRIPTEN)
100-
set(TEST_SHARED_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/Release/")
100+
set(TEST_SHARED_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/$<CONFIG>/")
101101
string(REPLACE "@" "@@" ESCAPED_TEST_SHARED_LIBRARY_PATH "${TEST_SHARED_LIBRARY_PATH}")
102102
# Check explanation of Emscripten-specific link flags for CppInterOpTests above for DynamicLibraryManagerTests as well.
103103
target_link_options(DynamicLibraryManagerTests

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 21 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");
@@ -2406,6 +2406,20 @@ TEST(FunctionReflectionTest, Destruct) {
24062406
// Clean up resources
24072407
clang_Interpreter_takeInterpreterAsPtr(I);
24082408
clang_Interpreter_dispose(I);
2409+
2410+
// Failure test, this wrapper should not compile since we explicitly delete
2411+
// the destructor
2412+
Interp->declare(R"(
2413+
class D {
2414+
public:
2415+
D() {}
2416+
~D() = delete;
2417+
};
2418+
)");
2419+
2420+
scope = Cpp::GetNamed("D");
2421+
object = Cpp::Construct(scope);
2422+
EXPECT_FALSE(Cpp::Destruct(object, scope));
24092423
}
24102424

24112425
TEST(FunctionReflectionTest, DestructArray) {
@@ -2454,7 +2468,7 @@ TEST(FunctionReflectionTest, DestructArray) {
24542468

24552469
testing::internal::CaptureStdout();
24562470
// destruct 3 out of 5 objects
2457-
Cpp::Destruct(where, scope, false, 3);
2471+
EXPECT_TRUE(Cpp::Destruct(where, scope, false, 3));
24582472
output = testing::internal::GetCapturedStdout();
24592473

24602474
EXPECT_EQ(
@@ -2466,7 +2480,7 @@ TEST(FunctionReflectionTest, DestructArray) {
24662480
// destruct the rest
24672481
auto *new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
24682482
(Cpp::SizeOf(scope) * 3));
2469-
Cpp::Destruct(new_head, scope, false, 2);
2483+
EXPECT_TRUE(Cpp::Destruct(new_head, scope, false, 2));
24702484

24712485
output = testing::internal::GetCapturedStdout();
24722486
EXPECT_EQ(output, "\nDestructor Executed\n\nDestructor Executed\n");
@@ -2481,7 +2495,7 @@ TEST(FunctionReflectionTest, DestructArray) {
24812495
testing::internal::CaptureStdout();
24822496
// FIXME : This should work with the array of objects as well
24832497
// Cpp::Destruct(where, scope, true, 5);
2484-
Cpp::Destruct(where, scope, true);
2498+
EXPECT_TRUE(Cpp::Destruct(where, scope, true));
24852499
output = testing::internal::GetCapturedStdout();
24862500
EXPECT_EQ(output, "\nDestructor Executed\n");
24872501
output.clear();

0 commit comments

Comments
 (0)