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

Add test for GetFunctionUsingArgs #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 15 additions & 6 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 Expand Up @@ -554,12 +569,6 @@ namespace Cpp {
/// Tries to load provided objects in a string format (prettyprint).
CPPINTEROP_API std::string ObjToString(const char* type, void* obj);

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) {}
};
/// Builds a template instantiation for a given templated declaration.
/// Offers a single interface for instantiation of class, function and
/// variable templates
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
Loading