source: tests/designations.cfa@ c699602

Last change on this file since c699602 was 2e0bb92, checked in by Peter A. Buhr <pabuhr@…>, 10 months ago

remove warnings, and change I/O to work with gcc

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[bb1cd95]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// designations.c --
8//
9// Author : Rob Schluntz
10// Created On : Thu Jun 29 15:26:36 2017
[a04ce4d]11// Last Modified By : Peter A. Buhr
[2e0bb92]12// Last Modified On : Tue Jan 28 21:49:50 2025
13// Update Count : 82
[bb1cd95]14//
15
16// Note: this test case has been crafted so that it compiles with both cfa and with gcc without any modifications.
[2e0bb92]17#include <stdio.h>
[0cf5b79]18#ifdef __cforall
[8f2f185]19#define AT @
[bb1cd95]20#else
[8f2f185]21#define AT
[bb1cd95]22#endif
23
[2e0bb92]24#pragma GCC diagnostic ignored "-Wmissing-braces"
25#pragma GCC diagnostic ignored "-Woverride-init"
26#pragma GCC diagnostic ignored "-Wunused-variable"
27#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
28
29
[bb1cd95]30const int indentAmt = 2;
[8f2f185]31void indent( int level ) {
[2e0bb92]32 printf( "%*s", level, "" );
[bb1cd95]33}
34
35// A contains fields with different types (int vs. int *)
36struct A {
[62423350]37 int x, y;
38 int * ptr;
[bb1cd95]39};
[8f2f185]40void printA( struct A a, int level ) {
41 indent( level );
[2e0bb92]42 printf( "(A){ %d %d %p }\n", a.x, a.y, a.ptr );
[bb1cd95]43}
44
45// B contains struct members
46struct B {
[62423350]47 struct A a0, a1;
[bb1cd95]48};
[8f2f185]49void printB( struct B b, int level ) {
50 indent( level );
[2e0bb92]51 printf( "(B){\n" );
[8f2f185]52 printA( b.a0, level+indentAmt );
53 printA( b.a1, level+indentAmt );
54 indent( level );
[2e0bb92]55 printf( "}\n" );
[bb1cd95]56}
57
58// C contains an array - tests that after 3 ints, the members of B are initialized.
59struct C {
[62423350]60 int arr[3];
61 struct B b;
[bb1cd95]62};
[8f2f185]63void printC( struct C c, int level ) {
64 indent( level );
[2e0bb92]65 printf( "(C){\n" );
[8f2f185]66 indent( level+indentAmt );
[2e0bb92]67 printf( "(int[]{ %d %d %d }\n", c.arr[0], c.arr[1], c.arr[2] );
[8f2f185]68 printB( c.b, level+indentAmt );
69 indent( level );
[2e0bb92]70 printf( "}\n" );
[bb1cd95]71}
72
73// D contains an unnamed aggregate - tests that this doesn't interfere with initialization.
74struct D {
[62423350]75 struct {
76 int x;
77 };
[bb1cd95]78};
[8f2f185]79void printD( struct D d, int level ) {
80 indent( level);
[2e0bb92]81 printf( "(D){ %d }\n", d.x );
[bb1cd95]82}
83
84// E tests unions
85union E {
[62423350]86 struct A a;
87 struct B b;
88 struct C c;
89 struct D d;
90 int i;
[bb1cd95]91};
92
[a04ce4d]93struct Fred {
[85a3806]94 double i[3];
95 int j;
96 struct Mary {
97 struct Jane {
98 double j;
99 } j;
100 double i;
101 } m;
[a04ce4d]102};
[2e0bb92]103struct Fred s1 AT= { .m.j = { 3 } };
[c565d68]104struct Fred s2 AT= { .i = { [2] = 2 } };
[a04ce4d]105
[bb1cd95]106int main() {
[62423350]107 // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero)
108 struct A y0 = {
[c565d68]109 .x = 2,
110 .y = 3
[62423350]111 };
[bb1cd95]112
[62423350]113 // simple initializaiton case - initialize all elements explicitly with no designations
114 struct A y1 = {
115 2, 3, 0
116 };
[bb1cd95]117
118
[62423350]119 // use designation to move to member y, leaving x default-initialized (zero)
120 struct A y2 = {
[c565d68]121 .y = 3,
[62423350]122 0
123 };
[bb1cd95]124
125#if ERROR
[62423350]126 struct A yErr0 = {
127 {} // error - empty scalar initializer is illegal
128 };
[bb1cd95]129#endif
130
[2e0bb92]131 printf( "=====A=====\n" );
[8f2f185]132 printA( y0, 0 );
133 printA( y1, 0 );
134 printA( y2, 0 );
[2e0bb92]135 printf( "=====A=====\n\n" );
[62423350]136
137 // initialize only first element (z0.a.x), leaving everything else default-initialized (zero), no nested curly-braces
138 struct B z0 = { 5 };
139
140 // some nested curly braces, use designation to 'jump around' within structure, leaving some members default-initialized
141 struct B z1 = {
142 { 3 }, // z1.a0
143 { 4 }, // z1.a1
[c565d68]144 .a0 = { 5 }, // z1.a0
[62423350]145 { 6 }, // z1.a1
[c565d68]146 .a0.y = 2, // z1.a0.y
[62423350]147 0, // z1.a0.ptr
148 };
149
150 // z2.a0.y and z2.a0.ptr default-initialized, everything else explicit
151 struct B z2 = {
152 { 1 },
153 { 2, 3, 0 }
154 };
155
156 // initialize every member, omitting nested curly braces
157 struct B z3 = {
158 1, 2, 0, 4, 5, 0
159 };
160
161 // no initializer - legal C, but garbage values - don't print this one
162 struct B z4;
163
164 // no curly braces - initialize with object of same type
165 struct B z5 = z2;
166
167 // z6.a0.y and z6.a0.ptr default-initialized, everything else explicit.
168 // no curly braces on z6.a1 initializers
169 struct B z6 = {
170 { 1 },
171 2, 3, 0
172 };
173
[2e0bb92]174 printf( "=====B=====\n" );
[8f2f185]175 printB( z0, 0 );
176 printB( z1, 0 );
177 printB( z2, 0 );
178 printB( z3, 0 );
179 printB( z5, 0 );
180 printB( z6, 0 );
[2e0bb92]181 printf( "=====B=====\n\n" );
[62423350]182
183 // TODO: what about extra things in a nested init? are empty structs skipped??
184
185 // test that initializing 'past array bound' correctly moves to next member.
186 struct C c1 = {
187 2, 3, 4, // arr
188 5, 6, 0, // b.a0
189 7, 8, 0, // b.a1
190 };
191
[2e0bb92]192 printf( "=====C=====\n" );
[8f2f185]193 printC( c1, 0 );
[2e0bb92]194 printf( "=====C=====\n\n" );
[bb1cd95]195
196#if ERROR
[62423350]197 // nested initializer can't refer to same type in C
198 struct C cErr0 = { c1 };
[bb1cd95]199
[62423350]200 // must use curly braces to initialize members
201 struct C cErr1 = 2;
[bb1cd95]202
[62423350]203 // can't initialize with array compound literal
204 struct C cErr2 = {
205 (int[3]) { 1, 2, 3 } // error: array initialized from non-constant array expression
206 };
[bb1cd95]207#endif
208
209#if WARNING
[62423350]210 // can't initialize array with array - converts to int*
211 int cWarn0_arr[3] = { 1, 2, 3 };
212 struct C cWarn0 = {
213 cWarn0_arr // warning: initialization makes integer from ptr without cast
214 };
[bb1cd95]215#endif
[a04ce4d]216 // array designation
[c565d68]217 int i[2] = { [1] = 3 };
[2e0bb92]218
[62423350]219 // allowed to have 'too many' initialized lists - essentially they are ignored.
220 int i1 = { 3 };
[bb1cd95]221
[62423350]222 // doesn't work yet.
223 // designate unnamed object's members
[c565d68]224 // struct D d = { .x = 3 };
[bb1cd95]225#if ERROR
[c565d68]226 struct D d1 = { .y = 3 };
[bb1cd95]227#endif
228
[62423350]229 // simple union initialization - initialized first member (e0.a)
230 union E e0 = {
231 y0
232 };
233
234 // simple union initialization - initializes first member (e1.a) - with nested initializer list
235 union E e1 = {
236 { 2, 3, 0 }
237 };
238
239 // simple union initialization - initializes first member (e2.a) - without nested initializer list
240 union E e2 = {
241 2, 3, 0
242 };
243
244 // move cursor to e4.b.a0.x and initialize until e3.b.a1.ptr inclusive
245 union E e3 = {
[c565d68]246 .b.a0.x = 2, 3, 0, 5, 6, 0
[62423350]247 };
248
[2e0bb92]249 printf( "=====E=====\n" );
[8f2f185]250 printA( e0.a, 0 );
251 printA( e1.a, 0 );
252 printA( e2.a, 0 );
253 printB( e3.b, 0 );
[2e0bb92]254 printf( "=====E=====\n\n" );
[62423350]255
256 // special case of initialization: char[] can be initialized with a string literal
257 const char * str0 = "hello";
258 char str1[] = "hello";
[8f2f185]259 const char c2[] = "abc";
260 const char c3[] = { 'a', 'b', 'c' };
261 const char c4[][2] = { { 'a', 'b' }, { 'c', 'd'}, { 'c', 'd'} };
[2e0bb92]262 const char ch = c4[0][0];
[8f2f185]263
264 // more cases
265
[c565d68]266// int widths[] = { [3 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
267// int widths[] = { [3 ~ 9] = 1, [10 ~ 99] = 2, [100] = 3 };
[8f2f185]268 struct point { int x, y; };
[c565d68]269 struct point p = { .y = 5, .x = 7 };
[8f2f185]270 union foo { int i; double d; };
[c565d68]271 union foo f = { .d = 4 };
[2e0bb92]272 int v1 = 0, v2 = 0, v4 = 0;
[c565d68]273 int w[6] = { [1] = v1, v2, [4] = v4 };
274 int whitespace[256] = { [' '] = 1, ['\t'] = 1, ['\v'] = 1, ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
275 struct point ptarray[10] = { [2].y = 34, [2].x = 35, [0].x = 36 };
[bb1cd95]276}
277
278// Local Variables: //
279// tab-width: 4 //
280// End: //
Note: See TracBrowser for help on using the repository browser.