Skip to content

Add support for array of objects in Construct/Destruct #587

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

aaronj0
Copy link
Collaborator

@aaronj0 aaronj0 commented May 21, 2025

No description provided.

@aaronj0 aaronj0 requested a review from vgvassilev May 21, 2025 15:37
Copy link

codecov bot commented May 21, 2025

Codecov Report

Attention: Patch coverage is 76.13636% with 21 lines in your changes missing coverage. Please review.

Project coverage is 77.86%. Comparing base (360a117) to head (f3a20b2).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
lib/CppInterOp/CppInterOp.cpp 70.42% 21 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #587      +/-   ##
==========================================
+ Coverage   77.70%   77.86%   +0.16%     
==========================================
  Files           9        9              
  Lines        3749     3809      +60     
==========================================
+ Hits         2913     2966      +53     
- Misses        836      843       +7     
Files with missing lines Coverage Δ
include/CppInterOp/CppInterOp.h 100.00% <100.00%> (ø)
lib/CppInterOp/CXCppInterOp.cpp 48.86% <100.00%> (ø)
lib/CppInterOp/CppInterOp.cpp 85.16% <70.42%> (+0.02%) ⬆️
Files with missing lines Coverage Δ
include/CppInterOp/CppInterOp.h 100.00% <100.00%> (ø)
lib/CppInterOp/CXCppInterOp.cpp 48.86% <100.00%> (ø)
lib/CppInterOp/CppInterOp.cpp 85.16% <70.42%> (+0.02%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aaronj0 aaronj0 marked this pull request as draft May 21, 2025 16:47
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 22. Check the log or trigger a new build to see more.

// Forward if we intended to call a dtor with only 1 parameter.
if (m_Kind == kDestructorCall && result && !args.m_Args)
return InvokeDestructor(result, /*nary=*/0UL, /*withFree=*/true);
return InvokeDestructor(result, nary, /*withFree=*/true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]

Suggested change
return InvokeDestructor(result, nary, /*withFree=*/true);
{ InvokeDestructor(result, nary, /*withFree=*/true); return;
}


#ifndef NDEBUG
assert(AreArgumentsValid(result, args, self) && "Invalid args!");
ReportInvokeStart(result, args, self);
#endif // NDEBUG
m_GenericCall(self, args.m_ArgSize, args.m_Args, result);
m_GenericCall(self, args.m_ArgSize, args.m_Args, result, nary);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

      m_GenericCall(self, args.m_ArgSize, args.m_Args, result, nary);
      ^


#ifndef NDEBUG
assert(AreArgumentsValid(result, args, self) && "Invalid args!");
ReportInvokeStart(result, args, self);
#endif // NDEBUG
m_GenericCall(self, args.m_ArgSize, args.m_Args, result);
m_GenericCall(self, args.m_ArgSize, args.m_Args, result, nary);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: narrowing conversion from 'size_t' (aka 'unsigned long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]

      m_GenericCall(self, args.m_ArgSize, args.m_Args, result, nary);
                          ^

TCppObject_t Allocate(TCppScope_t scope) {
return (TCppObject_t)::operator new(Cpp::SizeOf(scope));
TCppObject_t Allocate(TCppScope_t scope, TCppIndex_t count) {
return (TCppObject_t)::operator new(Cpp::SizeOf(scope) * count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "operator new" is directly included [misc-include-cleaner]

lib/Interpreter/CppInterOp.cpp:39:

- #if CLANG_VERSION_MAJOR >= 19
+ #include <new>
+ #if CLANG_VERSION_MAJOR >= 19

::operator delete(address);
void Deallocate(TCppScope_t scope, TCppObject_t address, TCppIndex_t count) {
size_t bytes = Cpp::SizeOf(scope) * count;
::operator delete(address, bytes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "operator delete" is directly included [misc-include-cleaner]

    ::operator delete(address, bytes);
      ^

@@ -3596,7 +3633,8 @@
auto* const Ctor = GetDefaultConstructor(interp, Class);
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
if (arena) {
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
JC.Invoke(&arena, {}, (void*)~0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

        JC.Invoke(&arena, {}, (void*)~0,
                              ^

@@ -3596,7 +3633,8 @@
auto* const Ctor = GetDefaultConstructor(interp, Class);
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
if (arena) {
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
JC.Invoke(&arena, {}, (void*)~0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: integer to pointer cast pessimizes optimization opportunities [performance-no-int-to-ptr]

        JC.Invoke(&arena, {}, (void*)~0,
                              ^

@@ -3596,7 +3633,8 @@
auto* const Ctor = GetDefaultConstructor(interp, Class);
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
if (arena) {
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
JC.Invoke(&arena, {}, (void*)~0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: multilevel pointer conversion from 'void **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]

        JC.Invoke(&arena, {}, (void*)~0,
                  ^

@aaronj0 aaronj0 force-pushed the structor-api branch 2 times, most recently from 0b3d49d to b6d01ac Compare June 2, 2025 13:54
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 22. Check the log or trigger a new build to see more.

@@ -768,19 +798,24 @@ enum : long int {
CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type);

/// Allocates memory for a given class.
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope);
/// \c count is used to indicate the number of objects to allocate for.
CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: enum '(unnamed enum at /github/workspace/include/CppInterOp/CppInterOp.h:802:1)' uses a larger base type ('long', size: 8 bytes) than necessary for its value set, consider using 'std::int8_t' (1 byte) as the base type to reduce its size [performance-enum-size]

enum : long int {
^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should adjust the types to match the signature types to match the generated code types and disable the clang-tidy complaints if we have to.

}

// Standard branch:
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName(args...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'auto *CD' can be declared as 'const auto *CD' [readability-qualified-auto]

Suggested change
// (*(ClassName**)ret) = (obj) ? new (*(ClassName**)ret) ClassName(args...)
const auto* CD = dyn_cast<CXXConstructorDecl>(FD);

@@ -2831,6 +2904,13 @@ CPPINTEROP_API JitCall MakeFunctionCallable(TInterp_t I,
return {};
}

if (const auto* Ctor = dyn_cast<CXXConstructorDecl>(D)) {
if (auto Wrapper = make_wrapper(*interp, cast<FunctionDecl>(D)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::make_pair" is directly included [misc-include-cleaner]

    gDtorWrapperStore.insert(make_pair(D, F));
                             ^

@@ -1883,7 +1883,7 @@ TEST(FunctionReflectionTest, Construct) {
testing::internal::CaptureStdout();
auto* I = clang_createInterpreterFromRawPtr(Cpp::GetInterpreter());
auto scope_c = make_scope(static_cast<clang::Decl*>(scope), I);
auto object_c = clang_construct(scope_c, nullptr);
auto object_c = clang_construct(scope_c, nullptr, 1UL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'auto object_c' can be declared as 'auto *object_c' [llvm-qualified-auto]

Suggested change
auto object_c = clang_construct(scope_c, nullptr, 1UL);
auto *object_c = clang_construct(scope_c, nullptr, 1UL);

@aaronj0 aaronj0 marked this pull request as ready for review June 2, 2025 13:56
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 20. Check the log or trigger a new build to see more.

"Number of objects to construct/destruct should be atleast 1");

if (Cpp::IsConstructor(m_FD)) {
const auto* CD = cast<CXXConstructorDecl>((const Decl*)m_FD);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

    const auto* CD = cast<CXXConstructorDecl>((const Decl*)m_FD);
                                              ^

@@ -3613,33 +3694,36 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
auto* const Ctor = GetDefaultConstructor(interp, Class);
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
if (arena) {
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
JC.InvokeConstructor(&arena, count, {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: multilevel pointer conversion from 'void **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]

      JC.InvokeConstructor(&arena, count, {},
                           ^

return arena;
}

void* obj = nullptr;
JC.Invoke(&obj);
JC.InvokeConstructor(&obj, count, {}, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: multilevel pointer conversion from 'void **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]

    JC.InvokeConstructor(&obj, count, {}, false);
                         ^

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 15. Check the log or trigger a new build to see more.

@@ -3613,33 +3694,36 @@ TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
auto* const Ctor = GetDefaultConstructor(interp, Class);
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
if (arena) {
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
JC.InvokeConstructor(&arena, count, {},
(void*)~0); // Tell Invoke to use placement new.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

                           (void*)~0); // Tell Invoke to use placement new.
                           ^

@@ -3613,33 +3694,36 @@
auto* const Ctor = GetDefaultConstructor(interp, Class);
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
if (arena) {
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
JC.InvokeConstructor(&arena, count, {},
(void*)~0); // Tell Invoke to use placement new.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: integer to pointer cast pessimizes optimization opportunities [performance-no-int-to-ptr]

                           (void*)~0); // Tell Invoke to use placement new.
                           ^

return arena;
}

void* obj = nullptr;
JC.Invoke(&obj);
JC.InvokeConstructor(&obj, count, {}, nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: multilevel pointer conversion from 'void **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]

    JC.InvokeConstructor(&obj, count, {}, nullptr);
                         ^

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

testing::internal::CaptureStdout();

// destruct the rest
auto new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'auto new_head' can be declared as 'auto *new_head' [llvm-qualified-auto]

Suggested change
auto new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
auto *new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +

testing::internal::CaptureStdout();

// destruct the rest
auto new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

  auto new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
                                          ^

testing::internal::CaptureStdout();

// destruct the rest
auto new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

  auto new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
                  ^

Copy link
Contributor

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we are heading in a good direction. Let's make clang-tidy and codecov happy first.

if (Cpp::IsConstructor(m_FD)) {
const auto* CD = cast<CXXConstructorDecl>((const Decl*)m_FD);
if (CD->getMinRequiredArguments() != 0 && nary > 1) {
assert(0 &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a death test here.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

testing::internal::CaptureStdout();

// destruct the rest
auto *new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

  auto *new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
                                           ^

testing::internal::CaptureStdout();

// destruct the rest
auto *new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

  auto *new_head = reinterpret_cast<void*>(reinterpret_cast<char*>(where) +
                   ^

Comment on lines 1932 to 1935
if (N) {
callbuf << "(";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move that in the loop, maybe it improves readability...

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

return obj;
}
return nullptr;
}

TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
return Construct(getInterp(), scope, arena);
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: multilevel pointer conversion from 'void **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]

      JC.InvokeConstructor(&arena, count, {},
                           ^

TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
return Construct(getInterp(), scope, arena);
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
TCppIndex_t count /*=1UL*/) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

                           (void*)~0); // Tell Invoke to use placement new.
                           ^

TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
return Construct(getInterp(), scope, arena);
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
TCppIndex_t count /*=1UL*/) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: integer to pointer cast pessimizes optimization opportunities [performance-no-int-to-ptr]

                           (void*)~0); // Tell Invoke to use placement new.
                           ^

}

void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
bool withFree) {
bool withFree, TCppIndex_t nary) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: multilevel pointer conversion from 'void **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]

    JC.InvokeConstructor(&obj, count, {}, nullptr);
                         ^

auto* Class = static_cast<Decl*>(scope);
Destruct(getInterp(), This, Class, withFree);
Destruct(getInterp(), This, Class, withFree, count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: function 'Destruct' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]

Suggested change
Destruct(getInterp(), This, Class, withFree, count);
static void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

return obj;
}
return nullptr;
}

TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
return Construct(getInterp(), scope, arena);
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "operator delete" is directly included [misc-include-cleaner]

  ::operator delete(address, bytes);
    ^

}

void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
bool withFree) {
bool withFree, TCppIndex_t nary) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

  auto* Class = (Decl*)scope;
                ^

return;
}
// FIXME: Diagnose.
}

void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: multilevel pointer conversion from 'void **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]

      JC.InvokeConstructor(&arena, count, {},
                           ^

return;
}
// FIXME: Diagnose.
}

void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
TCppIndex_t count /*=0UL*/) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

                           (void*)~0); // Tell Invoke to use placement new.
                           ^

return;
}
// FIXME: Diagnose.
}

void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/,
TCppIndex_t count /*=0UL*/) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: integer to pointer cast pessimizes optimization opportunities [performance-no-int-to-ptr]

                           (void*)~0); // Tell Invoke to use placement new.
                           ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants