Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: do not warn about 'uint32_t = uint...



details:   https://anonhg.NetBSD.org/src/rev/4b4db0e21ce6
branches:  trunk
changeset: 366441:4b4db0e21ce6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu May 26 20:17:40 2022 +0000

description:
lint: do not warn about 'uint32_t = uint64_t >> 32'

If all possible values fit into the destination type, there is no
possibility of losing accuracy.

Enhances PR 36668.

diffstat:

 tests/usr.bin/xlint/lint1/msg_132.c   |   3 +-
 tests/usr.bin/xlint/lint1/msg_132.exp |   5 +--
 usr.bin/xlint/lint1/tree.c            |  44 +++++++++++++++++++++++++++++++++-
 3 files changed, 45 insertions(+), 7 deletions(-)

diffs (115 lines):

diff -r fc009dfce066 -r 4b4db0e21ce6 tests/usr.bin/xlint/lint1/msg_132.c
--- a/tests/usr.bin/xlint/lint1/msg_132.c       Thu May 26 19:55:57 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_132.c       Thu May 26 20:17:40 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_132.c,v 1.12 2022/05/26 19:55:57 rillig Exp $      */
+/*     $NetBSD: msg_132.c,v 1.13 2022/05/26 20:17:40 rillig Exp $      */
 # 3 "msg_132.c"
 
 // Test for message: conversion from '%s' to '%s' may lose accuracy [132]
@@ -187,7 +187,6 @@
 test_ic_shr(u64_t x)
 {
        if (x > 3)
-               /* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132] */
                return x >> 32;
        if (x > 2)
                /* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132] */
diff -r fc009dfce066 -r 4b4db0e21ce6 tests/usr.bin/xlint/lint1/msg_132.exp
--- a/tests/usr.bin/xlint/lint1/msg_132.exp     Thu May 26 19:55:57 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_132.exp     Thu May 26 20:17:40 2022 +0000
@@ -25,6 +25,5 @@
 msg_132.c(125): error: operands of '+' have incompatible types (pointer != double) [107]
 msg_132.c(125): warning: function 'cover_build_plus_minus' expects to return value [214]
 msg_132.c(141): warning: conversion from 'unsigned long long' to 'int' may lose accuracy [132]
-msg_132.c(191): warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132]
-msg_132.c(194): warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132]
-msg_132.c(196): warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132]
+msg_132.c(193): warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132]
+msg_132.c(195): warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132]
diff -r fc009dfce066 -r 4b4db0e21ce6 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Thu May 26 19:55:57 2022 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Thu May 26 20:17:40 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.448 2022/05/26 18:08:33 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.449 2022/05/26 20:17:40 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.448 2022/05/26 18:08:33 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.449 2022/05/26 20:17:40 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -105,6 +105,14 @@
 
 extern sig_atomic_t fpe;
 
+static bool
+ic_maybe_signed(const type_t *tp, const integer_constraints *ic)
+{
+
+       return !is_uinteger(tp->t_tspec) &&
+           (ic->bclr & ((uint64_t)1 << 63)) == 0;
+}
+
 static integer_constraints
 ic_any(const type_t *tp)
 {
@@ -192,6 +200,9 @@
        integer_constraints c;
        unsigned int amount;
 
+       if (ic_maybe_signed(tp, &a))
+               return ic_any(tp);
+
        if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
                amount = (unsigned int)b.smin;
        else if (b.umin == b.umax && b.umin < 64)
@@ -209,6 +220,31 @@
 }
 
 static integer_constraints
+ic_shr(const type_t *tp, integer_constraints a, integer_constraints b)
+{
+       integer_constraints c;
+       unsigned int amount;
+
+       if (ic_maybe_signed(tp, &a))
+               return ic_any(tp);
+
+       if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
+               amount = (unsigned int)b.smin;
+       else if (b.umin == b.umax && b.umin < 64)
+               amount = (unsigned int)b.umin;
+       else
+               return ic_any(tp);
+
+       c.smin = INT64_MIN;
+       c.smax = INT64_MAX;
+       c.umin = 0;
+       c.umax = UINT64_MAX;
+       c.bset = a.bset >> amount;
+       c.bclr = a.bclr >> amount | ~(~(uint64_t)0 >> amount);
+       return c;
+}
+
+static integer_constraints
 ic_expr(const tnode_t *tn)
 {
        integer_constraints lc, rc;
@@ -223,6 +259,10 @@
                lc = ic_expr(tn->tn_left);
                rc = ic_expr(tn->tn_right);
                return ic_shl(tn->tn_type, lc, rc);
+       case SHR:
+               lc = ic_expr(tn->tn_left);
+               rc = ic_expr(tn->tn_right);
+               return ic_shr(tn->tn_type, lc, rc);
        case BITAND:
                lc = ic_expr(tn->tn_left);
                rc = ic_expr(tn->tn_right);



Home | Main Index | Thread Index | Old Index