Skip to content

Commit 1c924c8

Browse files
committed
[lldb][test] Add tests for alignof on class with overlapping bases
Follow-up to llvm#96932 Adds XFAILed test where LLDB incorrectly infers the alignment of a derived class whose bases are overlapping due to [[no_unique_address]]. Specifically, the `InferAlignment` code-path of the `ItaniumRecordLayoutBuilder` assumes that overlapping base offsets imply a packed structure and thus sets alignment to 1. See discussion in llvm#93809. Also adds test where LLDB correctly infers an alignment of `1` when a packed base class is overlapping with other bases. Lastly, there were a couple of `alignof` inconsistencies which I encapsulated in an XFAIL-ed `packed-alignof.cpp`.
1 parent b0f20f2 commit 1c924c8

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// XFAIL: *
2+
3+
// RUN: %clangxx_host -gdwarf -o %t %s
4+
// RUN: %lldb %t \
5+
// RUN: -o "expr alignof(OverlappingDerived)" \
6+
// RUN: -o "expr sizeof(OverlappingDerived)" \
7+
// RUN: -o exit | FileCheck %s
8+
9+
// CHECK: (lldb) expr alignof(OverlappingDerived)
10+
// CHECK-NEXT: ${{.*}} = 4
11+
// CHECK: (lldb) expr sizeof(OverlappingDerived)
12+
// CHECK-NEXT: ${{.*}} = 4
13+
14+
struct Empty {};
15+
16+
struct OverlappingBase {
17+
[[no_unique_address]] Empty e;
18+
};
19+
static_assert(sizeof(OverlappingBase) == 1);
20+
static_assert(alignof(OverlappingBase) == 1);
21+
22+
struct Base {
23+
int mem;
24+
};
25+
26+
struct OverlappingDerived : Base, OverlappingBase {};
27+
static_assert(alignof(OverlappingDerived) == 4);
28+
static_assert(sizeof(OverlappingDerived) == 4);
29+
30+
int main() { OverlappingDerived d; }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// XFAIL: *
2+
//
3+
// RUN: %clangxx_host -gdwarf -o %t %s
4+
// RUN: %lldb %t \
5+
// RUN: -o "expr alignof(base)" \
6+
// RUN: -o "expr alignof(packed_base)" \
7+
// RUN: -o "expr alignof(derived)" \
8+
// RUN: -o "expr sizeof(derived)" \
9+
// RUN: -o exit | FileCheck %s
10+
11+
// CHECK: (lldb) expr alignof(base)
12+
// CHECK-NEXT: ${{.*}} = 4
13+
// CHECK: (lldb) expr alignof(packed_base)
14+
// CHECK-NEXT: ${{.*}} = 1
15+
// CHECK: (lldb) expr alignof(derived)
16+
// CHECK-NEXT: ${{.*}} = 2
17+
// CHECK: (lldb) expr sizeof(derived)
18+
// CHECK-NEXT: ${{.*}} = 16
19+
20+
struct __attribute__((packed)) packed {
21+
int x;
22+
char y;
23+
int z;
24+
} g_packed_struct;
25+
26+
// LLDB incorrectly calculates alignof(base)
27+
struct foo {};
28+
struct base : foo { int x; };
29+
static_assert(alignof(base) == 4);
30+
31+
// LLDB incorrectly calculates alignof(packed_base)
32+
struct __attribute__((packed)) packed_base { int x; };
33+
static_assert(alignof(packed_base) == 1);
34+
35+
struct derived : packed, packed_base {
36+
short s;
37+
} g_derived;
38+
static_assert(alignof(derived) == 2);
39+
static_assert(sizeof(derived) == 16);
40+
41+
int main() {}

lldb/test/Shell/SymbolFile/DWARF/packed.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// RUN: -o "expr sizeof(packed)" \
55
// RUN: -o "expr alignof(packed_and_aligned)" \
66
// RUN: -o "expr sizeof(packed_and_aligned)" \
7+
// RUN: -o "expr alignof(derived)" \
8+
// RUN: -o "expr sizeof(derived)" \
79
// RUN: -o exit | FileCheck %s
810

911
// CHECK: (lldb) expr alignof(packed)
@@ -16,6 +18,11 @@
1618
// CHECK: (lldb) expr sizeof(packed_and_aligned)
1719
// CHECK-NEXT: ${{.*}} = 16
1820

21+
// CHECK: (lldb) expr alignof(derived)
22+
// CHECK-NEXT: ${{.*}} = 1
23+
// CHECK: (lldb) expr sizeof(derived)
24+
// CHECK-NEXT: ${{.*}} = 13
25+
1926
struct __attribute__((packed)) packed {
2027
int x;
2128
char y;
@@ -32,4 +39,11 @@ struct __attribute__((packed, aligned(16))) packed_and_aligned {
3239
static_assert(alignof(packed_and_aligned) == 16);
3340
static_assert(sizeof(packed_and_aligned) == 16);
3441

42+
struct __attribute__((packed)) packed_base { int x; };
43+
static_assert(alignof(packed_base) == 1);
44+
45+
struct derived : packed, packed_base {} g_derived;
46+
static_assert(alignof(derived) == 1);
47+
static_assert(sizeof(derived) == 13);
48+
3549
int main() {}

0 commit comments

Comments
 (0)