Skip to content

Commit e6ff45b

Browse files
Add IsClassPolymorphic function
1 parent 6a98fa6 commit e6ff45b

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ 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 klass polymorphic.
214+
/// which means that the class contains or inherits a virtual function
215+
CPPINTEROP_API bool IsClassPolymorphic(TCppScope_t klass);
216+
213217
// See TClingClassInfo::IsLoaded
214218
/// Checks if the class definition is present, or not. Performs a
215219
/// template instantiation if necessary.

lib/Interpreter/CppInterOp.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ namespace Cpp {
208208
return isa<CXXRecordDecl>(D);
209209
}
210210

211+
bool IsClassPolymorphic(TCppScope_t klass) {
212+
Decl* D = static_cast<Decl*>(klass);
213+
if (auto* CXXRD = llvm::dyn_cast<CXXRecordDecl>(D))
214+
if (auto* CXXRDD = CXXRD->getDefinition())
215+
return CXXRDD->isPolymorphic();
216+
return false;
217+
}
218+
211219
static SourceLocation GetValidSLoc(Sema& semaRef) {
212220
auto& SM = semaRef.getSourceManager();
213221
return SM.getLocForStartOfFile(SM.getMainFileID());

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ TEST(ScopeReflectionTest, IsClass) {
9191
EXPECT_FALSE(Cpp::IsClass(Decls[2]));
9292
}
9393

94+
TEST(ScopeReflectionTest, IsClassPolymorphic) {
95+
std::vector<Decl*> Decls;
96+
GetAllTopLevelDecls(R"(
97+
namespace N {}
98+
99+
class C{};
100+
101+
class C2 {
102+
public:
103+
virtual ~C2() {}
104+
};
105+
106+
int I;
107+
)",
108+
Decls);
109+
110+
EXPECT_FALSE(Cpp::IsClassPolymorphic(Decls[0]));
111+
EXPECT_FALSE(Cpp::IsClassPolymorphic(Decls[1]));
112+
EXPECT_TRUE(Cpp::IsClassPolymorphic(Decls[2]));
113+
EXPECT_FALSE(Cpp::IsClassPolymorphic(Decls[3]));
114+
}
115+
94116
TEST(ScopeReflectionTest, IsComplete) {
95117
std::vector<Decl*> Decls;
96118
std::string code = R"(

0 commit comments

Comments
 (0)