Skip to content

Commit

Permalink
emit zeroed arrays as [0; N]
Browse files Browse the repository at this point in the history
an array like `int x[16] = {}` was emitted as `[0, 0, 0, 0, ...]`,
but is not emitted as `[0; 16]`.
  • Loading branch information
folkertdev committed Nov 28, 2024
1 parent 68de84c commit e3405fd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions c2rust-transpile/src/translator/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ impl<'c> Translation<'c> {
if is_string {
let v = ids.first().unwrap();
self.convert_expr(ctx.used(), *v)
} else if ids.len() == 0 && ty.is_integral_type() || ty.is_floating_type() {
// this was likely a C array of the form `int x[16] = {}`,
// we'll emit that as [0; 16].
let len = mk().lit_expr(mk().int_unsuffixed_lit(n as u128));
self.implicit_default_expr(ty, ctx.is_static)?
.and_then(|default_value| {
Ok(WithStmts::new_val(mk().repeat_expr(default_value, len)))
})
} else {
Ok(ids
.iter()
Expand Down
4 changes: 3 additions & 1 deletion tests/arrays/src/arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ static char *foo = "mystring";
void entry(const unsigned buffer_size, int buffer[const])
{
int arr[1][1] = { 1 };

arr[0][0] += 9;

int arr2[16] = {};
arr2[15] += 9;

int i = 0;

char abc[] = "abc";
Expand Down

0 comments on commit e3405fd

Please sign in to comment.