Skip to content
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

fix MakeFunctionCallable's codegen when return type is a function pointer #502

Merged
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
15 changes: 11 additions & 4 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,9 @@ namespace Cpp {
//
ASTContext& C = FD->getASTContext();
PrintingPolicy Policy(C.getPrintingPolicy());
#if CLANG_VERSION_MAJOR > 16
Policy.SuppressElaboration = true;
#endif
refType = kNotReference;
if (QT->isRecordType() && forArgument) {
get_type_as_string(QT, type_name, C, Policy);
Expand Down Expand Up @@ -1984,18 +1987,22 @@ namespace Cpp {
EReferenceType refType = kNotReference;
bool isPointer = false;

std::ostringstream typedefbuf;
std::ostringstream callbuf;

collect_type_info(FD, QT, typedefbuf, callbuf, type_name, refType,
isPointer, indent_level, false);

buf << typedefbuf.str();

buf << "if (ret) {\n";
++indent_level;
{
std::ostringstream typedefbuf;
std::ostringstream callbuf;
//
// Write the placement part of the placement new.
//
indent(callbuf, indent_level);
callbuf << "new (ret) ";
collect_type_info(FD, QT, typedefbuf, callbuf, type_name, refType,
isPointer, indent_level, false);
//
// Write the type part of the placement new.
//
Expand Down
15 changes: 15 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,9 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
int f3() { return 3; }

extern "C" int f4() { return 4; }

typedef int(*int_func)(void);
int_func f5() { return f3; }
}
)");

Expand Down Expand Up @@ -920,6 +923,18 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
FCI4.Invoke(&ret4);
EXPECT_EQ(ret4, 4);

#if CLANG_VERSION_MAJOR > 16
Cpp::JitCall FCI5 =
Cpp::MakeFunctionCallable(Cpp::GetNamed("f5", Cpp::GetNamed("NS")));
EXPECT_TRUE(FCI5.getKind() == Cpp::JitCall::kGenericCall);

typedef int (*int_func)();
int_func callback = nullptr;
FCI5.Invoke((void*)&callback);
EXPECT_TRUE(callback);
EXPECT_EQ(callback(), 3);
#endif

// FIXME: Do we need to support private ctors?
Interp->process(R"(
class C {
Expand Down
Loading