Module Name:    src
Committed By:   rillig
Date:           Thu Feb 20 21:53:28 UTC 2025

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

Log Message:
lint: add detail to message about truncated constant


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_306.c
cvs rdiff -u -r1.263 -r1.264 src/usr.bin/xlint/lint1/err.c
cvs rdiff -u -r1.671 -r1.672 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_306.c
diff -u src/tests/usr.bin/xlint/lint1/msg_306.c:1.5 src/tests/usr.bin/xlint/lint1/msg_306.c:1.6
--- src/tests/usr.bin/xlint/lint1/msg_306.c:1.5	Fri Jul  7 19:45:22 2023
+++ src/tests/usr.bin/xlint/lint1/msg_306.c	Thu Feb 20 21:53:28 2025
@@ -1,32 +1,40 @@
-/*	$NetBSD: msg_306.c,v 1.5 2023/07/07 19:45:22 rillig Exp $	*/
+/*	$NetBSD: msg_306.c,v 1.6 2025/02/20 21:53:28 rillig Exp $	*/
 # 3 "msg_306.c"
 
-// Test for message: constant truncated by conversion, op '%s' [306]
+// Test for message: constant %s truncated by conversion, op '%s' [306]
 
 /* lint1-extra-flags: -X 351 */
 
-unsigned char
-to_u8(void)
-{
-	/* expect+1: warning: conversion of 'int' to 'unsigned char' is out of range [119] */
-	return 12345;
-}
-
-unsigned char
-and_u8(unsigned char a)
-{
-	/* XXX: unused bits in constant */
-	return a & 0x1234;
-}
+signed char s8;
+unsigned char u8;
 
-unsigned char
-or_u8(unsigned char a)
+void
+msg_306(void)
 {
-	/* expect+1: warning: constant truncated by conversion, op '|=' [306] */
-	a |= 0x1234;
+	u8 = 0xff;
+	/* expect+1: warning: constant truncated by assignment [165] */
+	u8 = 0x100;
+
+	u8 &= 0xff;
+	/* expect+1: warning: constant 0x100 truncated by conversion, op '&=' [306] */
+	u8 &= 0x100;
+	/* XXX: Lint doesn't care about the expanded form of the same code. */
+	u8 = u8 & 0x100;
 
+	u8 |= 0xff;
+	/* expect+1: warning: constant 0x100 truncated by conversion, op '|=' [306] */
+	u8 |= 0x100;
 	/* XXX: Lint doesn't care about the expanded form of the same code. */
-	a = a | 0x1234;
+	u8 = u8 | 0x100;
 
-	return a;
+	s8 &= 0xff;
+	/* expect+1: warning: constant 0x100 truncated by conversion, op '&=' [306] */
+	s8 &= 0x100;
+	/* XXX: Lint doesn't care about the expanded form of the same code. */
+	s8 = s8 & 0x100;
+	s8 |= 0xff;
+	/* expect+1: warning: constant 0x100 truncated by conversion, op '|=' [306] */
+	s8 |= 0x100;
+	/* XXX: Lint doesn't care about the expanded form of the same code. */
+	s8 = s8 | 0x100;
 }

Index: src/usr.bin/xlint/lint1/err.c
diff -u src/usr.bin/xlint/lint1/err.c:1.263 src/usr.bin/xlint/lint1/err.c:1.264
--- src/usr.bin/xlint/lint1/err.c:1.263	Thu Feb 20 20:59:34 2025
+++ src/usr.bin/xlint/lint1/err.c	Thu Feb 20 21:53:28 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: err.c,v 1.263 2025/02/20 20:59:34 rillig Exp $	*/
+/*	$NetBSD: err.c,v 1.264 2025/02/20 21:53:28 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: err.c,v 1.263 2025/02/20 20:59:34 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.264 2025/02/20 21:53:28 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -361,7 +361,7 @@ static const char *const msgs[] = {
 	"conversion of %s to %s requires a cast",			// 303
 	"conversion of %s to %s requires a cast, arg #%d",		// 304
 	"conversion of %s to %s requires a cast, op %s",		// 305
-	"constant truncated by conversion, op '%s'",			// 306
+	"constant %s truncated by conversion, op '%s'",			// 306
 	"static variable '%s' set but not used",			// 307
 	"invalid type for _Complex",					// 308
 	"extra bits set to 0 in conversion of '%s' to '%s', op '%s'",	// 309

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.671 src/usr.bin/xlint/lint1/tree.c:1.672
--- src/usr.bin/xlint/lint1/tree.c:1.671	Thu Feb 20 20:59:34 2025
+++ src/usr.bin/xlint/lint1/tree.c	Thu Feb 20 21:53:28 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.671 2025/02/20 20:59:34 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.672 2025/02/20 21:53:28 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.671 2025/02/20 20:59:34 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.672 2025/02/20 21:53:28 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -4045,17 +4045,27 @@ convert_constant_to_floating(tspec_t nt,
 	return true;
 }
 
-/*
- * Print a warning if bits which were set are lost due to the conversion.
- * This can happen with operator ORASS only.
- */
+static void
+warn_constant_truncated(op_t op, const val_t *v)
+{
+	char buf[256];
+	bool is_unsigned = is_uinteger(v->v_tspec);
+	int64_t val = v->u.integer;
+	unsigned long long abs_val = is_unsigned || val >= 0
+	    ? (unsigned long long)val
+	    : -(unsigned long long)val;
+	const char *sign = is_unsigned || val >= 0 ? "" : "-";
+	snprintf(buf, sizeof(buf), "%s%#llx", sign, abs_val);
+	/* constant %s truncated by conversion, op '%s' */
+	warning(306, buf, op_name(op));
+}
+
 static void
 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
 				   uint64_t xmask, op_t op)
 {
 	if (nsz < osz && (v->u.integer & xmask) != 0)
-		/* constant truncated by conversion, op '%s' */
-		warning(306, op_name(op));
+		warn_constant_truncated(op, v);
 }
 
 static void
@@ -4073,8 +4083,7 @@ convert_constant_check_range_bitand(size
 	} else if (nsz < osz &&
 	    (v->u.integer & xmask) != xmask &&
 	    (v->u.integer & xmask) != 0)
-		/* constant truncated by conversion, op '%s' */
-		warning(306, op_name(op));
+		warn_constant_truncated(op, v);
 }
 
 static void

Reply via email to