xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_230.c (revision e6298b924c5ba98f3a22919b56dab04a87cdbb1c)
1 /*	$NetBSD: msg_230.c,v 1.14 2023/07/07 19:45:22 rillig Exp $	*/
2 # 3 "msg_230.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: schar */
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 	 * On platforms where char is unsigned, lint warns that the
93 	 * comparison always evaluates to true; see msg_230_uchar.c.
94 	 */
95 	if (c >= 0)
96 		return;
97 
98 	/*
99 	 * XXX: The following two comparisons have the same effect, yet lint
100 	 * only warns about one of them.
101 	 */
102 	if (c > 127)
103 		return;
104 	/* expect+1: warning: nonportable character comparison '>= 128' [230] */
105 	if (c >= 128)
106 		return;
107 
108 	/* expect+1: warning: nonportable character comparison '> 128' [230] */
109 	if (c > 128)
110 		return;
111 	/* expect+1: warning: nonportable character comparison '>= 129' [230] */
112 	if (c >= 129)
113 		return;
114 }
115 
116 /* Comparing a char expression with a character constant is always valid. */
117 void
compare_with_character_literal(char ch)118 compare_with_character_literal(char ch)
119 {
120 	if (ch == '\200')
121 		return;
122 	if (ch == '\377')
123 		return;
124 	if (ch == '\000')
125 		return;
126 }
127