Skip to content

Commit 46e848a

Browse files
authored
[lldb][test] Add test-cases for packed/aligned structures (#96932)
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 #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 0606c64 commit 46e848a

File tree

4 files changed

+67
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)