Skip to content
Merged
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
50 changes: 50 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,56 @@ TYPED_TEST(CppInterOpTest, FunctionReflectionTestInstantiateVariadicFunction) {
"fixedParam, MyClass args, double args)");
}

TYPED_TEST(CppInterOpTest, FunctionReflectionTestBestOverloadFunctionMatch0) {
// make sure templates are not instantiated multiple times
std::vector<Decl*> Decls;
std::string code = R"(
template <typename T1, typename T2 = int>
void Tfn(T1& t1, T2& t2) {}
)";
GetAllTopLevelDecls(code, Decls);
EXPECT_EQ(Decls.size(), 1);

std::vector<Cpp::TCppFunction_t> candidates;
candidates.reserve(Decls.size());
for (auto* i : Decls)
candidates.push_back(i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation]

unittests/CppInterOp/FunctionReflectionTest.cpp:986:

-   for (auto i : Decls)
+   candidates.reserve(Decls.size());
+ for (auto i : Decls)


ASTContext& C = Interp->getCI()->getASTContext();

std::vector<Cpp::TemplateArgInfo> args0 = {
C.getLValueReferenceType(C.DoubleTy).getAsOpaquePtr(),
C.getLValueReferenceType(C.IntTy).getAsOpaquePtr(),
};

std::vector<Cpp::TemplateArgInfo> explicit_args0;
std::vector<Cpp::TemplateArgInfo> explicit_args1 = {
C.DoubleTy.getAsOpaquePtr()};
std::vector<Cpp::TemplateArgInfo> explicit_args2 = {
C.DoubleTy.getAsOpaquePtr(),
C.IntTy.getAsOpaquePtr(),
};

Cpp::TCppScope_t fn0 =
Cpp::BestOverloadFunctionMatch(candidates, explicit_args0, args0);
EXPECT_TRUE(fn0);

Cpp::TCppScope_t fn =
Cpp::BestOverloadFunctionMatch(candidates, explicit_args1, args0);
EXPECT_EQ(fn, fn0);

fn = Cpp::BestOverloadFunctionMatch(candidates, explicit_args2, args0);
EXPECT_EQ(fn, fn0);

fn = Cpp::InstantiateTemplate(Decls[0], explicit_args1.data(),
explicit_args1.size());
EXPECT_EQ(fn, fn0);

fn = Cpp::InstantiateTemplate(Decls[0], explicit_args2.data(),
explicit_args2.size());
EXPECT_EQ(fn, fn0);
}

TYPED_TEST(CppInterOpTest, FunctionReflectionTestBestOverloadFunctionMatch1) {
std::vector<Decl*> Decls;
std::string code = R"(
Expand Down
Loading