Module Name:    src
Committed By:   rillig
Date:           Sun Jul  4 09:13:59 UTC 2021

Modified Files:
        src/usr.bin/xlint/lint1: Makefile ckbool.c lint1.h mem1.c tree.c

Log Message:
lint: in strict bool mode, allow mixed types in generated C code

This allows flex lexers to be run through lint in strict bool mode.


To generate a diff of this commit:
cvs rdiff -u -r1.76 -r1.77 src/usr.bin/xlint/lint1/Makefile
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/xlint/lint1/ckbool.c
cvs rdiff -u -r1.109 -r1.110 src/usr.bin/xlint/lint1/lint1.h
cvs rdiff -u -r1.44 -r1.45 src/usr.bin/xlint/lint1/mem1.c
cvs rdiff -u -r1.305 -r1.306 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/Makefile
diff -u src/usr.bin/xlint/lint1/Makefile:1.76 src/usr.bin/xlint/lint1/Makefile:1.77
--- src/usr.bin/xlint/lint1/Makefile:1.76	Sun Jul  4 08:49:41 2021
+++ src/usr.bin/xlint/lint1/Makefile	Sun Jul  4 09:13:59 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.76 2021/07/04 08:49:41 rillig Exp $
+#	$NetBSD: Makefile,v 1.77 2021/07/04 09:13:59 rillig Exp $
 
 .include <bsd.own.mk>
 
@@ -19,7 +19,6 @@ CWARNFLAGS.clang+=	-Wno-error=implicit-i
 LINTFLAGS+=		-T
 LOBJS.${PROG}+=		${SRCS:M*.y:.y=.ln}
 LOBJS.${PROG}+=		${SRCS:M*.l:.l=.ln}
-LINTFLAGS.scan.c=	-X 107,126,330,331,332,333
 
 CPPFLAGS+=	-DIS_LINT1
 CPPFLAGS+=	-I${.CURDIR}

Index: src/usr.bin/xlint/lint1/ckbool.c
diff -u src/usr.bin/xlint/lint1/ckbool.c:1.6 src/usr.bin/xlint/lint1/ckbool.c:1.7
--- src/usr.bin/xlint/lint1/ckbool.c:1.6	Fri Jul  2 21:22:26 2021
+++ src/usr.bin/xlint/lint1/ckbool.c	Sun Jul  4 09:13:59 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: ckbool.c,v 1.6 2021/07/02 21:22:26 rillig Exp $ */
+/* $NetBSD: ckbool.c,v 1.7 2021/07/04 09:13:59 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
 #include <sys/cdefs.h>
 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: ckbool.c,v 1.6 2021/07/02 21:22:26 rillig Exp $");
+__RCSID("$NetBSD: ckbool.c,v 1.7 2021/07/04 09:13:59 rillig Exp $");
 #endif
 
 #include <string.h>
@@ -89,13 +89,12 @@ is_typeok_strict_bool_binary(op_t op,
 	if ((lt == BOOL) == (rt == BOOL))
 		return true;
 
-	if ((ln->tn_from_system_header || rn->tn_from_system_header) &&
+	if ((ln->tn_relaxed || rn->tn_relaxed) &&
 	    (is_int_constant_zero(ln, lt) || is_int_constant_zero(rn, rt)))
 		return true;
 
 	if (is_assignment_bool_or_other(op)) {
-		return lt != BOOL &&
-		       (ln->tn_from_system_header || rn->tn_from_system_header);
+		return lt != BOOL && (ln->tn_relaxed || rn->tn_relaxed);
 	}
 
 	return !is_symmetric_bool_or_other(op);
@@ -223,7 +222,7 @@ is_typeok_bool_operand(const tnode_t *tn
 	if (t == BOOL)
 		return true;
 
-	if (tn->tn_from_system_header && is_scalar(t))
+	if (tn->tn_relaxed && is_scalar(t))
 		return true;
 
 	/* For enums that are used as bit sets, allow "flags & FLAG". */

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.109 src/usr.bin/xlint/lint1/lint1.h:1.110
--- src/usr.bin/xlint/lint1/lint1.h:1.109	Fri Jul  2 18:22:09 2021
+++ src/usr.bin/xlint/lint1/lint1.h	Sun Jul  4 09:13:59 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.109 2021/07/02 18:22:09 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.110 2021/07/04 09:13:59 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -306,7 +306,10 @@ typedef	struct tnode {
 	bool	tn_lvalue : 1;	/* node is lvalue */
 	bool	tn_cast : 1;	/* if tn_op == CVT, it's an explicit cast */
 	bool	tn_parenthesized : 1;
-	bool	tn_from_system_header : 1;
+	bool	tn_relaxed : 1;	/* in strict bool mode, allow mixture between
+				 * bool and scalar, for backwards
+				 * compatibility
+				 */
 	bool	tn_system_dependent : 1; /* depends on sizeof or offsetof */
 	union {
 		struct {

Index: src/usr.bin/xlint/lint1/mem1.c
diff -u src/usr.bin/xlint/lint1/mem1.c:1.44 src/usr.bin/xlint/lint1/mem1.c:1.45
--- src/usr.bin/xlint/lint1/mem1.c:1.44	Sun Jun 20 18:51:50 2021
+++ src/usr.bin/xlint/lint1/mem1.c	Sun Jul  4 09:13:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: mem1.c,v 1.44 2021/06/20 18:51:50 rillig Exp $	*/
+/*	$NetBSD: mem1.c,v 1.45 2021/07/04 09:13:59 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: mem1.c,v 1.44 2021/06/20 18:51:50 rillig Exp $");
+__RCSID("$NetBSD: mem1.c,v 1.45 2021/07/04 09:13:59 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -346,6 +346,16 @@ expr_zalloc(size_t s)
 	return xgetblk(&tmblk, s);
 }
 
+static bool
+str_endswith(const char *haystack, const char *needle)
+{
+	size_t hlen = strlen(haystack);
+	size_t nlen = strlen(needle);
+
+	return nlen <= hlen &&
+	       memcmp(haystack + hlen - nlen, needle, nlen) == 0;
+}
+
 /*
  * Return a freshly allocated tree node that is freed at the end of the
  * current expression.
@@ -354,7 +364,14 @@ tnode_t *
 expr_zalloc_tnode(void)
 {
 	tnode_t *tn = expr_zalloc(sizeof(*tn));
-	tn->tn_from_system_header = in_system_header;
+	/*
+	 * files named *.c that are different from the main translation unit
+	 * typically contain generated code that cannot be influenced, such
+	 * as a flex lexer or a yacc parser.
+	 */
+	tn->tn_relaxed = in_system_header ||
+			 (curr_pos.p_file != csrc_pos.p_file &&
+			  str_endswith(curr_pos.p_file, ".c"));
 	return tn;
 }
 

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.305 src/usr.bin/xlint/lint1/tree.c:1.306
--- src/usr.bin/xlint/lint1/tree.c:1.305	Sun Jul  4 08:19:06 2021
+++ src/usr.bin/xlint/lint1/tree.c	Sun Jul  4 09:13:59 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.305 2021/07/04 08:19:06 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.306 2021/07/04 09:13:59 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.305 2021/07/04 08:19:06 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.306 2021/07/04 09:13:59 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -1642,10 +1642,10 @@ new_tnode(op_t op, type_t *type, tnode_t
 
 	ntn->tn_op = op;
 	ntn->tn_type = type;
-	if (ln->tn_from_system_header)
-		ntn->tn_from_system_header = true;
-	if (rn != NULL && rn->tn_from_system_header)
-		ntn->tn_from_system_header = true;
+	if (ln->tn_relaxed)
+		ntn->tn_relaxed = true;
+	if (rn != NULL && rn->tn_relaxed)
+		ntn->tn_relaxed = true;
 	ntn->tn_left = ln;
 	ntn->tn_right = rn;
 
@@ -1887,7 +1887,7 @@ convert(op_t op, int arg, type_t *tp, tn
 	ntn->tn_op = CVT;
 	ntn->tn_type = tp;
 	ntn->tn_cast = op == CVT;
-	ntn->tn_from_system_header |= tn->tn_from_system_header;
+	ntn->tn_relaxed |= tn->tn_relaxed;
 	ntn->tn_right = NULL;
 	if (tn->tn_op != CON || nt == VOID) {
 		ntn->tn_left = tn;

Reply via email to