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: provide more backgroun...



details:   https://anonhg.NetBSD.org/src/rev/e58cc7a52d11
branches:  trunk
changeset: 1023373:e58cc7a52d11
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Sep 04 21:20:44 2021 +0000

description:
tests/lint: provide more background information on signed '>>'

diffstat:

 tests/usr.bin/xlint/lint1/msg_117.c   |  69 ++++++++++++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/msg_117.exp |   5 ++
 2 files changed, 73 insertions(+), 1 deletions(-)

diffs (92 lines):

diff -r 9547aa9d0dc9 -r e58cc7a52d11 tests/usr.bin/xlint/lint1/msg_117.c
--- a/tests/usr.bin/xlint/lint1/msg_117.c       Sat Sep 04 20:39:17 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_117.c       Sat Sep 04 21:20:44 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_117.c,v 1.9 2021/08/27 17:59:46 rillig Exp $       */
+/*     $NetBSD: msg_117.c,v 1.10 2021/09/04 21:20:44 rillig Exp $      */
 # 3 "msg_117.c"
 
 // Test for message: bitwise '%s' on signed value possibly nonportable [117]
@@ -66,3 +66,70 @@
         */
        return (unsigned char)((unsigned char)(bit - 1) >> 5);
 }
+
+/*
+ * C90 3.3.7, C99 6.5.7 and C11 6.5.7 all say the same: If E1 has a signed
+ * type and a negative value, the resulting value is implementation-defined.
+ *
+ * These standards don't guarantee anything about the lower bits of the
+ * resulting value, which are generally independent of whether the shift is
+ * performed in signed arithmetics or in unsigned arithmetics.  The C99
+ * rationale talks about signed shifts, but does not provide any guarantee
+ * either.  It merely suggests that platforms are free to use unsigned shifts
+ * even if the operand type is signed.
+ *
+ * K&R provides more guarantees by saying: Right shifting a signed quantity
+ * will fill with sign bits ("arithmetic shift") on some machines such as the
+ * PDP-Il, and with 0-bits ("logical shift") on others.
+ *
+ * https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html says:
+ * Signed '>>' acts on negative numbers by sign extension.
+ *
+ * This means that at least in GCC mode, lint may decide to not warn about
+ * these cases.
+ */
+void
+shr_signed_ignoring_high_bits(int x)
+{
+
+       /*
+        * All sane platforms should define that 'x >> 0 == x', even if x is
+        * negative.
+        */
+       /* expect+1: warning: bitwise '>>' on signed value possibly nonportable [117] */
+       if (x >> 0 != 0)
+               return;
+
+       /*
+        * If x is negative, x >> 1 is nonzero, no matter whether the shift
+        * is arithmetic or logical.
+        */
+       /* expect+1: warning: bitwise '>>' on signed value possibly nonportable [117] */
+       if (x >> 1 != 0)
+               return;
+
+       /*
+        * The highest bit may be 0 or 1, the others should be well-defined
+        * on all sane platforms, making it irrelevant whether the actual
+        * shift operation is arithmetic or logical.
+        */
+       /* expect+1: warning: bitwise '>>' on signed value possibly nonportable [117] */
+       if (((x >> 1) & 1) != 0)
+               return;
+
+       /*
+        * The result of this expression is the same with arithmetic and
+        * logical shifts since the filled bits are masked out.
+        */
+       /* expect+1: warning: bitwise '>>' on signed value possibly nonportable [117] */
+       if (((x >> 31) & 1) != 0)
+               return;
+
+       /*
+        * In this case, arithmetic shift results in 2 while logical shift
+        * results in 0.  This difference is what this warning is about.
+        */
+       /* expect+1: warning: bitwise '>>' on signed value possibly nonportable [117] */
+       if (((x >> 31) & 2) != 0)
+               return;
+}
diff -r 9547aa9d0dc9 -r e58cc7a52d11 tests/usr.bin/xlint/lint1/msg_117.exp
--- a/tests/usr.bin/xlint/lint1/msg_117.exp     Sat Sep 04 20:39:17 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_117.exp     Sat Sep 04 21:20:44 2021 +0000
@@ -5,3 +5,8 @@
 msg_117.c(35): warning: bitwise '>>' on signed value possibly nonportable [117]
 msg_117.c(35): warning: negative shift [121]
 msg_117.c(57): warning: bitwise '>>' on signed value possibly nonportable [117]
+msg_117.c(100): warning: bitwise '>>' on signed value possibly nonportable [117]
+msg_117.c(108): warning: bitwise '>>' on signed value possibly nonportable [117]
+msg_117.c(117): warning: bitwise '>>' on signed value possibly nonportable [117]
+msg_117.c(125): warning: bitwise '>>' on signed value possibly nonportable [117]
+msg_117.c(133): warning: bitwise '>>' on signed value possibly nonportable [117]



Home | Main Index | Thread Index | Old Index