Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/usr.bin/xlint/lint1 tests/lint: add example for anonym...



details:   https://anonhg.NetBSD.org/src/rev/bccee610a6a8
branches:  trunk
changeset: 953473:bccee610a6a8
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Mar 09 23:40:43 2021 +0000

description:
tests/lint: add example for anonymous enum type in switch expression

diffstat:

 tests/usr.bin/xlint/lint1/msg_130.c   |  38 ++++++++++++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/msg_130.exp |   1 +
 2 files changed, 38 insertions(+), 1 deletions(-)

diffs (57 lines):

diff -r 0d2e7df301b2 -r bccee610a6a8 tests/usr.bin/xlint/lint1/msg_130.c
--- a/tests/usr.bin/xlint/lint1/msg_130.c       Tue Mar 09 23:09:48 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_130.c       Tue Mar 09 23:40:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_130.c,v 1.9 2021/03/09 23:09:48 rillig Exp $       */
+/*     $NetBSD: msg_130.c,v 1.10 2021/03/09 23:40:43 rillig Exp $      */
 # 3 "msg_130.c"
 
 // Test for message: enum type mismatch: '%s' '%s' '%s' [130]
@@ -90,3 +90,39 @@
 
        return 0;
 }
+
+/*
+ * A typical legitimate use case for an anonymous enum type that should not
+ * be mixed with other types is a state machine.
+ *
+ * This example demonstrates that the type of the 'switch' expression can be
+ * an anonymous enum.
+ */
+void
+state_machine(const char *str)
+{
+       enum {
+               begin,
+               seen_letter,
+               seen_letter_digit,
+               error
+       } state = begin;
+
+       for (const char *p = str; *p != '\0'; p++) {
+               switch (state) {
+               case begin:
+                       state = *p == 'A' ? seen_letter : error;
+                       break;
+               case seen_letter:
+                       state = *p == '1' ? seen_letter_digit : error;
+                       break;
+               default:
+                       state = error;
+               }
+       }
+
+       if (state == 2)                 /* might be worth a warning */
+               return;
+       if (state == sizeof_int)        /* expect: 130 */
+               return;
+}
diff -r 0d2e7df301b2 -r bccee610a6a8 tests/usr.bin/xlint/lint1/msg_130.exp
--- a/tests/usr.bin/xlint/lint1/msg_130.exp     Tue Mar 09 23:09:48 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_130.exp     Tue Mar 09 23:40:43 2021 +0000
@@ -7,3 +7,4 @@
 msg_130.c(75): warning: enum type mismatch: 'int' '==' 'enum <unnamed>' [130]
 msg_130.c(77): warning: enum type mismatch: 'int' '==' 'enum <unnamed>' [130]
 msg_130.c(88): warning: enum type mismatch: 'enum <unnamed>' '==' 'enum <unnamed>' [130]
+msg_130.c(126): warning: enum type mismatch: 'enum <unnamed>' '==' 'enum <unnamed>' [130]



Home | Main Index | Thread Index | Old Index