Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/usr.bin/xlint/lint1 tests/lint: add more tests for siz...



details:   https://anonhg.NetBSD.org/src/rev/4c79719c7ced
branches:  trunk
changeset: 377183:4c79719c7ced
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 30 09:21:52 2023 +0000

description:
tests/lint: add more tests for sizeof, offsetof, alignof

diffstat:

 tests/usr.bin/xlint/lint1/d_alignof.c   |  57 ++++++++++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/expr_sizeof.c |  13 ++++---
 tests/usr.bin/xlint/lint1/init_braces.c |   9 ++--
 tests/usr.bin/xlint/lint1/msg_102.c     |  34 ++++++++++++++++--
 4 files changed, 97 insertions(+), 16 deletions(-)

diffs (197 lines):

diff -r 732b55ede35d -r 4c79719c7ced tests/usr.bin/xlint/lint1/d_alignof.c
--- a/tests/usr.bin/xlint/lint1/d_alignof.c     Fri Jun 30 08:48:38 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/d_alignof.c     Fri Jun 30 09:21:52 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: d_alignof.c,v 1.8 2022/06/22 19:23:18 rillig Exp $     */
+/*     $NetBSD: d_alignof.c,v 1.9 2023/06/30 09:21:52 rillig Exp $     */
 # 3 "d_alignof.c"
 
 /* https://gcc.gnu.org/onlinedocs/gcc/Alignment.html */
@@ -67,3 +67,58 @@ alignof_pointer_to_member(void)
 
        return __alignof__(ptr)->member + ptr->member;
 }
+
+void
+alignof_variants(void)
+{
+       /* expect+1: error: negative array dimension (-4) [20] */
+       typedef int array_int[-(int)__alignof(int[3])];
+
+       /* expect+1: error: negative array dimension (-8) [20] */
+       typedef int array_double[-(int)__alignof(double[3])];
+
+       /* expect+1: error: cannot take size/alignment of function type 'function(int) returning int' [144] */
+       typedef int func[-(int)__alignof(int(int))];
+
+       struct int_double {
+               int i;
+               double d;
+       };
+       /* expect+1: error: negative array dimension (-8) [20] */
+       typedef int struct_int_double[-(int)__alignof(struct int_double)];
+
+       struct chars {
+               char name[20];
+       };
+       /* expect+1: error: negative array dimension (-1) [20] */
+       typedef int struct_chars[-(int)__alignof(struct chars)];
+
+       /* expect+1: warning: struct 'incomplete_struct' never defined [233] */
+       struct incomplete_struct;
+       /* expect+1: error: cannot take size/alignment of incomplete type [143] */
+       typedef int incomplete_struct[-(int)__alignof(struct incomplete_struct)];
+
+       /* expect+1: warning: union 'incomplete_union' never defined [234] */
+       union incomplete_union;
+       /* expect+1: error: cannot take size/alignment of incomplete type [143] */
+       typedef int incomplete_union[-(int)__alignof(union incomplete_union)];
+
+       /* expect+1: warning: enum 'incomplete_enum' never defined [235] */
+       enum incomplete_enum;
+       /* expect+1: error: negative array dimension (-4) [20] */
+       typedef int incomplete_enum[-(int)__alignof(enum incomplete_enum)];
+
+       struct bit_fields {
+               _Bool bit_field:1;
+       };
+       /*
+        * FIXME: This is not an attempt to initialize the typedef, it's the
+        * initialization of a nested expression.
+        */
+       /* expect+2: error: cannot initialize typedef '00000000_tmp' [25] */
+       /* expect+1: error: cannot take size/alignment of bit-field [145] */
+       typedef int bit_field[-(int)__alignof((struct bit_fields){0}.bit_field)];
+
+       /* expect+1: error: cannot take size/alignment of void [146] */
+       typedef int plain_void[-(int)__alignof(void)];
+}
diff -r 732b55ede35d -r 4c79719c7ced tests/usr.bin/xlint/lint1/expr_sizeof.c
--- a/tests/usr.bin/xlint/lint1/expr_sizeof.c   Fri Jun 30 08:48:38 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_sizeof.c   Fri Jun 30 09:21:52 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expr_sizeof.c,v 1.8 2023/06/30 08:03:01 rillig Exp $   */
+/*     $NetBSD: expr_sizeof.c,v 1.9 2023/06/30 09:21:52 rillig Exp $   */
 # 3 "expr_sizeof.c"
 
 /*
@@ -116,22 +116,23 @@ bit_fields(void)
        /*
         * The bit-fields in a struct may be merged into the same storage
         * units, even if their types differ. GCC 10, Clang 15 and lint all
-        * agree in packing the bit-fields and the char into 4 bytes, even
-        * though their underlying types
+        * agree in packing the first group of bit-fields and the char into
+        * 4 bytes, even though their underlying types differ.  The second
+        * group of bit-fields gets its own storage unit.
         */
        struct mixed {
                _Bool flag0:1;
                signed int signed0:1;
                unsigned int unsigned0:1;
-               char ch;
+               char ch[3];
                _Bool flag1:1;
                signed int signed1:1;
                unsigned int unsigned1:1;
        } mixed;
-       /* expect+1: error: negative array dimension (-4) [20] */
+       /* expect+1: error: negative array dimension (-8) [20] */
        typedef int sizeof_mixed[-(int)sizeof(mixed)];
        /* FIXME: Implement build_offsetof correctly. */
-       /* expect+3: error: negative array dimension (-4) [20] */
+       /* expect+3: error: negative array dimension (-8) [20] */
        typedef int offsetof_mixed_ch[
            -(int)__builtin_offsetof(struct mixed, ch)
        ];
diff -r 732b55ede35d -r 4c79719c7ced tests/usr.bin/xlint/lint1/init_braces.c
--- a/tests/usr.bin/xlint/lint1/init_braces.c   Fri Jun 30 08:48:38 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/init_braces.c   Fri Jun 30 09:21:52 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: init_braces.c,v 1.3 2023/06/28 15:04:07 rillig Exp $   */
+/*     $NetBSD: init_braces.c,v 1.4 2023/06/30 09:21:52 rillig Exp $   */
 # 3 "init_braces.c"
 
 /*
@@ -62,8 +62,9 @@ init_string(void)
        char name4[] = {{{{ "" }}}};
 }
 
+/* C11 6.7.2.1p13 */
 unsigned long
-init_nested_struct_and_union(void)
+init_anonymous_struct_and_union(void)
 {
        struct time {
                unsigned long ns;
@@ -83,8 +84,8 @@ init_nested_struct_and_union(void)
        };
 
        struct outer var = {    /* struct outer */
-               {               /* unnamed union */
-                       {       /* unnamed struct */
+               {               /* anonymous union */
+                       {       /* anonymous struct */
 /* FIXME: GCC and Clang both compile this initializer. */
 /* expect+1: error: type 'struct time' does not have member 'times' [101] */
                                .times = {
diff -r 732b55ede35d -r 4c79719c7ced tests/usr.bin/xlint/lint1/msg_102.c
--- a/tests/usr.bin/xlint/lint1/msg_102.c       Fri Jun 30 08:48:38 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_102.c       Fri Jun 30 09:21:52 2023 +0000
@@ -1,11 +1,11 @@
-/*     $NetBSD: msg_102.c,v 1.4 2022/06/19 12:14:34 rillig Exp $       */
+/*     $NetBSD: msg_102.c,v 1.5 2023/06/30 09:21:52 rillig Exp $       */
 # 3 "msg_102.c"
 
 // Test for message: illegal use of member '%s' [102]
 
 // Anonymous members are defined in C11 6.7.2.1p2.
 
-struct bit_fields_and_bits {
+struct unrelated {
        union {
                struct {
                        unsigned bit_0:1;
@@ -15,15 +15,39 @@ struct bit_fields_and_bits {
        };
 };
 
+struct bit_fields_and_bits {
+       union {
+               struct {
+                       unsigned bf_bit_0:1;
+                       unsigned bf_bit_1:1;
+               };
+               unsigned bf_bits;
+       };
+};
+
+static struct unrelated *u1, *u2;
+static struct bit_fields_and_bits *b1, *b2;
+
 static inline _Bool
-eq(const struct bit_fields_and_bits *a, const struct bit_fields_and_bits *b)
+eq(int x)
 {
        /*
         * TODO: Once this is fixed, enable lint in
         * external/mit/xorg/lib/dri.old/Makefile again.
         */
-       /* TODO: Add support for C11 anonymous struct and union members. */
+
+       if (x == 0)
+               /* expect+2: error: illegal use of member 'bits' [102] */
+               /* expect+1: error: illegal use of member 'bits' [102] */
+               return u1->bits == u2->bits;
+
+       /*
+        * The struct does not have a member named 'bits'.  There's another
+        * struct with a member of that name, and in traditional C, it was
+        * possible but discouraged to access members of other structs via
+        * their plain name.
+        */
        /* expect+2: error: illegal use of member 'bits' [102] */
        /* expect+1: error: illegal use of member 'bits' [102] */
-       return a->bits == b->bits;
+       return b1->bits == b2->bits;
 }



Home | Main Index | Thread Index | Old Index