diff options
| -rw-r--r-- | tests/usr.bin/xlint/lint1/expr_sizeof.c | 42 | ||||
| -rw-r--r-- | usr.bin/xlint/lint1/decl.c | 6 |
2 files changed, 45 insertions, 3 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. diff --git a/usr.bin/xlint/lint1/decl.c b/usr.bin/xlint/lint1/decl.c index dad963a4f71..62cb98d72bb 100644 --- a/usr.bin/xlint/lint1/decl.c +++ b/usr.bin/xlint/lint1/decl.c @@ -1,4 +1,4 @@ -/* $NetBSD: decl.c,v 1.323 2023/06/29 22:52:44 rillig Exp $ */ +/* $NetBSD: decl.c,v 1.324 2023/06/30 07:18:02 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: decl.c,v 1.323 2023/06/29 22:52:44 rillig Exp $"); +__RCSID("$NetBSD: decl.c,v 1.324 2023/06/30 07:18:02 rillig Exp $"); #endif #include <sys/param.h> @@ -463,8 +463,10 @@ bit_field_width(sym_t **mem) width += (*mem)->s_type->t_bit_field_width; *mem = (*mem)->s_next; } + // XXX: Why INT_SIZE? C99 6.7.2.1p4 allows bit-fields to have type // XXX: _Bool or another implementation-defined type. + // XXX: Why round down instead of up? See expr_sizeof.c, anonymous_flags. return width - width % INT_SIZE; } |
