init.c revision 1.6
1/* $NetBSD: init.c,v 1.6 2021/12/17 10:51:45 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.214 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.214 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.214 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 /* expect+1: error: cannot initialize 'struct <unnamed>' from 'pointer to function(void) returning void' [185] */ 73 do_nothing, 74 /* expect+2: error: too many array initializers, expected 1 [173] */ 75 /* expect+1: error: cannot initialize 'struct <unnamed>' from 'pointer to function(void) returning void' [185] */ 76 do_nothing, 77}; 78