init.c revision 1.11
1/* $NetBSD: init.c,v 1.11 2022/06/11 11:52:13 rillig Exp $ */ 2# 3 "init.c" 3 4/* 5 * Tests for initialization. 6 * 7 * C99 6.7.8 8 */ 9 10/* 11 * C99 does not allow empty initializer braces syntactically. 12 * Lint allows this syntactically, it just complains if the resulting 13 * object is empty. 14 */ 15/* expect+1: error: empty array declaration: empty_array_with_initializer [190] */ 16double empty_array_with_initializer[] = {}; 17double array_with_empty_initializer[3] = {}; 18 19/* 20 * C99 does not allow empty initializer braces syntactically. 21 */ 22struct { 23 int member; 24} empty_struct_initializer = {}; 25 26 27typedef struct { 28 const char *key; 29 int n; 30} histogram_entry; 31 32/* 33 * The C standards allow omitting braces around the structural levels. For 34 * human readers, it is usually clearer to include them. 35 * 36 * Seen in external/ibm-public/postfix/dist/src/util/dict.c(624). 37 * 38 * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17, 39 * the below initialization sets in->in_err but shouldn't. 40 */ 41const histogram_entry hgr[] = { 42 "odd", 5, 43 "even", 5, 44}; 45 46 47/* 48 * Initialization with fewer braces than usual, must still be accepted. 49 * 50 * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17, 51 * the below initialization sets in->in_err but shouldn't. 52 */ 53struct { 54 int x, y; 55} points[] = { 56 0, 0, 3, 0, 0, 4, 3, 4 57}; 58 59 60/* 61 * Initialization with fewer braces than usual, must still be accepted. 62 * 63 * TODO: Properly handle this situation; as of init.c 1.215 from 2021-12-17, 64 * the below initialization sets in->in_err but shouldn't. 65 */ 66void do_nothing(void); 67 68struct { 69 void (*action_1) (void); 70 void (*action_2) (void); 71} actions[1] = { 72 do_nothing, 73 do_nothing, 74}; 75 76 77/* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */ 78struct incomplete_struct s1 = { 79 1, 80/* expect+1: error: 's1' has incomplete type 'incomplete struct incomplete_struct' [31] */ 81}; 82 83/* expect+1: error: initialization of incomplete type 'incomplete struct incomplete_struct' [175] */ 84struct incomplete_struct s2 = { 85 .member = 1, 86/* expect+1: error: 's2' has incomplete type 'incomplete struct incomplete_struct' [31] */ 87}; 88 89struct incomplete_struct { 90 int num; 91}; 92 93 94/* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */ 95union incomplete_union u1 = { 96 1, 97/* expect+1: error: 'u1' has incomplete type 'incomplete union incomplete_union' [31] */ 98}; 99 100/* expect+1: error: initialization of incomplete type 'incomplete union incomplete_union' [175] */ 101union incomplete_union u2 = { 102 .member = 1, 103/* expect+1: error: 'u2' has incomplete type 'incomplete union incomplete_union' [31] */ 104}; 105 106union incomplete_union { 107 int num; 108}; 109 110 111/* expect+1: warning: cannot initialize extern declaration: extern_var [26] */ 112extern int extern_var = 1; 113int defined_var = 1; 114/* expect+1: warning: static variable 'static_var' unused [226] */ 115static int static_var = 1; 116/* expect+1: error: illegal storage class [8] */ 117register int register_var = 1; 118/* expect+1: error: cannot initialize typedef: typedef_var [25] */ 119typedef int typedef_var = 1; 120 121 122/* 123 * In an array of unknown size that is declared using fewer braces than 124 * recommended, ensure that the array size is updated at the end of the 125 * initializer. 126 */ 127struct { 128 int x; 129 int y; 130} points_of_unknown_size[] = { 131 3, 4, 132}; 133