Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enum structs not getting zero padding. #997

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading