| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: d_c99_init.c,v 1.17 2021/03/28 14:01:50 rillig Exp $ */ | | 1 | /* $NetBSD: d_c99_init.c,v 1.18 2021/03/28 18:48:32 rillig Exp $ */ |
2 | # 3 "d_c99_init.c" | | 2 | # 3 "d_c99_init.c" |
3 | | | 3 | |
4 | /* | | 4 | /* |
5 | * Test C99 initializers. | | 5 | * Test C99 initializers. |
6 | * | | 6 | * |
7 | * See C99 6.7.8 "Initialization". | | 7 | * See C99 6.7.8 "Initialization". |
8 | */ | | 8 | */ |
9 | | | 9 | |
10 | | | 10 | |
11 | void use(const void *); | | 11 | void use(const void *); |
12 | | | 12 | |
13 | typedef struct any { | | 13 | typedef struct any { |
14 | const void *value; | | 14 | const void *value; |
| @@ -24,43 +24,58 @@ int scalar_with_too_many_initializers = | | | @@ -24,43 +24,58 @@ int scalar_with_too_many_initializers = |
24 | | | 24 | |
25 | | | 25 | |
26 | // See init_using_expr, 'handing over to ASSIGN'. | | 26 | // See init_using_expr, 'handing over to ASSIGN'. |
27 | void | | 27 | void |
28 | struct_initialization_via_assignment(any arg) | | 28 | struct_initialization_via_assignment(any arg) |
29 | { | | 29 | { |
30 | any local = arg; | | 30 | any local = arg; |
31 | use(&local); | | 31 | use(&local); |
32 | } | | 32 | } |
33 | | | 33 | |
34 | | | 34 | |
35 | // See init_using_expr, initstack_string. | | 35 | // See init_using_expr, initstack_string. |
36 | char static_duration[] = "static duration"; | | 36 | char static_duration[] = "static duration"; |
| | | 37 | signed char static_duration_signed[] = "static duration"; |
| | | 38 | unsigned char static_duration_unsigned[] = "static duration"; |
| | | 39 | int static_duration_wchar[] = L"static duration"; |
37 | | | 40 | |
38 | // See init_using_expr. | | 41 | // See init_using_expr. |
39 | void | | 42 | void |
40 | initialization_by_braced_string(void) | | 43 | initialization_by_braced_string(void) |
41 | { | | 44 | { |
42 | any local = { "hello" }; | | 45 | any local = { "hello" }; |
43 | use(&local); | | 46 | use(&local); |
44 | } | | 47 | } |
45 | | | 48 | |
46 | void | | 49 | void |
47 | initialization_with_redundant_braces(any arg) | | 50 | initialization_by_redundantly_braced_string(void) |
| | | 51 | { |
| | | 52 | any local = {{{{ "hello" }}}}; |
| | | 53 | use(&local); |
| | | 54 | } |
| | | 55 | |
| | | 56 | /* |
| | | 57 | * Only scalar expressions and string literals may be enclosed by additional |
| | | 58 | * braces. Since 'arg' is a struct, this is a compile-time error. |
| | | 59 | */ |
| | | 60 | void |
| | | 61 | initialization_with_too_many_braces(any arg) |
48 | { | | 62 | { |
49 | any local = { arg }; /* expect: 185 */ | | 63 | any local = { arg }; /* expect: 185 */ |
50 | use(&arg); | | 64 | use(&arg); |
51 | } | | 65 | } |
52 | | | 66 | |
53 | // Some of the following examples are mentioned in init.c. | | 67 | // Some of the following examples are mentioned in the introduction comment |
| | | 68 | // in init.c. |
54 | | | 69 | |
55 | int number = 12345; | | 70 | int number = 12345; |
56 | | | 71 | |
57 | int number_with_braces_and_comma = { | | 72 | int number_with_braces_and_comma = { |
58 | 12345, | | 73 | 12345, |
59 | }; | | 74 | }; |
60 | | | 75 | |
61 | int array_with_fixed_size[3] = { | | 76 | int array_with_fixed_size[3] = { |
62 | 111, | | 77 | 111, |
63 | 222, | | 78 | 222, |
64 | 333, | | 79 | 333, |
65 | 444, /* expect: too many array initializers */ | | 80 | 444, /* expect: too many array initializers */ |
66 | }; | | 81 | }; |
| @@ -207,14 +222,24 @@ struct geometry geometry = { | | | @@ -207,14 +222,24 @@ struct geometry geometry = { |
207 | /* TODO: expect+1: array index 2 must be between 0 and 1 */ | | 222 | /* TODO: expect+1: array index 2 must be between 0 and 1 */ |
208 | .points[0][0][2] = {21, 22 }, | | 223 | .points[0][0][2] = {21, 22 }, |
209 | }; | | 224 | }; |
210 | | | 225 | |
211 | struct ends_with_unnamed_bit_field { | | 226 | struct ends_with_unnamed_bit_field { |
212 | int member; | | 227 | int member; |
213 | int : 0; | | 228 | int : 0; |
214 | } ends_with_unnamed_bit_field = { | | 229 | } ends_with_unnamed_bit_field = { |
215 | 12345, | | 230 | 12345, |
216 | /* expect+1: too many struct/union initializers */ | | 231 | /* expect+1: too many struct/union initializers */ |
217 | 23456, | | 232 | 23456, |
218 | }; | | 233 | }; |
219 | | | 234 | |
220 | // See d_struct_init_nested.c for a more complicated example. | | 235 | char prefixed_message[] = { |
| | | 236 | 'E', ':', ' ', |
| | | 237 | /* expect+1: illegal combination of integer (char) and pointer */ |
| | | 238 | "message\n", |
| | | 239 | }; |
| | | 240 | |
| | | 241 | char message_with_suffix[] = { |
| | | 242 | "message", |
| | | 243 | /* expect+1: too many array initializers */ |
| | | 244 | '\n', |
| | | 245 | }; |