Module Name: src
Committed By: rillig
Date: Tue Aug 8 20:15:11 UTC 2023
Modified Files:
src/tests/usr.bin/xlint/lint1: msg_132_ilp32.c
src/usr.bin/xlint/lint1: tree.c
Log Message:
lint: in -a mode, do not warn about integer conversions from 'int'
Since tree.c 1.552 from 2023-07-08, lint warned about integer
conversions from 'int' or 'unsigned int' to smaller integer types. This
only affected 32-bit platforms where size_t is 'unsigned int' rather
than 'unsigned long', as on these platforms, the integer ranks of 'int'
and 'long' are the same, see INT_RANK in inittyp.c.
Discovered by lib/libkvm, which fails on i386 when lint generates any
warnings.
To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/lint1/msg_132_ilp32.c
cvs rdiff -u -r1.576 -r1.577 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_ilp32.c
diff -u src/tests/usr.bin/xlint/lint1/msg_132_ilp32.c:1.4 src/tests/usr.bin/xlint/lint1/msg_132_ilp32.c:1.5
--- src/tests/usr.bin/xlint/lint1/msg_132_ilp32.c:1.4 Tue Aug 8 19:57:23 2023
+++ src/tests/usr.bin/xlint/lint1/msg_132_ilp32.c Tue Aug 8 20:15:11 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: msg_132_ilp32.c,v 1.4 2023/08/08 19:57:23 rillig Exp $ */
+/* $NetBSD: msg_132_ilp32.c,v 1.5 2023/08/08 20:15:11 rillig Exp $ */
# 3 "msg_132_ilp32.c"
// Test for message: conversion from '%s' to '%s' may lose accuracy [132]
@@ -76,9 +76,7 @@ convert_all(void)
char_var = uchar_var;
char_var = short_var;
char_var = ushort_var;
- /* expect+1: warning: conversion from 'int' to 'char' may lose accuracy [132] */
char_var = int_var;
- /* expect+1: warning: conversion from 'unsigned int' to 'char' may lose accuracy [132] */
char_var = uint_var;
/* expect+1: warning: conversion from 'long' to 'char' may lose accuracy [132] */
char_var = long_var;
@@ -95,9 +93,7 @@ convert_all(void)
schar_var = uchar_var;
schar_var = short_var;
schar_var = ushort_var;
- /* expect+1: warning: conversion from 'int' to 'signed char' may lose accuracy [132] */
schar_var = int_var;
- /* expect+1: warning: conversion from 'unsigned int' to 'signed char' may lose accuracy [132] */
schar_var = uint_var;
/* expect+1: warning: conversion from 'long' to 'signed char' may lose accuracy [132] */
schar_var = long_var;
@@ -114,9 +110,7 @@ convert_all(void)
uchar_var = uchar_var;
uchar_var = short_var;
uchar_var = ushort_var;
- /* expect+1: warning: conversion from 'int' to 'unsigned char' may lose accuracy [132] */
uchar_var = int_var;
- /* expect+1: warning: conversion from 'unsigned int' to 'unsigned char' may lose accuracy [132] */
uchar_var = uint_var;
/* expect+1: warning: conversion from 'long' to 'unsigned char' may lose accuracy [132] */
uchar_var = long_var;
@@ -133,9 +127,7 @@ convert_all(void)
short_var = uchar_var;
short_var = short_var;
short_var = ushort_var;
- /* expect+1: warning: conversion from 'int' to 'short' may lose accuracy [132] */
short_var = int_var;
- /* expect+1: warning: conversion from 'unsigned int' to 'short' may lose accuracy [132] */
short_var = uint_var;
/* expect+1: warning: conversion from 'long' to 'short' may lose accuracy [132] */
short_var = long_var;
@@ -152,9 +144,7 @@ convert_all(void)
ushort_var = uchar_var;
ushort_var = short_var;
ushort_var = ushort_var;
- /* expect+1: warning: conversion from 'int' to 'unsigned short' may lose accuracy [132] */
ushort_var = int_var;
- /* expect+1: warning: conversion from 'unsigned int' to 'unsigned short' may lose accuracy [132] */
ushort_var = uint_var;
/* expect+1: warning: conversion from 'long' to 'unsigned short' may lose accuracy [132] */
ushort_var = long_var;
Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.576 src/usr.bin/xlint/lint1/tree.c:1.577
--- src/usr.bin/xlint/lint1/tree.c:1.576 Sat Aug 5 10:13:39 2023
+++ src/usr.bin/xlint/lint1/tree.c Tue Aug 8 20:15:10 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: tree.c,v 1.576 2023/08/05 10:13:39 rillig Exp $ */
+/* $NetBSD: tree.c,v 1.577 2023/08/08 20:15:10 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.576 2023/08/05 10:13:39 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.577 2023/08/08 20:15:10 rillig Exp $");
#endif
#include <float.h>
@@ -3386,6 +3386,27 @@ can_represent(const type_t *tp, const tn
return false;
}
+static bool
+should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
+ const tnode_t *otn, tspec_t ot)
+{
+
+ // XXX: The portable_rank_cmp aims at portable mode, independent of the
+ // current platform, while can_represent acts on the actual type sizes
+ // from the current platform. This mix is inconsistent, but anything
+ // else would make the exact conditions too complicated to grasp.
+ if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
+ if (ot == LONG || ot == ULONG
+ || ot == LLONG || ot == ULLONG
+#ifdef INT128_SIZE
+ || ot == INT128 || ot == UINT128
+#endif
+ || aflag > 1)
+ return !can_represent(ntp, otn);
+ }
+ return false;
+}
+
static void
convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
type_t *tp, tnode_t *tn)
@@ -3417,15 +3438,7 @@ convert_integer_from_integer(op_t op, in
op_name(tn->tn_op));
}
- if (aflag > 0 &&
- portable_rank_cmp(nt, ot) < 0 &&
- (portable_rank_cmp(ot, LONG) >= 0 || aflag > 1) &&
- // XXX: The portable_rank_cmp above aims at portable mode,
- // independent of the current platform, while can_represent acts
- // on the actual type sizes from the current platform. This mix
- // is inconsistent, but anything else would make the exact
- // conditions too complicated to grasp.
- !can_represent(tp, tn)) {
+ if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
if (op == FARG) {
/* conversion from '%s' to '%s' may lose ... */
warning(298,