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 information t...



details:   https://anonhg.NetBSD.org/src/rev/3e5508ee4a33
branches:  trunk
changeset: 369684:3e5508ee4a33
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Aug 27 23:24:37 2022 +0000

description:
tests/lint: add more information to test for __attribute__((aligned))

Declaring an array type having a negative dimension is the simplest way
to embed an integer into a lint diagnostic, thereby revealing what size
and alignment lint has calculated for a struct.

While here, move these "compile-time assertions" closer to their
corresponding struct, to make reading easier.

diffstat:

 tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c |  36 +++++++++++++++-------
 1 files changed, 25 insertions(+), 11 deletions(-)

diffs (69 lines):

diff -r dbcfbad19f27 -r 3e5508ee4a33 tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c
--- a/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c Sat Aug 27 21:59:41 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c Sat Aug 27 23:24:37 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gcc_attribute_aligned.c,v 1.4 2022/08/27 21:59:41 rillig Exp $ */
+/*     $NetBSD: gcc_attribute_aligned.c,v 1.5 2022/08/27 23:24:37 rillig Exp $ */
 # 3 "gcc_attribute_aligned.c"
 
 /*
@@ -20,11 +20,22 @@
        } fa_32;
 } __attribute__((packed)) __attribute__((aligned(4)));
 
+/* Each variant of the union has size 8. */
+/* expect+1: error: negative array dimension (-8) [20] */
+typedef int sizeof_fp_addr[-(int)sizeof(union fp_addr)];
+
 struct fpacc87 {
        uint64_t f87_mantissa;
        uint16_t f87_exp_sign;
 } __attribute__((packed)) __attribute__((aligned(2)));
 
+/*
+ * Due to the 'packed', the uint64_t does not need to be aligned on an 8-byte
+ * boundary, which allows the struct to have the minimal required size of 10.
+ */
+/* expect+1: error: negative array dimension (-10) [20] */
+typedef int sizeof_fpacc87[-(int)sizeof(struct fpacc87)];
+
 struct save87 {
        uint16_t s87_cw __attribute__((aligned(4)));
        uint16_t s87_sw __attribute__((aligned(4)));
@@ -34,15 +45,9 @@
        struct fpacc87 s87_ac[8];
 };
 
-struct {
-       unsigned int sizeof_fp_addr: sizeof(union fp_addr) == 8 ? 1 : -1;
-
-       unsigned int sizeof_fpacc87: sizeof(struct fpacc87) == 10 ? 1 : -1;
-
-       /* FIXME: @4 2 + @4 2 + @4 2 + @4 8 + @4 8 + @2 (8 * 10) == 108 */
-       /* expect+1: error: illegal bit-field size: 255 [36] */
-       unsigned int sizeof_save87: sizeof(struct save87) == 108 ? 1 : -1;
-};
+/* FIXME: @4 2 + @4 2 + @4 2 + @4 8 + @4 8 + @2 (8 * 10) == 108 */
+/* expect+1: error: negative array dimension (-104) [20] */
+typedef int sizeof_save87[-(int)sizeof(struct save87)];
 
 
 void
@@ -53,7 +58,16 @@
                int second __attribute__((__aligned__(16)));
        };
 
-       /* TODO: should be -20 instead of -8. */
+       /*
+        * Aligning 'second' to a 16-bytes boundary not only aligns the member
+        * inside the structure, it also affects the alignment requirement of
+        * the whole structure.  Due to this struct alignment, the size of the
+        * structure gets rounded up to 32 instead of using the minimally
+        * necessary storage of 20.
+        *
+        * https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html
+        */
+       /* TODO: should be -32 instead of -8. */
        /* expect+1: error: negative array dimension (-8) [20] */
        typedef int ctassert[-(int)sizeof(struct aligned)];
 }



Home | Main Index | Thread Index | Old Index