Module Name: src
Committed By: rillig
Date: Fri Mar 5 17:10:06 UTC 2021
Modified Files:
src/tests/usr.bin/xlint/lint1: msg_130.c msg_130.exp
src/usr.bin/xlint/lint1: func.c
Log Message:
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.
To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/msg_130.c
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_130.exp
cvs rdiff -u -r1.74 -r1.75 src/usr.bin/xlint/lint1/func.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/tests/usr.bin/xlint/lint1/msg_130.c
diff -u src/tests/usr.bin/xlint/lint1/msg_130.c:1.7 src/tests/usr.bin/xlint/lint1/msg_130.c:1.8
--- src/tests/usr.bin/xlint/lint1/msg_130.c:1.7 Fri Mar 5 16:35:52 2021
+++ src/tests/usr.bin/xlint/lint1/msg_130.c Fri Mar 5 17:10:06 2021
@@ -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 @@ void
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:
Index: src/tests/usr.bin/xlint/lint1/msg_130.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_130.exp:1.5 src/tests/usr.bin/xlint/lint1/msg_130.exp:1.6
--- src/tests/usr.bin/xlint/lint1/msg_130.exp:1.5 Fri Mar 5 16:35:52 2021
+++ src/tests/usr.bin/xlint/lint1/msg_130.exp Fri Mar 5 17:10:06 2021
@@ -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]
Index: src/usr.bin/xlint/lint1/func.c
diff -u src/usr.bin/xlint/lint1/func.c:1.74 src/usr.bin/xlint/lint1/func.c:1.75
--- src/usr.bin/xlint/lint1/func.c:1.74 Sun Feb 28 19:16:05 2021
+++ src/usr.bin/xlint/lint1/func.c Fri Mar 5 17:10:05 2021
@@ -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 @@ named_label(sym_t *sym)
}
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 @@ check_case_label(tnode_t *tn, cstk_t *ci
return;
}
+ check_case_label_enum(tn, ci);
+
lint_assert(ci->c_swtype != NULL);
if (reached && !ftflg) {