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 tests for comparis...



details:   https://anonhg.NetBSD.org/src/rev/9da66976a12d
branches:  trunk
changeset: 960165:9da66976a12d
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Mar 09 23:09:48 2021 +0000

description:
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.

diffstat:

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

diffs (60 lines):

diff -r f4b63ef7bc06 -r 9da66976a12d tests/usr.bin/xlint/lint1/msg_130.c
--- a/tests/usr.bin/xlint/lint1/msg_130.c       Tue Mar 09 20:43:19 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_130.c       Tue Mar 09 23:09:48 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_130.c,v 1.8 2021/03/05 17:10:06 rillig Exp $       */
+/*     $NetBSD: msg_130.c,v 1.9 2021/03/09 23:09:48 rillig Exp $       */
 # 3 "msg_130.c"
 
 // Test for message: enum type mismatch: '%s' '%s' '%s' [130]
@@ -53,3 +53,40 @@
                break;
        }
 }
+
+/*
+ * Unnamed enum types can be used as a container for constants, especially
+ * since in C90 and C99, even after the declaration 'static const int x = 3',
+ * 'x' is not a constant expression.
+ */
+enum {
+       sizeof_int = sizeof(int),
+       sizeof_long = sizeof(long)
+};
+
+enum {
+       sizeof_uint = sizeof(unsigned int)
+};
+
+int
+enum_constant_from_unnamed_type(int x)
+{
+       switch (x) {
+       case sizeof_int:                /* expect: 130 *//* FIXME */
+               return 1;
+       case sizeof_long:               /* expect: 130 *//* FIXME */
+               return 2;
+       default:
+               break;
+       }
+
+       if (x == sizeof_int)
+               return 4;
+       if (x > sizeof_int)
+               return 5;
+
+       if (sizeof_int == sizeof_uint)  /* expect: 130 *//* FIXME */
+               return 6;
+
+       return 0;
+}
diff -r f4b63ef7bc06 -r 9da66976a12d tests/usr.bin/xlint/lint1/msg_130.exp
--- a/tests/usr.bin/xlint/lint1/msg_130.exp     Tue Mar 09 20:43:19 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_130.exp     Tue Mar 09 23:09:48 2021 +0000
@@ -4,3 +4,6 @@
 msg_130.c(47): warning: enum type mismatch: 'enum color' '==' 'enum daytime' [130]
 msg_130.c(48): warning: enum type mismatch: 'enum color' '==' 'enum size' [130]
 msg_130.c(49): warning: enum type mismatch: 'enum color' '==' 'int' [130]
+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]



Home | Main Index | Thread Index | Old Index