Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GetFunctionNumArgs API for template functions #219

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'Decls' is not initialized [cppcoreguidelines-init-variables]

Suggested change
std::vector<Decl*> Decls, TemplateSubDecls;
std::vector<Decl*> Decls = 0, 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
Loading