Source-Changes-HG archive

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

[src/trunk]: src tests/lint: add test for prototype conversions in C90



details:   https://anonhg.NetBSD.org/src/rev/10ead96c8860
branches:  trunk
changeset: 1023278:10ead96c8860
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Aug 31 18:59:26 2021 +0000

description:
tests/lint: add test for prototype conversions in C90

The purpose of warning 259 is to find function calls that differ in the
ABI.  The warning's original purpose was not to warn about lossy
conversions, that's just a side effect.

Warning 259 had been implemented before C99 was published, which is more
than 20 years ago.  In the meantime, almost all code has migrated to
using function prototypes.  With the default lint flags from NetBSD's
<sys.mk>, it would rather make sense to focus on lossy conversions now.

To prepare for potentially upcoming differences in lint's C90 and C99
modes, clone the test now as far as possible.  The test for C90 mode is
smaller than for C99 mode, since 'long long' was not available back
then.

diffstat:

 distrib/sets/lists/tests/mi               |    4 +-
 tests/usr.bin/xlint/lint1/Makefile        |    4 +-
 tests/usr.bin/xlint/lint1/msg_259.c       |   18 +++-
 tests/usr.bin/xlint/lint1/msg_259.exp     |   22 +++--
 tests/usr.bin/xlint/lint1/msg_259_c90.c   |  110 ++++++++++++++++++++++++++++++
 tests/usr.bin/xlint/lint1/msg_259_c90.exp |   10 ++
 6 files changed, 150 insertions(+), 18 deletions(-)

diffs (259 lines):

diff -r 9cbbc40c6e44 -r 10ead96c8860 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Tue Aug 31 18:15:56 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Tue Aug 31 18:59:26 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1120 2021/08/30 17:08:13 rillig Exp $
+# $NetBSD: mi,v 1.1121 2021/08/31 18:59:26 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -6844,6 +6844,8 @@
 ./usr/tests/usr.bin/xlint/lint1/msg_258.exp                    tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_259.c                      tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_259.exp                    tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/msg_259_c90.c                  tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/msg_259_c90.exp                        tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_259_ilp32.c                        tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_259_ilp32.exp              tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_260.c                      tests-usr.bin-tests     compattestfile,atf
diff -r 9cbbc40c6e44 -r 10ead96c8860 tests/usr.bin/xlint/lint1/Makefile
--- a/tests/usr.bin/xlint/lint1/Makefile        Tue Aug 31 18:15:56 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/Makefile        Tue Aug 31 18:59:26 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.107 2021/08/25 22:04:52 rillig Exp $
+# $NetBSD: Makefile,v 1.108 2021/08/31 18:59:26 rillig Exp $
 
 NOMAN=         # defined
 MAX_MESSAGE=   346             # see lint1/err.c
@@ -16,6 +16,8 @@
        @echo archsubdir=${ARCHSUBDIR} >${.TARGET}
 
 FILESDIR=      ${TESTSDIR}
+FILES+=                msg_259_c90.c
+FILES+=                msg_259_c90.exp
 FILES+=                c11_generic_expression.c
 FILES+=                c11_generic_expression.exp
 FILES+=                c90.c
diff -r 9cbbc40c6e44 -r 10ead96c8860 tests/usr.bin/xlint/lint1/msg_259.c
--- a/tests/usr.bin/xlint/lint1/msg_259.c       Tue Aug 31 18:15:56 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_259.c       Tue Aug 31 18:59:26 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_259.c,v 1.14 2021/08/31 18:15:56 rillig Exp $      */
+/*     $NetBSD: msg_259.c,v 1.15 2021/08/31 18:59:26 rillig Exp $      */
 # 3 "msg_259.c"
 
 // Test for message: argument #%d is converted from '%s' to '%s' due to prototype [259]
@@ -65,8 +65,8 @@
 
        /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259] */
        unsigned_long(sl);
-
-       unsigned_long(si);
+       /* expect+1: warning: argument #1 is converted from 'long long' to 'unsigned long' due to prototype [259] */
+       unsigned_long(sll);
 
        /*
         * XXX: Why no warning?  Even though 'unsigned long long' is 64 bits
@@ -93,7 +93,8 @@
        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);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'long' due to prototype [259] */
+       signed_long(ull);
        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);
@@ -111,7 +112,8 @@
        signed_int(sll);
        signed_long(si);
        signed_long(sl);
-       signed_long(si);
+       /* expect+1: warning: argument #1 is converted from 'long long' to 'long' due to prototype [259] */
+       signed_long(sll);
        signed_long_long(si);
        /* expect+1: warning: argument #1 is converted from 'long' to 'long long' due to prototype [259] */
        signed_long_long(sl);
@@ -128,7 +130,8 @@
        unsigned_int(ull);
        unsigned_long(ui);
        unsigned_long(ul);
-       unsigned_long(ui);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long long' to 'unsigned long' due to prototype [259] */
+       unsigned_long(ull);
        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);
@@ -141,7 +144,8 @@
        /*
         * XXX: Even though the expression has type size_t, it has a constant
         * value that fits effortless into an 'unsigned int', it's so small
-        * that it would even fit into a 3-bit bit-field.
+        * that it would even fit into a 3-bit bit-field, so lint should not
+        * warn here.
         */
        /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
        unsigned_int(sizeof(int));
diff -r 9cbbc40c6e44 -r 10ead96c8860 tests/usr.bin/xlint/lint1/msg_259.exp
--- a/tests/usr.bin/xlint/lint1/msg_259.exp     Tue Aug 31 18:15:56 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_259.exp     Tue Aug 31 18:59:26 2021 +0000
@@ -3,18 +3,22 @@
 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(69): warning: argument #1 is converted from 'long 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]
+msg_259.c(97): warning: argument #1 is converted from 'unsigned long long' to 'long' due to prototype [259]
+msg_259.c(100): warning: argument #1 is converted from 'unsigned long' to 'long long' due to prototype [259]
+msg_259.c(102): warning: argument #1 is converted from 'unsigned long long' to 'long long' due to prototype [259]
+msg_259.c(110): warning: argument #1 is converted from 'long' to 'int' due to prototype [259]
+msg_259.c(112): warning: argument #1 is converted from 'long long' to 'int' due to prototype [259]
+msg_259.c(116): warning: argument #1 is converted from 'long long' to 'long' due to prototype [259]
+msg_259.c(119): warning: argument #1 is converted from 'long' to 'long long' due to prototype [259]
+msg_259.c(128): warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259]
+msg_259.c(130): 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 long' to 'unsigned long' due to prototype [259]
+msg_259.c(137): warning: argument #1 is converted from 'unsigned long' to 'unsigned long long' due to prototype [259]
+msg_259.c(151): warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259]
diff -r 9cbbc40c6e44 -r 10ead96c8860 tests/usr.bin/xlint/lint1/msg_259_c90.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_259_c90.c   Tue Aug 31 18:59:26 2021 +0000
@@ -0,0 +1,110 @@
+/*     $NetBSD: msg_259_c90.c,v 1.1 2021/08/31 18:59:26 rillig Exp $   */
+# 3 "msg_259_c90.c"
+
+/* Test for message: argument #%d is converted from '%s' to '%s' due to prototype [259] */
+
+/* lint1-only-if: lp64 */
+/* XXX: The flag '-s' suppresses all warnings.  Why? */
+/* lint1-flags: -h -w */
+
+void plain_char(char);
+void signed_int(int);
+void unsigned_int(unsigned int);
+void signed_long(long);
+void unsigned_long(unsigned long);
+/* No 'long long' since it requires C99. */
+
+void
+change_in_type_width(char c, int i, long l)
+{
+       plain_char(c);
+       signed_int(c);
+       /* No warning 259 on LP64, only on ILP32 */
+       signed_long(c);
+
+       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] */
+       signed_int(l);
+       signed_long(l);
+}
+
+/*
+ * Converting a signed integer type to its corresponding unsigned integer
+ * 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
+signed_to_unsigned(int si, long sl)
+{
+       /* expect+1: warning: argument #1 is converted from 'int' to 'unsigned int' due to prototype [259] */
+       unsigned_int(si);
+
+       /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned int' due to prototype [259] */
+       unsigned_int(sl);
+
+       /*
+        * XXX: Why no warning?  Even though 'unsigned long' is 64 bits
+        * wide, it cannot represent negative 32-bit values.
+        */
+       unsigned_long(si);
+
+       /* expect+1: warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259] */
+       unsigned_long(sl);
+}
+
+void
+unsigned_to_signed(unsigned int ui, unsigned long ul)
+{
+       /* 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);
+       signed_long(ui);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'long' due to prototype [259] */
+       signed_long(ul);
+}
+
+void
+signed_to_signed(signed int si, signed long sl)
+{
+       signed_int(si);
+       /* expect+1: warning: argument #1 is converted from 'long' to 'int' due to prototype [259] */
+       signed_int(sl);
+       signed_long(si);
+       signed_long(sl);
+}
+
+void
+unsigned_to_unsigned(unsigned int ui, unsigned long ul)
+{
+       unsigned_int(ui);
+       /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
+       unsigned_int(ul);
+       unsigned_long(ui);
+       unsigned_long(ul);
+}
+
+void
+pass_sizeof_as_smaller_type(void)
+{
+       /*
+        * XXX: Even though the expression has type size_t, it has a constant
+        * value that fits effortless into an 'unsigned int', it's so small
+        * that it would even fit into a 3-bit bit-field, so lint should not
+        * warn here.
+        */
+       /* expect+1: warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
+       unsigned_int(sizeof(int));
+}
diff -r 9cbbc40c6e44 -r 10ead96c8860 tests/usr.bin/xlint/lint1/msg_259_c90.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_259_c90.exp Tue Aug 31 18:59:26 2021 +0000
@@ -0,0 +1,10 @@
+msg_259_c90.c(32): warning: argument #1 is converted from 'long' to 'int' due to prototype [259]
+msg_259_c90.c(52): warning: argument #1 is converted from 'int' to 'unsigned int' due to prototype [259]
+msg_259_c90.c(55): warning: argument #1 is converted from 'long' to 'unsigned int' due to prototype [259]
+msg_259_c90.c(64): warning: argument #1 is converted from 'long' to 'unsigned long' due to prototype [259]
+msg_259_c90.c(71): warning: argument #1 is converted from 'unsigned int' to 'int' due to prototype [259]
+msg_259_c90.c(73): warning: argument #1 is converted from 'unsigned long' to 'int' due to prototype [259]
+msg_259_c90.c(76): warning: argument #1 is converted from 'unsigned long' to 'long' due to prototype [259]
+msg_259_c90.c(84): warning: argument #1 is converted from 'long' to 'int' due to prototype [259]
+msg_259_c90.c(94): warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259]
+msg_259_c90.c(109): warning: argument #1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259]



Home | Main Index | Thread Index | Old Index