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: invert condition in control flow a...



details:   https://anonhg.NetBSD.org/src/rev/52db2e7a736f
branches:  trunk
changeset: 380002:52db2e7a736f
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Jun 30 11:29:29 2021 +0000

description:
lint: invert condition in control flow analysis of switch statements

The condition previously contained many negations, which was hard to
understand.

No functional change.

diffstat:

 usr.bin/xlint/lint1/func.c |  38 ++++++++++++++++++++------------------
 1 files changed, 20 insertions(+), 18 deletions(-)

diffs (72 lines):

diff -r e348392cc5c6 -r 52db2e7a736f usr.bin/xlint/lint1/func.c
--- a/usr.bin/xlint/lint1/func.c        Wed Jun 30 11:20:32 2021 +0000
+++ b/usr.bin/xlint/lint1/func.c        Wed Jun 30 11:29:29 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: func.c,v 1.111 2021/06/19 19:59:02 rillig Exp $        */
+/*     $NetBSD: func.c,v 1.112 2021/06/30 11:29:29 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.111 2021/06/19 19:59:02 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.112 2021/06/30 11:29:29 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -747,12 +747,11 @@ switch2(void)
 
        lint_assert(cstmt->c_switch_type != NULL);
 
-       /*
-        * If the switch expression was of type enumeration, count the case
-        * labels and the number of enumerators. If both counts are not
-        * equal print a warning.
-        */
        if (cstmt->c_switch_type->t_is_enum) {
+               /*
+                * Warn if the number of case labels is different from the
+                * number of enumerators.
+                */
                nenum = nclab = 0;
                lint_assert(cstmt->c_switch_type->t_enum != NULL);
                for (esym = cstmt->c_switch_type->t_enum->en_first_enumerator;
@@ -771,22 +770,25 @@ switch2(void)
 
        if (cstmt->c_break) {
                /*
-                * end of switch always reached (c_break is only set if the
-                * break statement can be reached).
+                * The end of the switch statement is always reached since
+                * c_break is only set if a break statement can actually
+                * be reached.
                 */
                set_reached(true);
-       } else if (!cstmt->c_default &&
-                  (!hflag || !cstmt->c_switch_type->t_is_enum ||
-                   nenum != nclab)) {
+       } else if (cstmt->c_default ||
+                  (hflag && cstmt->c_switch_type->t_is_enum &&
+                   nenum == nclab)) {
                /*
-                * there are possible values which are not handled in
-                * switch
+                * The end of the switch statement is reached if the end
+                * of the last statement inside it is reached.
+                */
+       } else {
+               /*
+                * There are possible values that are not handled in the
+                * switch statement.
                 */
                set_reached(true);
-       }       /*
-                * otherwise the end of the switch expression is reached
-                * if the end of the last statement inside it is reached.
-                */
+       }
 
        end_control_statement(CS_SWITCH);
 }



Home | Main Index | Thread Index | Old Index