Module Name: src
Committed By: rillig
Date: Sat Jan 16 19:11:36 UTC 2021
Modified Files:
src/usr.bin/xlint/lint1: tree.c
Log Message:
lint: refactor strict bool mode and improve comments
The previous comment above typeok_strict_bool_compatible was too hard to
understand.
To generate a diff of this commit:
cvs rdiff -u -r1.162 -r1.163 src/usr.bin/xlint/lint1/tree.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.162 src/usr.bin/xlint/lint1/tree.c:1.163
--- src/usr.bin/xlint/lint1/tree.c:1.162 Sat Jan 16 19:03:47 2021
+++ src/usr.bin/xlint/lint1/tree.c Sat Jan 16 19:11:36 2021
@@ -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 @@ typeok_assign(const mod_t *mp, const tno
}
/*
- * 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 @@ typeok_strict_bool_compatible(op_t op, i
}
/*
- * 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 @@ typeok_scalar_strict_bool(op_t op, const
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) {