xref: /netbsd-src/tests/usr.bin/xlint/lint1/gcc_init_compound_literal.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: gcc_init_compound_literal.c,v 1.6 2022/06/17 18:54:53 rillig Exp $	*/
2 # 3 "gcc_init_compound_literal.c"
3 
4 /*
5  * C99 says in 6.7.8p4:
6  *
7  *	All the expressions in an initializer for an object that has static
8  *	storage duration shall be constant expressions or string literals.
9  *
10  * The term "constant expression" is defined in C99 6.6, where 6.6p9 allows
11  * "constant expressions" in initializers to also be an "address constant".
12  * Using these address constants, it is possible to reference an unnamed
13  * object created by a compound literal (C99 6.5.2.5), using either an
14  * explicit '&' or the implicit array-to-pointer conversion from C99 6.3.2.1.
15  *
16  * Before init.c 1.195 from 2021-04-17, lint failed with an assertion failure
17  * in check_global_variable, called by check_global_symbols since these
18  * temporary objects have neither storage class EXTERN nor STATIC.
19  */
20 
21 // Seen in sys/crypto/aes/aes_ccm.c.
22 const struct {
23     const unsigned char *ctxt;
24 } T = {
25 	.ctxt = (const unsigned char[4]){
26 	    1, 2, 3, 4
27 	},
28 };
29 
30 struct node {
31 	int num;
32 	struct node *left;
33 	struct node *right;
34 };
35 
36 /*
37  * Initial tree for representing the decisions in the classic number guessing
38  * game often used in teaching the basics of programming.
39  */
40 /* expect+1: warning: static variable 'guess' unused [226] */
41 static const struct node guess = {
42 	50,
43 	&(struct node){
44 		25,
45 		&(struct node){
46 			12,
47 			(void *)0,
48 			(void *)0,
49 		},
50 		&(struct node){
51 			37,
52 			(void *)0,
53 			(void *)0,
54 		},
55 	},
56 	(void *)0
57 };
58