Tue Mar 9 23:40:43 2021 UTC ()
tests/lint: add example for anonymous enum type in switch expression


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

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

--- src/tests/usr.bin/xlint/lint1/msg_130.c 2021/03/09 23:09:48 1.9
+++ src/tests/usr.bin/xlint/lint1/msg_130.c 2021/03/09 23:40:43 1.10
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: msg_130.c,v 1.9 2021/03/09 23:09:48 rillig Exp $ */ 1/* $NetBSD: msg_130.c,v 1.10 2021/03/09 23:40:43 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 {
@@ -80,13 +80,49 @@ enum_constant_from_unnamed_type(int x) @@ -80,13 +80,49 @@ enum_constant_from_unnamed_type(int x)
80 break; 80 break;
81 } 81 }
82 82
83 if (x == sizeof_int) 83 if (x == sizeof_int)
84 return 4; 84 return 4;
85 if (x > sizeof_int) 85 if (x > sizeof_int)
86 return 5; 86 return 5;
87 87
88 if (sizeof_int == sizeof_uint) /* expect: 130 *//* FIXME */ 88 if (sizeof_int == sizeof_uint) /* expect: 130 *//* FIXME */
89 return 6; 89 return 6;
90 90
91 return 0; 91 return 0;
92} 92}
 93
 94/*
 95 * A typical legitimate use case for an anonymous enum type that should not
 96 * be mixed with other types is a state machine.
 97 *
 98 * This example demonstrates that the type of the 'switch' expression can be
 99 * an anonymous enum.
 100 */
 101void
 102state_machine(const char *str)
 103{
 104 enum {
 105 begin,
 106 seen_letter,
 107 seen_letter_digit,
 108 error
 109 } state = begin;
 110
 111 for (const char *p = str; *p != '\0'; p++) {
 112 switch (state) {
 113 case begin:
 114 state = *p == 'A' ? seen_letter : error;
 115 break;
 116 case seen_letter:
 117 state = *p == '1' ? seen_letter_digit : error;
 118 break;
 119 default:
 120 state = error;
 121 }
 122 }
 123
 124 if (state == 2) /* might be worth a warning */
 125 return;
 126 if (state == sizeof_int) /* expect: 130 */
 127 return;
 128}

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

--- src/tests/usr.bin/xlint/lint1/Attic/msg_130.exp 2021/03/09 23:09:48 1.7
+++ src/tests/usr.bin/xlint/lint1/Attic/msg_130.exp 2021/03/09 23:40:43 1.8
@@ -1,9 +1,10 @@ @@ -1,9 +1,10 @@
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] 7msg_130.c(75): warning: enum type mismatch: 'int' '==' 'enum <unnamed>' [130]
8msg_130.c(77): 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] 9msg_130.c(88): warning: enum type mismatch: 'enum <unnamed>' '==' 'enum <unnamed>' [130]
 10msg_130.c(126): warning: enum type mismatch: 'enum <unnamed>' '==' 'enum <unnamed>' [130]