Skip to content

Commit

Permalink
Teacg GetFunctionReturnType to return the type of template functions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronj0 authored Apr 7, 2024
1 parent 1272d1b commit c13391b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,10 @@ namespace Cpp {
return FD->getReturnType().getAsOpaquePtr();
}

if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D)) {
return (FD->getTemplatedDecl())->getReturnType().getAsOpaquePtr();
}

return 0;
}

Expand Down
51 changes: 42 additions & 9 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ TEST(FunctionReflectionTest, GetFunctionsUsingName) {
}

TEST(FunctionReflectionTest, GetFunctionReturnType) {
std::vector<Decl*> Decls, SubDecls;
std::vector<Decl*> Decls, SubDecls, TemplateSubDecls;
std::string code = R"(
namespace N { class C {}; }
enum Switch { OFF, ON };
Expand All @@ -238,7 +238,6 @@ TEST(FunctionReflectionTest, GetFunctionReturnType) {
int f () { return 0; }
};
void f1() {}
double f2() { return 0.2; }
Switch f3() { return ON; }
Expand All @@ -248,22 +247,56 @@ TEST(FunctionReflectionTest, GetFunctionReturnType) {
volatile N::C f7() { return N::C(); }
const volatile N::C f8() { return N::C(); }
int n;
class MyTemplatedMethodClass {
template<class A>
char get_string(A) {
return 'A';
}
template<class A>
void get_size() {}
template<class A>
long add_size (int i) {
return sizeof(A) + i;
}
};
)";

GetAllTopLevelDecls(code, Decls, true);
GetAllSubDecls(Decls[2], SubDecls);
GetAllSubDecls(Decls[12], TemplateSubDecls);

// #include <string>

EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[3])), "void");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[4])), "double");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[5])), "Switch");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[4])),
"double");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[5])),
"Switch");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[6])), "N::C");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[7])), "N::C *");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[8])), "const N::C");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[9])), "volatile N::C");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[10])), "const volatile N::C");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[11])), "NULL TYPE");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[7])),
"N::C *");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[8])),
"const N::C");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[9])),
"volatile N::C");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[10])),
"const volatile N::C");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(Decls[11])),
"NULL TYPE");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(SubDecls[1])), "void");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(SubDecls[2])), "int");
EXPECT_EQ(
Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(TemplateSubDecls[1])),
"char");
EXPECT_EQ(
Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(TemplateSubDecls[2])),
"void");
EXPECT_EQ(
Cpp::GetTypeAsString(Cpp::GetFunctionReturnType(TemplateSubDecls[3])),
"long");
}

TEST(FunctionReflectionTest, GetFunctionNumArgs) {
Expand Down

0 comments on commit c13391b

Please sign in to comment.