Skip to content

Commit

Permalink
Fix enum structs not getting zero padding.
Browse files Browse the repository at this point in the history
Bug: issue #995
Test: new test case
  • Loading branch information
dvander committed Nov 28, 2024
1 parent 81e7a10 commit 72cdef5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions compiler/array-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringExpr>()) {
// Substrings can only appear in an enum struct. Normal 2D
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions tests/enum-structs/zero-fill.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0, 1, 2, 0
1, 4, 5, 6
2, 7, 0, 0
21 changes: 21 additions & 0 deletions tests/enum-structs/zero-fill.sp
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 72cdef5

Please sign in to comment.