Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,15 @@ namespace Cpp {
GetStaticDatamembers(TCppScope_t scope,
std::vector<TCppScope_t>& datamembers);

/// Gets all the Enum Constants declared in a Class
///\param[in] scope - class
///\param[out] funcs - vector of static data members
///\param[in] include_enum_class - include enum constants from enum class
CPPINTEROP_API
void GetEnumConstantDatamembers(TCppScope_t scope,
std::vector<TCppScope_t>& datamembers,
bool include_enum_class = true);

/// This is a Lookup function to be used specifically for data members.
CPPINTEROP_API TCppScope_t LookupDatamember(const std::string& name,
TCppScope_t parent);
Expand Down
22 changes: 22 additions & 0 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_os_ostream.h"

#include <algorithm>
#include <iterator>
#include <map>
#include <set>
#include <sstream>
Expand Down Expand Up @@ -1223,6 +1225,23 @@ namespace Cpp {
GetClassDecls<VarDecl>(scope, datamembers);
}

void GetEnumConstantDatamembers(TCppScope_t scope,
std::vector<TCppScope_t>& datamembers,
bool include_enum_class) {
std::vector<TCppScope_t> EDs;
GetClassDecls<EnumDecl>(scope, EDs);
for (TCppScope_t i : EDs) {
auto* ED = static_cast<EnumDecl*>(i);

bool is_class_tagged = ED->isScopedUsingClassTag();
if (is_class_tagged && !include_enum_class)
continue;

std::copy(ED->enumerator_begin(), ED->enumerator_end(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::copy" is directly included [misc-include-cleaner]

lib/Interpreter/CppInterOp.cpp:26:

- #if CLANG_VERSION_MAJOR >= 19
+ #include <algorithm>
+ #if CLANG_VERSION_MAJOR >= 19

std::back_inserter(datamembers));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "std::back_inserter" is directly included [misc-include-cleaner]

lib/Interpreter/CppInterOp.cpp:26:

- #if CLANG_VERSION_MAJOR >= 19
+ #include <iterator>
+ #if CLANG_VERSION_MAJOR >= 19

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are good suggestions...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved.

}
}

TCppScope_t LookupDatamember(const std::string& name, TCppScope_t parent) {
clang::DeclContext *Within = 0;
if (parent) {
Expand Down Expand Up @@ -1256,6 +1275,9 @@ namespace Cpp {
return QT.getAsOpaquePtr();
}

if (auto* ECD = llvm::dyn_cast_or_null<EnumConstantDecl>(D))
return ECD->getType().getAsOpaquePtr();

return 0;
}

Expand Down
24 changes: 24 additions & 0 deletions unittests/CppInterOp/VariableReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,27 @@ TEST(VariableReflectionTest, StaticConstExprDatamember) {
offset = Cpp::GetVariableOffset(datamembers[0]);
EXPECT_EQ(2, *(int*)offset);
}

TEST(VariableReflectionTest, GetEnumConstantDatamembers) {
Cpp::CreateInterpreter();

Cpp::Declare(R"(
class MyEnumClass {
enum { FOUR, FIVE, SIX };
enum A { ONE, TWO, THREE };
enum class B { SEVEN, EIGHT, NINE };
};
)");

Cpp::TCppScope_t MyEnumClass = Cpp::GetNamed("MyEnumClass");
EXPECT_TRUE(MyEnumClass);

std::vector<Cpp::TCppScope_t> datamembers;
Cpp::GetEnumConstantDatamembers(MyEnumClass, datamembers);
EXPECT_EQ(datamembers.size(), 9);
EXPECT_TRUE(Cpp::IsEnumType(Cpp::GetVariableType(datamembers[0])));

std::vector<Cpp::TCppScope_t> datamembers2;
Cpp::GetEnumConstantDatamembers(MyEnumClass, datamembers2, false);
EXPECT_EQ(datamembers2.size(), 6);
}
Loading