Skip to content
Merged
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
2 changes: 2 additions & 0 deletions lib/Interpreter/exports.ld
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@
-Wl,--export=_ZNK5clang11DeclContext6lookupENS_15DeclarationNameE
-Wl,--export=_ZNK5clang17ClassTemplateDecl18getSpecializationsEv
-Wl,--export=_ZNK5clang4Sema15getStdNamespaceEv
-Wl,--export=_ZNK5clang4Type14isFloatingTypeEv
-Wl,--export=_ZNK5clang12FunctionDecl12getNumParamsEv
-Wl,--export=__clang_Interpreter_SetValueNoAlloc
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
65 changes: 65 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "clang/Interpreter/CppInterOp.h"
#include "clang/Sema/Sema.h"

#include <llvm/ADT/ArrayRef.h>

#include "clang-c/CXCppInterOp.h"

#include "gtest/gtest.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: 'gtest/gtest.h' file not found [clang-diagnostic-error]

#include "gtest/gtest.h"
         ^

Expand Down Expand Up @@ -846,6 +848,69 @@ TEST(FunctionReflectionTest, GetClassTemplatedMethods_VariadicsAndOthers) {
"void MyClass::staticVariadic(T t, Args ...args)");
}

TEST(FunctionReflectionTest, InstantiateVariadicFunction) {
std::vector<Decl*> Decls;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: function 'TEST' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]

Suggested change
std::vector<Decl*> Decls;
TESTstatic (FunctionReflectionTest, InstantiateVariadicFunction) {

std::string code = R"(
class MyClass {};

template<typename... Args>
void VariadicFn(Args... args) {}

template<typename... Args>
void VariadicFnExtended(int fixedParam, Args... args) {}
)";

GetAllTopLevelDecls(code, Decls);
ASTContext& C = Interp->getCI()->getASTContext();

std::vector<Cpp::TemplateArgInfo> args1 = {C.DoubleTy.getAsOpaquePtr(),
C.IntTy.getAsOpaquePtr()};
auto Instance1 = Cpp::InstantiateTemplate(Decls[1], args1.data(),
/*type_size*/ args1.size());
EXPECT_TRUE(Cpp::IsTemplatedFunction(Instance1));
EXPECT_EQ(Cpp::GetFunctionSignature(Instance1),
"template<> void VariadicFn<<double, int>>(double args, int args)");

FunctionDecl* FD = cast<FunctionDecl>((Decl*)Instance1);
FunctionDecl* FnTD1 = FD->getTemplateInstantiationPattern();
EXPECT_TRUE(FnTD1->isThisDeclarationADefinition());
EXPECT_EQ(FD->getNumParams(), 2);

const TemplateArgumentList* TA1 = FD->getTemplateSpecializationArgs();
llvm::ArrayRef<TemplateArgument> Args = TA1->get(0).getPackAsArray();
EXPECT_EQ(Args.size(), 2);
EXPECT_TRUE(Args[0].getAsType()->isFloatingType());
EXPECT_TRUE(Args[1].getAsType()->isIntegerType());

// handle to MyClass type
auto MyClassType = Cpp::GetTypeFromScope(Decls[0]);
std::vector<Cpp::TemplateArgInfo> args2 = {MyClassType,
C.DoubleTy.getAsOpaquePtr()};

// instantiate VariadicFnExtended
auto Instance2 =
Cpp::InstantiateTemplate(Decls[2], args2.data(), args2.size());
EXPECT_TRUE(Cpp::IsTemplatedFunction(Instance2));

FunctionDecl* FD2 = cast<FunctionDecl>((Decl*)Instance2);
FunctionDecl* FnTD2 = FD2->getTemplateInstantiationPattern();
EXPECT_TRUE(FnTD2->isThisDeclarationADefinition());

// VariadicFnExtended has one fixed param + 2 elements in TemplateArgument
// pack
EXPECT_EQ(FD2->getNumParams(), 3);

const TemplateArgumentList* TA2 = FD2->getTemplateSpecializationArgs();
llvm::ArrayRef<TemplateArgument> PackArgs2 = TA2->get(0).getPackAsArray();
EXPECT_EQ(PackArgs2.size(), 2);

EXPECT_TRUE(PackArgs2[0].getAsType()->isRecordType()); // MyClass
EXPECT_TRUE(PackArgs2[1].getAsType()->isFloatingType()); // double
EXPECT_EQ(Cpp::GetFunctionSignature(Instance2),
"template<> void VariadicFnExtended<<MyClass, double>>(int "
"fixedParam, MyClass args, double args)");
}

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