Skip to content

Commit

Permalink
Update GetFunctionRequiredArgs for template functions (#220)
Browse files Browse the repository at this point in the history
* Update GetFunctionRequiredArgs

* Update lib/Interpreter/CppInterOp.cpp

---------

Co-authored-by: Vassil Vassilev <v.g.vassilev@gmail.com>
  • Loading branch information
aaronj0 and vgvassilev authored Apr 7, 2024
1 parent c13391b commit 786ebee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,10 @@ namespace Cpp {
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl> (D)) {
return FD->getMinRequiredArguments();
}

if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
return (FD->getTemplatedDecl())->getMinRequiredArguments();

return 0;
}

Expand Down
19 changes: 18 additions & 1 deletion unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,38 @@ TEST(FunctionReflectionTest, GetFunctionNumArgs) {
}

TEST(FunctionReflectionTest, GetFunctionRequiredArgs) {
std::vector<Decl*> Decls, SubDecls;
std::vector<Decl*> Decls, TemplateSubDecls;
std::string code = R"(
void f1() {}
void f2(int i, double d, long l, char ch) {}
void f3(int i, double d, long l = 0, char ch = 'a') {}
void f4(int i = 0, double d = 0.0, long l = 0, char ch = 'a') {}
int a;
class MyTemplatedMethodClass {
template<class A, class B>
long get_size(A, B, int i = 0) {}
template<class A = int, class B = char>
long get_size(int i, A a = A(), B b = B()) {}
template<class A>
void get_size(long k, A, char ch = 'a', double l = 0.0) {}
};
)";

GetAllTopLevelDecls(code, Decls);
GetAllSubDecls(Decls[5], TemplateSubDecls);

EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[0]), (size_t) 0);
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[1]), (size_t) 4);
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[2]), (size_t) 2);
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[3]), (size_t) 0);
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(Decls[4]), 0);

EXPECT_EQ(Cpp::GetFunctionRequiredArgs(TemplateSubDecls[1]), 2);
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(TemplateSubDecls[2]), 1);
EXPECT_EQ(Cpp::GetFunctionRequiredArgs(TemplateSubDecls[3]), 2);
}

TEST(FunctionReflectionTest, GetFunctionArgType) {
Expand Down

0 comments on commit 786ebee

Please # to comment.