| @@ -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 | |
8 | enum color { | | 8 | enum 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 | |
14 | enum size { | | 14 | enum size { |
15 | SMALL, | | 15 | SMALL, |
16 | MEDIUM, | | 16 | MEDIUM, |
17 | LARGE | | 17 | LARGE |
18 | }; | | 18 | }; |
19 | | | 19 | |
20 | enum daytime { | | 20 | enum daytime { |
21 | NIGHT, MORNING, NOON, EVENING | | 21 | NIGHT, MORNING, NOON, EVENING |
22 | }; | | 22 | }; |
23 | | | 23 | |
24 | void sink(_Bool); | | 24 | void sink(_Bool); |
25 | | | 25 | |
26 | void | | 26 | void |
27 | example(_Bool cond, enum color c, enum size s) | | 27 | example(_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 | |
43 | void | | 43 | void |
44 | switch_example(enum color c) | | 44 | switch_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 | */ |
| | | 62 | enum { |
| | | 63 | sizeof_int = sizeof(int), |
| | | 64 | sizeof_long = sizeof(long) |
| | | 65 | }; |
| | | 66 | |
| | | 67 | enum { |
| | | 68 | sizeof_uint = sizeof(unsigned int) |
| | | 69 | }; |
| | | 70 | |
| | | 71 | int |
| | | 72 | enum_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 | } |