Module Name: src Committed By: rillig Date: Fri Jun 30 09:21:53 UTC 2023
Modified Files: src/tests/usr.bin/xlint/lint1: d_alignof.c expr_sizeof.c init_braces.c msg_102.c Log Message: tests/lint: add more tests for sizeof, offsetof, alignof To generate a diff of this commit: cvs rdiff -u -r1.8 -r1.9 src/tests/usr.bin/xlint/lint1/d_alignof.c \ src/tests/usr.bin/xlint/lint1/expr_sizeof.c cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/xlint/lint1/init_braces.c cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/lint1/msg_102.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/d_alignof.c diff -u src/tests/usr.bin/xlint/lint1/d_alignof.c:1.8 src/tests/usr.bin/xlint/lint1/d_alignof.c:1.9 --- src/tests/usr.bin/xlint/lint1/d_alignof.c:1.8 Wed Jun 22 19:23:18 2022 +++ src/tests/usr.bin/xlint/lint1/d_alignof.c Fri Jun 30 09:21:52 2023 @@ -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)]; +} Index: src/tests/usr.bin/xlint/lint1/expr_sizeof.c diff -u src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.8 src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.9 --- src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.8 Fri Jun 30 08:03:01 2023 +++ src/tests/usr.bin/xlint/lint1/expr_sizeof.c Fri Jun 30 09:21:52 2023 @@ -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) ]; Index: src/tests/usr.bin/xlint/lint1/init_braces.c diff -u src/tests/usr.bin/xlint/lint1/init_braces.c:1.3 src/tests/usr.bin/xlint/lint1/init_braces.c:1.4 --- src/tests/usr.bin/xlint/lint1/init_braces.c:1.3 Wed Jun 28 15:04:07 2023 +++ src/tests/usr.bin/xlint/lint1/init_braces.c Fri Jun 30 09:21:52 2023 @@ -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 = { Index: src/tests/usr.bin/xlint/lint1/msg_102.c diff -u src/tests/usr.bin/xlint/lint1/msg_102.c:1.4 src/tests/usr.bin/xlint/lint1/msg_102.c:1.5 --- src/tests/usr.bin/xlint/lint1/msg_102.c:1.4 Sun Jun 19 12:14:34 2022 +++ src/tests/usr.bin/xlint/lint1/msg_102.c Fri Jun 30 09:21:52 2023 @@ -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; }