Skip to content

Commit

Permalink
Add test for GetFunctionUsingArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-panda authored and vgvassilev committed Jul 16, 2024
1 parent cff06c7 commit 723af42
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ namespace Cpp {
using TCppFuncAddr_t = void*;
using TInterp_t = void*;
using TCppObject_t = void*;

struct TemplateArgInfo {
TCppType_t m_Type;
const char* m_IntegralValue;
TemplateArgInfo(TCppScope_t type, const char* integral_value = nullptr)
: m_Type(type), m_IntegralValue(integral_value) {}
};

/// A class modeling function calls for functions produced by the interpreter
/// in compiled code. It provides an information if we are calling a standard
/// function, constructor or destructor.
Expand Down Expand Up @@ -318,6 +326,13 @@ namespace Cpp {
CPPINTEROP_API std::vector<TCppFunction_t>
GetFunctionsUsingName(TCppScope_t scope, const std::string& name);

TCppFunction_t GetFunctionUsingArgs(TCppScope_t scope,
const std::string& name,
TCppType_t* arg_types,
size_t arg_types_size,
TemplateArgInfo* template_args,
size_t template_args_size);

/// Gets the return type of the provided function.
CPPINTEROP_API TCppType_t GetFunctionReturnType(TCppFunction_t func);

Expand Down
77 changes: 77 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,83 @@ TEST(FunctionReflectionTest, GetClassDecls) {
EXPECT_EQ(Cpp::GetName(methods[3]) , Cpp::GetName(SubDecls[8]));
}

TEST(FunctionReflectionTest, GetFunctionUsingArgs) {
std::vector<Decl*> Decls;
std::string code = R"(
// Overloaded functions
void f1(int a) {}
void f1() {}
// Templated functions
template <typename T>
void f2() { T t; }
// Templated functions deducible from args
template <typename T>
void f3(const T& t1, T* t2) {}
// Overloaded templated functions
template <typename T>
void f4() { T t; }
template <typename T1, typename T2>
void f4() { T1 t1; T2 t2; }
)";

auto get_function_name_using_args =
[&](Cpp::TCppScope_t scope, const std::string& name,
std::vector<Cpp::TCppType_t> arg_types,
std::vector<Cpp::TemplateArgInfo> template_args = {}) {
Cpp::TCppFunction_t function = Cpp::GetFunctionUsingArgs(
scope, name, arg_types.data(), arg_types.size(),
template_args.data(), template_args.size());

if (function == (void*)-1 || function == 0)
return std::string("");

return Cpp::GetFunctionSignature(function);
};

std::vector<Cpp::TCppType_t> arg_types;
std::vector<Cpp::TemplateArgInfo> template_args;

arg_types = {};
EXPECT_EQ(get_function_name_using_args(nullptr, "f1", arg_types),
"void f1()");
arg_types = {Cpp::GetType("int")};
EXPECT_EQ(get_function_name_using_args(nullptr, "f1", arg_types),
"void f1(int a)");

arg_types = {};
template_args = {{Cpp::GetType("int")}};
EXPECT_EQ(
get_function_name_using_args(nullptr, "f2", arg_types, template_args),
"void f2()");
arg_types = {};
template_args = {{Cpp::GetType("double")}};
EXPECT_EQ(
get_function_name_using_args(nullptr, "f2", arg_types, template_args),
"void f2()");

arg_types = {Cpp::GetType("const std::string &"),
Cpp::GetType("std::string *")};
EXPECT_EQ(get_function_name_using_args(nullptr, "f3", arg_types),
"void f3(const std::string &t1, std::string *t2)");
arg_types = {Cpp::GetType("const int &"), Cpp::GetType("int *")};
EXPECT_EQ(get_function_name_using_args(nullptr, "f3", arg_types),
"void f3(const int &t1, int *t2)");

arg_types = {};
template_args = {{Cpp::GetType("double")}};
EXPECT_EQ(
get_function_name_using_args(nullptr, "f4", arg_types, template_args),
"void f4()");
arg_types = {};
template_args = {{Cpp::GetType("double")}, {Cpp::GetType("int")}};
EXPECT_EQ(
get_function_name_using_args(nullptr, "f4", arg_types, template_args),
"void f4()");
}

TEST(FunctionReflectionTest, GetFunctionReturnType) {
std::vector<Decl*> Decls, SubDecls, TemplateSubDecls;
std::string code = R"(
Expand Down

0 comments on commit 723af42

Please sign in to comment.