Skip to content

Commit 786ebee

Browse files
aaronj0vgvassilev
andauthored
Update GetFunctionRequiredArgs for template functions (#220)
* Update GetFunctionRequiredArgs * Update lib/Interpreter/CppInterOp.cpp --------- Co-authored-by: Vassil Vassilev <[email protected]>
1 parent c13391b commit 786ebee

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/Interpreter/CppInterOp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,10 @@ namespace Cpp {
847847
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl> (D)) {
848848
return FD->getMinRequiredArguments();
849849
}
850+
851+
if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
852+
return (FD->getTemplatedDecl())->getMinRequiredArguments();
853+
850854
return 0;
851855
}
852856

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,21 +339,38 @@ TEST(FunctionReflectionTest, GetFunctionNumArgs) {
339339
}
340340

341341
TEST(FunctionReflectionTest, GetFunctionRequiredArgs) {
342-
std::vector<Decl*> Decls, SubDecls;
342+
std::vector<Decl*> Decls, TemplateSubDecls;
343343
std::string code = R"(
344344
void f1() {}
345345
void f2(int i, double d, long l, char ch) {}
346346
void f3(int i, double d, long l = 0, char ch = 'a') {}
347347
void f4(int i = 0, double d = 0.0, long l = 0, char ch = 'a') {}
348348
int a;
349+
350+
class MyTemplatedMethodClass {
351+
template<class A, class B>
352+
long get_size(A, B, int i = 0) {}
353+
354+
template<class A = int, class B = char>
355+
long get_size(int i, A a = A(), B b = B()) {}
356+
357+
template<class A>
358+
void get_size(long k, A, char ch = 'a', double l = 0.0) {}
359+
};
349360
)";
350361

351362
GetAllTopLevelDecls(code, Decls);
363+
GetAllSubDecls(Decls[5], TemplateSubDecls);
364+
352365
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[0]), (size_t) 0);
353366
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[1]), (size_t) 4);
354367
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[2]), (size_t) 2);
355368
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[3]), (size_t) 0);
356369
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[4]), 0);
370+
371+
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(TemplateSubDecls[1]), 2);
372+
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(TemplateSubDecls[2]), 1);
373+
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(TemplateSubDecls[3]), 2);
357374
}
358375

359376
TEST(FunctionReflectionTest, GetFunctionArgType) {

0 commit comments

Comments
 (0)