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
6 changes: 4 additions & 2 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,8 @@ void GetDatamembers(TCppScope_t scope, std::vector<TCppScope_t>& datamembers) {

if (auto* CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(D)) {
getSema().ForceDeclarationOfImplicitMembers(CXXRD);
if (CXXRD->hasDefinition())
CXXRD = CXXRD->getDefinition();

llvm::SmallVector<RecordDecl::decl_iterator, 2> stack_begin;
llvm::SmallVector<RecordDecl::decl_iterator, 2> stack_end;
Expand Down Expand Up @@ -1445,7 +1447,7 @@ intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D,
}
offset += C.toCharUnitsFromBits(C.getFieldOffset(FD)).getQuantity();
}
if (BaseCXXRD && BaseCXXRD != FieldParentRecordDecl) {
if (BaseCXXRD && BaseCXXRD != FieldParentRecordDecl->getCanonicalDecl()) {
// FieldDecl FD belongs to some class C, but the base class BaseCXXRD is
// not C. That means BaseCXXRD derives from C. Offset needs to be
// calculated for Derived class
Expand Down Expand Up @@ -1474,7 +1476,7 @@ intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D,
}
if (auto* RD = llvm::dyn_cast<CXXRecordDecl>(FieldParentRecordDecl)) {
// add in the offsets for the (multi level) base classes
while (BaseCXXRD != RD) {
while (BaseCXXRD != RD->getCanonicalDecl()) {
CXXRecordDecl* Parent = direction.at(RD);
offset +=
C.getASTRecordLayout(Parent).getBaseClassOffset(RD).getQuantity();
Expand Down
19 changes: 19 additions & 0 deletions unittests/CppInterOp/VariableReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,25 @@ TEST(VariableReflectionTest, GetVariableOffset) {

auto* VD_C_s_a = Cpp::GetNamed("s_a", Decls[4]); // C::s_a
EXPECT_TRUE((bool) Cpp::GetVariableOffset(VD_C_s_a));

struct K {
int x;
int y;
int z;
};
Cpp::Declare("struct K;");
Cpp::TCppScope_t k = Cpp::GetNamed("K");
EXPECT_TRUE(k);

Cpp::Declare("struct K { int x; int y; int z; };");

datamembers.clear();
Cpp::GetDatamembers(k, datamembers);
EXPECT_EQ(datamembers.size(), 3);

EXPECT_EQ(Cpp::GetVariableOffset(datamembers[0]), offsetof(K, x));
EXPECT_EQ(Cpp::GetVariableOffset(datamembers[1]), offsetof(K, y));
EXPECT_EQ(Cpp::GetVariableOffset(datamembers[2]), offsetof(K, z));
}

#define CODE \
Expand Down
Loading