Home | History | Annotate | Line # | Download | only in lint1
gcc_cast_union.c revision 1.1
      1  1.1  rillig /*	$NetBSD: gcc_cast_union.c,v 1.1 2021/08/03 20:34:23 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.1  rillig union anything {
     15  1.1  rillig 	_Bool m_bool;
     16  1.1  rillig 	char m_char;
     17  1.1  rillig 	signed char m_signed_char;
     18  1.1  rillig 	unsigned char m_unsigned_char;
     19  1.1  rillig 	short m_short;
     20  1.1  rillig 	unsigned short m_unsigned_short;
     21  1.1  rillig 	int m_int;
     22  1.1  rillig 	unsigned int m_unsigned_int;
     23  1.1  rillig 	long m_long;
     24  1.1  rillig 	unsigned long m_unsigned_long;
     25  1.1  rillig 	long long m_long_long;
     26  1.1  rillig 	unsigned long long m_unsigned_long_long;
     27  1.1  rillig 	/* skip __int128_t and __uint128_t for now */
     28  1.1  rillig 	float m_float;
     29  1.1  rillig 	double m_double;
     30  1.1  rillig 	long double m_long_double;
     31  1.1  rillig 
     32  1.1  rillig 	struct m_struct {
     33  1.1  rillig 		int member;
     34  1.1  rillig 	} m_struct;
     35  1.1  rillig 	union m_union {
     36  1.1  rillig 		int member;
     37  1.1  rillig 	} m_union;
     38  1.1  rillig 	enum m_enum1 {
     39  1.1  rillig 		E1
     40  1.1  rillig 	} m_enum1;
     41  1.1  rillig 	enum m_enum2 {
     42  1.1  rillig 		E2
     43  1.1  rillig 	} m_enum2;
     44  1.1  rillig 	const char *m_const_char_pointer;
     45  1.1  rillig 	double m_double_array[5];
     46  1.1  rillig 	void (*m_function)(void *);
     47  1.1  rillig 	void (*m_function_varargs)(void *, ...);
     48  1.1  rillig 	float _Complex m_float_complex;
     49  1.1  rillig 	double _Complex m_double_complex;
     50  1.1  rillig 	long double _Complex m_long_double_complex;
     51  1.1  rillig };
     52  1.1  rillig 
     53  1.1  rillig enum other_enum {
     54  1.1  rillig 	OTHER
     55  1.1  rillig };
     56  1.1  rillig 
     57  1.1  rillig void
     58  1.1  rillig test(void)
     59  1.1  rillig {
     60  1.1  rillig 	union anything any;
     61  1.1  rillig 
     62  1.1  rillig 	any = (union anything)(_Bool)0;
     63  1.1  rillig 	any = (union anything)(char)'\0';
     64  1.1  rillig 	any = (union anything)(signed char)'\0';
     65  1.1  rillig 	any = (union anything)(unsigned char)'\0';
     66  1.1  rillig 	any = (union anything)(short)'\0';
     67  1.1  rillig 	any = (union anything)(unsigned short)'\0';
     68  1.1  rillig 	any = (union anything)(int)'\0';
     69  1.1  rillig 	any = (union anything)(unsigned int)'\0';
     70  1.1  rillig 	any = (union anything)(long)'\0';
     71  1.1  rillig 	any = (union anything)(unsigned long)'\0';
     72  1.1  rillig 	any = (union anything)(long long)'\0';
     73  1.1  rillig 	any = (union anything)(unsigned long long)'\0';
     74  1.1  rillig 	any = (union anything)0.0F;
     75  1.1  rillig 	any = (union anything)0.0;
     76  1.1  rillig 	any = (union anything)0.0L;
     77  1.1  rillig 	any = (union anything)(struct m_struct){ 0 };
     78  1.1  rillig 	any = (union anything)(union m_union){ 0 };
     79  1.1  rillig 	any = (union anything)E1;
     80  1.1  rillig 	any = (union anything)E2;
     81  1.1  rillig 	/* GCC allows enum mismatch even with -Wenum-conversion */
     82  1.1  rillig 	/* expect+1: error: type 'enum other_enum' is not a member of 'union anything' [329] */
     83  1.1  rillig 	any = (union anything)OTHER;
     84  1.1  rillig 	/* GCC strictly complains that 'char *' is not in the union. */
     85  1.1  rillig 	any = (union anything)"char *";
     86  1.1  rillig 	any = (union anything)(const char *)"char *";
     87  1.1  rillig 	/* expect+1: error: type 'pointer to double' is not a member of 'union anything' [329] */
     88  1.1  rillig 	any = (union anything)(double[5]){ 0.0, 1.0, 2.0, 3.0, 4.0 };
     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[4]){ 0.0, 1.0, 2.0, 3.0 };
     91  1.1  rillig 	/* expect+1: error: type 'pointer to int' is not a member of 'union anything' [329] */
     92  1.1  rillig 	any = (union anything)(int[5]){ 0, 1, 2, 3, 4 };
     93  1.1  rillig 	any = (union anything)(float _Complex)0.0F;
     94  1.1  rillig 	any = (union anything)(double _Complex)0.0;
     95  1.1  rillig 	any = (union anything)(long double _Complex)0.0L;
     96  1.1  rillig 
     97  1.1  rillig 	any = any;
     98  1.1  rillig }
     99