Skip to content

Commit

Permalink
Add IsFunctionPointerType function (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa authored Feb 16, 2025
1 parent fc852d5 commit 36c101f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ namespace Cpp {
return isa<CXXRecordDecl>(D);
}

bool IsFunctionPointerType(TCppType_t type) {
QualType QT = QualType::getFromOpaquePtr(type);
return QT->isFunctionPointerType();
}

bool IsClassPolymorphic(TCppScope_t klass) {
Decl* D = static_cast<Decl*>(klass);
if (auto* CXXRD = llvm::dyn_cast<CXXRecordDecl>(D))
Expand Down
18 changes: 17 additions & 1 deletion unittests/CppInterOp/TypeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
}

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"))));
}

0 comments on commit 36c101f

Please sign in to comment.