Skip to content

Commit d2c2870

Browse files
committed
[lldb][test] Add test-cases for packed structures
Adds test that checks whether LLDB correctly infers the alignment of packed structures. Specifically, the `InferAlignment` code-path of the `ItaniumRecordLayoutBuilder` where it assumes that overlapping field offsets imply a packed structure and thus sets alignment to `1`. See discussion in llvm#93809. While here, also added a test-case where we check alignment of a class whose base has an explicit `DW_AT_alignment (those don't get transitively propagated in DWARF, but don't seem like a problem for LLDB). Lastly, also added an XFAIL-ed tests where the aforementioned `InferAlignment` kicks in for overlapping fields (but in this case incorrectly since the structure isn't actually packed).
1 parent e258bb3 commit d2c2870

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

lldb/test/API/lang/cpp/alignas_base_class/TestAlignAsBaseClass.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ def test(self):
1616
# Verify specified class alignments.
1717
self.expect_expr("alignof(B2)", result_value="8")
1818
self.expect_expr("alignof(EmptyClassAlign8)", result_value="8")
19+
self.expect_expr("alignof(Derived)", result_value="8")

lldb/test/API/lang/cpp/alignas_base_class/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ D d3g;
1313
struct alignas(8) EmptyClassAlign8 {
1414
} t;
1515

16+
struct alignas(8) __attribute__((packed)) AlignedAndPackedBase {
17+
} foo;
18+
19+
struct Derived : AlignedAndPackedBase {
20+
} bar;
21+
static_assert(alignof(Derived) == 8);
22+
1623
int main() {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// XFAIL: *
2+
3+
// RUN: %clangxx_host -gdwarf -o %t %s
4+
// RUN: %lldb %t \
5+
// RUN: -o "b main" \
6+
// RUN: -o "expr alignof(OverlappingFields)" \
7+
// RUN: -o "expr sizeof(OverlappingFields)" \
8+
// RUN: -o exit | FileCheck %s
9+
10+
// CHECK: (lldb) expr alignof(OverlappingFields)
11+
// CHECK-NEXT: ${{.*}} = 4
12+
// CHECK: (lldb) expr sizeof(OverlappingFields)
13+
// CHECK-NEXT: ${{.*}} = 8
14+
15+
struct Empty {};
16+
17+
struct OverlappingFields {
18+
char y;
19+
[[no_unique_address]] Empty e;
20+
int z;
21+
} g_packed_struct;
22+
static_assert(alignof(OverlappingFields) == 4);
23+
static_assert(sizeof(OverlappingFields) == 8);
24+
25+
int main() {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %clangxx_host -gdwarf -o %t %s
2+
// RUN: %lldb %t \
3+
// RUN: -o "b main" \
4+
// RUN: -o "expr alignof(packed)" \
5+
// RUN: -o "expr sizeof(packed)" \
6+
// RUN: -o "expr alignof(packed_and_aligned)" \
7+
// RUN: -o "expr sizeof(packed_and_aligned)" \
8+
// RUN: -o exit | FileCheck %s
9+
10+
// CHECK: (lldb) expr alignof(packed)
11+
// CHECK-NEXT: ${{.*}} = 1
12+
// CHECK: (lldb) expr sizeof(packed)
13+
// CHECK-NEXT: ${{.*}} = 9
14+
15+
// CHECK: (lldb) expr alignof(packed_and_aligned)
16+
// CHECK-NEXT: ${{.*}} = 16
17+
// CHECK: (lldb) expr sizeof(packed_and_aligned)
18+
// CHECK-NEXT: ${{.*}} = 16
19+
20+
struct __attribute__((packed)) packed {
21+
int x;
22+
char y;
23+
int z;
24+
} g_packed_struct;
25+
static_assert(alignof(packed) == 1);
26+
static_assert(sizeof(packed) == 9);
27+
28+
struct __attribute__((packed, aligned(16))) packed_and_aligned {
29+
int x;
30+
char y;
31+
int z;
32+
} g_packed_and_aligned_struct;
33+
static_assert(alignof(packed_and_aligned) == 16);
34+
static_assert(sizeof(packed_and_aligned) == 16);
35+
36+
int main() {}

0 commit comments

Comments
 (0)