Skip to content

Commit a20ba02

Browse files
Add IsClassPolymorphic function
1 parent 2bb5c33 commit a20ba02

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
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 klass polymorphic.
214+
CPPINTEROP_API bool IsClassPolymorphic(TCppScope_t klass);
215+
213216
// See TClingClassInfo::IsLoaded
214217
/// Checks if the class definition is present, or not. Performs a
215218
/// template instantiation if necessary.

lib/Interpreter/CppInterOp.cpp

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

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

unittests/CppInterOp/ScopeReflectionTest.cpp

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

107+
TEST(ScopeReflectionTest, IsClassPolymorphic) {
108+
std::vector<Decl*> Decls;
109+
GetAllTopLevelDecls(R"(
110+
namespace N {}
111+
112+
class C{};
113+
114+
class C2 {
115+
public:
116+
virtual ~C2() {}
117+
};
118+
119+
int I;
120+
)",
121+
Decls);
122+
123+
EXPECT_FALSE(Cpp::IsClassPolymorphic(Decls[0]));
124+
EXPECT_FALSE(Cpp::IsClassPolymorphic(Decls[1]));
125+
EXPECT_TRUE(Cpp::IsClassPolymorphic(Decls[2]));
126+
EXPECT_FALSE(Cpp::IsClassPolymorphic(Decls[3]));
127+
}
128+
107129
TEST(ScopeReflectionTest, IsComplete) {
108130
std::vector<Decl*> Decls;
109131
std::string code = R"(

0 commit comments

Comments
 (0)