xref: /netbsd-src/tests/usr.bin/xlint/lint1/gcc_init_compound_literal.c (revision ed9a7a8560a644b1ee377783ab836abd322ee20f)
1 /*	$NetBSD: gcc_init_compound_literal.c,v 1.8 2023/07/29 07:49:15 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, an assertion in check_global_variable
17  * failed since these temporary objects have neither storage class EXTERN nor
18  * STATIC.
19  */
20 
21 /* lint1-extra-flags: -X 351 */
22 
23 // Seen in sys/crypto/aes/aes_ccm.c.
24 const struct {
25     const unsigned char *ctxt;
26 } T = {
27 	.ctxt = (const unsigned char[4]){
28 	    1, 2, 3, 4
29 	},
30 };
31 
32 struct node {
33 	int num;
34 	struct node *left;
35 	struct node *right;
36 };
37 
38 /*
39  * Initial tree for representing the decisions in the classic number guessing
40  * game often used in teaching the basics of programming.
41  */
42 /* expect+1: warning: static variable 'guess' unused [226] */
43 static const struct node guess = {
44 	50,
45 	&(struct node){
46 		25,
47 		&(struct node){
48 			12,
49 			(void *)0,
50 			(void *)0,
51 		},
52 		&(struct node){
53 			37,
54 			(void *)0,
55 			(void *)0,
56 		},
57 	},
58 	(void *)0
59 };
60