From 36c101f24baac4665a4a6d2969e95033438c236e Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Sun, 16 Feb 2025 18:45:38 +0530 Subject: [PATCH] Add `IsFunctionPointerType` function (#503) --- include/clang/Interpreter/CppInterOp.h | 3 +++ lib/Interpreter/CppInterOp.cpp | 5 +++++ unittests/CppInterOp/TypeReflectionTest.cpp | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/clang/Interpreter/CppInterOp.h b/include/clang/Interpreter/CppInterOp.h index 9aa7f5676..96677dbe1 100644 --- a/include/clang/Interpreter/CppInterOp.h +++ b/include/clang/Interpreter/CppInterOp.h @@ -210,6 +210,9 @@ namespace Cpp { /// Checks if the scope is a class or not. CPPINTEROP_API bool IsClass(TCppScope_t scope); + /// Checks if the type is a function pointer. + CPPINTEROP_API bool IsFunctionPointerType(TCppType_t type); + /// Checks if the klass polymorphic. /// which means that the class contains or inherits a virtual function CPPINTEROP_API bool IsClassPolymorphic(TCppScope_t klass); diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index 4d360ac2a..bc697e60b 100755 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -210,6 +210,11 @@ namespace Cpp { return isa(D); } + bool IsFunctionPointerType(TCppType_t type) { + QualType QT = QualType::getFromOpaquePtr(type); + return QT->isFunctionPointerType(); + } + bool IsClassPolymorphic(TCppScope_t klass) { Decl* D = static_cast(klass); if (auto* CXXRD = llvm::dyn_cast(D)) diff --git a/unittests/CppInterOp/TypeReflectionTest.cpp b/unittests/CppInterOp/TypeReflectionTest.cpp index af864942e..4f47c0229 100644 --- a/unittests/CppInterOp/TypeReflectionTest.cpp +++ b/unittests/CppInterOp/TypeReflectionTest.cpp @@ -583,4 +583,20 @@ TEST(TypeReflectionTest, IsSmartPtrType) { EXPECT_TRUE(Cpp::IsSmartPtrType(get_type_from_varname("smart_ptr6"))); EXPECT_FALSE(Cpp::IsSmartPtrType(get_type_from_varname("raw_ptr"))); EXPECT_FALSE(Cpp::IsSmartPtrType(get_type_from_varname("object"))); -} \ No newline at end of file +} + +TEST(TypeReflectionTest, IsFunctionPointerType) { + Cpp::CreateInterpreter(); + + Interp->declare(R"( + typedef int (*int_func)(int, int); + int sum(int x, int y) { return x + y; } + int_func f = sum; + int i = 2; + )"); + + EXPECT_TRUE( + Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("f")))); + EXPECT_FALSE( + Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("i")))); +}