Source-Changes-HG archive

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

[src/trunk]: src/tests/usr.bin/xlint/lint1 lint: add enum flags to the test a...



details:   https://anonhg.NetBSD.org/src/rev/b9ccf27ed383
branches:  trunk
changeset: 949367:b9ccf27ed383
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jan 10 21:45:50 2021 +0000

description:
lint: add enum flags to the test about strict boolean mode

diffstat:

 tests/usr.bin/xlint/lint1/d_c99_bool_strict.c |  35 +++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 2 deletions(-)

diffs (53 lines):

diff -r ddb11df20734 -r b9ccf27ed383 tests/usr.bin/xlint/lint1/d_c99_bool_strict.c
--- a/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c     Sun Jan 10 21:26:12 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c     Sun Jan 10 21:45:50 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: d_c99_bool_strict.c,v 1.1 2021/01/10 17:43:46 rillig Exp $     */
+/*     $NetBSD: d_c99_bool_strict.c,v 1.2 2021/01/10 21:45:50 rillig Exp $     */
 # 3 "d_c99_bool_strict.c"
 
 /*
@@ -23,7 +23,7 @@
  * its main operator is _Bool.
  */
 
-// Not yet implemented: /* lint1-extra-flags: -B */
+// Not yet implemented: /* lint1-extra-flags: -T */
 
 /*
  * The header <stdbool.h> defines the macros bool = _Bool, false = 0 and
@@ -250,3 +250,34 @@
 
        LOGAND = 0 && 1,        /* ok */
 };
+
+enum BitSet {
+       ONE = 1 << 0,
+       TWO = 1 << 1,
+       FOUR = 1 << 2
+};
+
+/*
+ * It is debatable whether it is a good idea to allow expressions like these
+ * for _Bool.  The strict rules above ensure that the code works in the same
+ * way whether or not the special rule C99 6.3.1.2 is active or not.
+ *
+ * If the code were to switch away from the C99 bool type to an ordinary
+ * unsigned integer type, the behavior might silently change.  Because the
+ * rule C99 6.3.1.2 is no longer active in that case, high bits of the enum
+ * constant may get lost, thus evaluating to false even though a bit is set.
+ *
+ * It's probably better to not allow this kind of expressions, even though
+ * it may be popular, especially in usr.bin/make.
+ */
+int
+S007_allow_flag_test_on_bit_set_enums(enum BitSet bs)
+{
+       if (bs & ONE)
+               return 1;
+       if (!(bs & TWO))
+               return 2;
+       if (bs & FOUR)
+               return 2;
+       return 4;
+}



Home | Main Index | Thread Index | Old Index