Module Name: src Committed By: rillig Date: Sat Aug 28 14:45:19 UTC 2021
Modified Files: src/tests/usr.bin/xlint/lint1: msg_162.c msg_162.exp msg_230.c msg_230.exp msg_230_uchar.c Log Message: tests/lint: extend test for nonportable character comparison To generate a diff of this commit: cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/xlint/lint1/msg_162.c \ src/tests/usr.bin/xlint/lint1/msg_162.exp cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/msg_230.c cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_230.exp cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/msg_230_uchar.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/xlint/lint1/msg_162.c diff -u src/tests/usr.bin/xlint/lint1/msg_162.c:1.3 src/tests/usr.bin/xlint/lint1/msg_162.c:1.4 --- src/tests/usr.bin/xlint/lint1/msg_162.c:1.3 Mon Aug 23 17:47:34 2021 +++ src/tests/usr.bin/xlint/lint1/msg_162.c Sat Aug 28 14:45:19 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: msg_162.c,v 1.3 2021/08/23 17:47:34 rillig Exp $ */ +/* $NetBSD: msg_162.c,v 1.4 2021/08/28 14:45:19 rillig Exp $ */ # 3 "msg_162.c" // Test for message: comparison of %s with %s, op %s [162] @@ -51,3 +51,34 @@ right_unsigned(unsigned int ui) if (0 >= ui) { } } + +/* + * Lint does not care about these comparisons, even though they are obviously + * out of range. + */ +void +compare_signed_char(signed char sc) +{ + if (sc == -129) + return; + if (sc == -128) + return; + if (sc == 127) + return; + if (sc == 128) + return; +} + +void +compare_unsigned_char(unsigned char uc) +{ + /* expect+1: warning: comparison of unsigned char with negative constant, op == [162] */ + if (uc == -1) + return; + if (uc == 0) + return; + if (uc == 255) + return; + if (uc == 256) + return; +} Index: src/tests/usr.bin/xlint/lint1/msg_162.exp diff -u src/tests/usr.bin/xlint/lint1/msg_162.exp:1.3 src/tests/usr.bin/xlint/lint1/msg_162.exp:1.4 --- src/tests/usr.bin/xlint/lint1/msg_162.exp:1.3 Mon Aug 23 17:47:34 2021 +++ src/tests/usr.bin/xlint/lint1/msg_162.exp Sat Aug 28 14:45:19 2021 @@ -6,3 +6,4 @@ msg_162.c(39): warning: comparison of ne msg_162.c(43): warning: comparison of 0 with unsigned int, op > [162] msg_162.c(47): warning: comparison of 0 with unsigned int, op <= [162] msg_162.c(51): warning: comparison of 0 with unsigned int, op >= [162] +msg_162.c(76): warning: comparison of unsigned char with negative constant, op == [162] Index: src/tests/usr.bin/xlint/lint1/msg_230.c diff -u src/tests/usr.bin/xlint/lint1/msg_230.c:1.6 src/tests/usr.bin/xlint/lint1/msg_230.c:1.7 --- src/tests/usr.bin/xlint/lint1/msg_230.c:1.6 Mon Aug 23 17:47:34 2021 +++ src/tests/usr.bin/xlint/lint1/msg_230.c Sat Aug 28 14:45:19 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: msg_230.c,v 1.6 2021/08/23 17:47:34 rillig Exp $ */ +/* $NetBSD: msg_230.c,v 1.7 2021/08/28 14:45:19 rillig Exp $ */ # 3 "msg_230.c" // Test for message: nonportable character comparison, op %s [230] @@ -6,36 +6,105 @@ /* lint1-flags: -S -g -p -w */ /* lint1-only-if: schar */ -void example(char c, unsigned char uc, signed char sc) +/* + * C11 6.2.5p15 defines that 'char' has the same range, representation, and + * behavior as either 'signed char' or 'unsigned char'. + * + * The portable range of 'char' is from 0 to 127 since all lint platforms + * define CHAR_SIZE to be 8. + * + * See msg_162.c, which covers 'signed char' and 'unsigned char'. + */ + +void +compare_plain_char(char c) { - if (c < 0) + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (c == -129) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (c == -128) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (c == -1) + return; + if (c == 0) return; - /* expect+1: warning: comparison of unsigned char with 0, op < [162] */ - if (uc < 0) + if (c == 127) return; - if (sc < 0) + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (c == 128) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (c == 255) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (c == 256) + return; +} + +void +compare_plain_char_yoda(char c) +{ + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (-129 == c) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (-128 == c) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (-1 == c) + return; + if (0 == c) + return; + if (127 == c) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (128 == c) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (255 == c) + return; + /* expect+1: warning: nonportable character comparison, op == [230] */ + if (256 == c) + return; +} + +void +compare_lt(char c) +{ + + /* expect+1: warning: nonportable character comparison, op > [230] */ + if (c > -2) + return; + /* expect+1: warning: nonportable character comparison, op >= [230] */ + if (c >= -1) return; /* - * XXX: The comparison "<= -1" looks very similar to "< 0", - * nevertheless "< 0" does not generate a warning. - * - * The comparisons may actually differ subtly because of the usual - * arithmetic promotions. + * XXX: The following two comparisons have the same effect, yet lint + * only warns about one of them. */ - /* expect+1: warning: nonportable character comparison, op <= [230] */ - if (c <= -1) + /* expect+1: warning: nonportable character comparison, op > [230] */ + if (c > -1) + return; + if (c >= 0) return; - /* expect+1: warning: comparison of unsigned char with negative constant, op <= [162] */ - if (uc <= -1) + + /* + * XXX: The following two comparisons have the same effect, yet lint + * only warns about one of them. + */ + if (c > 127) return; - if (sc <= -1) + /* expect+1: warning: nonportable character comparison, op >= [230] */ + if (c >= 128) return; - /* expect+1: warning: nonportable character comparison, op <= [230] */ - if (-1 <= c) + /* expect+1: warning: nonportable character comparison, op > [230] */ + if (c > 128) return; - /* expect+1: warning: nonportable character comparison, op <= [230] */ - if (256 <= c) + /* expect+1: warning: nonportable character comparison, op >= [230] */ + if (c >= 129) return; } Index: src/tests/usr.bin/xlint/lint1/msg_230.exp diff -u src/tests/usr.bin/xlint/lint1/msg_230.exp:1.5 src/tests/usr.bin/xlint/lint1/msg_230.exp:1.6 --- src/tests/usr.bin/xlint/lint1/msg_230.exp:1.5 Mon Aug 23 17:47:34 2021 +++ src/tests/usr.bin/xlint/lint1/msg_230.exp Sat Aug 28 14:45:19 2021 @@ -1,5 +1,18 @@ -msg_230.c(14): warning: comparison of unsigned char with 0, op < [162] -msg_230.c(27): warning: nonportable character comparison, op <= [230] -msg_230.c(30): warning: comparison of unsigned char with negative constant, op <= [162] -msg_230.c(36): warning: nonportable character comparison, op <= [230] -msg_230.c(39): warning: nonportable character comparison, op <= [230] +msg_230.c(23): warning: nonportable character comparison, op == [230] +msg_230.c(26): warning: nonportable character comparison, op == [230] +msg_230.c(29): warning: nonportable character comparison, op == [230] +msg_230.c(36): warning: nonportable character comparison, op == [230] +msg_230.c(39): warning: nonportable character comparison, op == [230] +msg_230.c(42): warning: nonportable character comparison, op == [230] +msg_230.c(50): warning: nonportable character comparison, op == [230] +msg_230.c(53): warning: nonportable character comparison, op == [230] +msg_230.c(56): warning: nonportable character comparison, op == [230] +msg_230.c(63): warning: nonportable character comparison, op == [230] +msg_230.c(66): warning: nonportable character comparison, op == [230] +msg_230.c(69): warning: nonportable character comparison, op == [230] +msg_230.c(78): warning: nonportable character comparison, op > [230] +msg_230.c(81): warning: nonportable character comparison, op >= [230] +msg_230.c(89): warning: nonportable character comparison, op > [230] +msg_230.c(101): warning: nonportable character comparison, op >= [230] +msg_230.c(105): warning: nonportable character comparison, op > [230] +msg_230.c(108): warning: nonportable character comparison, op >= [230] Index: src/tests/usr.bin/xlint/lint1/msg_230_uchar.c diff -u src/tests/usr.bin/xlint/lint1/msg_230_uchar.c:1.2 src/tests/usr.bin/xlint/lint1/msg_230_uchar.c:1.3 --- src/tests/usr.bin/xlint/lint1/msg_230_uchar.c:1.2 Sat Aug 21 11:50:57 2021 +++ src/tests/usr.bin/xlint/lint1/msg_230_uchar.c Sat Aug 28 14:45:19 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: msg_230_uchar.c,v 1.2 2021/08/21 11:50:57 rillig Exp $ */ +/* $NetBSD: msg_230_uchar.c,v 1.3 2021/08/28 14:45:19 rillig Exp $ */ # 3 "msg_230_uchar.c" // Test for message: nonportable character comparison, op %s [230] @@ -23,7 +23,7 @@ void example(char c, unsigned char uc, s * * The comparisons may actually differ subtly because of the usual * arithmetic promotions. - * */ + */ /* expect+1: warning: nonportable character comparison, op <= [230] */ if (c <= -1) return;