Skip to content

Commit a244a5f

Browse files
sudo-pandavgvassilev
authored andcommitted
Add test for GetFunctionUsingArgs
1 parent cff06c7 commit a244a5f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ namespace Cpp {
3535
using TCppFuncAddr_t = void*;
3636
using TInterp_t = void*;
3737
using TCppObject_t = void*;
38+
39+
struct TemplateArgInfo {
40+
TCppType_t m_Type;
41+
const char* m_IntegralValue;
42+
TemplateArgInfo(TCppScope_t type, const char* integral_value = nullptr)
43+
: m_Type(type), m_IntegralValue(integral_value) {}
44+
};
45+
3846
/// A class modeling function calls for functions produced by the interpreter
3947
/// in compiled code. It provides an information if we are calling a standard
4048
/// function, constructor or destructor.
@@ -318,6 +326,11 @@ namespace Cpp {
318326
CPPINTEROP_API std::vector<TCppFunction_t>
319327
GetFunctionsUsingName(TCppScope_t scope, const std::string& name);
320328

329+
TCppFunction_t GetFunctionUsingArgs(
330+
TCppScope_t scope, const std::string& name,
331+
TCppType_t* arg_types, size_t arg_types_size,
332+
TemplateArgInfo* template_args, size_t template_args_size);
333+
321334
/// Gets the return type of the provided function.
322335
CPPINTEROP_API TCppType_t GetFunctionReturnType(TCppFunction_t func);
323336

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,71 @@ TEST(FunctionReflectionTest, GetClassDecls) {
277277
EXPECT_EQ(Cpp::GetName(methods[3]) , Cpp::GetName(SubDecls[8]));
278278
}
279279

280+
TEST(FunctionReflectionTest, GetFunctionUsingArgs) {
281+
std::vector<Decl*> Decls;
282+
std::string code = R"(
283+
// Overloaded functions
284+
void f1(int a) {}
285+
void f1() {}
286+
287+
// Templated functions
288+
template <typename T>
289+
void f2() { T t; }
290+
291+
// Templated functions deducible from args
292+
template <typename T>
293+
void f3(const T& t1, T* t2) {}
294+
295+
// Overloaded templated functions
296+
template <typename T>
297+
void f4() { T t; }
298+
template <typename T1, typename T2>
299+
void f4() { T1 t1; T2 t2; }
300+
)";
301+
302+
auto get_function_name_using_args =
303+
[&](Cpp::TCppScope_t scope, const std::string& name,
304+
std::vector<Cpp::TCppType_t> arg_types,
305+
std::vector<Cpp::TemplateArgInfo> template_args = {}){
306+
Cpp::TCppFunction_t function = Cpp::GetFunctionUsingArgs(
307+
scope, name,
308+
arg_types.data(), arg_types.size(),
309+
template_args.data(), template_args.size());
310+
311+
if (function == (void *) -1 || function == 0)
312+
return std::string("");
313+
314+
return Cpp::GetFunctionSignature(function);
315+
};
316+
317+
std::vector<Cpp::TCppType_t> arg_types;
318+
std::vector<Cpp::TemplateArgInfo> template_args;
319+
320+
arg_types = {};
321+
EXPECT_EQ(get_function_name_using_args(nullptr, "f1", arg_types), "void f1()");
322+
arg_types = {Cpp::GetType("int")};
323+
EXPECT_EQ(get_function_name_using_args(nullptr, "f1", arg_types), "void f1(int a)");
324+
325+
arg_types = {};
326+
template_args = {{Cpp::GetType("int")}};
327+
EXPECT_EQ(get_function_name_using_args(nullptr, "f2", arg_types, template_args), "void f2()");
328+
arg_types = {};
329+
template_args = {{Cpp::GetType("double")}};
330+
EXPECT_EQ(get_function_name_using_args(nullptr, "f2", arg_types, template_args), "void f2()");
331+
332+
arg_types = {Cpp::GetType("const std::string &"), Cpp::GetType("std::string *")};
333+
EXPECT_EQ(get_function_name_using_args(nullptr, "f3", arg_types), "void f3(const std::string &t1, std::string *t2)");
334+
arg_types = {Cpp::GetType("const int &"), Cpp::GetType("int *")};
335+
EXPECT_EQ(get_function_name_using_args(nullptr, "f3", arg_types), "void f3(const int &t1, int *t2)");
336+
337+
arg_types = {};
338+
template_args = {{Cpp::GetType("double")}};
339+
EXPECT_EQ(get_function_name_using_args(nullptr, "f4", arg_types, template_args), "void f4()");
340+
arg_types = {};
341+
template_args = {{Cpp::GetType("double")}, {Cpp::GetType("int")}};
342+
EXPECT_EQ(get_function_name_using_args(nullptr, "f4", arg_types, template_args), "void f4()");
343+
}
344+
280345
TEST(FunctionReflectionTest, GetFunctionReturnType) {
281346
std::vector<Decl*> Decls, SubDecls, TemplateSubDecls;
282347
std::string code = R"(

0 commit comments

Comments
 (0)