From a88eea7df94f2252a491c9395e41cc447de6c762 Mon Sep 17 00:00:00 2001 From: rillig Date: Tue, 3 Aug 2021 20:34:23 +0000 Subject: [PATCH] tests/lint: test GCC extension for casting to union type --- distrib/sets/lists/tests/mi | 4 +- tests/usr.bin/xlint/lint1/Makefile | 4 +- tests/usr.bin/xlint/lint1/d_c99_union_cast.c | 4 +- tests/usr.bin/xlint/lint1/gcc_cast_union.c | 98 ++++++++++++++++++++ tests/usr.bin/xlint/lint1/gcc_cast_union.exp | 4 + 5 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 tests/usr.bin/xlint/lint1/gcc_cast_union.c create mode 100644 tests/usr.bin/xlint/lint1/gcc_cast_union.exp diff --git a/distrib/sets/lists/tests/mi b/distrib/sets/lists/tests/mi index 1f2fc582d2ab..9547f5c45867 100644 --- a/distrib/sets/lists/tests/mi +++ b/distrib/sets/lists/tests/mi @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.1101 2021/08/03 18:03:54 rillig Exp $ +# $NetBSD: mi,v 1.1102 2021/08/03 20:34:23 rillig Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -6266,6 +6266,8 @@ ./usr/tests/usr.bin/xlint/lint1/gcc_attribute_var.exp tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/xlint/lint1/gcc_bit_field_types.c tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/xlint/lint1/gcc_bit_field_types.exp tests-usr.bin-tests compattestfile,atf +./usr/tests/usr.bin/xlint/lint1/gcc_cast_union.c tests-usr.bin-tests compattestfile,atf +./usr/tests/usr.bin/xlint/lint1/gcc_cast_union.exp tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/xlint/lint1/gcc_init_compound_literal.c tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/xlint/lint1/gcc_init_compound_literal.exp tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/xlint/lint1/gcc_stmt_asm.c tests-usr.bin-tests compattestfile,atf diff --git a/tests/usr.bin/xlint/lint1/Makefile b/tests/usr.bin/xlint/lint1/Makefile index 52c25d87e8bd..07db70d03110 100644 --- a/tests/usr.bin/xlint/lint1/Makefile +++ b/tests/usr.bin/xlint/lint1/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.100 2021/08/03 18:03:54 rillig Exp $ +# $NetBSD: Makefile,v 1.101 2021/08/03 20:34:23 rillig Exp $ NOMAN= # defined MAX_MESSAGE= 345 # see lint1/err.c @@ -160,6 +160,8 @@ FILES+= gcc_attribute_var.c FILES+= gcc_attribute_var.exp FILES+= gcc_bit_field_types.c FILES+= gcc_bit_field_types.exp +FILES+= gcc_cast_union.c +FILES+= gcc_cast_union.exp FILES+= gcc_init_compound_literal.c FILES+= gcc_init_compound_literal.exp FILES+= gcc_stmt_asm.c diff --git a/tests/usr.bin/xlint/lint1/d_c99_union_cast.c b/tests/usr.bin/xlint/lint1/d_c99_union_cast.c index e7e39a51da9d..5ce45dcd4f18 100644 --- a/tests/usr.bin/xlint/lint1/d_c99_union_cast.c +++ b/tests/usr.bin/xlint/lint1/d_c99_union_cast.c @@ -1,7 +1,7 @@ -/* $NetBSD: d_c99_union_cast.c,v 1.4 2021/02/21 09:07:58 rillig Exp $ */ +/* $NetBSD: d_c99_union_cast.c,v 1.5 2021/08/03 20:34:23 rillig Exp $ */ # 3 "d_c99_union_cast.c" -/* union cast */ +/* C99 does not define union cast, it is a GCC extension. */ struct bar { int a; diff --git a/tests/usr.bin/xlint/lint1/gcc_cast_union.c b/tests/usr.bin/xlint/lint1/gcc_cast_union.c new file mode 100644 index 000000000000..2ad0fc6c1f36 --- /dev/null +++ b/tests/usr.bin/xlint/lint1/gcc_cast_union.c @@ -0,0 +1,98 @@ +/* $NetBSD: gcc_cast_union.c,v 1.1 2021/08/03 20:34:23 rillig Exp $ */ +# 3 "gcc_cast_union.c" + +/* + * Test the GCC extension for casting to a union type. + * + * As of GCC 10.3.0, GCC only prints a generic warning without any details: + * error: cast to union type from type not present in union + * No idea why it neither mentions the union type nor the actual type. + * + * https://gcc.gnu.org/onlinedocs/gcc/Cast-to-Union.html + */ + +union anything { + _Bool m_bool; + char m_char; + signed char m_signed_char; + unsigned char m_unsigned_char; + short m_short; + unsigned short m_unsigned_short; + int m_int; + unsigned int m_unsigned_int; + long m_long; + unsigned long m_unsigned_long; + long long m_long_long; + unsigned long long m_unsigned_long_long; + /* skip __int128_t and __uint128_t for now */ + float m_float; + double m_double; + long double m_long_double; + + struct m_struct { + int member; + } m_struct; + union m_union { + int member; + } m_union; + enum m_enum1 { + E1 + } m_enum1; + enum m_enum2 { + E2 + } m_enum2; + const char *m_const_char_pointer; + double m_double_array[5]; + void (*m_function)(void *); + void (*m_function_varargs)(void *, ...); + float _Complex m_float_complex; + double _Complex m_double_complex; + long double _Complex m_long_double_complex; +}; + +enum other_enum { + OTHER +}; + +void +test(void) +{ + union anything any; + + any = (union anything)(_Bool)0; + any = (union anything)(char)'\0'; + any = (union anything)(signed char)'\0'; + any = (union anything)(unsigned char)'\0'; + any = (union anything)(short)'\0'; + any = (union anything)(unsigned short)'\0'; + any = (union anything)(int)'\0'; + any = (union anything)(unsigned int)'\0'; + any = (union anything)(long)'\0'; + any = (union anything)(unsigned long)'\0'; + any = (union anything)(long long)'\0'; + any = (union anything)(unsigned long long)'\0'; + any = (union anything)0.0F; + any = (union anything)0.0; + any = (union anything)0.0L; + any = (union anything)(struct m_struct){ 0 }; + any = (union anything)(union m_union){ 0 }; + any = (union anything)E1; + any = (union anything)E2; + /* GCC allows enum mismatch even with -Wenum-conversion */ + /* expect+1: error: type 'enum other_enum' is not a member of 'union anything' [329] */ + any = (union anything)OTHER; + /* GCC strictly complains that 'char *' is not in the union. */ + any = (union anything)"char *"; + any = (union anything)(const char *)"char *"; + /* expect+1: error: type 'pointer to double' is not a member of 'union anything' [329] */ + any = (union anything)(double[5]){ 0.0, 1.0, 2.0, 3.0, 4.0 }; + /* expect+1: error: type 'pointer to double' is not a member of 'union anything' [329] */ + any = (union anything)(double[4]){ 0.0, 1.0, 2.0, 3.0 }; + /* expect+1: error: type 'pointer to int' is not a member of 'union anything' [329] */ + any = (union anything)(int[5]){ 0, 1, 2, 3, 4 }; + any = (union anything)(float _Complex)0.0F; + any = (union anything)(double _Complex)0.0; + any = (union anything)(long double _Complex)0.0L; + + any = any; +} diff --git a/tests/usr.bin/xlint/lint1/gcc_cast_union.exp b/tests/usr.bin/xlint/lint1/gcc_cast_union.exp new file mode 100644 index 000000000000..c26149ffb9ad --- /dev/null +++ b/tests/usr.bin/xlint/lint1/gcc_cast_union.exp @@ -0,0 +1,4 @@ +gcc_cast_union.c(83): error: type 'enum other_enum' is not a member of 'union anything' [329] +gcc_cast_union.c(88): error: type 'pointer to double' is not a member of 'union anything' [329] +gcc_cast_union.c(90): error: type 'pointer to double' is not a member of 'union anything' [329] +gcc_cast_union.c(92): error: type 'pointer to int' is not a member of 'union anything' [329]