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
16 changes: 14 additions & 2 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "clang/AST/DeclAccessPair.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DeclarationName.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
Expand Down Expand Up @@ -1616,6 +1617,9 @@ TCppType_t GetFunctionArgType(TCppFunction_t func, TCppIndex_t iarg) {
INTEROP_TRACE(func, iarg);
auto* D = (clang::Decl*)func;

if (auto* FTD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
D = FTD->getTemplatedDecl();

if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D)) {
if (iarg < FD->getNumParams()) {
auto* PVD = FD->getParamDecl(iarg);
Expand All @@ -1626,6 +1630,12 @@ TCppType_t GetFunctionArgType(TCppFunction_t func, TCppIndex_t iarg) {
return INTEROP_RETURN(nullptr);
}

bool IsTemplateParmType(TCppType_t typ) {
INTEROP_TRACE(typ);
clang::QualType QT = clang::QualType::getFromOpaquePtr(typ);
return INTEROP_RETURN(QT->isTemplateTypeParmType());
}

std::string GetFunctionSignature(TCppFunction_t func) {
INTEROP_TRACE(func);
if (!func)
Expand Down Expand Up @@ -1873,8 +1883,10 @@ bool CheckMethodAccess(TCppFunction_t method, AccessSpecifier AS) {

bool IsMethod(TCppConstFunction_t method) {
INTEROP_TRACE(method);
return INTEROP_RETURN(
dyn_cast_or_null<CXXMethodDecl>(static_cast<const clang::Decl*>(method)));
const auto* D = static_cast<const clang::Decl*>(method);
if (const auto* FTD = dyn_cast_or_null<FunctionTemplateDecl>(D))
D = FTD->getTemplatedDecl();
return INTEROP_RETURN(dyn_cast_or_null<CXXMethodDecl>(D));
}

bool IsPublicMethod(TCppFunction_t method) {
Expand Down
8 changes: 8 additions & 0 deletions lib/CppInterOp/CppInterOp.td
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,14 @@ This function parses and instantiates a template function.}];
let Args = [Arg<"const char*", "function_template">];
}

def IsTemplateParmType : CppInterOpAPI {
let Doc = "Checks if the given type is a template parameter type.";
let ReturnType = "bool";
let Args = [
Arg<"TCppType_t", "typ">
];
}

def IsFunction : CppInterOpAPI {
let Doc = "Checks if the scope is a function.";
let ReturnType = "bool";
Expand Down
38 changes: 38 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,18 +570,56 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_GetFunctionArgType) {
void f1(int i, double d, long l, char ch) {}
void f2(const int i, double d[], long *l, char ch[4]) {}
int a;

template <typename T> void f3(T t, const T& r, int i) {}
)";

GetAllTopLevelDecls(code, Decls);
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[0], 0)), "int");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[0], 1)), "double");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[0], 2)), "long");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[0], 3)), "char");
EXPECT_EQ(Cpp::GetFunctionArgType(Decls[0], 4), nullptr);

EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[1], 0)), "const int");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[1], 1)), "double[]");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[1], 2)), "long *");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[1], 3)), "char[4]");

EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[2], 0)), "NULL TYPE");

EXPECT_TRUE(Cpp::IsTemplatedFunction(Decls[3]));
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[3], 0)), "T");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[3], 1)),
"const T &");
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetFunctionArgType(Decls[3], 2)), "int");
}

TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_IsMethod) {
std::vector<Decl*> Decls, SubDecls;
std::string code = R"(
void f1() {}

template <typename T> void f2(T t) {}

class MyClass {
void m1() {}

template <typename T>
void m2(T t) {}
};
)";

GetAllTopLevelDecls(code, Decls);
GetAllSubDecls(Decls[2], SubDecls);

EXPECT_FALSE(Cpp::IsMethod(Decls[0]));
EXPECT_FALSE(Cpp::IsMethod(Decls[1]));
EXPECT_FALSE(Cpp::IsMethod(SubDecls[0]));
EXPECT_TRUE(Cpp::IsMethod(SubDecls[1]));
EXPECT_TRUE(Cpp::IsTemplatedFunction(SubDecls[2]));
EXPECT_TRUE(Cpp::IsMethod(SubDecls[2]));
EXPECT_FALSE(Cpp::IsMethod(nullptr));
}

TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_FunctionTypes) {
Expand Down
14 changes: 14 additions & 0 deletions unittests/CppInterOp/TypeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, TypeReflection_IsPODType) {
EXPECT_FALSE(Cpp::IsPODType(0));
}

TYPED_TEST(CPPINTEROP_TEST_MODE, TypeReflection_IsTemplateParmType) {
std::vector<Decl*> Decls;

std::string code = R"(
template <typename T> void f(T t, const T& r, int i) {}
)";

GetAllTopLevelDecls(code, Decls);

EXPECT_TRUE(Cpp::IsTemplateParmType(Cpp::GetFunctionArgType(Decls[0], 0)));
EXPECT_FALSE(Cpp::IsTemplateParmType(Cpp::GetFunctionArgType(Decls[0], 1)));
EXPECT_FALSE(Cpp::IsTemplateParmType(Cpp::GetFunctionArgType(Decls[0], 2)));
}

TYPED_TEST(CPPINTEROP_TEST_MODE, TypeReflection_IsSmartPtrType) {
#if CLANG_VERSION_MAJOR == 20 && defined(CPPINTEROP_USE_CLING) && defined(_WIN32)
GTEST_SKIP() << "Test fails with Cling on Windows";
Expand Down
Loading