translate-c: Implement flexible arrays

Fixes #8759
This commit is contained in:
Evan Haas
2021-05-21 16:32:53 -07:00
committed by Veikka Tuominen
parent d8b133d733
commit 45212e3b33
8 changed files with 283 additions and 21 deletions

View File

@@ -1519,4 +1519,25 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0;
\\}
, "");
cases.add("Flexible arrays",
\\#include <stdlib.h>
\\#include <stdint.h>
\\typedef struct { char foo; int bar; } ITEM;
\\typedef struct { size_t count; ITEM items[]; } ITEM_LIST;
\\typedef struct { unsigned char count; int items[]; } INT_LIST;
\\#define SIZE 10
\\int main(void) {
\\ ITEM_LIST *list = malloc(sizeof(ITEM_LIST) + SIZE * sizeof(ITEM));
\\ for (int i = 0; i < SIZE; i++) list->items[i] = (ITEM) {.foo = i, .bar = i + 1};
\\ const ITEM_LIST *const c_list = list;
\\ for (int i = 0; i < SIZE; i++) if (c_list->items[i].foo != i || c_list->items[i].bar != i + 1) abort();
\\ INT_LIST *int_list = malloc(sizeof(INT_LIST) + SIZE * sizeof(int));
\\ for (int i = 0; i < SIZE; i++) int_list->items[i] = i;
\\ const INT_LIST *const c_int_list = int_list;
\\ const int *const ints = int_list->items;
\\ for (int i = 0; i < SIZE; i++) if (ints[i] != i) abort();
\\ return 0;
\\}
, "");
}