diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index e4ef404a7..5d6988daa 100755 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -958,20 +958,25 @@ namespace Cpp { return ""; auto *D = (clang::Decl *) func; - if (auto *FD = llvm::dyn_cast(D)) { - std::string Signature; - raw_string_ostream SS(Signature); - PrintingPolicy Policy = getASTContext().getPrintingPolicy(); - // Skip printing the body - Policy.TerseOutput = true; - Policy.FullyQualifiedName = true; - Policy.SuppressDefaultTemplateArgs = false; - FD->print(SS, Policy); - SS.flush(); - return Signature; - } + clang::FunctionDecl* FD; + + if (llvm::dyn_cast(D)) + FD = llvm::dyn_cast(D); + else if (auto* FTD = llvm::dyn_cast(D)) + FD = FTD->getTemplatedDecl(); + else + return ""; - return ""; + std::string Signature; + raw_string_ostream SS(Signature); + PrintingPolicy Policy = getASTContext().getPrintingPolicy(); + // Skip printing the body + Policy.TerseOutput = true; + Policy.FullyQualifiedName = true; + Policy.SuppressDefaultTemplateArgs = false; + FD->print(SS, Policy); + SS.flush(); + return Signature; } // Internal functions that are not needed outside the library are diff --git a/unittests/CppInterOp/FunctionReflectionTest.cpp b/unittests/CppInterOp/FunctionReflectionTest.cpp index 8862fbd72..77e39cbca 100644 --- a/unittests/CppInterOp/FunctionReflectionTest.cpp +++ b/unittests/CppInterOp/FunctionReflectionTest.cpp @@ -464,6 +464,8 @@ TEST(FunctionReflectionTest, GetFunctionSignature) { std::string code = R"( class C { void f(int i, double d, long l = 0, char ch = 'a') {} + template + void ft(T a) {} }; namespace N @@ -471,6 +473,8 @@ TEST(FunctionReflectionTest, GetFunctionSignature) { void f(int i, double d, long l = 0, char ch = 'a') {} } + template + void ft(T a) {} void f1() {} C f2(int i, double d, long l = 0, char ch = 'a') { return C(); } C *f3(int i, double d, long l = 0, char ch = 'a') { return new C(); } @@ -482,18 +486,19 @@ TEST(FunctionReflectionTest, GetFunctionSignature) { GetAllSubDecls(Decls[0], Decls); GetAllSubDecls(Decls[1], Decls); - EXPECT_EQ(Cpp::GetFunctionSignature(Decls[2]), "void f1()"); - EXPECT_EQ(Cpp::GetFunctionSignature(Decls[3]), - "C f2(int i, double d, long l = 0, char ch = 'a')"); + EXPECT_EQ(Cpp::GetFunctionSignature(Decls[2]), "void ft(T a)"); + EXPECT_EQ(Cpp::GetFunctionSignature(Decls[3]), "void f1()"); EXPECT_EQ(Cpp::GetFunctionSignature(Decls[4]), - "C *f3(int i, double d, long l = 0, char ch = 'a')"); + "C f2(int i, double d, long l = 0, char ch = 'a')"); EXPECT_EQ(Cpp::GetFunctionSignature(Decls[5]), - "void f4(int i = 0, double d = 0., long l = 0, char ch = 'a')"); + "C *f3(int i, double d, long l = 0, char ch = 'a')"); EXPECT_EQ(Cpp::GetFunctionSignature(Decls[6]), - ""); - EXPECT_EQ(Cpp::GetFunctionSignature(Decls[8]), + "void f4(int i = 0, double d = 0., long l = 0, char ch = 'a')"); + EXPECT_EQ(Cpp::GetFunctionSignature(Decls[7]), ""); + EXPECT_EQ(Cpp::GetFunctionSignature(Decls[9]), "void C::f(int i, double d, long l = 0, char ch = 'a')"); - EXPECT_EQ(Cpp::GetFunctionSignature(Decls[13]), + EXPECT_EQ(Cpp::GetFunctionSignature(Decls[10]), "void C::ft(T a)"); + EXPECT_EQ(Cpp::GetFunctionSignature(Decls[15]), "void N::f(int i, double d, long l = 0, char ch = 'a')"); EXPECT_EQ(Cpp::GetFunctionSignature(nullptr), ""); }