boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

032-local-struct-desig.c (1335B)


      1 /* tests/cc-e2e/32-local-struct-desig.c — local struct designated init.
      2  *
      3  * Verifies C11 §6.7.9 ¶21: any field of an aggregate with at least one
      4  * initializer that is NOT explicitly initialized must be zeroed.
      5  *
      6  * The earlier %parse-init-local-struct-list dropped positional state
      7  * after a backwards designator and left those fields uninitialized.
      8  * Read both fields through a pointer (so a stale stack slot can't be
      9  * folded away by anything trivial). */
     10 
     11 struct Point { int x; int y; };
     12 
     13 int sum_via_ptr(struct Point *p) { return p->x + p->y; }
     14 
     15 int test_local_struct_desig_partial(void) {
     16     struct Point a = { .y = 5 };            /* x must be zeroed */
     17     return sum_via_ptr(&a);                 /* 0 + 5 = 5 */
     18 }
     19 
     20 int test_local_struct_desig_reverse(void) {
     21     struct Point b = { .y = 5, .x = 7 };    /* both written, no zero needed */
     22     return sum_via_ptr(&b);                 /* 7 + 5 = 12 */
     23 }
     24 
     25 int test_local_struct_desig_only_first(void) {
     26     struct Point c = { .x = 9 };            /* y must be zeroed */
     27     return sum_via_ptr(&c);                 /* 9 + 0 = 9 */
     28 }
     29 
     30 int main(int argc, char **argv) {
     31     if (test_local_struct_desig_partial()    != 5)  return 1;
     32     if (test_local_struct_desig_reverse()    != 12) return 2;
     33     if (test_local_struct_desig_only_first() != 9)  return 3;
     34     return 0;
     35 }