Home | History | Annotate | Line # | Download | only in lint1
gcc_cast_union.c revision 1.4
      1  1.4  rillig /*	$NetBSD: gcc_cast_union.c,v 1.4 2023/07/07 19:45:22 rillig Exp $	*/
      2  1.1  rillig # 3 "gcc_cast_union.c"
      3  1.1  rillig 
      4  1.1  rillig /*
      5  1.1  rillig  * Test the GCC extension for casting to a union type.
      6  1.1  rillig  *
      7  1.1  rillig  * As of GCC 10.3.0, GCC only prints a generic warning without any details:
      8  1.1  rillig  *	error: cast to union type from type not present in union
      9  1.1  rillig  * No idea why it neither mentions the union type nor the actual type.
     10  1.1  rillig  *
     11  1.1  rillig  * https://gcc.gnu.org/onlinedocs/gcc/Cast-to-Union.html
     12  1.1  rillig  */
     13  1.1  rillig 
     14  1.4  rillig /* lint1-extra-flags: -e -X 351 */
     15  1.2  rillig 
     16  1.1  rillig union anything {
     17  1.1  rillig 	_Bool m_bool;
     18  1.1  rillig 	char m_char;
     19  1.1  rillig 	signed char m_signed_char;
     20  1.1  rillig 	unsigned char m_unsigned_char;
     21  1.1  rillig 	short m_short;
     22  1.1  rillig 	unsigned short m_unsigned_short;
     23  1.1  rillig 	int m_int;
     24  1.1  rillig 	unsigned int m_unsigned_int;
     25  1.1  rillig 	long m_long;
     26  1.1  rillig 	unsigned long m_unsigned_long;
     27  1.1  rillig 	long long m_long_long;
     28  1.1  rillig 	unsigned long long m_unsigned_long_long;
     29  1.1  rillig 	/* skip __int128_t and __uint128_t for now */
     30  1.1  rillig 	float m_float;
     31  1.1  rillig 	double m_double;
     32  1.1  rillig 	long double m_long_double;
     33  1.1  rillig 
     34  1.1  rillig 	struct m_struct {
     35  1.1  rillig 		int member;
     36  1.1  rillig 	} m_struct;
     37  1.1  rillig 	union m_union {
     38  1.1  rillig 		int member;
     39  1.1  rillig 	} m_union;
     40  1.1  rillig 	enum m_enum1 {
     41  1.1  rillig 		E1
     42  1.1  rillig 	} m_enum1;
     43  1.1  rillig 	enum m_enum2 {
     44  1.1  rillig 		E2
     45  1.1  rillig 	} m_enum2;
     46  1.1  rillig 	const char *m_const_char_pointer;
     47  1.1  rillig 	double m_double_array[5];
     48  1.1  rillig 	void (*m_function)(void *);
     49  1.1  rillig 	void (*m_function_varargs)(void *, ...);
     50  1.1  rillig 	float _Complex m_float_complex;
     51  1.1  rillig 	double _Complex m_double_complex;
     52  1.1  rillig 	long double _Complex m_long_double_complex;
     53  1.1  rillig };
     54  1.1  rillig 
     55  1.1  rillig enum other_enum {
     56  1.1  rillig 	OTHER
     57  1.1  rillig };
     58  1.1  rillig 
     59  1.1  rillig void
     60  1.1  rillig test(void)
     61  1.1  rillig {
     62  1.1  rillig 	union anything any;
     63  1.1  rillig 
     64  1.1  rillig 	any = (union anything)(_Bool)0;
     65  1.1  rillig 	any = (union anything)(char)'\0';
     66  1.1  rillig 	any = (union anything)(signed char)'\0';
     67  1.1  rillig 	any = (union anything)(unsigned char)'\0';
     68  1.1  rillig 	any = (union anything)(short)'\0';
     69  1.1  rillig 	any = (union anything)(unsigned short)'\0';
     70  1.1  rillig 	any = (union anything)(int)'\0';
     71  1.1  rillig 	any = (union anything)(unsigned int)'\0';
     72  1.1  rillig 	any = (union anything)(long)'\0';
     73  1.1  rillig 	any = (union anything)(unsigned long)'\0';
     74  1.1  rillig 	any = (union anything)(long long)'\0';
     75  1.1  rillig 	any = (union anything)(unsigned long long)'\0';
     76  1.1  rillig 	any = (union anything)0.0F;
     77  1.1  rillig 	any = (union anything)0.0;
     78  1.1  rillig 	any = (union anything)0.0L;
     79  1.1  rillig 	any = (union anything)(struct m_struct){ 0 };
     80  1.1  rillig 	any = (union anything)(union m_union){ 0 };
     81  1.1  rillig 	any = (union anything)E1;
     82  1.1  rillig 	any = (union anything)E2;
     83  1.1  rillig 	/* GCC allows enum mismatch even with -Wenum-conversion */
     84  1.3  rillig 	/* expect+1: error: type 'enum other_enum' is not a member of 'union anything' [329] */
     85  1.1  rillig 	any = (union anything)OTHER;
     86  1.2  rillig 	/* expect+1: error: type 'pointer to char' is not a member of 'union anything' [329] */
     87  1.1  rillig 	any = (union anything)"char *";
     88  1.1  rillig 	any = (union anything)(const char *)"char *";
     89  1.1  rillig 	/* expect+1: error: type 'pointer to double' is not a member of 'union anything' [329] */
     90  1.1  rillig 	any = (union anything)(double[5]){ 0.0, 1.0, 2.0, 3.0, 4.0 };
     91  1.1  rillig 	/* expect+1: error: type 'pointer to double' is not a member of 'union anything' [329] */
     92  1.1  rillig 	any = (union anything)(double[4]){ 0.0, 1.0, 2.0, 3.0 };
     93  1.1  rillig 	/* expect+1: error: type 'pointer to int' is not a member of 'union anything' [329] */
     94  1.1  rillig 	any = (union anything)(int[5]){ 0, 1, 2, 3, 4 };
     95  1.1  rillig 	any = (union anything)(float _Complex)0.0F;
     96  1.1  rillig 	any = (union anything)(double _Complex)0.0;
     97  1.1  rillig 	any = (union anything)(long double _Complex)0.0L;
     98  1.1  rillig 
     99  1.1  rillig 	any = any;
    100  1.1  rillig }
    101