Module Name: src Committed By: rillig Date: Thu May 26 20:17:40 UTC 2022
Modified Files: src/tests/usr.bin/xlint/lint1: msg_132.c msg_132.exp src/usr.bin/xlint/lint1: tree.c Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.12 -r1.13 src/tests/usr.bin/xlint/lint1/msg_132.c cvs rdiff -u -r1.11 -r1.12 src/tests/usr.bin/xlint/lint1/msg_132.exp cvs rdiff -u -r1.448 -r1.449 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.12 src/tests/usr.bin/xlint/lint1/msg_132.c:1.13 --- src/tests/usr.bin/xlint/lint1/msg_132.c:1.12 Thu May 26 19:55:57 2022 +++ src/tests/usr.bin/xlint/lint1/msg_132.c Thu May 26 20:17:40 2022 @@ -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 @@ u32_t 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] */ Index: src/tests/usr.bin/xlint/lint1/msg_132.exp diff -u src/tests/usr.bin/xlint/lint1/msg_132.exp:1.11 src/tests/usr.bin/xlint/lint1/msg_132.exp:1.12 --- src/tests/usr.bin/xlint/lint1/msg_132.exp:1.11 Thu May 26 19:55:57 2022 +++ src/tests/usr.bin/xlint/lint1/msg_132.exp Thu May 26 20:17:40 2022 @@ -25,6 +25,5 @@ msg_132.c(99): warning: conversion from 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] Index: src/usr.bin/xlint/lint1/tree.c diff -u src/usr.bin/xlint/lint1/tree.c:1.448 src/usr.bin/xlint/lint1/tree.c:1.449 --- src/usr.bin/xlint/lint1/tree.c:1.448 Thu May 26 18:08:33 2022 +++ src/usr.bin/xlint/lint1/tree.c Thu May 26 20:17:40 2022 @@ -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 @@ static void check_precedence_confusion(t 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 @@ ic_shl(const type_t *tp, integer_constra 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 @@ ic_shl(const type_t *tp, integer_constra } 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 @@ ic_expr(const tnode_t *tn) 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);