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 lint: add test for "suggest cast" ...



details:   https://anonhg.NetBSD.org/src/rev/35c72f32db04
branches:  trunk
changeset: 949226:35c72f32db04
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Jan 05 22:38:51 2021 +0000

description:
lint: add test for "suggest cast" [324]

This warning is the only one that calls print_tnode, which in turn uses
the redundant operator names in str_op_t.

There is another list of operator names in ops.c, but those names
include more clutter, for example "p + p" instead of a simple "+".
Using those operator names would therefore rather be confusing. These
two lists should be merged, to remove unnecessary redundancy.

diffstat:

 tests/usr.bin/xlint/lint1/msg_324.c   |  39 ++++++++++++++++++++++++++++++++--
 tests/usr.bin/xlint/lint1/msg_324.exp |   8 ++++++-
 2 files changed, 43 insertions(+), 4 deletions(-)

diffs (59 lines):

diff -r 176143b27d0a -r 35c72f32db04 tests/usr.bin/xlint/lint1/msg_324.c
--- a/tests/usr.bin/xlint/lint1/msg_324.c       Tue Jan 05 22:17:40 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_324.c       Tue Jan 05 22:38:51 2021 +0000
@@ -1,7 +1,40 @@
-/*     $NetBSD: msg_324.c,v 1.1 2021/01/02 10:22:44 rillig Exp $       */
+/*     $NetBSD: msg_324.c,v 1.2 2021/01/05 22:38:51 rillig Exp $       */
 # 3 "msg_324.c"
 
 // Test for message: suggest cast from '%s' to '%s' on op %s to avoid overflow [324]
 
-TODO: "Add example code that triggers the above message."
-TODO: "Add example code that almost triggers the above message."
+/*
+ * This warning applies to binary operators if the result of the operator
+ * is converted to a type that is bigger than the operands' result type
+ * after the usual arithmetic promotions.
+ *
+ * In such a case, the operator's result would be truncated to the operator's
+ * result type (invoking undefined behavior for signed integers), and that
+ * truncated value would then be converted.  At that point, a few bits may
+ * have been lost.
+ */
+
+/* lint1-flags: -g -S -w -P */
+
+void
+example(char c, int i, unsigned u)
+{
+       long l;
+       unsigned long ul;
+
+       l = c + i;
+       l = i - c;
+       ul = c * u;
+       ul = u + c;
+       ul = i - u;
+       ul = u * i;
+       l = i << c;
+
+       /*
+        * The operators SHR, DIV and MOD cannot produce an overflow,
+        * therefore no warning is necessary for them.
+        */
+       l = i >> c;
+       ul = u / c;
+       ul = u % c;
+}
diff -r 176143b27d0a -r 35c72f32db04 tests/usr.bin/xlint/lint1/msg_324.exp
--- a/tests/usr.bin/xlint/lint1/msg_324.exp     Tue Jan 05 22:17:40 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_324.exp     Tue Jan 05 22:38:51 2021 +0000
@@ -1,1 +1,7 @@
-msg_324.c(6): syntax error ':' [249]
+msg_324.c(25): warning: suggest cast from 'int' to 'long' on op + to avoid overflow [324]
+msg_324.c(26): warning: suggest cast from 'int' to 'long' on op - to avoid overflow [324]
+msg_324.c(27): warning: suggest cast from 'unsigned int' to 'unsigned long' on op * to avoid overflow [324]
+msg_324.c(28): warning: suggest cast from 'unsigned int' to 'unsigned long' on op + to avoid overflow [324]
+msg_324.c(29): warning: suggest cast from 'unsigned int' to 'unsigned long' on op - to avoid overflow [324]
+msg_324.c(30): warning: suggest cast from 'unsigned int' to 'unsigned long' on op * to avoid overflow [324]
+msg_324.c(31): warning: suggest cast from 'int' to 'long' on op << to avoid overflow [324]



Home | Main Index | Thread Index | Old Index