Skip to content

Commit 36c101f

Browse files
Add IsFunctionPointerType function (#503)
1 parent fc852d5 commit 36c101f

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ namespace Cpp {
210210
/// Checks if the scope is a class or not.
211211
CPPINTEROP_API bool IsClass(TCppScope_t scope);
212212

213+
/// Checks if the type is a function pointer.
214+
CPPINTEROP_API bool IsFunctionPointerType(TCppType_t type);
215+
213216
/// Checks if the klass polymorphic.
214217
/// which means that the class contains or inherits a virtual function
215218
CPPINTEROP_API bool IsClassPolymorphic(TCppScope_t klass);

lib/Interpreter/CppInterOp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ namespace Cpp {
210210
return isa<CXXRecordDecl>(D);
211211
}
212212

213+
bool IsFunctionPointerType(TCppType_t type) {
214+
QualType QT = QualType::getFromOpaquePtr(type);
215+
return QT->isFunctionPointerType();
216+
}
217+
213218
bool IsClassPolymorphic(TCppScope_t klass) {
214219
Decl* D = static_cast<Decl*>(klass);
215220
if (auto* CXXRD = llvm::dyn_cast<CXXRecordDecl>(D))

unittests/CppInterOp/TypeReflectionTest.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,20 @@ TEST(TypeReflectionTest, IsSmartPtrType) {
583583
EXPECT_TRUE(Cpp::IsSmartPtrType(get_type_from_varname("smart_ptr6")));
584584
EXPECT_FALSE(Cpp::IsSmartPtrType(get_type_from_varname("raw_ptr")));
585585
EXPECT_FALSE(Cpp::IsSmartPtrType(get_type_from_varname("object")));
586-
}
586+
}
587+
588+
TEST(TypeReflectionTest, IsFunctionPointerType) {
589+
Cpp::CreateInterpreter();
590+
591+
Interp->declare(R"(
592+
typedef int (*int_func)(int, int);
593+
int sum(int x, int y) { return x + y; }
594+
int_func f = sum;
595+
int i = 2;
596+
)");
597+
598+
EXPECT_TRUE(
599+
Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("f"))));
600+
EXPECT_FALSE(
601+
Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("i"))));
602+
}

0 commit comments

Comments
 (0)