Skip to content

Update GetFunctionArgName API for template functions #224

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

Merged
merged 2 commits into from
Apr 10, 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
8 changes: 6 additions & 2 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3047,8 +3047,12 @@ namespace Cpp {
std::string GetFunctionArgName(TCppFunction_t func, TCppIndex_t param_index)
{
auto *D = (clang::Decl *)func;
auto *FD = llvm::cast<clang::FunctionDecl>(D);
auto PI = FD->getParamDecl(param_index);
clang::ParmVarDecl* PI;
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 'PI' is not initialized [cppcoreguidelines-init-variables]

Suggested change
clang::ParmVarDecl* PI;
clang::ParmVarDecl* PI = nullptr;


if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D))
PI = FD->getParamDecl(param_index);
else if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
PI = (FD->getTemplatedDecl())->getParamDecl(param_index);

return PI->getNameAsString();
}
Expand Down
22 changes: 22 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,15 @@ TEST(FunctionReflectionTest, GetFunctionArgName) {
std::string code = R"(
void f1(int i, double d, long l, char ch) {}
void f2(const int i, double d[], long *l, char ch[4]) {}

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);
Expand All @@ -829,6 +838,19 @@ TEST(FunctionReflectionTest, GetFunctionArgName) {
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[1], 1), "d");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[1], 2), "l");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[1], 3), "ch");

EXPECT_EQ(Cpp::GetFunctionArgName(Decls[2], 0), "");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[2], 1), "");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[2], 2), "i");

EXPECT_EQ(Cpp::GetFunctionArgName(Decls[3], 0), "i");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[3], 1), "a");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[3], 2), "b");

EXPECT_EQ(Cpp::GetFunctionArgName(Decls[4], 0), "k");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[4], 1), "");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[4], 2), "ch");
EXPECT_EQ(Cpp::GetFunctionArgName(Decls[4], 3), "l");
}

TEST(FunctionReflectionTest, GetFunctionArgDefault) {
Expand Down