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
  • Loading branch information
Vipul-Cariappa committed Feb 16, 2025
1 parent fc852d5 commit 3dcb1c8
Show file tree
Hide file tree
Showing 2 changed files with 23 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 @@ -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
12 changes: 12 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 All @@ -900,6 +903,9 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
Cpp::JitCall FCI4 =
Cpp::MakeFunctionCallable(Cpp::GetNamed("f4", Cpp::GetNamed("NS")));
EXPECT_TRUE(FCI4.getKind() == Cpp::JitCall::kGenericCall);
Cpp::JitCall FCI5 =
Cpp::MakeFunctionCallable(Cpp::GetNamed("f5", Cpp::GetNamed("NS")));
EXPECT_TRUE(FCI5.getKind() == Cpp::JitCall::kGenericCall);

int i = 9, ret1, ret3, ret4;
std::string s("Hello World!\n");
Expand All @@ -920,6 +926,12 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
FCI4.Invoke(&ret4);
EXPECT_EQ(ret4, 4);

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

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

0 comments on commit 3dcb1c8

Please sign in to comment.