From 7b1a3e13bdb1b2a91630f75d7be2f172e86c5290 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 27 Nov 2024 12:12:06 -0800 Subject: [PATCH] Fix enum structs not getting zero padding. Bug: issue #995 Test: new test case --- compiler/array-helpers.cpp | 7 +++++++ tests/enum-structs/zero-fill.out | 3 +++ tests/enum-structs/zero-fill.sp | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 tests/enum-structs/zero-fill.out create mode 100644 tests/enum-structs/zero-fill.sp diff --git a/compiler/array-helpers.cpp b/compiler/array-helpers.cpp index 122386b0..0809e6e2 100644 --- a/compiler/array-helpers.cpp +++ b/compiler/array-helpers.cpp @@ -957,6 +957,7 @@ void CompoundEmitter::AddInlineEnumStruct(EnumStructDecl* es, ArrayExpr* array) auto field_list = &es->fields(); auto field_iter = field_list->begin(); + size_t start_pos = data_size(); for (const auto& item : array->exprs()) { if (StringExpr* expr = item->as()) { // Substrings can only appear in an enum struct. Normal 2D @@ -981,6 +982,12 @@ void CompoundEmitter::AddInlineEnumStruct(EnumStructDecl* es, ArrayExpr* array) assert(field_iter != field_list->end()); field_iter++; } + + size_t emitted = data_size() - start_pos; + if (emitted < es->array_size()) + pending_zeroes_ += es->array_size() - emitted; + + assert(data_size() - start_pos == es->array_size()); } void CompoundEmitter::AddInlineArray(LayoutFieldDecl* field, ArrayExpr* array) { diff --git a/tests/enum-structs/zero-fill.out b/tests/enum-structs/zero-fill.out new file mode 100644 index 00000000..a8f2a1da --- /dev/null +++ b/tests/enum-structs/zero-fill.out @@ -0,0 +1,3 @@ +0, 1, 2, 0 +1, 4, 5, 6 +2, 7, 0, 0 diff --git a/tests/enum-structs/zero-fill.sp b/tests/enum-structs/zero-fill.sp new file mode 100644 index 00000000..9eaad483 --- /dev/null +++ b/tests/enum-structs/zero-fill.sp @@ -0,0 +1,21 @@ +native void printnums(...); + +enum struct Numbers +{ + int first; + int second; + int third; +} + +static Numbers list[] = { + { 1, 2 }, + { 4, 5, 6 }, + { 7 }, +} + +public void main() +{ + for (int i = 0; i < sizeof(list); i++) { + printnums(i, list[i].first, list[i].second, list[i].third); + } +}