Skip to content

Commit 8e064dc

Browse files
Add IsClassPolymorphic function
1 parent fb60be4 commit 8e064dc

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
@@ -95,6 +95,28 @@ TEST(ScopeReflectionTest, IsClass) {
9595
EXPECT_FALSE(Cpp::IsClass(Decls[2]));
9696
}
9797

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

0 commit comments

Comments
 (0)