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 missing test cases...



details:   https://anonhg.NetBSD.org/src/rev/9cbbc40c6e44
branches:  trunk
changeset: 1023277:9cbbc40c6e44
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Aug 31 18:15:56 2021 +0000

description:
tests/lint: add missing test cases for integer conversions

While adding the missing test cases, I re-read the comments and
discarded several of them, since converting a signed value to an
unsigned type can be lossy as well, which warrants a warning.

diffstat:

 tests/usr.bin/xlint/lint1/msg_259.c   |  136 +++++++++++++++++++++++++--------
 tests/usr.bin/xlint/lint1/msg_259.exp |   26 +++++-
 2 files changed, 121 insertions(+), 41 deletions(-)

diffs (213 lines):

diff -r a13c469090ed -r 9cbbc40c6e44 tests/usr.bin/xlint/lint1/msg_259.c
--- a/tests/usr.bin/xlint/lint1/msg_259.c       Tue Aug 31 17:51:30 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_259.c       Tue Aug 31 18:15:56 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_259.c,v 1.13 2021/08/30 18:33:37 rillig Exp $      */
+/*     $NetBSD: msg_259.c,v 1.14 2021/08/31 18:15:56 rillig Exp $      */
 # 3 "msg_259.c"
 
 // Test for message: argument #%d is converted from '%s' to '%s' due to prototype [259]
@@ -6,67 +6,133 @@
 /* lint1-only-if: lp64 */
 /* lint1-extra-flags: -h */
 
-void farg_char(char);
-void farg_int(int);
-void farg_long(long);
+void plain_char(char);
+void signed_int(int);
+void unsigned_int(unsigned int);
+void signed_long(long);
+void unsigned_long(unsigned long);
+void signed_long_long(long long);
+void unsigned_long_long(unsigned long long);
 
 void
-example(char c, int i, long l)
+change_in_type_width(char c, int i, long l)
 {
-       farg_char(c);
-       farg_int(c);
-       /* No warning 259 on LP64, only on ILP32 */
-       farg_long(c);
-
-       farg_char(i);           /* XXX: why no warning? */
-       farg_int(i);
+       plain_char(c);
+       signed_int(c);
        /* No warning 259 on LP64, only on ILP32 */
-       farg_long(i);
+       signed_long(c);
 
-       farg_char(l);           /* XXX: why no warning? */
+       plain_char(i);          /* XXX: why no warning? */
+       signed_int(i);
+       /* No warning 259 on LP64, only on ILP32 */
+       signed_long(i);
+
+       plain_char(l);          /* XXX: why no warning? */
        /* expect+1: from 'long' to 'int' due to prototype [259] */
-       farg_int(l);
-       farg_long(l);
+       signed_int(l);
+       signed_long(l);
 }
 
-void farg_unsigned_int(unsigned int);
-void farg_unsigned_long(unsigned long);
-void farg_unsigned_long_long(unsigned long long);
-
 /*
  * Converting a signed integer type to its corresponding unsigned integer
- * type (C99 6.2.5p6) is usually not a problem.  A common case where it
- * occurs is when the difference of two pointers is converted to size_t.
+ * type (C99 6.2.5p6) is usually not a problem since the actual values of the
+ * expressions are usually not anywhere near the maximum signed value.  From
+ * a technical standpoint, it is correct to warn here since even small
+ * negative numbers may result in very large positive numbers.
+ *
+ * A common case where it occurs is when the difference of two pointers is
+ * converted to size_t.  The type ptrdiff_t is defined to be signed, but in
+ * many practical cases, the expression is '(end - start)', which makes the
+ * resulting value necessarily positive.
  */
 void
-convert_to_corresponding_unsigned(int i, long l, long long ll)
+signed_to_unsigned(int si, long sl, long long sll)
 {
-       /* TODO: don't warn here. */
        /* expect+1: warning: argument #1 is converted from 'int' to 'unsigned int' due to prototype [259] */
-       farg_unsigned_int(i);
+       unsigned_int(si);
 
-       /* TODO: don't warn here. */
-       /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259] */
-       farg_unsigned_long(l);
+       /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned int' due to prototype [259] */
+       unsigned_int(sl);
 
-       /* TODO: don't warn here. */
-       /* expect+1: warning: argument #1 is converted from 'long long' to 'unsigned long long' due to prototype [259] */
-       farg_unsigned_long_long(ll);
+       /* expect+1: warning: argument #1 is converted from 'long long' to 'unsigned int' due to prototype [259] */
+       unsigned_int(sll);
 
        /*
         * XXX: Why no warning?  Even though 'unsigned long' is 64 bits
         * wide, it cannot represent negative 32-bit values.
         */
-       farg_unsigned_long(i);
+       unsigned_long(si);
+
+       /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259] */
+       unsigned_long(sl);
+
+       unsigned_long(si);
 
        /*
         * XXX: Why no warning?  Even though 'unsigned long long' is 64 bits
         * wide, it cannot represent negative 32-bit values.
         */
-       farg_unsigned_long_long(i);
+       unsigned_long_long(si);
 
        /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long long' due to prototype [259] */
-       farg_unsigned_long_long(l);
+       unsigned_long_long(sl);
+
+       /* expect+1: warning: argument #1 is converted from 'long long' to 'unsigned long long' due to prototype [259] */
+       unsigned_long_long(sll);
+}
+
+void
+unsigned_to_signed(unsigned int ui, unsigned long ul, unsigned long long ull)
+{
+       /* expect+1: warning: argument #1 is converted from 'unsigned int' to 'int' due to prototype [259] */
+       signed_int(ui);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'int' due to prototype [259] */
+       signed_int(ul);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'int' due to prototype [259] */
+       signed_int(ull);
+       signed_long(ui);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'long' due to prototype [259] */
+       signed_long(ul);
+       signed_long(ui);
+       signed_long_long(ui);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'long long' due to prototype [259] */
+       signed_long_long(ul);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'long long' due to prototype [259] */
+       signed_long_long(ull);
+}
+
+void
+signed_to_signed(signed int si, signed long sl, signed long long sll)
+{
+       signed_int(si);
+       /* expect+1: warning: argument #1 is converted from 'long' to 'int' due to prototype [259] */
+       signed_int(sl);
+       /* expect+1: warning: argument #1 is converted from 'long long' to 'int' due to prototype [259] */
+       signed_int(sll);
+       signed_long(si);
+       signed_long(sl);
+       signed_long(si);
+       signed_long_long(si);
+       /* expect+1: warning: argument #1 is converted from 'long' to 'long long' due to prototype [259] */
+       signed_long_long(sl);
+       signed_long_long(sll);
+}
+
+void
+unsigned_to_unsigned(unsigned int ui, unsigned long ul, unsigned long long ull)
+{
+       unsigned_int(ui);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
+       unsigned_int(ul);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'unsigned int' due to prototype [259] */
+       unsigned_int(ull);
+       unsigned_long(ui);
+       unsigned_long(ul);
+       unsigned_long(ui);
+       unsigned_long_long(ui);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned long long' due to prototype [259] */
+       unsigned_long_long(ul);
+       unsigned_long_long(ull);
 }
 
 void
@@ -78,5 +144,5 @@
         * that it would even fit into a 3-bit bit-field.
         */
        /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
-       farg_unsigned_int(sizeof(int));
+       unsigned_int(sizeof(int));
 }
diff -r a13c469090ed -r 9cbbc40c6e44 tests/usr.bin/xlint/lint1/msg_259.exp
--- a/tests/usr.bin/xlint/lint1/msg_259.exp     Tue Aug 31 17:51:30 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_259.exp     Tue Aug 31 18:15:56 2021 +0000
@@ -1,6 +1,20 @@
-msg_259.c(28): warning: argument #1 is converted from 'long' to 'int' due to prototype [259]
-msg_259.c(46): warning: argument #1 is converted from 'int' to 'unsigned int' due to prototype [259]
-msg_259.c(50): warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259]
-msg_259.c(54): warning: argument #1 is converted from 'long long' to 'unsigned long long' due to prototype [259]
-msg_259.c(69): warning: argument #1 is converted from 'long' to 'unsigned long long' due to prototype [259]
-msg_259.c(81): warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259]
+msg_259.c(32): warning: argument #1 is converted from 'long' to 'int' due to prototype [259]
+msg_259.c(52): warning: argument #1 is converted from 'int' to 'unsigned int' due to prototype [259]
+msg_259.c(55): warning: argument #1 is converted from 'long' to 'unsigned int' due to prototype [259]
+msg_259.c(58): warning: argument #1 is converted from 'long long' to 'unsigned int' due to prototype [259]
+msg_259.c(67): warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259]
+msg_259.c(78): warning: argument #1 is converted from 'long' to 'unsigned long long' due to prototype [259]
+msg_259.c(81): warning: argument #1 is converted from 'long long' to 'unsigned long long' due to prototype [259]
+msg_259.c(88): warning: argument #1 is converted from 'unsigned int' to 'int' due to prototype [259]
+msg_259.c(90): warning: argument #1 is converted from 'unsigned long' to 'int' due to prototype [259]
+msg_259.c(92): warning: argument #1 is converted from 'unsigned long long' to 'int' due to prototype [259]
+msg_259.c(95): warning: argument #1 is converted from 'unsigned long' to 'long' due to prototype [259]
+msg_259.c(99): warning: argument #1 is converted from 'unsigned long' to 'long long' due to prototype [259]
+msg_259.c(101): warning: argument #1 is converted from 'unsigned long long' to 'long long' due to prototype [259]
+msg_259.c(109): warning: argument #1 is converted from 'long' to 'int' due to prototype [259]
+msg_259.c(111): warning: argument #1 is converted from 'long long' to 'int' due to prototype [259]
+msg_259.c(117): warning: argument #1 is converted from 'long' to 'long long' due to prototype [259]
+msg_259.c(126): warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259]
+msg_259.c(128): warning: argument #1 is converted from 'unsigned long long' to 'unsigned int' due to prototype [259]
+msg_259.c(134): warning: argument #1 is converted from 'unsigned long' to 'unsigned long long' due to prototype [259]
+msg_259.c(147): warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259]



Home | Main Index | Thread Index | Old Index