Skip to content
Open
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
38 changes: 38 additions & 0 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,33 @@
get_type_as_string(QT, type_name, C, Policy);
}

static bool IsCopyConstructorDeleted(QualType QT) {
CXXRecordDecl* RD = QT->getAsCXXRecordDecl();
if (!RD) {
// For types that are not C++ records (such as PODs), we assume that they
// are copyable, ie their copy constructor is not deleted.
return false;
}

RD = RD->getDefinition();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can add complete type call to the cppinterop interfaces which has probably a raii object as this operation might trigger deserialization.

assert(RD && "expecting a definition");

if (RD->hasSimpleCopyConstructor())
return false;

Check warning on line 2583 in lib/CppInterOp/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/CppInterOp/CppInterOp.cpp#L2583

Added line #L2583 was not covered by tests

for (auto* Ctor : RD->ctors()) {
if (Ctor->isCopyConstructor()) {
return Ctor->isDeleted();
}
}
Comment on lines +2585 to +2589

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (auto* Ctor : RD->ctors()) {
if (Ctor->isCopyConstructor()) {
return Ctor->isDeleted();
}
}
for (auto* Ctor : RD->ctors())
if (Ctor->isCopyConstructor())
return Ctor->isDeleted();
}


assert(0 && "did not find a copy constructor?");

Check warning on line 2591 in lib/CppInterOp/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/CppInterOp/CppInterOp.cpp#L2591

Added line #L2591 was not covered by tests
// Should never happen and the return value is somewhat arbitrary, but we did
// not see a deleted copy ctor. The user will be told if the generated code
// doesn't compile.
return false;
}

void make_narg_ctor(const FunctionDecl* FD, const unsigned N,
std::ostringstream& typedefbuf, std::ostringstream& callbuf,
const std::string& class_name, int indent_level,
Expand Down Expand Up @@ -2612,7 +2639,18 @@
} else if (isPointer) {
callbuf << "*(" << type_name.c_str() << "**)args[" << i << "]";
} else {
// By-value construction: Figure out if the type can be
// copy-constructed. This is tricky and cannot be done in a fully
// reliable way, also because std::vector<T> always defines a copy
// constructor, even if the type T is only moveable. As a heuristic, we
// only check if the copy constructor is deleted, or would be if
// implicit.
bool Move = IsCopyConstructorDeleted(QT);
if (Move)
callbuf << "static_cast<" << type_name << "&&>(";
callbuf << "*(" << type_name.c_str() << "*)args[" << i << "]";
if (Move)
callbuf << ")";
}
}
callbuf << ")";
Expand Down
27 changes: 27 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,33 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_FailingTest1) {
EXPECT_FALSE(Cpp::Declare("int y = x;"));
}

TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_DeletedCopyConstrutor) {
std::vector<Decl*> Decls;
std::vector<Decl*> SubDecls;
std::string code = R"(
struct GH {
int fMember;
GH(int m = 1) : fMember(m) {}
GH(const GH&) = delete;
GH(GH&&) = default;
};
struct GH_Default {
int fMember;
GH_Default(GH p = GH()) : fMember(p.fMember) {}
};
)";

GetAllTopLevelDecls(code, Decls, false, {"-include", "new"});
EXPECT_EQ(Decls.size(), 2);

GetAllSubDecls(Decls[1], SubDecls);
EXPECT_EQ(SubDecls.size(), 3);

EXPECT_TRUE(Cpp::IsConstructor(SubDecls[2]));
auto Fn = Cpp::MakeFunctionCallable(SubDecls[2]);
EXPECT_EQ(Fn.getKind(), Cpp::JitCall::kConstructorCall);
}

TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_IsExplicit) {
std::vector<Decl*> Decls;
std::vector<Decl*> SubDecls;
Expand Down
Loading