Skip to content

Commit 1272d1b

Browse files
aaronj0vgvassilev
authored andcommitted
Update GetFunctionNumArgs
1 parent 32efeba commit 1272d1b

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,12 @@ namespace Cpp {
828828
TCppIndex_t GetFunctionNumArgs(TCppFunction_t func)
829829
{
830830
auto *D = (clang::Decl *) func;
831-
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D)) {
831+
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D))
832832
return FD->getNumParams();
833-
}
833+
834+
if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
835+
return (FD->getTemplatedDecl())->getNumParams();
836+
834837
return 0;
835838
}
836839

@@ -882,9 +885,8 @@ namespace Cpp {
882885
// encompassed in an anonymous namespace as follows.
883886
namespace {
884887
bool IsTemplatedFunction(Decl *D) {
885-
if (llvm::isa_and_nonnull<FunctionTemplateDecl>(D)) {
888+
if (llvm::isa_and_nonnull<FunctionTemplateDecl>(D))
886889
return true;
887-
}
888890

889891
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D)) {
890892
auto TK = FD->getTemplatedKind();

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,42 @@ TEST(FunctionReflectionTest, GetFunctionReturnType) {
267267
}
268268

269269
TEST(FunctionReflectionTest, GetFunctionNumArgs) {
270-
std::vector<Decl*> Decls, SubDecls;
270+
std::vector<Decl*> Decls, TemplateSubDecls;
271271
std::string code = R"(
272272
void f1() {}
273273
void f2(int i, double d, long l, char ch) {}
274274
void f3(int i, double d, long l = 0, char ch = 'a') {}
275275
void f4(int i = 0, double d = 0.0, long l = 0, char ch = 'a') {}
276276
int a;
277+
278+
class MyTemplatedMethodClass {
279+
template<class A>
280+
char get_string(A, int i) {
281+
return 'A';
282+
}
283+
284+
template<class A>
285+
void get_size() {}
286+
287+
template<class A, class B>
288+
long add_size (A, int i, B) {
289+
return sizeof(A) + i;
290+
}
291+
};
292+
277293
)";
278294

279295
GetAllTopLevelDecls(code, Decls);
296+
GetAllSubDecls(Decls[5], TemplateSubDecls);
280297
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[0]), (size_t) 0);
281298
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[1]), (size_t) 4);
282299
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[2]), (size_t) 4);
283300
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[3]), (size_t) 4);
284301
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[4]), 0);
302+
303+
EXPECT_EQ(Cpp::GetFunctionNumArgs(TemplateSubDecls[1]), 2);
304+
EXPECT_EQ(Cpp::GetFunctionNumArgs(TemplateSubDecls[2]), 0);
305+
EXPECT_EQ(Cpp::GetFunctionNumArgs(TemplateSubDecls[3]), 3);
285306
}
286307

287308
TEST(FunctionReflectionTest, GetFunctionRequiredArgs) {

0 commit comments

Comments
 (0)