Source-Changes-HG archive

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

[src/trunk]: src/tests/usr.bin/xlint/lint1 tests/lint: demonstrate wrong 'may...



details:   https://anonhg.NetBSD.org/src/rev/2c7530f84abe
branches:  trunk
changeset: 366414:2c7530f84abe
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu May 26 07:03:03 2022 +0000

description:
tests/lint: demonstrate wrong 'may lose accuracy' warning

Reported in PR 36668, fixed in sys/sys/endian.h 1.26 from 2007-07-20,
unfixed in sys/sys/endian.h 1.29 from 2014-03-18.

diffstat:

 tests/usr.bin/xlint/lint1/msg_132.c   |  36 ++++++++++++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/msg_132.exp |   1 +
 2 files changed, 36 insertions(+), 1 deletions(-)

diffs (55 lines):

diff -r d257f3bdcb56 -r 2c7530f84abe tests/usr.bin/xlint/lint1/msg_132.c
--- a/tests/usr.bin/xlint/lint1/msg_132.c       Thu May 26 06:48:36 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_132.c       Thu May 26 07:03:03 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_132.c,v 1.9 2022/04/21 19:48:18 rillig Exp $       */
+/*     $NetBSD: msg_132.c,v 1.10 2022/05/26 07:03:03 rillig Exp $      */
 # 3 "msg_132.c"
 
 // Test for message: conversion from '%s' to '%s' may lose accuracy [132]
@@ -140,3 +140,37 @@
        /* expect+1: warning: conversion from 'unsigned long long' to 'int' may lose accuracy [132] */
        return not_a_constant * 8ULL;
 }
+
+typedef unsigned char u8_t;
+typedef unsigned short u16_t;
+typedef unsigned int u32_t;
+typedef unsigned long long u64_t;
+
+/*
+ * PR 36668 notices that lint wrongly complains about the possible loss.
+ * The expression 'uint8_t << 8' is guaranteed to fit into an 'unsigned short'.
+ * 'unsigned short | unsigned char' is guaranteed to fit into 'unsigned short'
+ */
+static inline u16_t
+be16dec(const void *buf)
+{
+       const u8_t *p = buf;
+
+       /* expect+1: warning: conversion from 'int' to 'unsigned short' may lose accuracy [132] */
+       return ((u16_t)p[0]) << 8 | p[1];
+}
+
+/*
+ * Since tree.c 1.434 from 2022-04-19, lint infers the possible values of
+ * expressions of the form 'integer & constant', see can_represent.
+ */
+static inline void
+be32enc(void *buf, u32_t u)
+{
+       u8_t *p = buf;
+
+       p[0] = u >> 24 & 0xff;
+       p[1] = u >> 16 & 0xff;
+       p[2] = u >> 8 & 0xff;
+       p[3] = u & 0xff;
+}
diff -r d257f3bdcb56 -r 2c7530f84abe tests/usr.bin/xlint/lint1/msg_132.exp
--- a/tests/usr.bin/xlint/lint1/msg_132.exp     Thu May 26 06:48:36 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_132.exp     Thu May 26 07:03:03 2022 +0000
@@ -25,3 +25,4 @@
 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(160): warning: conversion from 'int' to 'unsigned short' may lose accuracy [132]



Home | Main Index | Thread Index | Old Index