Module Name: src Committed By: rillig Date: Sat Aug 27 23:24:37 UTC 2022
Modified Files: src/tests/usr.bin/xlint/lint1: gcc_attribute_aligned.c Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 \ src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c diff -u src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c:1.4 src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c:1.5 --- src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c:1.4 Sat Aug 27 21:59:41 2022 +++ src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c Sat Aug 27 23:24:37 2022 @@ -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 @@ union fp_addr { } 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 save87 { 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 @@ aligned_struct_member(void) 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)]; }