Skip to content

Commit

Permalink
fix MakeFunctionCallable's codegen when return type is a function poi…
Browse files Browse the repository at this point in the history
…nter (#502)
  • Loading branch information
Vipul-Cariappa authored Feb 18, 2025
1 parent 6c6f94a commit 9c818c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,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 @@ -1989,18 +1992,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 @@ -888,6 +888,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 @@ -923,6 +926,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

0 comments on commit 9c818c5

Please sign in to comment.