Module Name:    src
Committed By:   rillig
Date:           Mon Mar 25 23:39:14 UTC 2024

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_132.c
        src/usr.bin/xlint/lint1: tree.c

Log Message:
lint: fix warnings about loss of accuracy on bit-field operations


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/tests/usr.bin/xlint/lint1/msg_132.c
cvs rdiff -u -r1.625 -r1.626 src/usr.bin/xlint/lint1/tree.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/msg_132.c
diff -u src/tests/usr.bin/xlint/lint1/msg_132.c:1.37 src/tests/usr.bin/xlint/lint1/msg_132.c:1.38
--- src/tests/usr.bin/xlint/lint1/msg_132.c:1.37	Mon Mar 25 22:46:23 2024
+++ src/tests/usr.bin/xlint/lint1/msg_132.c	Mon Mar 25 23:39:14 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_132.c,v 1.37 2024/03/25 22:46:23 rillig Exp $	*/
+/*	$NetBSD: msg_132.c,v 1.38 2024/03/25 23:39:14 rillig Exp $	*/
 # 3 "msg_132.c"
 
 // Test for message: conversion from '%s' to '%s' may lose accuracy [132]
@@ -428,19 +428,25 @@ compare_bit_field_to_integer_constant(vo
 	b = !b;
 }
 
-_Bool
+/*
+ * Before tree.c 1.626 from 2024-03-26, the usual arithmetic conversions for
+ * bit-field types with the same base type but different widths simply took
+ * the type of the left operand, leading to wrong warnings about loss of
+ * accuracy when the right operand was wider than the left operand.
+ */
+void
 binary_operators_on_bit_fields(void)
 {
 	struct {
-		unsigned long long u15:15;
-		unsigned long long u48:48;
-		unsigned long long u64;
+		u64_t u15:15;
+		u64_t u48:48;
+		u64_t u64;
 	} s = { 0, 0, 0 };
 
 	u64 = s.u15 | s.u48;
-	/* expect+1: warning: conversion from 'unsigned long long:16' to 'int:17' may lose accuracy [132] */
+	u64 = s.u48 | s.u15;
 	u64 = s.u15 | s.u48 | s.u64;
-	/* expect+2: warning: conversion from 'unsigned long long:16' to 'int:17' may lose accuracy [132] */
-	/* expect+1: warning: conversion from 'unsigned long long:17' to 'int:18' may lose accuracy [132] */
-	return (s.u15 | s.u48 | s.u64) != 0;
+	u64 = s.u64 | s.u48 | s.u15;
+	cond = (s.u15 | s.u48 | s.u64) != 0;
+	cond = (s.u64 | s.u48 | s.u15) != 0;
 }

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.625 src/usr.bin/xlint/lint1/tree.c:1.626
--- src/usr.bin/xlint/lint1/tree.c:1.625	Tue Mar 19 23:19:04 2024
+++ src/usr.bin/xlint/lint1/tree.c	Mon Mar 25 23:39:13 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.625 2024/03/19 23:19:04 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.626 2024/03/25 23:39:13 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.625 2024/03/19 23:19:04 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.626 2024/03/25 23:39:13 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -762,6 +762,13 @@ balance(op_t op, tnode_t **lnp, tnode_t 
 		*lnp = apply_usual_arithmetic_conversions(op, *lnp, t);
 	if (t != rt)
 		*rnp = apply_usual_arithmetic_conversions(op, *rnp, t);
+
+	unsigned lw = (*lnp)->tn_type->t_bit_field_width;
+	unsigned rw = (*rnp)->tn_type->t_bit_field_width;
+	if (lw < rw)
+		*lnp = convert(NOOP, 0, (*rnp)->tn_type, *lnp);
+	if (rw < lw)
+		*rnp = convert(NOOP, 0, (*lnp)->tn_type, *rnp);
 }
 
 static tnode_t *

Reply via email to