xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_230_uchar.c (revision 1d1794a80f5e3b9141458440f4c1a3176d8385e7)
1 /*	$NetBSD: msg_230_uchar.c,v 1.13 2023/07/08 11:03:00 rillig Exp $	*/
2 # 3 "msg_230_uchar.c"
3 
4 // Test for message: nonportable character comparison '%s' [230]
5 
6 /* lint1-flags: -S -g -p -w -X 351 */
7 /* lint1-only-if: uchar */
8 
9 /*
10  * C11 6.2.5p15 defines that 'char' has the same range, representation, and
11  * behavior as either 'signed char' or 'unsigned char'.
12  *
13  * The portable range of 'char' is from 0 to 127 since all lint platforms
14  * define CHAR_SIZE to be 8.
15  *
16  * See msg_162.c, which covers 'signed char' and 'unsigned char'.
17  */
18 
19 void
compare_plain_char(char c)20 compare_plain_char(char c)
21 {
22 	/* expect+1: warning: nonportable character comparison '== -129' [230] */
23 	if (c == -129)
24 		return;
25 	/* expect+1: warning: nonportable character comparison '== -128' [230] */
26 	if (c == -128)
27 		return;
28 	/* expect+1: warning: nonportable character comparison '== -1' [230] */
29 	if (c == -1)
30 		return;
31 	if (c == 0)
32 		return;
33 	if (c == 127)
34 		return;
35 	/* expect+1: warning: nonportable character comparison '== 128' [230] */
36 	if (c == 128)
37 		return;
38 	/* expect+1: warning: nonportable character comparison '== 255' [230] */
39 	if (c == 255)
40 		return;
41 	/* expect+1: warning: nonportable character comparison '== 256' [230] */
42 	if (c == 256)
43 		return;
44 }
45 
46 void
compare_plain_char_yoda(char c)47 compare_plain_char_yoda(char c)
48 {
49 	/* expect+1: warning: nonportable character comparison '-129 == ?' [230] */
50 	if (-129 == c)
51 		return;
52 	/* expect+1: warning: nonportable character comparison '-128 == ?' [230] */
53 	if (-128 == c)
54 		return;
55 	/* expect+1: warning: nonportable character comparison '-1 == ?' [230] */
56 	if (-1 == c)
57 		return;
58 	if (0 == c)
59 		return;
60 	if (127 == c)
61 		return;
62 	/* expect+1: warning: nonportable character comparison '128 == ?' [230] */
63 	if (128 == c)
64 		return;
65 	/* expect+1: warning: nonportable character comparison '255 == ?' [230] */
66 	if (255 == c)
67 		return;
68 	/* expect+1: warning: nonportable character comparison '256 == ?' [230] */
69 	if (256 == c)
70 		return;
71 }
72 
73 void
compare_greater(char c)74 compare_greater(char c)
75 {
76 
77 	/* expect+1: warning: nonportable character comparison '> -2' [230] */
78 	if (c > -2)
79 		return;
80 	/* expect+1: warning: nonportable character comparison '>= -1' [230] */
81 	if (c >= -1)
82 		return;
83 
84 	/*
85 	 * XXX: The following two comparisons have the same effect, yet lint
86 	 * only warns about one of them.
87 	 */
88 	/* expect+1: warning: nonportable character comparison '> -1' [230] */
89 	if (c > -1)
90 		return;
91 	/*
92 	 * This warning only occurs on uchar platforms since on these
93 	 * platforms it is always true.  Code that needs this ordered
94 	 * comparison on values of type plain char is questionable since it
95 	 * behaves differently depending on the platform.  Such a comparison
96 	 * should never be needed.
97 	 */
98 	/* expect+1: warning: operator '>=' compares 'char' with '0' [162] */
99 	if (c >= 0)
100 		return;
101 
102 	/*
103 	 * XXX: The following two comparisons have the same effect, yet lint
104 	 * only warns about one of them.
105 	 */
106 	if (c > 127)
107 		return;
108 	/* expect+1: warning: nonportable character comparison '>= 128' [230] */
109 	if (c >= 128)
110 		return;
111 
112 	/* expect+1: warning: nonportable character comparison '> 128' [230] */
113 	if (c > 128)
114 		return;
115 	/* expect+1: warning: nonportable character comparison '>= 129' [230] */
116 	if (c >= 129)
117 		return;
118 }
119 
120 /* Comparing a char expression with a character constant is always valid. */
121 void
compare_with_character_literal(char ch)122 compare_with_character_literal(char ch)
123 {
124 	if (ch == '\200')
125 		return;
126 	if (ch == '\377')
127 		return;
128 	if (ch == '\000')
129 		return;
130 }
131