Source-Changes-HG archive

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

[src/trunk]: src lint: add more tests for system headers in strict bool mode



details:   https://anonhg.NetBSD.org/src/rev/c26ab0ecf402
branches:  trunk
changeset: 958735:c26ab0ecf402
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jan 17 23:00:41 2021 +0000

description:
lint: add more tests for system headers in strict bool mode

diffstat:

 distrib/sets/lists/tests/mi                            |   4 +-
 tests/usr.bin/xlint/lint1/Makefile                     |   4 +-
 tests/usr.bin/xlint/lint1/d_c99_bool_strict.c          |  25 +----
 tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp        |   3 -
 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c   |  92 ++++++++++++++++++
 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp |   6 +
 tests/usr.bin/xlint/lint1/t_integration.sh             |   3 +-
 7 files changed, 107 insertions(+), 30 deletions(-)

diffs (205 lines):

diff -r 7983667bd597 -r c26ab0ecf402 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sun Jan 17 22:32:25 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Sun Jan 17 23:00:41 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1011 2021/01/14 22:18:13 rillig Exp $
+# $NetBSD: mi,v 1.1012 2021/01/17 23:00:41 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -5749,6 +5749,8 @@
 ./usr/tests/usr.bin/xlint/lint1/d_c99_bool.exp                 tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c            tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp          tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c     tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp   tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_complex_num.c            tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_complex_split.c          tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_compound_literal_comma.c tests-usr.bin-tests     compattestfile,atf
diff -r 7983667bd597 -r c26ab0ecf402 tests/usr.bin/xlint/lint1/Makefile
--- a/tests/usr.bin/xlint/lint1/Makefile        Sun Jan 17 22:32:25 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/Makefile        Sun Jan 17 23:00:41 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.29 2021/01/14 22:18:14 rillig Exp $
+# $NetBSD: Makefile,v 1.30 2021/01/17 23:00:41 rillig Exp $
 
 NOMAN=         # defined
 
@@ -15,6 +15,8 @@
 FILES+=                d_c99_bool.exp
 FILES+=                d_c99_bool_strict.c
 FILES+=                d_c99_bool_strict.exp
+FILES+=                d_c99_bool_strict_syshdr.c
+FILES+=                d_c99_bool_strict_syshdr.exp
 FILES+=                d_c99_anon_struct.c
 FILES+=                d_c99_anon_union.c
 FILES+=                d_c99_complex_num.c
diff -r 7983667bd597 -r c26ab0ecf402 tests/usr.bin/xlint/lint1/d_c99_bool_strict.c
--- a/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c     Sun Jan 17 22:32:25 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c     Sun Jan 17 23:00:41 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: d_c99_bool_strict.c,v 1.16 2021/01/17 21:19:06 rillig Exp $    */
+/*     $NetBSD: d_c99_bool_strict.c,v 1.17 2021/01/17 23:00:41 rillig Exp $    */
 # 3 "d_c99_bool_strict.c"
 
 /*
@@ -727,26 +727,3 @@
 
        (void)((s.flag = s.flag) != __lint_false);
 }
-
-/*
- * Macros from system headers that have return type bool may or may not
- * include the '!= 0' test, that is, their result can have any scalar type.
- */
-void
-strict_bool_system_header(void)
-{
-
-       do {
-               println("nothing");
-       } while (/*CONSTCOND*/0);       /* expect: 333 */
-
-# 744 "d_c99_bool_strict.c" 3 4
-       do {
-               println("nothing");
-       } while (/*CONSTCOND*/0);       /*TODO*//* expect: 333 */
-
-# 749 "d_c99_bool_strict.c"
-       do {
-               println("nothing");
-       } while (/*CONSTCOND*/0);       /* expect: 333 */
-}
diff -r 7983667bd597 -r c26ab0ecf402 tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp
--- a/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp   Sun Jan 17 22:32:25 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp   Sun Jan 17 23:00:41 2021 +0000
@@ -146,6 +146,3 @@
 d_c99_bool_strict.c(677): operands of '=' have incompatible types (_Bool != int) [107]
 d_c99_bool_strict.c(652): warning: argument flags unused in function strict_bool_bitwise_and_enum [231]
 d_c99_bool_strict.c(716): operands of '==' have incompatible types (_Bool != int) [107]
-d_c99_bool_strict.c(741): controlling expression must be bool, not 'int' [333]
-d_c99_bool_strict.c(746): controlling expression must be bool, not 'int' [333]
-d_c99_bool_strict.c(751): controlling expression must be bool, not 'int' [333]
diff -r 7983667bd597 -r c26ab0ecf402 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c      Sun Jan 17 23:00:41 2021 +0000
@@ -0,0 +1,92 @@
+/*     $NetBSD: d_c99_bool_strict_syshdr.c,v 1.1 2021/01/17 23:00:41 rillig Exp $      */
+# 3 "d_c99_bool_strict_syshdr.c"
+
+/*
+ * Macros from system headers may use int expressions where bool expressions
+ * are expected.  These headers are not allowed to include <stdbool.h>
+ * themselves, and even if they could, lint must accept other scalar types
+ * as well, since system headers are not changed lightheartedly.
+ */
+
+/* lint1-extra-flags: -T */
+
+/*
+ * On NetBSD 8, <sys/select.h> defines FD_ISSET by enclosing the statements
+ * in the well-known 'do { ... } while (constcond 0)' loop.  The 0 in the
+ * controlling expression has type INT but should be allowed nevertheless.
+ */
+void
+strict_bool_system_header_statement_macro(void)
+{
+
+       do {
+               println("nothing");
+       } while (/*CONSTCOND*/0);       /* expect: 333 */
+
+# 27 "d_c99_bool_strict_syshdr.c" 3 4
+       do {
+               println("nothing");
+       } while (/*CONSTCOND*/0);       /*FIXME*//* expect: 333 */
+
+# 32 "d_c99_bool_strict_syshdr.c"
+       do {
+               println("nothing");
+       } while (/*CONSTCOND*/0);       /* expect: 333 */
+}
+
+
+/*
+ * The macros from <ctype.h> can be implemented in different ways.  The C
+ * standard defines them as returning 'int'.  In strict bool mode, the actual
+ * return type can be INT or BOOL, depending on whether the macros do the
+ * comparison against 0 themselves.
+ *
+ * Since that is more code to write and in exceptional situations more code
+ * to execute, they will probably leave out the extra comparison, but both
+ * ways are possible.
+ *
+ * In strict mode, there must be a way to call these function-like macros
+ * portably, without triggering type errors, no matter whether they return
+ * BOOL or INT.
+ *
+ * The expressions from this example cross the boundary between system header
+ * and application code.  They need to carry the information that they are
+ * half-BOOL, half-INT across the enclosing expressions.
+ */
+void
+strict_bool_system_header_ctype(int c)
+{
+       static const unsigned short *ctype_table;
+
+
+       /*
+        * The macro returns INT, which may be outside the range of a
+        * uint8_t variable, therefore it must not be assigned directly.
+        * All other combinations of type are safe from truncation.
+        */
+       _Bool system_int_assigned_to_bool =
+# 69 "d_c99_bool_strict_syshdr.c" 3 4
+           (int)((ctype_table + 1)[c] & 0x0040)        /* INT */
+# 71 "d_c99_bool_strict_syshdr.c"
+           ;                   /* expect: 107 */
+
+       int system_bool_assigned_to_int =
+# 75 "d_c99_bool_strict_syshdr.c" 3 4
+           (int)((ctype_table + 1)[c] & 0x0040) != 0   /* BOOL */
+# 77 "d_c99_bool_strict_syshdr.c"
+           ;                   /* expect: 107 */
+
+       if (
+# 81 "d_c99_bool_strict_syshdr.c" 3 4
+           (int)((ctype_table + 1)[c] & 0x0040)        /* INT */
+# 83 "d_c99_bool_strict_syshdr.c"
+       )                       /*FIXME*//* expect: 333 */
+       println("system macro returning INT");
+
+       if (
+# 88 "d_c99_bool_strict_syshdr.c" 3 4
+           ((ctype_table + 1)[c] & 0x0040) != 0        /* BOOL */
+# 90 "d_c99_bool_strict_syshdr.c"
+       )
+       println("system macro returning BOOL");
+}
diff -r 7983667bd597 -r c26ab0ecf402 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp    Sun Jan 17 23:00:41 2021 +0000
@@ -0,0 +1,6 @@
+d_c99_bool_strict_syshdr.c(24): controlling expression must be bool, not 'int' [333]
+d_c99_bool_strict_syshdr.c(29): controlling expression must be bool, not 'int' [333]
+d_c99_bool_strict_syshdr.c(34): controlling expression must be bool, not 'int' [333]
+d_c99_bool_strict_syshdr.c(71): operands of '=' have incompatible types (_Bool != int) [107]
+d_c99_bool_strict_syshdr.c(77): operands of '=' have incompatible types (int != _Bool) [107]
+d_c99_bool_strict_syshdr.c(83): controlling expression must be bool, not 'int' [333]
diff -r 7983667bd597 -r c26ab0ecf402 tests/usr.bin/xlint/lint1/t_integration.sh
--- a/tests/usr.bin/xlint/lint1/t_integration.sh        Sun Jan 17 22:32:25 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/t_integration.sh        Sun Jan 17 23:00:41 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_integration.sh,v 1.26 2021/01/14 22:18:14 rillig Exp $
+# $NetBSD: t_integration.sh,v 1.27 2021/01/17 23:00:41 rillig Exp $
 #
 # Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -94,6 +94,7 @@
 test_case c99_anon_union
 test_case c99_bool
 test_case c99_bool_strict
+test_case c99_bool_strict_syshdr
 test_case c99_compound_literal_comma
 test_case c99_decls_after_stmt2
 test_case c99_flex_array_packed



Home | Main Index | Thread Index | Old Index