Skip to content
Open
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
6 changes: 5 additions & 1 deletion lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,11 @@ TCppIndex_t GetEnumConstantValue(TCppScope_t handle) {
auto* D = (clang::Decl*)handle;
if (auto* ECD = llvm::dyn_cast_or_null<clang::EnumConstantDecl>(D)) {
const llvm::APSInt& Val = ECD->getInitVal();
return INTEROP_RETURN(Val.getExtValue());
if (Val.isRepresentableByInt64())
return INTEROP_RETURN(Val.getExtValue());
llvm::SmallString<40> StrVal;
Val.toString(StrVal);
return INTEROP_RETURN(std::stoul(StrVal.c_str()));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Do we need macro to check if sizeof(unsigned long) == sizeof(size_t) then call stoul, else if sizeof(unsigned long long) == sizeof(size_t), call stoull? For all the systems we are interested (64 bit), I believe sizeof(unsigned long) == sizeof(unsigned long long) == 64. Then we will also have to change the return type. What is the enum value is negative...!?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

}
return INTEROP_RETURN(0);
}
Expand Down
9 changes: 9 additions & 0 deletions unittests/CppInterOp/EnumReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, EnumReflection_GetEnumConstantValue) {
MinusNine
};
int a = 10;

enum TooLong : unsigned long long {
BIG = ((unsigned long long)1)<<63
};
)";

GetAllTopLevelDecls(code, Decls);
Expand All @@ -250,7 +254,12 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, EnumReflection_GetEnumConstantValue) {
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[4]), 54);
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[5]), -10);
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[6]), -9);

EXPECT_EQ(Cpp::GetEnumConstantValue(Decls[1]), 0); // Checking value of non enum constant

EnumConstants = Cpp::GetEnumConstants(Decls[2]);
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[0]), ((unsigned long long)1)
<< 63);
}

TYPED_TEST(CPPINTEROP_TEST_MODE, EnumReflection_GetEnums) {
Expand Down
Loading