Home | History | Annotate | Line # | Download | only in lint1
      1  1.12  rillig /*	$NetBSD: decl_direct_abstract.c,v 1.12 2024/01/28 08:17:27 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.10  rillig 	/* expect+1: ... 'function(pointer to function(long) returning short) returning char' ... */
     46   1.9  rillig 	x = (char(short (*)(long)))x;
     47   1.9  rillig 
     48   1.9  rillig 	/* expect+1: warning: nested 'extern' declaration of 'f1' [352] */
     49   1.9  rillig 	char f1(short (*)(long));
     50   1.9  rillig 
     51   1.9  rillig 	/* expect+1: ... 'pointer to function(pointer to function(long) returning short) returning char' ... */
     52   1.9  rillig 	x = f1;
     53   1.9  rillig }
     54   1.9  rillig 
     55   1.9  rillig void
     56   1.9  rillig function_returning_pointer(void)
     57   1.9  rillig {
     58   1.9  rillig 	// GCC says 'error: cast specifies function type'.
     59   1.9  rillig 	// Clang says 'char (short *(*)(long))'.
     60  1.12  rillig 	/* expect+1: ... 'function(pointer to function(long) returning pointer to short) returning char' ... */
     61   1.9  rillig 	x = (char(short *(long)))x;
     62   1.9  rillig 
     63   1.9  rillig 	/* expect+1: warning: nested 'extern' declaration of 'f2' [352] */
     64   1.9  rillig 	char f2(short *(long));
     65   1.9  rillig 
     66   1.9  rillig 	// GCC adds two pointers, saying 'char (*)(short int * (*)(long int))'.
     67   1.9  rillig 	// Clang says 'char (short *(*)(long))' */
     68   1.9  rillig 	/* cdecl says 'syntax error' */
     69  1.10  rillig 	/* expect+1: ... 'pointer to function(pointer to function(long) returning pointer to short) returning char' ... */
     70   1.9  rillig 	x = f2;
     71   1.9  rillig }
     72   1.9  rillig 
     73   1.2  rillig 
     74   1.2  rillig void int_array(int[]);
     75   1.2  rillig void int_array_3(int[3]);
     76   1.4  rillig /* supported since cgram.y 1.363 from 2021-09-14 */
     77   1.2  rillig void int_array_ast(int[*]);
     78   1.2  rillig /* expect+1: error: null dimension [17] */
     79   1.2  rillig void int_array_7_array(int[7][]);
     80   1.2  rillig void int_array_7_array_3(int[7][3]);
     81   1.2  rillig /* expect+1: error: null dimension [17] */
     82   1.2  rillig void int_array_7_array_ast(int[7][*]);
     83   1.2  rillig 
     84   1.2  rillig void int_array_array(int[][7]);
     85   1.2  rillig void int_array_3_array(int[3][7]);
     86   1.4  rillig /* supported since cgram.y 1.363 from 2021-09-14 */
     87   1.2  rillig void int_array_ast_array(int[*][7]);
     88   1.5  rillig 
     89   1.6  rillig /* expect+1: error: cannot take size/alignment of function type 'function() returning int' [144] */
     90   1.5  rillig unsigned long size_unspecified_args = sizeof(int());
     91  1.10  rillig /* expect+1: error: cannot take size/alignment of function type 'function(void) returning int' [144] */
     92   1.5  rillig unsigned long size_prototype_void = sizeof(int(void));
     93  1.10  rillig /* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
     94   1.5  rillig unsigned long size_prototype_unnamed = sizeof(int(double));
     95  1.10  rillig /* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
     96   1.5  rillig unsigned long size_prototype_named = sizeof(int(double dbl));
     97   1.6  rillig 
     98   1.6  rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning int' [144] */
     99   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    100   1.6  rillig int size_unspecified_args_return_int[-1000 - (int)sizeof(int())];
    101   1.6  rillig 
    102   1.6  rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning char' [144] */
    103   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    104   1.6  rillig int size_unspecified_args_return_char[-1000 - (int)sizeof(char())];
    105   1.6  rillig 
    106  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning int' [144] */
    107   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    108   1.6  rillig int size_prototype_void_return_int[-1000 - (int)sizeof(int(void))];
    109   1.6  rillig 
    110  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning double' [144] */
    111   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    112   1.6  rillig int size_prototype_void_return_double[-1000 - (int)sizeof(double(void))];
    113   1.6  rillig 
    114  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
    115  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    116   1.6  rillig int size_prototype_unnamed_return_int[-1000 - (int)sizeof(int(double))];
    117   1.6  rillig 
    118  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(double) returning pointer to char' [144] */
    119  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    120   1.6  rillig int size_prototype_unnamed_return_pchar[-1000 - (int)sizeof(char *(double))];
    121   1.6  rillig 
    122  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
    123  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    124   1.6  rillig int size_prototype_named_return_int[-1000 - (int)sizeof(int(double dbl))];
    125   1.6  rillig 
    126  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(double) returning pointer to pointer to pointer to char' [144] */
    127  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    128   1.6  rillig int size_prototype_named_return_pppchar[-1000 - (int)sizeof(char ***(double dbl))];
    129   1.6  rillig 
    130   1.6  rillig 
    131   1.6  rillig typedef struct {
    132   1.6  rillig 	char a[1];
    133   1.6  rillig } a01;
    134   1.6  rillig 
    135   1.6  rillig typedef struct {
    136   1.6  rillig 	char a[4];
    137   1.6  rillig } a04;
    138   1.6  rillig 
    139   1.6  rillig typedef struct {
    140   1.6  rillig 	char a[8];
    141   1.6  rillig } a08;
    142   1.6  rillig 
    143   1.6  rillig typedef struct {
    144   1.6  rillig 	char a[32];
    145   1.6  rillig } a32;
    146   1.6  rillig 
    147   1.6  rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a01' [144] */
    148   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    149   1.6  rillig int unspecified_args_return_01[-1000 - (int)sizeof(a01())];
    150   1.6  rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a04' [144] */
    151   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    152   1.6  rillig int unspecified_args_return_04[-1000 - (int)sizeof(a04())];
    153   1.6  rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a08' [144] */
    154   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    155   1.6  rillig int unspecified_args_return_08[-1000 - (int)sizeof(a08())];
    156   1.6  rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a32' [144] */
    157   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    158   1.6  rillig int unspecified_args_return_32[-1000 - (int)sizeof(a32())];
    159   1.6  rillig 
    160  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a01' [144] */
    161   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    162   1.6  rillig int prototype_void_return_01[-1000 - (int)sizeof(a01(void))];
    163  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a04' [144] */
    164   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    165   1.6  rillig int prototype_void_return_04[-1000 - (int)sizeof(a04(void))];
    166  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a08' [144] */
    167   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    168   1.6  rillig int prototype_void_return_08[-1000 - (int)sizeof(a08(void))];
    169  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a32' [144] */
    170   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    171   1.6  rillig int prototype_void_return_32[-1000 - (int)sizeof(a32(void))];
    172   1.6  rillig 
    173  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a01) returning struct typedef a32' [144] */
    174  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    175   1.6  rillig int prototype_unnamed_01_return_32[-1000 - (int)sizeof(a32(a01))];
    176  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a04) returning struct typedef a32' [144] */
    177  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    178   1.6  rillig int prototype_unnamed_04_return_32[-1000 - (int)sizeof(a32(a04))];
    179  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a08) returning struct typedef a32' [144] */
    180  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    181   1.6  rillig int prototype_unnamed_08_return_32[-1000 - (int)sizeof(a32(a08))];
    182   1.6  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */
    183   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    184   1.6  rillig int prototype_unnamed_32_return_32[-1000 - (int)sizeof(a32(a32))];
    185   1.6  rillig 
    186  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a01' [144] */
    187  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    188   1.6  rillig int prototype_unnamed_32_return_01[-1000 - (int)sizeof(a01(a32))];
    189  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a04' [144] */
    190  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    191   1.6  rillig int prototype_unnamed_32_return_04[-1000 - (int)sizeof(a04(a32))];
    192  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a08' [144] */
    193  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    194   1.6  rillig int prototype_unnamed_32_return_08[-1000 - (int)sizeof(a08(a32))];
    195   1.6  rillig 
    196  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a01) returning struct typedef a32' [144] */
    197  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    198   1.6  rillig int prototype_named_01_return_32[-1000 - (int)sizeof(a32(a01 arg))];
    199  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a04) returning struct typedef a32' [144] */
    200  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    201   1.6  rillig int prototype_named_04_return_32[-1000 - (int)sizeof(a32(a04 arg))];
    202  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a08) returning struct typedef a32' [144] */
    203  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    204   1.6  rillig int prototype_named_08_return_32[-1000 - (int)sizeof(a32(a08 arg))];
    205   1.6  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */
    206   1.6  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    207   1.6  rillig int prototype_named_32_return_32[-1000 - (int)sizeof(a32(a32 arg))];
    208   1.6  rillig 
    209  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a01' [144] */
    210  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    211   1.6  rillig int prototype_named_32_return_01[-1000 - (int)sizeof(a01(a32 arg))];
    212  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a04' [144] */
    213  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    214   1.6  rillig int prototype_named_32_return_04[-1000 - (int)sizeof(a04(a32 arg))];
    215  1.10  rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a08' [144] */
    216  1.10  rillig /* expect+1: error: negative array dimension (-1000) [20] */
    217   1.6  rillig int prototype_named_32_return_08[-1000 - (int)sizeof(a08(a32 arg))];
    218  1.11  rillig 
    219  1.11  rillig void
    220  1.11  rillig abstract_decl_param_list_with_attributes(void)
    221  1.11  rillig {
    222  1.11  rillig 	typedef int unspecified_parameters(void (*)() __attribute__(()));
    223  1.11  rillig 	typedef int no_parameters(void (*)(void) __attribute__(()));
    224  1.11  rillig 	typedef int single_parameter(void (*)(int) __attribute__(()));
    225  1.11  rillig 	typedef int several_parameters(void (*)(int, int) __attribute__(()));
    226  1.11  rillig }
    227