Skip to content

Commit 9c818c5

Browse files
fix MakeFunctionCallable's codegen when return type is a function pointer (#502)
1 parent 6c6f94a commit 9c818c5

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,9 @@ namespace Cpp {
16751675
//
16761676
ASTContext& C = FD->getASTContext();
16771677
PrintingPolicy Policy(C.getPrintingPolicy());
1678+
#if CLANG_VERSION_MAJOR > 16
1679+
Policy.SuppressElaboration = true;
1680+
#endif
16781681
refType = kNotReference;
16791682
if (QT->isRecordType() && forArgument) {
16801683
get_type_as_string(QT, type_name, C, Policy);
@@ -1989,18 +1992,22 @@ namespace Cpp {
19891992
EReferenceType refType = kNotReference;
19901993
bool isPointer = false;
19911994

1995+
std::ostringstream typedefbuf;
1996+
std::ostringstream callbuf;
1997+
1998+
collect_type_info(FD, QT, typedefbuf, callbuf, type_name, refType,
1999+
isPointer, indent_level, false);
2000+
2001+
buf << typedefbuf.str();
2002+
19922003
buf << "if (ret) {\n";
19932004
++indent_level;
19942005
{
1995-
std::ostringstream typedefbuf;
1996-
std::ostringstream callbuf;
19972006
//
19982007
// Write the placement part of the placement new.
19992008
//
20002009
indent(callbuf, indent_level);
20012010
callbuf << "new (ret) ";
2002-
collect_type_info(FD, QT, typedefbuf, callbuf, type_name, refType,
2003-
isPointer, indent_level, false);
20042011
//
20052012
// Write the type part of the placement new.
20062013
//

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,9 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
888888
int f3() { return 3; }
889889
890890
extern "C" int f4() { return 4; }
891+
892+
typedef int(*int_func)(void);
893+
int_func f5() { return f3; }
891894
}
892895
)");
893896

@@ -923,6 +926,18 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
923926
FCI4.Invoke(&ret4);
924927
EXPECT_EQ(ret4, 4);
925928

929+
#if CLANG_VERSION_MAJOR > 16
930+
Cpp::JitCall FCI5 =
931+
Cpp::MakeFunctionCallable(Cpp::GetNamed("f5", Cpp::GetNamed("NS")));
932+
EXPECT_TRUE(FCI5.getKind() == Cpp::JitCall::kGenericCall);
933+
934+
typedef int (*int_func)();
935+
int_func callback = nullptr;
936+
FCI5.Invoke((void*)&callback);
937+
EXPECT_TRUE(callback);
938+
EXPECT_EQ(callback(), 3);
939+
#endif
940+
926941
// FIXME: Do we need to support private ctors?
927942
Interp->process(R"(
928943
class C {

0 commit comments

Comments
 (0)