Skip to content

Commit 9969e0b

Browse files
Add IsClassPolymorphic function
1 parent 5d513ca commit 9969e0b

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
@@ -196,6 +196,14 @@ namespace Cpp {
196196
return isa<CXXRecordDecl>(D);
197197
}
198198

199+
bool IsClassPolymorphic(TCppScope_t klass) {
200+
Decl* D = static_cast<Decl*>(klass);
201+
if (auto* CXXRD = llvm::dyn_cast<CXXRecordDecl>(D))
202+
if (auto* CXXRDD = CXXRD->getDefinition())
203+
return CXXRDD->isPolymorphic();
204+
return false;
205+
}
206+
199207
static SourceLocation GetValidSLoc(Sema& semaRef) {
200208
auto& SM = semaRef.getSourceManager();
201209
return SM.getLocForStartOfFile(SM.getMainFileID());

unittests/CppInterOp/ScopeReflectionTest.cpp

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

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

0 commit comments

Comments
 (0)