Skip to content

Commit 83d43df

Browse files
authored
Fix GetFunctionSignature to work for FunctionTemplateDecls (#536)
1 parent 6dc5278 commit 83d43df

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -963,20 +963,25 @@ namespace Cpp {
963963
return "<unknown>";
964964

965965
auto *D = (clang::Decl *) func;
966-
if (auto *FD = llvm::dyn_cast<FunctionDecl>(D)) {
967-
std::string Signature;
968-
raw_string_ostream SS(Signature);
969-
PrintingPolicy Policy = getASTContext().getPrintingPolicy();
970-
// Skip printing the body
971-
Policy.TerseOutput = true;
972-
Policy.FullyQualifiedName = true;
973-
Policy.SuppressDefaultTemplateArgs = false;
974-
FD->print(SS, Policy);
975-
SS.flush();
976-
return Signature;
977-
}
966+
clang::FunctionDecl* FD;
967+
968+
if (llvm::dyn_cast<FunctionDecl>(D))
969+
FD = llvm::dyn_cast<FunctionDecl>(D);
970+
else if (auto* FTD = llvm::dyn_cast<clang::FunctionTemplateDecl>(D))
971+
FD = FTD->getTemplatedDecl();
972+
else
973+
return "<unknown>";
978974

979-
return "<unknown>";
975+
std::string Signature;
976+
raw_string_ostream SS(Signature);
977+
PrintingPolicy Policy = getASTContext().getPrintingPolicy();
978+
// Skip printing the body
979+
Policy.TerseOutput = true;
980+
Policy.FullyQualifiedName = true;
981+
Policy.SuppressDefaultTemplateArgs = false;
982+
FD->print(SS, Policy);
983+
SS.flush();
984+
return Signature;
980985
}
981986

982987
// Internal functions that are not needed outside the library are

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,17 @@ TEST(FunctionReflectionTest, GetFunctionSignature) {
497497
std::string code = R"(
498498
class C {
499499
void f(int i, double d, long l = 0, char ch = 'a') {}
500+
template<typename T>
501+
void ft(T a) {}
500502
};
501503
502504
namespace N
503505
{
504506
void f(int i, double d, long l = 0, char ch = 'a') {}
505507
}
506508
509+
template<typename T>
510+
void ft(T a) {}
507511
void f1() {}
508512
C f2(int i, double d, long l = 0, char ch = 'a') { return C(); }
509513
C *f3(int i, double d, long l = 0, char ch = 'a') { return new C(); }
@@ -515,18 +519,19 @@ TEST(FunctionReflectionTest, GetFunctionSignature) {
515519
GetAllSubDecls(Decls[0], Decls);
516520
GetAllSubDecls(Decls[1], Decls);
517521

518-
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[2]), "void f1()");
519-
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[3]),
520-
"C f2(int i, double d, long l = 0, char ch = 'a')");
522+
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[2]), "void ft(T a)");
523+
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[3]), "void f1()");
521524
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[4]),
522-
"C *f3(int i, double d, long l = 0, char ch = 'a')");
525+
"C f2(int i, double d, long l = 0, char ch = 'a')");
523526
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[5]),
524-
"void f4(int i = 0, double d = 0., long l = 0, char ch = 'a')");
527+
"C *f3(int i, double d, long l = 0, char ch = 'a')");
525528
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[6]),
526-
"<unknown>");
527-
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[8]),
529+
"void f4(int i = 0, double d = 0., long l = 0, char ch = 'a')");
530+
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[7]), "<unknown>");
531+
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[9]),
528532
"void C::f(int i, double d, long l = 0, char ch = 'a')");
529-
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[13]),
533+
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[10]), "void C::ft(T a)");
534+
EXPECT_EQ(Cpp::GetFunctionSignature(Decls[15]),
530535
"void N::f(int i, double d, long l = 0, char ch = 'a')");
531536
EXPECT_EQ(Cpp::GetFunctionSignature(nullptr), "<unknown>");
532537
}

0 commit comments

Comments
 (0)