Skip to content

Commit

Permalink
Update GetFunctionNumArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronj0 committed Apr 6, 2024
1 parent 32efeba commit cac4731
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,12 @@ namespace Cpp {
TCppIndex_t GetFunctionNumArgs(TCppFunction_t func)
{
auto *D = (clang::Decl *) func;
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D)) {
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D))
return FD->getNumParams();
}

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

return 0;
}

Expand Down Expand Up @@ -882,9 +885,8 @@ namespace Cpp {
// encompassed in an anonymous namespace as follows.
namespace {
bool IsTemplatedFunction(Decl *D) {
if (llvm::isa_and_nonnull<FunctionTemplateDecl>(D)) {
if (llvm::isa_and_nonnull<FunctionTemplateDecl>(D))
return true;
}

if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D)) {
auto TK = FD->getTemplatedKind();
Expand Down
23 changes: 22 additions & 1 deletion unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,42 @@ TEST(FunctionReflectionTest, GetFunctionReturnType) {
}

TEST(FunctionReflectionTest, GetFunctionNumArgs) {
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>
char get_string(A, int i) {
return 'A';
}
template<class A>
void get_size() {}
template<class A, class B>
long add_size (A, int i, B) {
return sizeof(A) + i;
}
};
)";

GetAllTopLevelDecls(code, Decls);
GetAllSubDecls(Decls[5], TemplateSubDecls);
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[0]), (size_t) 0);
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[1]), (size_t) 4);
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[2]), (size_t) 4);
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[3]), (size_t) 4);
EXPECT_EQ(Cpp::GetFunctionNumArgs(Decls[4]), 0);

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

TEST(FunctionReflectionTest, GetFunctionRequiredArgs) {
Expand Down

0 comments on commit cac4731

Please sign in to comment.