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: refactor strict bool mode and impr...



details:   https://anonhg.NetBSD.org/src/rev/c0a13bdd3b2a
branches:  trunk
changeset: 949778:c0a13bdd3b2a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Jan 16 19:11:36 2021 +0000

description:
lint: refactor strict bool mode and improve comments

The previous comment above typeok_strict_bool_compatible was too hard to
understand.

diffstat:

 usr.bin/xlint/lint1/tree.c |  49 ++++++++++++++++++++++++---------------------
 1 files changed, 26 insertions(+), 23 deletions(-)

diffs (88 lines):

diff -r 4b83a8970275 -r c0a13bdd3b2a usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sat Jan 16 19:03:47 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat Jan 16 19:11:36 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.162 2021/01/16 19:03:47 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.163 2021/01/16 19:11:36 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.162 2021/01/16 19:03:47 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.163 2021/01/16 19:11:36 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -1082,14 +1082,33 @@
 }
 
 /*
- * For assignment operators in strict bool mode, the type check is stricter
- * than for operators that compare to 0.  Code that passes this strict check
- * can be compiled in a pre-C99 environment that doesn't implement the
- * special rule C99 6.3.1.2, without silent change in behavior.
+ * Whether the operator can handle (bool, bool) as well as (scalar, scalar),
+ * but not mixtures between the two type classes.
+ */
+static bool
+needs_compatible_types(op_t op)
+{
+       return op == EQ || op == NE ||
+              op == AND || op == XOR || op == OR ||
+              op == COLON ||
+              op == ASSIGN || op == ANDASS || op == XORASS || op == ORASS ||
+              op == RETURN ||
+              op == FARG;
+}
+
+/*
+ * Some operators require that either both operands are bool or both are
+ * scalar.
+ *
+ * Code that passes this check can be compiled in a pre-C99 environment that
+ * doesn't implement the special rule C99 6.3.1.2, without silent change in
+ * behavior.
  */
 static bool
 typeok_strict_bool_compatible(op_t op, int arg, tspec_t lt, tspec_t rt)
 {
+       if (!needs_compatible_types(op))
+               return true;
        if ((lt == BOOL) == (rt == BOOL))
                return true;
 
@@ -1108,21 +1127,6 @@
 }
 
 /*
- * Whether the operator can handle (bool, bool) as well as (scalar, scalar),
- * but not mixtures between the two type classes.
- */
-static bool
-needs_compatible_types(op_t op)
-{
-       return op == EQ || op == NE ||
-              op == AND || op == XOR || op == OR ||
-              op == COLON ||
-              op == ASSIGN || op == ANDASS || op == XORASS || op == ORASS ||
-              op == RETURN ||
-              op == FARG;
-}
-
-/*
  * In strict bool mode, check whether the types of the operands match the
  * operator.
  */
@@ -1144,8 +1148,7 @@
                rt = NOTSPEC;
        }
 
-       if (needs_compatible_types(op) &&
-           !typeok_strict_bool_compatible(op, arg, lt, rt))
+       if (!typeok_strict_bool_compatible(op, arg, lt, rt))
                return false;
 
        if (mp->m_takes_only_bool || op == QUEST) {



Home | Main Index | Thread Index | Old Index