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: add test for warning a...



details:   https://anonhg.NetBSD.org/src/rev/a25b199c7dc9
branches:  trunk
changeset: 983322:a25b199c7dc9
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 16 10:08:01 2021 +0000

description:
tests/lint: add test for warning about zero-bits in '&'

diffstat:

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

diffs (52 lines):

diff -r 3807e158f3e5 -r a25b199c7dc9 tests/usr.bin/xlint/lint1/msg_309.c
--- a/tests/usr.bin/xlint/lint1/msg_309.c       Sun May 16 09:43:39 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_309.c       Sun May 16 10:08:01 2021 +0000
@@ -1,7 +1,39 @@
-/*     $NetBSD: msg_309.c,v 1.2 2021/02/21 09:07:58 rillig Exp $       */
+/*     $NetBSD: msg_309.c,v 1.3 2021/05/16 10:08:01 rillig Exp $       */
 # 3 "msg_309.c"
 
 // Test for message: extra bits set to 0 in conversion of '%s' to '%s', op %s [309]
 
-TODO: "Add example code that triggers the above message." /* expect: 249 */
-TODO: "Add example code that almost triggers the above message."
+int
+scale(unsigned long long x) {
+
+       /*
+        * Both operands of '&' have the same type, therefore no conversion
+        * is necessary and no bits can get lost.
+        */
+       if ((x & 0xffffffff00000000ULL) != 0)
+               return 32;
+
+       /*
+        * The constant has type 'unsigned 32-bit'.  The usual arithmetic
+        * conversions of '&' convert this constant to unsigned 64-bit.
+        * The programmer may or may not have intended to sign-extend the
+        * bit mask here.  This situation may occur during migration from a
+        * 32-bit to a 64-bit platform.
+        */
+       if ((x & 0xffff0000) != 0)      /* expect: 309 */
+               return 16;
+
+       /*
+        * In the remaining cases, the constant does not have its most
+        * significant bit set, therefore there is no ambiguity.
+        */
+       if ((x & 0xff00) != 0)
+               return 8;
+       if ((x & 0xf0) != 0)
+               return 4;
+       if ((x & 0xc) != 0)
+               return 2;
+       if ((x & 0x2) != 0)
+               return 1;
+       return (int)(x & 0x1);
+}
diff -r 3807e158f3e5 -r a25b199c7dc9 tests/usr.bin/xlint/lint1/msg_309.exp
--- a/tests/usr.bin/xlint/lint1/msg_309.exp     Sun May 16 09:43:39 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_309.exp     Sun May 16 10:08:01 2021 +0000
@@ -1,1 +1,1 @@
-msg_309.c(6): error: syntax error ':' [249]
+msg_309.c(23): warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op & [309]



Home | Main Index | Thread Index | Old Index