Tue Mar 9 23:09:48 2021 UTC ()
tests/lint: add tests for comparison between unnamed enums

Since unnamed enum types cannot be used in type casts, there is no
sensible way that this type mismatch could be resolved, without changing
the definition of the enum type itself, but that may be in a
non-modifiable header.

Therefore, comparisons with enum constants of unnamed types cannot be
sensibly warned about.


(rillig)
diff -r1.8 -r1.9 src/tests/usr.bin/xlint/lint1/msg_130.c
diff -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/msg_130.exp

cvs diff -r1.8 -r1.9 src/tests/usr.bin/xlint/lint1/msg_130.c (switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/msg_130.c 2021/03/05 17:10:06 1.8
+++ src/tests/usr.bin/xlint/lint1/msg_130.c 2021/03/09 23:09:48 1.9
@@ -1,55 +1,92 @@ @@ -1,55 +1,92 @@
1/* $NetBSD: msg_130.c,v 1.8 2021/03/05 17:10:06 rillig Exp $ */ 1/* $NetBSD: msg_130.c,v 1.9 2021/03/09 23:09:48 rillig Exp $ */
2# 3 "msg_130.c" 2# 3 "msg_130.c"
3 3
4// Test for message: enum type mismatch: '%s' '%s' '%s' [130] 4// Test for message: enum type mismatch: '%s' '%s' '%s' [130]
5 5
6/* See also msg_241.c, which covers unusual operators on enums. */ 6/* See also msg_241.c, which covers unusual operators on enums. */
7 7
8enum color { 8enum color {
9 RED = 1 << 0, 9 RED = 1 << 0,
10 GREEN = 1 << 1, 10 GREEN = 1 << 1,
11 BLUE = 1 << 2 11 BLUE = 1 << 2
12}; 12};
13 13
14enum size { 14enum size {
15 SMALL, 15 SMALL,
16 MEDIUM, 16 MEDIUM,
17 LARGE 17 LARGE
18}; 18};
19 19
20enum daytime { 20enum daytime {
21 NIGHT, MORNING, NOON, EVENING 21 NIGHT, MORNING, NOON, EVENING
22}; 22};
23 23
24void sink(_Bool); 24void sink(_Bool);
25 25
26void 26void
27example(_Bool cond, enum color c, enum size s) 27example(_Bool cond, enum color c, enum size s)
28{ 28{
29 sink(cond ? GREEN : MORNING); /* expect: 130 */ 29 sink(cond ? GREEN : MORNING); /* expect: 130 */
30 30
31 sink(c != s); /* expect: 130 */ 31 sink(c != s); /* expect: 130 */
32 sink(c == s); /* expect: 130 */ 32 sink(c == s); /* expect: 130 */
33 sink((c & MEDIUM) != 0); /* might be useful to warn about */ 33 sink((c & MEDIUM) != 0); /* might be useful to warn about */
34 sink((c | MEDIUM) != 0); /* might be useful to warn about */ 34 sink((c | MEDIUM) != 0); /* might be useful to warn about */
35 35
36 c |= MEDIUM; /* might be useful to warn about */ 36 c |= MEDIUM; /* might be useful to warn about */
37 c &= MEDIUM; /* might be useful to warn about */ 37 c &= MEDIUM; /* might be useful to warn about */
38 38
39 /* The cast to unsigned is required by GCC at WARNS=6. */ 39 /* The cast to unsigned is required by GCC at WARNS=6. */
40 c &= ~(unsigned)MEDIUM; /* might be useful to warn about */ 40 c &= ~(unsigned)MEDIUM; /* might be useful to warn about */
41} 41}
42 42
43void 43void
44switch_example(enum color c) 44switch_example(enum color c)
45{ 45{
46 switch (c) { 46 switch (c) {
47 case EVENING: /* expect: 130 */ 47 case EVENING: /* expect: 130 */
48 case LARGE: /* expect: 130 */ 48 case LARGE: /* expect: 130 */
49 case 0: /* expect: 130 */ 49 case 0: /* expect: 130 */
50 sink(1 == 1); 50 sink(1 == 1);
51 break; 51 break;
52 default: 52 default:
53 break; 53 break;
54 } 54 }
55} 55}
 56
 57/*
 58 * Unnamed enum types can be used as a container for constants, especially
 59 * since in C90 and C99, even after the declaration 'static const int x = 3',
 60 * 'x' is not a constant expression.
 61 */
 62enum {
 63 sizeof_int = sizeof(int),
 64 sizeof_long = sizeof(long)
 65};
 66
 67enum {
 68 sizeof_uint = sizeof(unsigned int)
 69};
 70
 71int
 72enum_constant_from_unnamed_type(int x)
 73{
 74 switch (x) {
 75 case sizeof_int: /* expect: 130 *//* FIXME */
 76 return 1;
 77 case sizeof_long: /* expect: 130 *//* FIXME */
 78 return 2;
 79 default:
 80 break;
 81 }
 82
 83 if (x == sizeof_int)
 84 return 4;
 85 if (x > sizeof_int)
 86 return 5;
 87
 88 if (sizeof_int == sizeof_uint) /* expect: 130 *//* FIXME */
 89 return 6;
 90
 91 return 0;
 92}

cvs diff -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/Attic/msg_130.exp (switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/Attic/msg_130.exp 2021/03/05 17:10:06 1.6
+++ src/tests/usr.bin/xlint/lint1/Attic/msg_130.exp 2021/03/09 23:09:48 1.7
@@ -1,6 +1,9 @@ @@ -1,6 +1,9 @@
1msg_130.c(29): warning: enum type mismatch: 'enum color' ':' 'enum daytime' [130] 1msg_130.c(29): warning: enum type mismatch: 'enum color' ':' 'enum daytime' [130]
2msg_130.c(31): warning: enum type mismatch: 'enum color' '!=' 'enum size' [130] 2msg_130.c(31): warning: enum type mismatch: 'enum color' '!=' 'enum size' [130]
3msg_130.c(32): warning: enum type mismatch: 'enum color' '==' 'enum size' [130] 3msg_130.c(32): warning: enum type mismatch: 'enum color' '==' 'enum size' [130]
4msg_130.c(47): warning: enum type mismatch: 'enum color' '==' 'enum daytime' [130] 4msg_130.c(47): warning: enum type mismatch: 'enum color' '==' 'enum daytime' [130]
5msg_130.c(48): warning: enum type mismatch: 'enum color' '==' 'enum size' [130] 5msg_130.c(48): warning: enum type mismatch: 'enum color' '==' 'enum size' [130]
6msg_130.c(49): warning: enum type mismatch: 'enum color' '==' 'int' [130] 6msg_130.c(49): warning: enum type mismatch: 'enum color' '==' 'int' [130]
 7msg_130.c(75): warning: enum type mismatch: 'int' '==' 'enum <unnamed>' [130]
 8msg_130.c(77): warning: enum type mismatch: 'int' '==' 'enum <unnamed>' [130]
 9msg_130.c(88): warning: enum type mismatch: 'enum <unnamed>' '==' 'enum <unnamed>' [130]