Skip to content

Commit fc1e4df

Browse files
committed
[cxx-interop] Empty base classes have zero offset.
Make sure empty base classes don't affect the offset of stored properties.
1 parent cc08a31 commit fc1e4df

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/IRGen/GenStruct.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,14 @@ namespace {
460460

461461
unsigned baseOffset = 0;
462462
if (auto cxxRecord = dyn_cast<clang::CXXRecordDecl>(ClangDecl)) {
463-
baseOffset =
464-
std::distance(cxxRecord->bases().begin(), cxxRecord->bases().end());
463+
baseOffset = llvm::count_if(cxxRecord->bases(), [](auto base) {
464+
auto baseType = base.getType().getCanonicalType();
465+
466+
auto baseRecord = cast<clang::RecordType>(baseType)->getDecl();
467+
auto baseCxxRecord = cast<clang::CXXRecordDecl>(baseRecord);
468+
469+
return !baseCxxRecord->isEmpty();
470+
});
465471
}
466472

467473
// Otherwise, project from the base.

test/Interop/Cxx/class/inheritance/Inputs/functions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,12 @@ struct DerivedFromDerived : Derived {
4949
};
5050

5151
struct DerivedFromNonTrivial : NonTrivial {};
52+
53+
struct EmptyBaseClass {
54+
const char *inBase() const { return "EmptyBaseClass::inBase"; }
55+
};
56+
57+
struct DerivedFromEmptyBaseClass : EmptyBaseClass {
58+
int a = 42;
59+
int b = 42;
60+
};

test/Interop/Cxx/class/inheritance/functions.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,10 @@ FunctionsTestSuite.test("base member from derived from non trivial") {
6969
expectEqual(String(cString: dnt.inNonTrivialWithArgs(0, 1)!), "NonTrivial::inNonTrivialWithArgs")
7070
}
7171

72+
FunctionsTestSuite.test("non-empty derived from empty class") {
73+
let derived = DerivedFromEmptyBaseClass()
74+
expectEqual(String(cString: derived.inBase()!), "EmptyBaseClass::inBase")
75+
expectEqual(derived.b, 42)
76+
}
77+
7278
runAllTests()

0 commit comments

Comments
 (0)