1 1.8 rillig /* $NetBSD: decl_direct_abstract.c,v 1.8 2023/03/28 14:44:34 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 /* 15 1.1 rillig * The following tests do not use int, to avoid confusion with the implicit 16 1.1 rillig * return type. 17 1.1 rillig */ 18 1.1 rillig 19 1.1 rillig char func0001(short (*)(long)); 20 1.1 rillig 21 1.1 rillig /* GCC says 'char (*)(short int (*)(long int))' */ 22 1.1 rillig /* Clang says 'char (short (*)(long))' */ 23 1.1 rillig /* cdecl says 'function (pointer to function (long) returning short) returning char' */ 24 1.7 rillig /* expect+1: error: cannot initialize 'double' from 'pointer to function(pointer to function(long) returning short) returning char' [185] */ 25 1.1 rillig double type_of_func0001 = func0001; 26 1.1 rillig 27 1.1 rillig char func0002(short *(long)); 28 1.1 rillig 29 1.1 rillig /* GCC says 'char (*)(short int * (*)(long int))' */ 30 1.1 rillig /* Clang says 'char (short *(*)(long))' */ 31 1.1 rillig /* cdecl says 'syntax error' */ 32 1.1 rillig /* FIXME: lint is wrong, it discards the 'short *' */ 33 1.7 rillig /* expect+1: error: cannot initialize 'double' from 'pointer to function(long) returning char' [185] */ 34 1.1 rillig double type_of_func0002 = func0002; 35 1.1 rillig 36 1.1 rillig void c99_6_7_6_example_a(int); 37 1.1 rillig void c99_6_7_6_example_b(int *); 38 1.1 rillig void c99_6_7_6_example_c(int *[3]); 39 1.1 rillig void c99_6_7_6_example_d(int (*)[3]); 40 1.1 rillig void c99_6_7_6_example_e(int (*)[*]); 41 1.1 rillig void c99_6_7_6_example_f(int *()); 42 1.1 rillig void c99_6_7_6_example_g(int (*)(void)); 43 1.1 rillig void c99_6_7_6_example_h(int (*const[])(unsigned int, ...)); 44 1.1 rillig 45 1.1 rillig struct incompatible { 46 1.1 rillig int member; 47 1.1 rillig } x; 48 1.1 rillig 49 1.7 rillig /* expect+1: ... 'pointer to function(int) returning void' ... */ 50 1.1 rillig double type_of_c99_6_7_6_example_a = c99_6_7_6_example_a; 51 1.7 rillig /* expect+1: ... 'pointer to function(pointer to int) returning void' ... */ 52 1.1 rillig double type_of_c99_6_7_6_example_b = c99_6_7_6_example_b; 53 1.7 rillig /* expect+1: ... 'pointer to function(pointer to pointer to int) returning void' ... */ 54 1.1 rillig double type_of_c99_6_7_6_example_c = c99_6_7_6_example_c; 55 1.7 rillig /* expect+1: ... 'pointer to function(pointer to array[3] of int) returning void' ... */ 56 1.1 rillig double type_of_c99_6_7_6_example_d = c99_6_7_6_example_d; 57 1.7 rillig /* expect+1: ... 'pointer to function(pointer to array[unknown_size] of int) returning void' ... */ 58 1.1 rillig double type_of_c99_6_7_6_example_e = c99_6_7_6_example_e; 59 1.5 rillig /* Wrong type before decl.c 1.256 from 2022-04-01. */ 60 1.7 rillig /* expect+1: ... 'pointer to function(pointer to function() returning pointer to int) returning void' ... */ 61 1.1 rillig double type_of_c99_6_7_6_example_f = c99_6_7_6_example_f; 62 1.7 rillig /* expect+1: ... 'pointer to function(pointer to function(void) returning int) returning void' ... */ 63 1.1 rillig double type_of_c99_6_7_6_example_g = c99_6_7_6_example_g; 64 1.7 rillig /* expect+1: ... 'pointer to function(pointer to const pointer to function(unsigned int, ...) returning int) returning void' ... */ 65 1.1 rillig double type_of_c99_6_7_6_example_h = c99_6_7_6_example_h; 66 1.2 rillig 67 1.2 rillig void int_array(int[]); 68 1.2 rillig void int_array_3(int[3]); 69 1.4 rillig /* supported since cgram.y 1.363 from 2021-09-14 */ 70 1.2 rillig void int_array_ast(int[*]); 71 1.2 rillig /* expect+1: error: null dimension [17] */ 72 1.2 rillig void int_array_7_array(int[7][]); 73 1.2 rillig void int_array_7_array_3(int[7][3]); 74 1.2 rillig /* expect+1: error: null dimension [17] */ 75 1.2 rillig void int_array_7_array_ast(int[7][*]); 76 1.2 rillig 77 1.2 rillig void int_array_array(int[][7]); 78 1.2 rillig void int_array_3_array(int[3][7]); 79 1.4 rillig /* supported since cgram.y 1.363 from 2021-09-14 */ 80 1.2 rillig void int_array_ast_array(int[*][7]); 81 1.5 rillig 82 1.6 rillig /* expect+1: error: cannot take size/alignment of function type 'function() returning int' [144] */ 83 1.5 rillig unsigned long size_unspecified_args = sizeof(int()); 84 1.5 rillig /* FIXME: Must be 'of function', not 'of void'. */ 85 1.5 rillig /* expect+1: error: cannot take size/alignment of void [146] */ 86 1.5 rillig unsigned long size_prototype_void = sizeof(int(void)); 87 1.6 rillig /* TODO: error: cannot take size/alignment of function type 'function(double) returning int' [144] */ 88 1.5 rillig unsigned long size_prototype_unnamed = sizeof(int(double)); 89 1.6 rillig /* TODO: error: cannot take size/alignment of function type 'function(double) returning int' [144] */ 90 1.5 rillig unsigned long size_prototype_named = sizeof(int(double dbl)); 91 1.6 rillig 92 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning int' [144] */ 93 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 94 1.6 rillig int size_unspecified_args_return_int[-1000 - (int)sizeof(int())]; 95 1.6 rillig 96 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning char' [144] */ 97 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 98 1.6 rillig int size_unspecified_args_return_char[-1000 - (int)sizeof(char())]; 99 1.6 rillig 100 1.6 rillig /* FIXME: 'of void' must be 'of function'. */ 101 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 102 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 103 1.6 rillig int size_prototype_void_return_int[-1000 - (int)sizeof(int(void))]; 104 1.6 rillig 105 1.6 rillig /* FIXME: 'of void' must be 'of function'. */ 106 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 107 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 108 1.6 rillig int size_prototype_void_return_double[-1000 - (int)sizeof(double(void))]; 109 1.6 rillig 110 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 111 1.6 rillig int size_prototype_unnamed_return_int[-1000 - (int)sizeof(int(double))]; 112 1.6 rillig 113 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 114 1.6 rillig int size_prototype_unnamed_return_pchar[-1000 - (int)sizeof(char *(double))]; 115 1.6 rillig 116 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 117 1.6 rillig int size_prototype_named_return_int[-1000 - (int)sizeof(int(double dbl))]; 118 1.6 rillig 119 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 120 1.6 rillig int size_prototype_named_return_pppchar[-1000 - (int)sizeof(char ***(double dbl))]; 121 1.6 rillig 122 1.6 rillig 123 1.6 rillig typedef struct { 124 1.6 rillig char a[1]; 125 1.6 rillig } a01; 126 1.6 rillig 127 1.6 rillig typedef struct { 128 1.6 rillig char a[4]; 129 1.6 rillig } a04; 130 1.6 rillig 131 1.6 rillig typedef struct { 132 1.6 rillig char a[8]; 133 1.6 rillig } a08; 134 1.6 rillig 135 1.6 rillig typedef struct { 136 1.6 rillig char a[32]; 137 1.6 rillig } a32; 138 1.6 rillig 139 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a01' [144] */ 140 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 141 1.6 rillig int unspecified_args_return_01[-1000 - (int)sizeof(a01())]; 142 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a04' [144] */ 143 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 144 1.6 rillig int unspecified_args_return_04[-1000 - (int)sizeof(a04())]; 145 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a08' [144] */ 146 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 147 1.6 rillig int unspecified_args_return_08[-1000 - (int)sizeof(a08())]; 148 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a32' [144] */ 149 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 150 1.6 rillig int unspecified_args_return_32[-1000 - (int)sizeof(a32())]; 151 1.6 rillig 152 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 153 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 154 1.6 rillig int prototype_void_return_01[-1000 - (int)sizeof(a01(void))]; 155 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 156 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 157 1.6 rillig int prototype_void_return_04[-1000 - (int)sizeof(a04(void))]; 158 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 159 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 160 1.6 rillig int prototype_void_return_08[-1000 - (int)sizeof(a08(void))]; 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_32[-1000 - (int)sizeof(a32(void))]; 164 1.6 rillig 165 1.6 rillig /* expect+1: error: negative array dimension (-1001) [20] */ 166 1.6 rillig int prototype_unnamed_01_return_32[-1000 - (int)sizeof(a32(a01))]; 167 1.6 rillig /* expect+1: error: negative array dimension (-1004) [20] */ 168 1.6 rillig int prototype_unnamed_04_return_32[-1000 - (int)sizeof(a32(a04))]; 169 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 170 1.6 rillig int prototype_unnamed_08_return_32[-1000 - (int)sizeof(a32(a08))]; 171 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */ 172 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 173 1.6 rillig int prototype_unnamed_32_return_32[-1000 - (int)sizeof(a32(a32))]; 174 1.6 rillig 175 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 176 1.6 rillig int prototype_unnamed_32_return_01[-1000 - (int)sizeof(a01(a32))]; 177 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 178 1.6 rillig int prototype_unnamed_32_return_04[-1000 - (int)sizeof(a04(a32))]; 179 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 180 1.6 rillig int prototype_unnamed_32_return_08[-1000 - (int)sizeof(a08(a32))]; 181 1.6 rillig 182 1.6 rillig /* expect+1: error: negative array dimension (-1001) [20] */ 183 1.6 rillig int prototype_named_01_return_32[-1000 - (int)sizeof(a32(a01 arg))]; 184 1.6 rillig /* expect+1: error: negative array dimension (-1004) [20] */ 185 1.6 rillig int prototype_named_04_return_32[-1000 - (int)sizeof(a32(a04 arg))]; 186 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 187 1.6 rillig int prototype_named_08_return_32[-1000 - (int)sizeof(a32(a08 arg))]; 188 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */ 189 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 190 1.6 rillig int prototype_named_32_return_32[-1000 - (int)sizeof(a32(a32 arg))]; 191 1.6 rillig 192 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 193 1.6 rillig int prototype_named_32_return_01[-1000 - (int)sizeof(a01(a32 arg))]; 194 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 195 1.6 rillig int prototype_named_32_return_04[-1000 - (int)sizeof(a04(a32 arg))]; 196 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 197 1.6 rillig int prototype_named_32_return_08[-1000 - (int)sizeof(a08(a32 arg))]; 198