Module Name:    src
Committed By:   rillig
Date:           Fri Jun 30 07:18:02 UTC 2023

Modified Files:
        src/tests/usr.bin/xlint/lint1: expr_sizeof.c
        src/usr.bin/xlint/lint1: decl.c

Log Message:
tests/lint: demonstrate bugs in anonymous struct/union handling


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/expr_sizeof.c
cvs rdiff -u -r1.323 -r1.324 src/usr.bin/xlint/lint1/decl.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/expr_sizeof.c
diff -u src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.6 src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.7
--- src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.6	Wed Jun 28 21:41:27 2023
+++ src/tests/usr.bin/xlint/lint1/expr_sizeof.c	Fri Jun 30 07:18:02 2023
@@ -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.

Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.323 src/usr.bin/xlint/lint1/decl.c:1.324
--- src/usr.bin/xlint/lint1/decl.c:1.323	Thu Jun 29 22:52:44 2023
+++ src/usr.bin/xlint/lint1/decl.c	Fri Jun 30 07:18:02 2023
@@ -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;
 }
 

Reply via email to