Skip to content

Commit 9b24c54

Browse files
Add IsClassPolymorphic function
1 parent e3774fe commit 9b24c54

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

include/clang/Interpreter/CppInterOp.h

+3
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

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ namespace Cpp {
197197
return isa<CXXRecordDecl>(D);
198198
}
199199

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

unittests/CppInterOp/ScopeReflectionTest.cpp

+22
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)