Skip to content

Commit 360a117

Browse files
fix GetFunctionReturnType for templated class's method (#624)
1 parent 47f7389 commit 360a117

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -913,12 +913,22 @@ TCppType_t GetFunctionReturnType(TCppFunction_t func) {
913913
auto* D = (clang::Decl*)func;
914914
if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D)) {
915915
QualType Type = FD->getReturnType();
916-
if (Type->isUndeducedAutoType() && IsTemplatedFunction(FD) &&
917-
!FD->isDefined()) {
916+
if (Type->isUndeducedAutoType()) {
917+
bool needInstantiation = false;
918+
if (IsTemplatedFunction(FD) && !FD->isDefined())
919+
needInstantiation = true;
920+
if (auto* MD = llvm::dyn_cast<clang::CXXMethodDecl>(FD)) {
921+
if (IsTemplateSpecialization(MD->getParent()))
922+
needInstantiation = true;
923+
}
924+
925+
if (needInstantiation) {
918926
#ifdef CPPINTEROP_USE_CLING
919-
cling::Interpreter::PushTransactionRAII RAII(&getInterp());
927+
cling::Interpreter::PushTransactionRAII RAII(&getInterp());
920928
#endif
921-
getSema().InstantiateFunctionDefinition(SourceLocation(), FD, true, true);
929+
getSema().InstantiateFunctionDefinition(SourceLocation(), FD, true,
930+
true);
931+
}
922932
Type = FD->getReturnType();
923933
}
924934
return Type.getAsOpaquePtr();

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ TEST(FunctionReflectionTest, GetFunctionReturnType) {
385385
template<class ...T> auto rttest_make_tlist(T ... args) {
386386
return RTTest_TemplatedList<T...>{};
387387
}
388+
389+
template<typename T>
390+
struct Klass {
391+
auto func(T t) { return t; }
392+
};
388393
)";
389394

390395
GetAllTopLevelDecls(code, Decls, true);
@@ -430,6 +435,11 @@ TEST(FunctionReflectionTest, GetFunctionReturnType) {
430435
Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(
431436
Cpp::BestOverloadFunctionMatch(candidates, explicit_args, args))),
432437
"RTTest_TemplatedList<int, double>");
438+
439+
std::vector<Cpp::TemplateArgInfo> args2 = {C.DoubleTy.getAsOpaquePtr()};
440+
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Cpp::GetNamed(
441+
"func", Cpp::InstantiateTemplate(Decls[15], args2.data(), 1)))),
442+
"double");
433443
}
434444

435445
TEST(FunctionReflectionTest, GetFunctionNumArgs) {

0 commit comments

Comments
 (0)