Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/xlint/lint1 lint: warn about enum/enum or enum/int t...



details:   https://anonhg.NetBSD.org/src/rev/111b43264ec3
branches:  trunk
changeset: 953315:111b43264ec3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Mar 05 17:10:05 2021 +0000

description:
lint: warn about enum/enum or enum/int type mismatch in switch

This is something that neither GCC 10 nor Clang 8 do, even though it
seems useful.  Lint didn't do it up to now, but that was probably an
oversight since it is easy to miss the implicit '==' operator in the
switch statement.

diffstat:

 tests/usr.bin/xlint/lint1/msg_130.c   |   7 ++++---
 tests/usr.bin/xlint/lint1/msg_130.exp |   3 +++
 usr.bin/xlint/lint1/func.c            |  22 ++++++++++++++++++++--
 3 files changed, 27 insertions(+), 5 deletions(-)

diffs (81 lines):

diff -r b6f29673323c -r 111b43264ec3 tests/usr.bin/xlint/lint1/msg_130.c
--- a/tests/usr.bin/xlint/lint1/msg_130.c       Fri Mar 05 16:35:52 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_130.c       Fri Mar 05 17:10:05 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_130.c,v 1.7 2021/03/05 16:35:52 rillig Exp $       */
+/*     $NetBSD: msg_130.c,v 1.8 2021/03/05 17:10:06 rillig Exp $       */
 # 3 "msg_130.c"
 
 // Test for message: enum type mismatch: '%s' '%s' '%s' [130]
@@ -44,8 +44,9 @@
 switch_example(enum color c)
 {
        switch (c) {
-       case EVENING:                   /* TODO: 130 */
-       case LARGE:                     /* TODO: 130 */
+       case EVENING:                   /* expect: 130 */
+       case LARGE:                     /* expect: 130 */
+       case 0:                         /* expect: 130 */
                sink(1 == 1);
                break;
        default:
diff -r b6f29673323c -r 111b43264ec3 tests/usr.bin/xlint/lint1/msg_130.exp
--- a/tests/usr.bin/xlint/lint1/msg_130.exp     Fri Mar 05 16:35:52 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_130.exp     Fri Mar 05 17:10:05 2021 +0000
@@ -1,3 +1,6 @@
 msg_130.c(29): warning: enum type mismatch: 'enum color' ':' 'enum daytime' [130]
 msg_130.c(31): warning: enum type mismatch: 'enum color' '!=' 'enum size' [130]
 msg_130.c(32): warning: enum type mismatch: 'enum color' '==' 'enum size' [130]
+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]
diff -r b6f29673323c -r 111b43264ec3 usr.bin/xlint/lint1/func.c
--- a/usr.bin/xlint/lint1/func.c        Fri Mar 05 16:35:52 2021 +0000
+++ b/usr.bin/xlint/lint1/func.c        Fri Mar 05 17:10:05 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: func.c,v 1.74 2021/02/28 19:16:05 rillig Exp $ */
+/*     $NetBSD: func.c,v 1.75 2021/03/05 17:10:05 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: func.c,v 1.74 2021/02/28 19:16:05 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.75 2021/03/05 17:10:05 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -427,6 +427,22 @@
 }
 
 static void
+check_case_label_enum(const tnode_t *tn, const cstk_t *ci)
+{
+       /* similar to typeok_enum in tree.c */
+
+       if (!(tn->tn_type->t_is_enum || ci->c_swtype->t_is_enum))
+               return;
+       if (tn->tn_type->t_is_enum && ci->c_swtype->t_is_enum &&
+           tn->tn_type->t_enum == ci->c_swtype->t_enum)
+               return;
+
+       /* enum type mismatch: '%s' '%s' '%s' */
+       warning(130, type_name(ci->c_swtype), getopname(EQ),
+           type_name(tn->tn_type));
+}
+
+static void
 check_case_label(tnode_t *tn, cstk_t *ci)
 {
        clst_t  *cl;
@@ -452,6 +468,8 @@
                return;
        }
 
+       check_case_label_enum(tn, ci);
+
        lint_assert(ci->c_swtype != NULL);
 
        if (reached && !ftflg) {



Home | Main Index | Thread Index | Old Index