Sun Mar 28 18:48:32 2021 UTC ()
tests/lint: add tests for initialization


(rillig)
diff -r1.17 -r1.18 src/tests/usr.bin/xlint/lint1/d_c99_init.c
diff -r1.13 -r1.14 src/tests/usr.bin/xlint/lint1/d_c99_init.exp

cvs diff -r1.17 -r1.18 src/tests/usr.bin/xlint/lint1/d_c99_init.c (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/d_c99_init.c 2021/03/28 14:01:50 1.17
+++ src/tests/usr.bin/xlint/lint1/d_c99_init.c 2021/03/28 18:48:32 1.18
@@ -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
11void use(const void *); 11void use(const void *);
12 12
13typedef struct any { 13typedef 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'.
27void 27void
28struct_initialization_via_assignment(any arg) 28struct_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.
36char static_duration[] = "static duration"; 36char static_duration[] = "static duration";
 37signed char static_duration_signed[] = "static duration";
 38unsigned char static_duration_unsigned[] = "static duration";
 39int static_duration_wchar[] = L"static duration";
37 40
38// See init_using_expr. 41// See init_using_expr.
39void 42void
40initialization_by_braced_string(void) 43initialization_by_braced_string(void)
41{ 44{
42 any local = { "hello" }; 45 any local = { "hello" };
43 use(&local); 46 use(&local);
44} 47}
45 48
46void 49void
47initialization_with_redundant_braces(any arg) 50initialization_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 */
 60void
 61initialization_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
55int number = 12345; 70int number = 12345;
56 71
57int number_with_braces_and_comma = { 72int number_with_braces_and_comma = {
58 12345, 73 12345,
59}; 74};
60 75
61int array_with_fixed_size[3] = { 76int 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
211struct ends_with_unnamed_bit_field { 226struct 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. 235char prefixed_message[] = {
 236 'E', ':', ' ',
 237 /* expect+1: illegal combination of integer (char) and pointer */
 238 "message\n",
 239};
 240
 241char message_with_suffix[] = {
 242 "message",
 243 /* expect+1: too many array initializers */
 244 '\n',
 245};

cvs diff -r1.13 -r1.14 src/tests/usr.bin/xlint/lint1/Attic/d_c99_init.exp (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/Attic/d_c99_init.exp 2021/03/28 14:01:50 1.13
+++ src/tests/usr.bin/xlint/lint1/Attic/d_c99_init.exp 2021/03/28 18:48:32 1.14
@@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
1d_c99_init.c(23): error: too many initializers [174] 1d_c99_init.c(23): error: too many initializers [174]
2d_c99_init.c(49): error: cannot initialize 'pointer to const void' from 'struct any' [185] 2d_c99_init.c(63): error: cannot initialize 'pointer to const void' from 'struct any' [185]
3d_c99_init.c(65): error: too many array initializers, expected 3 [173] 3d_c99_init.c(80): error: too many array initializers, expected 3 [173]
4d_c99_init.c(130): error: syntax error 'named member must only be used with struct/union' [249] 4d_c99_init.c(145): error: syntax error 'named member must only be used with struct/union' [249]
5d_c99_init.c(217): error: too many struct/union initializers [172] 5d_c99_init.c(232): error: too many struct/union initializers [172]
 6d_c99_init.c(238): warning: illegal combination of integer (char) and pointer (pointer to char) [183]
 7d_c99_init.c(244): error: too many array initializers, expected 8 [173]