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 lossy assignmen...



details:   https://anonhg.NetBSD.org/src/rev/453ea357b52a
branches:  trunk
changeset: 1018352:453ea357b52a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jan 31 13:11:08 2021 +0000

description:
lint: add test for lossy assignments to bit-fields (164, 166)

diffstat:

 tests/usr.bin/xlint/lint1/msg_166.c   |  58 +++++++++++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/msg_166.exp |  14 +++++++-
 2 files changed, 68 insertions(+), 4 deletions(-)

diffs (84 lines):

diff -r df06c633afb9 -r 453ea357b52a tests/usr.bin/xlint/lint1/msg_166.c
--- a/tests/usr.bin/xlint/lint1/msg_166.c       Sun Jan 31 12:44:34 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_166.c       Sun Jan 31 13:11:08 2021 +0000
@@ -1,7 +1,59 @@
-/*     $NetBSD: msg_166.c,v 1.1 2021/01/02 10:22:43 rillig Exp $       */
+/*     $NetBSD: msg_166.c,v 1.2 2021/01/31 13:11:08 rillig Exp $       */
 # 3 "msg_166.c"
 
 // Test for message: precision lost in bit-field assignment [166]
 
-TODO: "Add example code that triggers the above message."
-TODO: "Add example code that almost triggers the above message."
+/* lint1-extra-flags: -hp */
+
+struct bit_set {
+
+       /*
+        * C99 6.7.2p5 and 6.7.2.1p9 footnote 104 say that for bit-fields of
+        * underlying type 'int', "it is implementation-defined whether the
+        * specifier 'int' designates the same type as 'signed int' or the
+        * same type as 'unsigned int'".
+        *
+        * https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations
+        * -and-bit-fields-implementation.html says: "By default it is treated
+        * as 'signed int' but this may be changed by the
+        * '-funsigned-bitfields' option".
+        *
+        * Clang doesn't document implementation-defined behavior, see
+        * https://bugs.llvm.org/show_bug.cgi?id=11272.
+        */
+
+       int minus_1_to_0: 1;            /* expect: 34 */
+       int minus_8_to_7: 4;            /* expect: 34 */
+       unsigned zero_to_1: 1;
+       unsigned zero_to_15: 4;
+};
+
+void example(void) {
+       struct bit_set bits;
+
+       /* Clang doesn't warn about the 1. */
+       bits.minus_1_to_0 = -2;         /* expect: 166 */
+       bits.minus_1_to_0 = -1;
+       bits.minus_1_to_0 = 0;
+       bits.minus_1_to_0 = 1;          /* expect: 166 */
+       bits.minus_1_to_0 = 2;          /* expect: 166 */
+
+       bits.minus_8_to_7 = -9;         /* expect: 166 */
+       bits.minus_8_to_7 = -8;
+       bits.minus_8_to_7 = 7;
+       bits.minus_8_to_7 = 8;          /* expect: 166 */
+
+       /* Clang doesn't warn about the -1. */
+       bits.zero_to_1 = -2;            /* expect: 164 */
+       bits.zero_to_1 = -1;            /* expect: 164 */
+       bits.zero_to_1 = 0;
+       bits.zero_to_1 = 1;
+       bits.zero_to_1 = 2;             /* expect: 166 */
+
+       /* Clang doesn't warn about the -8. */
+       bits.zero_to_15 = -9;           /* expect: 164 */
+       bits.zero_to_15 = -8;           /* expect: 164 */
+       bits.zero_to_15 = 0;
+       bits.zero_to_15 = 15;
+       bits.zero_to_15 = 16;           /* expect: 166 */
+}
diff -r df06c633afb9 -r 453ea357b52a tests/usr.bin/xlint/lint1/msg_166.exp
--- a/tests/usr.bin/xlint/lint1/msg_166.exp     Sun Jan 31 12:44:34 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_166.exp     Sun Jan 31 13:11:08 2021 +0000
@@ -1,1 +1,13 @@
-msg_166.c(6): syntax error ':' [249]
+msg_166.c(25): warning: nonportable bit-field type [34]
+msg_166.c(26): warning: nonportable bit-field type [34]
+msg_166.c(35): warning: precision lost in bit-field assignment [166]
+msg_166.c(38): warning: precision lost in bit-field assignment [166]
+msg_166.c(39): warning: precision lost in bit-field assignment [166]
+msg_166.c(41): warning: precision lost in bit-field assignment [166]
+msg_166.c(44): warning: precision lost in bit-field assignment [166]
+msg_166.c(47): warning: assignment of negative constant to unsigned type [164]
+msg_166.c(48): warning: assignment of negative constant to unsigned type [164]
+msg_166.c(51): warning: precision lost in bit-field assignment [166]
+msg_166.c(54): warning: assignment of negative constant to unsigned type [164]
+msg_166.c(55): warning: assignment of negative constant to unsigned type [164]
+msg_166.c(58): warning: precision lost in bit-field assignment [166]



Home | Main Index | Thread Index | Old Index