diff options
| author | rillig <rillig@NetBSD.org> | 2023-06-30 07:18:02 +0000 |
|---|---|---|
| committer | rillig <rillig@NetBSD.org> | 2023-06-30 07:18:02 +0000 |
| commit | 032755a0f56d3941855c576404e393dc280ce6bb (patch) | |
| tree | 6a1d5afbf195c6a4057961ba1c0181f7aefd91e7 /tests | |
| parent | 8c6d76b7325e0d94a6be1485bad70f0037bccfc6 (diff) | |
tests/lint: demonstrate bugs in anonymous struct/union handling
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/usr.bin/xlint/lint1/expr_sizeof.c | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/tests/usr.bin/xlint/lint1/expr_sizeof.c b/tests/usr.bin/xlint/lint1/expr_sizeof.c index 4adea8aceea..05463ba603e 100644 --- a/tests/usr.bin/xlint/lint1/expr_sizeof.c +++ b/tests/usr.bin/xlint/lint1/expr_sizeof.c @@ -1,4 +1,4 @@ -/* $NetBSD: expr_sizeof.c,v 1.6 2023/06/28 21:41:27 rillig Exp $ */ +/* $NetBSD: expr_sizeof.c,v 1.7 2023/06/30 07:18:02 rillig Exp $ */ # 3 "expr_sizeof.c" /* @@ -74,6 +74,46 @@ variable_length_array(int n) typedef int sizeof_local_arr[-(int)sizeof(local_arr)]; } +void +bit_fields(void) +{ + struct { + _Bool flag0:1; + _Bool flag1:1; + _Bool flag2:1; + } flags; + /* expect+1: error: negative array dimension (-1) [20] */ + typedef int sizeof_flags[-(int)sizeof(flags)]; + + struct { + struct { + _Bool flag0:1; + _Bool flag1:1; + _Bool flag2:1; + }; + } anonymous_flags; + /* FIXME: sizeof must be 1, not 0. */ + typedef int sizeof_anonymous_flags[-(int)sizeof(anonymous_flags)]; + + struct { + unsigned int bits0:16; + unsigned int bits1:16; + } same_storage_unit; + /* expect+1: error: negative array dimension (-4) [20] */ + typedef int sizeof_same_storage_unit[-(int)sizeof(same_storage_unit)]; + + // Detect whether a bit-field can span multiple storage units. + // If so, the size is 12, if not, the size is 16. + struct { + unsigned int bits0:24; + unsigned int bits1:24; + unsigned int bits2:24; + unsigned int bits3:24; + } cross_storage_unit; + /* expect+1: error: negative array dimension (-16) [20] */ + typedef int sizeof_cross_storage_unit[-(int)sizeof(cross_storage_unit)]; +} + /* * Ensure that anonymous structs and unions are handled correctly. They were * added in C11, and lint did not properly support them until 2023. |
