Module Name:    src
Committed By:   rillig
Date:           Sun Jan 17 23:00:41 UTC 2021

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/usr.bin/xlint/lint1: Makefile d_c99_bool_strict.c
            d_c99_bool_strict.exp t_integration.sh
Added Files:
        src/tests/usr.bin/xlint/lint1: d_c99_bool_strict_syshdr.c
            d_c99_bool_strict_syshdr.exp

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


To generate a diff of this commit:
cvs rdiff -u -r1.1011 -r1.1012 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.29 -r1.30 src/tests/usr.bin/xlint/lint1/Makefile
cvs rdiff -u -r1.16 -r1.17 src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c
cvs rdiff -u -r1.14 -r1.15 \
    src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp
cvs rdiff -u -r0 -r1.1 \
    src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c \
    src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp
cvs rdiff -u -r1.26 -r1.27 src/tests/usr.bin/xlint/lint1/t_integration.sh

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.1011 src/distrib/sets/lists/tests/mi:1.1012
--- src/distrib/sets/lists/tests/mi:1.1011	Thu Jan 14 22:18:13 2021
+++ src/distrib/sets/lists/tests/mi	Sun Jan 17 23:00:41 2021
@@ -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

Index: src/tests/usr.bin/xlint/lint1/Makefile
diff -u src/tests/usr.bin/xlint/lint1/Makefile:1.29 src/tests/usr.bin/xlint/lint1/Makefile:1.30
--- src/tests/usr.bin/xlint/lint1/Makefile:1.29	Thu Jan 14 22:18:14 2021
+++ src/tests/usr.bin/xlint/lint1/Makefile	Sun Jan 17 23:00:41 2021
@@ -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.c
 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

Index: src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c
diff -u src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c:1.16 src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c:1.17
--- src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c:1.16	Sun Jan 17 21:19:06 2021
+++ src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.c	Sun Jan 17 23:00:41 2021
@@ -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 @@ strict_bool_assign_bit_field_then_compar
 
 	(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 */
-}

Index: src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp
diff -u src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp:1.14 src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp:1.15
--- src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp:1.14	Sun Jan 17 21:19:06 2021
+++ src/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp	Sun Jan 17 23:00:41 2021
@@ -146,6 +146,3 @@ d_c99_bool_strict.c(670): operands of '=
 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]

Index: src/tests/usr.bin/xlint/lint1/t_integration.sh
diff -u src/tests/usr.bin/xlint/lint1/t_integration.sh:1.26 src/tests/usr.bin/xlint/lint1/t_integration.sh:1.27
--- src/tests/usr.bin/xlint/lint1/t_integration.sh:1.26	Thu Jan 14 22:18:14 2021
+++ src/tests/usr.bin/xlint/lint1/t_integration.sh	Sun Jan 17 23:00:41 2021
@@ -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_struct
 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

Added files:

Index: src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c
diff -u /dev/null src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c:1.1
--- /dev/null	Sun Jan 17 23:00:41 2021
+++ src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c	Sun Jan 17 23:00:41 2021
@@ -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");
+}
Index: src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp
diff -u /dev/null src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp:1.1
--- /dev/null	Sun Jan 17 23:00:41 2021
+++ src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp	Sun Jan 17 23:00:41 2021
@@ -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]

Reply via email to