Skip to content

Commit 6113779

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

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

include/clang/Interpreter/CppInterOp.h

+15-6
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,13 @@ namespace Cpp {
318326
CPPINTEROP_API std::vector<TCppFunction_t>
319327
GetFunctionsUsingName(TCppScope_t scope, const std::string& name);
320328

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

@@ -554,12 +569,6 @@ namespace Cpp {
554569
/// Tries to load provided objects in a string format (prettyprint).
555570
CPPINTEROP_API std::string ObjToString(const char* type, void* obj);
556571

557-
struct TemplateArgInfo {
558-
TCppType_t m_Type;
559-
const char* m_IntegralValue;
560-
TemplateArgInfo(TCppScope_t type, const char* integral_value = nullptr)
561-
: m_Type(type), m_IntegralValue(integral_value) {}
562-
};
563572
/// Builds a template instantiation for a given templated declaration.
564573
/// Offers a single interface for instantiation of class, function and
565574
/// variable templates

unittests/CppInterOp/FunctionReflectionTest.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,83 @@ 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, arg_types.data(), arg_types.size(),
308+
template_args.data(), template_args.size());
309+
310+
if (function == (void*)-1 || function == 0)
311+
return std::string("");
312+
313+
return Cpp::GetFunctionSignature(function);
314+
};
315+
316+
std::vector<Cpp::TCppType_t> arg_types;
317+
std::vector<Cpp::TemplateArgInfo> template_args;
318+
319+
arg_types = {};
320+
EXPECT_EQ(get_function_name_using_args(nullptr, "f1", arg_types),
321+
"void f1()");
322+
arg_types = {Cpp::GetType("int")};
323+
EXPECT_EQ(get_function_name_using_args(nullptr, "f1", arg_types),
324+
"void f1(int a)");
325+
326+
arg_types = {};
327+
template_args = {{Cpp::GetType("int")}};
328+
EXPECT_EQ(
329+
get_function_name_using_args(nullptr, "f2", arg_types, template_args),
330+
"void f2()");
331+
arg_types = {};
332+
template_args = {{Cpp::GetType("double")}};
333+
EXPECT_EQ(
334+
get_function_name_using_args(nullptr, "f2", arg_types, template_args),
335+
"void f2()");
336+
337+
arg_types = {Cpp::GetType("const std::string &"),
338+
Cpp::GetType("std::string *")};
339+
EXPECT_EQ(get_function_name_using_args(nullptr, "f3", arg_types),
340+
"void f3(const std::string &t1, std::string *t2)");
341+
arg_types = {Cpp::GetType("const int &"), Cpp::GetType("int *")};
342+
EXPECT_EQ(get_function_name_using_args(nullptr, "f3", arg_types),
343+
"void f3(const int &t1, int *t2)");
344+
345+
arg_types = {};
346+
template_args = {{Cpp::GetType("double")}};
347+
EXPECT_EQ(
348+
get_function_name_using_args(nullptr, "f4", arg_types, template_args),
349+
"void f4()");
350+
arg_types = {};
351+
template_args = {{Cpp::GetType("double")}, {Cpp::GetType("int")}};
352+
EXPECT_EQ(
353+
get_function_name_using_args(nullptr, "f4", arg_types, template_args),
354+
"void f4()");
355+
}
356+
280357
TEST(FunctionReflectionTest, GetFunctionReturnType) {
281358
std::vector<Decl*> Decls, SubDecls, TemplateSubDecls;
282359
std::string code = R"(

0 commit comments

Comments
 (0)