1 1.9 rillig /* $NetBSD: decl_direct_abstract.c,v 1.9 2023/07/01 20:57:37 rillig Exp $ */ 2 1.1 rillig # 3 "decl_direct_abstract.c" 3 1.1 rillig 4 1.1 rillig /* 5 1.1 rillig * Test parsing of direct-abstract-declarator (C99 6.7.6), which are a tricky 6 1.1 rillig * part of the C standard since they require lookahead and are so complicated 7 1.1 rillig * that GCC's parser dedicates 34 lines of comments to this topic. 8 1.1 rillig * 9 1.1 rillig * See msg_155.c. 10 1.1 rillig */ 11 1.1 rillig 12 1.8 rillig /* lint1-extra-flags: -X 351 */ 13 1.8 rillig 14 1.1 rillig struct incompatible { 15 1.1 rillig int member; 16 1.1 rillig } x; 17 1.1 rillig 18 1.9 rillig void 19 1.9 rillig c99_6_7_6_examples(void) 20 1.9 rillig { 21 1.9 rillig /* expect+1: ... 'int' ... */ 22 1.9 rillig x = (int)x; 23 1.9 rillig /* expect+1: ... 'pointer to int' ... */ 24 1.9 rillig x = (int *)x; 25 1.9 rillig /* expect+1: ... 'array[3] of pointer to int' ... */ 26 1.9 rillig x = (int *[3])x; 27 1.9 rillig /* expect+1: ... 'pointer to array[3] of int' ... */ 28 1.9 rillig x = (int (*)[3])x; 29 1.9 rillig /* expect+1: ... 'pointer to array[unknown_size] of int' ... */ 30 1.9 rillig x = (int (*)[*])x; 31 1.9 rillig /* expect+1: ... 'function() returning pointer to int' ... */ 32 1.9 rillig x = (int *())x; 33 1.9 rillig /* expect+1: ... 'pointer to function(void) returning int' ... */ 34 1.9 rillig x = (int (*)(void))x; 35 1.9 rillig /* expect+1: ... 'array[unknown_size] of const pointer to function(unsigned int, ...) returning int' ... */ 36 1.9 rillig x = (int (*const[])(unsigned int, ...))x; 37 1.9 rillig } 38 1.9 rillig 39 1.9 rillig void 40 1.9 rillig function_returning_char(void) 41 1.9 rillig { 42 1.9 rillig // GCC adds a pointer, then says 'char (*)(short int (*)(long int))'. 43 1.9 rillig // Clang says 'char (short (*)(long))'. 44 1.9 rillig /* cdecl says 'function (pointer to function (long) returning short) returning char' */ 45 1.9 rillig /* FIXME: It's a function type, not only 'short'. */ 46 1.9 rillig /* expect+1: ... 'short' ... */ 47 1.9 rillig x = (char(short (*)(long)))x; 48 1.9 rillig 49 1.9 rillig /* expect+1: warning: nested 'extern' declaration of 'f1' [352] */ 50 1.9 rillig char f1(short (*)(long)); 51 1.9 rillig 52 1.9 rillig /* expect+1: ... 'pointer to function(pointer to function(long) returning short) returning char' ... */ 53 1.9 rillig x = f1; 54 1.9 rillig } 55 1.9 rillig 56 1.9 rillig void 57 1.9 rillig function_returning_pointer(void) 58 1.9 rillig { 59 1.9 rillig // GCC says 'error: cast specifies function type'. 60 1.9 rillig // Clang says 'char (short *(*)(long))'. 61 1.9 rillig /* expect+1: error: invalid cast from 'struct incompatible' to 'short' [147] */ 62 1.9 rillig x = (char(short *(long)))x; 63 1.9 rillig 64 1.9 rillig /* expect+1: warning: nested 'extern' declaration of 'f2' [352] */ 65 1.9 rillig char f2(short *(long)); 66 1.9 rillig 67 1.9 rillig // GCC adds two pointers, saying 'char (*)(short int * (*)(long int))'. 68 1.9 rillig // Clang says 'char (short *(*)(long))' */ 69 1.9 rillig /* cdecl says 'syntax error' */ 70 1.9 rillig /* FIXME: lint is wrong, it discards the 'short *' */ 71 1.9 rillig /* expect+1: ... 'pointer to function(long) returning char' ... */ 72 1.9 rillig x = f2; 73 1.9 rillig } 74 1.9 rillig 75 1.2 rillig 76 1.2 rillig void int_array(int[]); 77 1.2 rillig void int_array_3(int[3]); 78 1.4 rillig /* supported since cgram.y 1.363 from 2021-09-14 */ 79 1.2 rillig void int_array_ast(int[*]); 80 1.2 rillig /* expect+1: error: null dimension [17] */ 81 1.2 rillig void int_array_7_array(int[7][]); 82 1.2 rillig void int_array_7_array_3(int[7][3]); 83 1.2 rillig /* expect+1: error: null dimension [17] */ 84 1.2 rillig void int_array_7_array_ast(int[7][*]); 85 1.2 rillig 86 1.2 rillig void int_array_array(int[][7]); 87 1.2 rillig void int_array_3_array(int[3][7]); 88 1.4 rillig /* supported since cgram.y 1.363 from 2021-09-14 */ 89 1.2 rillig void int_array_ast_array(int[*][7]); 90 1.5 rillig 91 1.6 rillig /* expect+1: error: cannot take size/alignment of function type 'function() returning int' [144] */ 92 1.5 rillig unsigned long size_unspecified_args = sizeof(int()); 93 1.5 rillig /* FIXME: Must be 'of function', not 'of void'. */ 94 1.5 rillig /* expect+1: error: cannot take size/alignment of void [146] */ 95 1.5 rillig unsigned long size_prototype_void = sizeof(int(void)); 96 1.6 rillig /* TODO: error: cannot take size/alignment of function type 'function(double) returning int' [144] */ 97 1.5 rillig unsigned long size_prototype_unnamed = sizeof(int(double)); 98 1.6 rillig /* TODO: error: cannot take size/alignment of function type 'function(double) returning int' [144] */ 99 1.5 rillig unsigned long size_prototype_named = sizeof(int(double dbl)); 100 1.6 rillig 101 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning int' [144] */ 102 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 103 1.6 rillig int size_unspecified_args_return_int[-1000 - (int)sizeof(int())]; 104 1.6 rillig 105 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning char' [144] */ 106 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 107 1.6 rillig int size_unspecified_args_return_char[-1000 - (int)sizeof(char())]; 108 1.6 rillig 109 1.6 rillig /* FIXME: 'of void' must be 'of function'. */ 110 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 111 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 112 1.6 rillig int size_prototype_void_return_int[-1000 - (int)sizeof(int(void))]; 113 1.6 rillig 114 1.6 rillig /* FIXME: 'of void' must be 'of function'. */ 115 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 116 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 117 1.6 rillig int size_prototype_void_return_double[-1000 - (int)sizeof(double(void))]; 118 1.6 rillig 119 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 120 1.6 rillig int size_prototype_unnamed_return_int[-1000 - (int)sizeof(int(double))]; 121 1.6 rillig 122 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 123 1.6 rillig int size_prototype_unnamed_return_pchar[-1000 - (int)sizeof(char *(double))]; 124 1.6 rillig 125 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 126 1.6 rillig int size_prototype_named_return_int[-1000 - (int)sizeof(int(double dbl))]; 127 1.6 rillig 128 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 129 1.6 rillig int size_prototype_named_return_pppchar[-1000 - (int)sizeof(char ***(double dbl))]; 130 1.6 rillig 131 1.6 rillig 132 1.6 rillig typedef struct { 133 1.6 rillig char a[1]; 134 1.6 rillig } a01; 135 1.6 rillig 136 1.6 rillig typedef struct { 137 1.6 rillig char a[4]; 138 1.6 rillig } a04; 139 1.6 rillig 140 1.6 rillig typedef struct { 141 1.6 rillig char a[8]; 142 1.6 rillig } a08; 143 1.6 rillig 144 1.6 rillig typedef struct { 145 1.6 rillig char a[32]; 146 1.6 rillig } a32; 147 1.6 rillig 148 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a01' [144] */ 149 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 150 1.6 rillig int unspecified_args_return_01[-1000 - (int)sizeof(a01())]; 151 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a04' [144] */ 152 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 153 1.6 rillig int unspecified_args_return_04[-1000 - (int)sizeof(a04())]; 154 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a08' [144] */ 155 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 156 1.6 rillig int unspecified_args_return_08[-1000 - (int)sizeof(a08())]; 157 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a32' [144] */ 158 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 159 1.6 rillig int unspecified_args_return_32[-1000 - (int)sizeof(a32())]; 160 1.6 rillig 161 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 162 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 163 1.6 rillig int prototype_void_return_01[-1000 - (int)sizeof(a01(void))]; 164 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 165 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 166 1.6 rillig int prototype_void_return_04[-1000 - (int)sizeof(a04(void))]; 167 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 168 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 169 1.6 rillig int prototype_void_return_08[-1000 - (int)sizeof(a08(void))]; 170 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 171 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 172 1.6 rillig int prototype_void_return_32[-1000 - (int)sizeof(a32(void))]; 173 1.6 rillig 174 1.6 rillig /* expect+1: error: negative array dimension (-1001) [20] */ 175 1.6 rillig int prototype_unnamed_01_return_32[-1000 - (int)sizeof(a32(a01))]; 176 1.6 rillig /* expect+1: error: negative array dimension (-1004) [20] */ 177 1.6 rillig int prototype_unnamed_04_return_32[-1000 - (int)sizeof(a32(a04))]; 178 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 179 1.6 rillig int prototype_unnamed_08_return_32[-1000 - (int)sizeof(a32(a08))]; 180 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */ 181 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 182 1.6 rillig int prototype_unnamed_32_return_32[-1000 - (int)sizeof(a32(a32))]; 183 1.6 rillig 184 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 185 1.6 rillig int prototype_unnamed_32_return_01[-1000 - (int)sizeof(a01(a32))]; 186 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 187 1.6 rillig int prototype_unnamed_32_return_04[-1000 - (int)sizeof(a04(a32))]; 188 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 189 1.6 rillig int prototype_unnamed_32_return_08[-1000 - (int)sizeof(a08(a32))]; 190 1.6 rillig 191 1.6 rillig /* expect+1: error: negative array dimension (-1001) [20] */ 192 1.6 rillig int prototype_named_01_return_32[-1000 - (int)sizeof(a32(a01 arg))]; 193 1.6 rillig /* expect+1: error: negative array dimension (-1004) [20] */ 194 1.6 rillig int prototype_named_04_return_32[-1000 - (int)sizeof(a32(a04 arg))]; 195 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 196 1.6 rillig int prototype_named_08_return_32[-1000 - (int)sizeof(a32(a08 arg))]; 197 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */ 198 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 199 1.6 rillig int prototype_named_32_return_32[-1000 - (int)sizeof(a32(a32 arg))]; 200 1.6 rillig 201 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 202 1.6 rillig int prototype_named_32_return_01[-1000 - (int)sizeof(a01(a32 arg))]; 203 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 204 1.6 rillig int prototype_named_32_return_04[-1000 - (int)sizeof(a04(a32 arg))]; 205 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 206 1.6 rillig int prototype_named_32_return_08[-1000 - (int)sizeof(a08(a32 arg))]; 207